Skip to content

Commit

Permalink
Cherry-pick 3651419. rdar://problem/110629287
Browse files Browse the repository at this point in the history
    [CSSOM] Fix insertion of rule into orphaned style rule
    https://bugs.webkit.org/show_bug.cgi?id=258017
    rdar://110629287

    Reviewed by Antti Koivisto.

    When a StyleRule is orphaned, we don't need any specific mechanism (such as RuleMutationScope)
    and convert it directly to a StyleRuleWithNesting.

    * LayoutTests/fast/css/cssom-mutation-stylerule-expected.html: Added.
    * LayoutTests/fast/css/cssom-mutation-stylerule.html: Added.
    * Source/WebCore/css/CSSStyleRule.cpp:
    (WebCore::CSSStyleRule::insertRule):

    Canonical link: https://commits.webkit.org/265154@main
Identifier: 259548.836@safari-7615-branch
  • Loading branch information
mdubet authored and MyahCobbs committed Jun 16, 2023
1 parent b63d456 commit 8b5bb60
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions LayoutTests/fast/css/cssom-mutation-stylerule-expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Test passes if no crash.
14 changes: 14 additions & 0 deletions LayoutTests/fast/css/cssom-mutation-stylerule.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<style>
aaa {
color: red;
}
</style>
<script>
onload = () => {
let cssStyleRule = document.styleSheets[0].cssRules[0];
document.styleSheets[0].deleteRule(0);
cssStyleRule.insertRule(`.bar { color: black; }`);
};
</script>

Test passes if no crash.
11 changes: 8 additions & 3 deletions Source/WebCore/css/CSSStyleRule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,14 @@ ExceptionOr<unsigned> CSSStyleRule::insertRule(const String& ruleString, unsigne
return Exception { HierarchyRequestError };

if (!m_styleRule->isStyleRuleWithNesting()) {
// Call the parent rule (or parent stylesheet if top-level) to transform the current StyleRule to StyleRuleWithNesting.
auto parent = parentRule();
auto styleRuleWithNesting = parent ? parent->prepareChildStyleRuleForNesting(m_styleRule) : styleSheet->prepareChildStyleRuleForNesting(WTFMove(m_styleRule.get()));
// Call the parent rule (or parent stylesheet if top-level or nothing if it's an orphaned rule) to transform the current StyleRule to StyleRuleWithNesting.
RefPtr<StyleRuleWithNesting> styleRuleWithNesting;
if (auto parent = parentRule())
styleRuleWithNesting = parent->prepareChildStyleRuleForNesting(m_styleRule);
else if (auto parent = parentStyleSheet())
styleRuleWithNesting = parent->prepareChildStyleRuleForNesting(WTFMove(m_styleRule.get()));
else
styleRuleWithNesting = StyleRuleWithNesting::create(WTFMove(m_styleRule.get()));
ASSERT(styleRuleWithNesting);
m_styleRule = *styleRuleWithNesting;
}
Expand Down

0 comments on commit 8b5bb60

Please sign in to comment.