From b2c435a18969a9e9f9ddabe8ab3ba1f1b16c29fc Mon Sep 17 00:00:00 2001 From: Francis Dominic Fajardo Date: Thu, 26 Jun 2025 21:01:56 +0800 Subject: [PATCH] Issue #2765 - Part 2: Create a helper function for resolving the non-NAC style context of a given NAC --- layout/base/nsLayoutUtils.cpp | 25 +++++++++++++++++++++++++ layout/base/nsLayoutUtils.h | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/layout/base/nsLayoutUtils.cpp b/layout/base/nsLayoutUtils.cpp index 87ad428ab8..06014e5c50 100644 --- a/layout/base/nsLayoutUtils.cpp +++ b/layout/base/nsLayoutUtils.cpp @@ -9337,3 +9337,28 @@ nsLayoutUtils::ComputeGeometryBox(nsIFrame* aFrame, return r; } + +/* static */ nsStyleContext* +nsLayoutUtils::GetNonAnonymousStyleContext(nsIFrame* aFrame) +{ + nsIContent* node = aFrame->GetContent(); + MOZ_ASSERT(node, "No content for the given frame?"); + while (node && node->IsInNativeAnonymousSubtree()) { + node = node->GetParent(); + } + MOZ_ASSERT(node, "Native anonymous element with no originating node?"); + if (nsIFrame* primaryFrame = node->GetPrimaryFrame()) { + return primaryFrame->StyleContext(); + } + // If the element doesn't have primary frame, get the computed style + // from the element directly. + nsPresContext* pc = aFrame->PresContext(); + MOZ_ASSERT(node == pc->Document()->GetRootElement(), + "Root element is the only case for this fallback " + "path to be triggered"); + RefPtr styleContext = + pc->StyleSet()->ResolveStyleFor(node->AsElement(), nullptr); + // Dropping the strong reference is fine because the style should be + // held strongly by the element. + return styleContext.get(); +} diff --git a/layout/base/nsLayoutUtils.h b/layout/base/nsLayoutUtils.h index 6091fb45e4..ea9a026007 100644 --- a/layout/base/nsLayoutUtils.h +++ b/layout/base/nsLayoutUtils.h @@ -2878,6 +2878,15 @@ public: return ResolveToLength(aGap, aPercentageBasis); } + /** + * Returns the style context associated with the nearest non-native-anonymous + * ancestor of the given frame. + * + * @param aFrame The frame associated with native anonymous content. + * @return The resolved style context of the nearest non-anonymous DOM ancestor. + */ + static nsStyleContext* GetNonAnonymousStyleContext(nsIFrame* aFrame); + private: static uint32_t sFontSizeInflationEmPerLine; static uint32_t sFontSizeInflationMinTwips;