Skip to content

Commit

Permalink
Cherry-pick 1ffbfef. rdar://problem/108758349
Browse files Browse the repository at this point in the history
    Disallow caching of StyleSheetContents that uses nesting
    https://bugs.webkit.org/show_bug.cgi?id=256449
    rdar://108758349

    Reviewed by Alan Baradlay.

    Mutations to cached document may affect other stylesheets.

    * Source/WebCore/css/CSSGroupingRule.cpp:
    (WebCore::CSSGroupingRule::prepareChildStyleRuleForNesting):
    * Source/WebCore/css/CSSStyleSheet.cpp:
    (WebCore::CSSStyleSheet::prepareChildStyleRuleForNesting):
    * Source/WebCore/css/StyleSheetContents.cpp:
    (WebCore::StyleSheetContents::isCacheable const):
    * Source/WebCore/css/StyleSheetContents.h:
    * Source/WebCore/css/parser/CSSParserImpl.cpp:
    (WebCore::CSSParserImpl::consumeRegularRuleList):
    (WebCore::CSSParserImpl::consumeStyleRule):

    Canonical link: https://commits.webkit.org/263799@main

Identifier: 259548.747@safari-7615.3.3.13-branch
  • Loading branch information
anttijk authored and drobson1005 committed May 8, 2023
1 parent 22a068a commit dc3172f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Source/WebCore/css/CSSGroupingRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ RefPtr<StyleRuleWithNesting> CSSGroupingRule::prepareChildStyleRuleForNesting(St
if (rule == &styleRule) {
auto styleRuleWithNesting = StyleRuleWithNesting::create(WTFMove(styleRule));
rules[i] = styleRuleWithNesting.ptr();
if (auto* styleSheet = parentStyleSheet())
styleSheet->contents().setHasNestingRules();
return styleRuleWithNesting;
}
}
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/css/CSSStyleSheet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ RefPtr<StyleRuleWithNesting> CSSStyleSheet::prepareChildStyleRuleForNesting(Styl
if (rules[i] == &styleRule) {
auto styleRuleWithNesting = StyleRuleWithNesting::create(WTFMove(styleRule));
rules[i] = styleRuleWithNesting.ptr();
m_contents->setHasNestingRules();
return styleRuleWithNesting;
}
}
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/css/StyleSheetContents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ bool StyleSheetContents::isCacheable() const
// FIXME: Valid mime type avoids the check too.
if (!m_hasSyntacticallyValidCSSHeader)
return false;
if (m_hasNestingRules)
return false;
return true;
}

Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/css/StyleSheetContents.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class StyleSheetContents final : public RefCounted<StyleSheetContents>, public C
bool isMutable() const { return m_isMutable; }
void setMutable() { m_isMutable = true; }

bool hasNestingRules() const { return m_hasNestingRules; }
void setHasNestingRules() { m_hasNestingRules = true; }

bool isInMemoryCache() const { return m_inMemoryCacheCount; }
void addedToMemoryCache();
void removedFromMemoryCache();
Expand Down Expand Up @@ -177,6 +180,7 @@ class StyleSheetContents final : public RefCounted<StyleSheetContents>, public C
bool m_didLoadErrorOccur { false };
bool m_usesStyleBasedEditability { false };
bool m_isMutable { false };
bool m_hasNestingRules { false };
unsigned m_inMemoryCacheCount { 0 };

CSSParserContext m_parserContext;
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/css/parser/CSSParserImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ Vector<RefPtr<StyleRuleBase>> CSSParserImpl::consumeRegularRuleList(CSSParserTok
// property declarations.
rules.append(createNestingParentRule());

m_styleSheet->setHasNestingRules();

if (m_observerWrapper)
m_observerWrapper->observer().markRuleBodyContainsImplicitlyNestedProperties();
}
Expand Down Expand Up @@ -1092,8 +1094,10 @@ RefPtr<StyleRuleBase> CSSParserImpl::consumeStyleRule(CSSParserTokenRange prelud
// We save memory by creating a simple StyleRule instead of a heavier StyleWithNestingSupportRule when we don't need the CSS Nesting features.
if (nestedRules.isEmpty() && !selectorList->hasExplicitNestingParent() && !isNestedContext())
styleRule = StyleRule::create(WTFMove(properties), m_context.hasDocumentSecurityOrigin, WTFMove(*selectorList));
else
else {
styleRule = StyleRuleWithNesting::create(WTFMove(properties), m_context.hasDocumentSecurityOrigin, WTFMove(*selectorList), WTFMove(nestedRules));
m_styleSheet->setHasNestingRules();
}
});

return styleRule;
Expand Down

0 comments on commit dc3172f

Please sign in to comment.