Bug 1289701 - Find the root style context by walkng up the style context tree instead of calling ResolveStyleFor or getting it from root element's primary frame. r=dbaron

The only call site of CalcLengthWith() with a null style context is
CalcLengthWithInitialFont(). CalcLengthWithInitialFont() calls
CalcLengthWith() with a valid nsStyleFont and aUseProvidedRootEmSize
true, so we can get the rem unit font size from the nsStyleFont when called
from CalcLengthWithInitialFont().

MozReview-Commit-ID: A9LKfQEozaB
This commit is contained in:
Hiroyuki Ikezoe 2016-11-30 07:46:27 +09:00
Родитель 84eaaaf97d
Коммит 1ef4ddc756
2 изменённых файлов: 27 добавлений и 22 удалений

Просмотреть файл

@ -442,6 +442,11 @@ static nsSize CalcViewportUnitsScale(nsPresContext* aPresContext)
return viewportSize; return viewportSize;
} }
// If |aStyleFont| is nullptr, aStyleContext->StyleFont() is used.
//
// In case that |aValue| is rem unit, if |aStyleContext| is null, callers must
// specify a valid |aStyleFont| and |aUseProvidedRootEmSize| must be true so
// that we can get the length from |aStyleFont|.
static nscoord CalcLengthWith(const nsCSSValue& aValue, static nscoord CalcLengthWith(const nsCSSValue& aValue,
nscoord aFontSize, nscoord aFontSize,
const nsStyleFont* aStyleFont, const nsStyleFont* aStyleFont,
@ -458,8 +463,8 @@ static nscoord CalcLengthWith(const nsCSSValue& aValue,
"not a length or calc unit"); "not a length or calc unit");
NS_ASSERTION(aStyleFont || aStyleContext, NS_ASSERTION(aStyleFont || aStyleContext,
"Must have style data"); "Must have style data");
NS_ASSERTION(!aStyleFont || !aStyleContext, NS_ASSERTION(aStyleContext || aUseProvidedRootEmSize,
"Duplicate sources of data"); "Must have style context or specify aUseProvidedRootEmSize");
NS_ASSERTION(aPresContext, "Must have prescontext"); NS_ASSERTION(aPresContext, "Must have prescontext");
if (aValue.IsFixedLengthUnit()) { if (aValue.IsFixedLengthUnit()) {
@ -554,21 +559,12 @@ static nscoord CalcLengthWith(const nsCSSValue& aValue,
} else { } else {
// This is not the root element or we are calculating something other // This is not the root element or we are calculating something other
// than font size, so rem is relative to the root element's font size. // than font size, so rem is relative to the root element's font size.
RefPtr<nsStyleContext> rootStyle; // Find the root style context by walking up the style context tree.
const nsStyleFont *rootStyleFont = styleFont; nsStyleContext* rootStyle = aStyleContext;
Element* docElement = aPresContext->Document()->GetRootElement(); while (rootStyle->GetParent()) {
rootStyle = rootStyle->GetParent();
if (docElement) {
nsIFrame* rootFrame = docElement->GetPrimaryFrame();
if (rootFrame) {
rootStyle = rootFrame->StyleContext();
} else {
rootStyle = aPresContext->StyleSet()->AsGecko()->ResolveStyleFor(docElement,
nullptr);
}
rootStyleFont = rootStyle->StyleFont();
} }
const nsStyleFont *rootStyleFont = rootStyle->StyleFont();
rootFontSize = rootStyleFont->mFont.size; rootFontSize = rootStyleFont->mFont.size;
} }
@ -3323,15 +3319,19 @@ struct SetFontSizeCalcOps : public css::BasicCoordCalcOps,
const nscoord mParentSize; const nscoord mParentSize;
const nsStyleFont* const mParentFont; const nsStyleFont* const mParentFont;
nsPresContext* const mPresContext; nsPresContext* const mPresContext;
nsStyleContext* const mStyleContext;
const bool mAtRoot; const bool mAtRoot;
RuleNodeCacheConditions& mConditions; RuleNodeCacheConditions& mConditions;
SetFontSizeCalcOps(nscoord aParentSize, const nsStyleFont* aParentFont, SetFontSizeCalcOps(nscoord aParentSize, const nsStyleFont* aParentFont,
nsPresContext* aPresContext, bool aAtRoot, nsPresContext* aPresContext,
nsStyleContext* aStyleContext,
bool aAtRoot,
RuleNodeCacheConditions& aConditions) RuleNodeCacheConditions& aConditions)
: mParentSize(aParentSize), : mParentSize(aParentSize),
mParentFont(aParentFont), mParentFont(aParentFont),
mPresContext(aPresContext), mPresContext(aPresContext),
mStyleContext(aStyleContext),
mAtRoot(aAtRoot), mAtRoot(aAtRoot),
mConditions(aConditions) mConditions(aConditions)
{ {
@ -3346,7 +3346,7 @@ struct SetFontSizeCalcOps : public css::BasicCoordCalcOps,
// between us and the parent is simply ignored. // between us and the parent is simply ignored.
size = CalcLengthWith(aValue, mParentSize, size = CalcLengthWith(aValue, mParentSize,
mParentFont, mParentFont,
nullptr, mPresContext, mAtRoot, mStyleContext, mPresContext, mAtRoot,
true, mConditions); true, mConditions);
if (!aValue.IsRelativeLengthUnit() && mParentFont->mAllowZoom) { if (!aValue.IsRelativeLengthUnit() && mParentFont->mAllowZoom) {
size = nsStyleFont::ZoomText(mPresContext, size); size = nsStyleFont::ZoomText(mPresContext, size);
@ -3370,6 +3370,7 @@ struct SetFontSizeCalcOps : public css::BasicCoordCalcOps,
/* static */ void /* static */ void
nsRuleNode::SetFontSize(nsPresContext* aPresContext, nsRuleNode::SetFontSize(nsPresContext* aPresContext,
nsStyleContext* aContext,
const nsRuleData* aRuleData, const nsRuleData* aRuleData,
const nsStyleFont* aFont, const nsStyleFont* aFont,
const nsStyleFont* aParentFont, const nsStyleFont* aParentFont,
@ -3436,7 +3437,8 @@ nsRuleNode::SetFontSize(nsPresContext* aPresContext,
sizeValue->GetUnit() == eCSSUnit_Percent || sizeValue->GetUnit() == eCSSUnit_Percent ||
sizeValue->IsCalcUnit()) { sizeValue->IsCalcUnit()) {
SetFontSizeCalcOps ops(aParentSize, aParentFont, SetFontSizeCalcOps ops(aParentSize, aParentFont,
aPresContext, aAtRoot, aPresContext, aContext,
aAtRoot,
aConditions); aConditions);
*aSize = css::ComputeCalc(*sizeValue, ops); *aSize = css::ComputeCalc(*sizeValue, ops);
if (*aSize < 0) { if (*aSize < 0) {
@ -3782,7 +3784,7 @@ nsRuleNode::SetFont(nsPresContext* aPresContext, nsStyleContext* aContext,
aFont->mScriptMinSize = aFont->mScriptMinSize =
CalcLengthWith(*scriptMinSizeValue, aParentFont->mSize, CalcLengthWith(*scriptMinSizeValue, aParentFont->mSize,
aParentFont, aParentFont,
nullptr, aPresContext, atRoot, true, aContext, aPresContext, atRoot, true /* aUseUserFontSet */,
aConditions); aConditions);
} }
@ -4024,7 +4026,8 @@ nsRuleNode::SetFont(nsPresContext* aPresContext, nsStyleContext* aContext,
scriptLevelAdjustedParentSize != scriptLevelAdjustedParentSize !=
scriptLevelAdjustedUnconstrainedParentSize; scriptLevelAdjustedUnconstrainedParentSize;
SetFontSize(aPresContext, aRuleData, aFont, aParentFont, SetFontSize(aPresContext, aContext,
aRuleData, aFont, aParentFont,
&aFont->mSize, &aFont->mSize,
systemFont, aParentFont->mSize, scriptLevelAdjustedParentSize, systemFont, aParentFont->mSize, scriptLevelAdjustedParentSize,
aUsedStartStruct, atRoot, aConditions); aUsedStartStruct, atRoot, aConditions);
@ -4054,7 +4057,8 @@ nsRuleNode::SetFont(nsPresContext* aPresContext, nsStyleContext* aContext,
// already called SetUncacheable. // already called SetUncacheable.
RuleNodeCacheConditions unconstrainedConditions; RuleNodeCacheConditions unconstrainedConditions;
SetFontSize(aPresContext, aRuleData, aFont, aParentFont, SetFontSize(aPresContext, aContext,
aRuleData, aFont, aParentFont,
&aFont->mScriptUnconstrainedSize, &aFont->mScriptUnconstrainedSize,
systemFont, aParentFont->mScriptUnconstrainedSize, systemFont, aParentFont->mScriptUnconstrainedSize,
scriptLevelAdjustedUnconstrainedParentSize, scriptLevelAdjustedUnconstrainedParentSize,

Просмотреть файл

@ -755,6 +755,7 @@ protected:
// helpers for |ComputeFontData| that need access to |mNoneBits|: // helpers for |ComputeFontData| that need access to |mNoneBits|:
static void SetFontSize(nsPresContext* aPresContext, static void SetFontSize(nsPresContext* aPresContext,
nsStyleContext* aContext,
const nsRuleData* aRuleData, const nsRuleData* aRuleData,
const nsStyleFont* aFont, const nsStyleFont* aFont,
const nsStyleFont* aParentFont, const nsStyleFont* aParentFont,