<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[283216] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/283216">283216</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2021-09-29 08:23:09 -0700 (Wed, 29 Sep 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>[CSS Cascade Layers] Add fonts and keyframes in cascade layer order
https://bugs.webkit.org/show_bug.cgi?id=230947

Reviewed by Simon Fraser.

LayoutTests/imported/w3c:

* web-platform-tests/css/css-cascade/layer-font-face-override-expected.txt:
* web-platform-tests/css/css-cascade/layer-keyframes-override-expected.txt:

Source/WebCore:

Ensure font and keyframe rules respect cascade layer ordering.

* style/RuleSet.cpp:
(WebCore::Style::RuleSet::Builder::addChildRules):

Collect resolver mutating rules instead of processing them immediately.

(WebCore::Style::RuleSet::Builder::~Builder):
(WebCore::Style::RuleSet::Builder::addMutatingRulesToResolver):

Process the rules in correct order after cascade layer order has been established.

In additive case we need to also re-process rules added from earlier stylesheets as they might
have higher priority than the newly added ones.

(WebCore::Style::RuleSet::shrinkToFit):
* style/RuleSet.h:
(WebCore::Style::RuleSet::cascadeLayerOrderForIdentifier const):
(WebCore::Style::RuleSet::cascadeLayerOrderFor const):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsimportedw3cChangeLog">trunk/LayoutTests/imported/w3c/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestscsscsscascadelayerfontfaceoverrideexpectedtxt">trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-font-face-override-expected.txt</a></li>
<li><a href="#trunkLayoutTestsimportedw3cwebplatformtestscsscsscascadelayerkeyframesoverrideexpectedtxt">trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-keyframes-override-expected.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorestyleRuleSetcpp">trunk/Source/WebCore/style/RuleSet.cpp</a></li>
<li><a href="#trunkSourceWebCorestyleRuleSeth">trunk/Source/WebCore/style/RuleSet.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsimportedw3cChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/ChangeLog (283215 => 283216)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/ChangeLog 2021-09-29 14:38:48 UTC (rev 283215)
+++ trunk/LayoutTests/imported/w3c/ChangeLog    2021-09-29 15:23:09 UTC (rev 283216)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2021-09-29  Antti Koivisto  <antti@apple.com>
+
+        [CSS Cascade Layers] Add fonts and keyframes in cascade layer order
+        https://bugs.webkit.org/show_bug.cgi?id=230947
+
+        Reviewed by Simon Fraser.
+
+        * web-platform-tests/css/css-cascade/layer-font-face-override-expected.txt:
+        * web-platform-tests/css/css-cascade/layer-keyframes-override-expected.txt:
+
</ins><span class="cx"> 2021-09-28  Cameron McCormack  <heycam@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Preserve color space when structured cloning ImageBitmaps
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestscsscsscascadelayerfontfaceoverrideexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-font-face-override-expected.txt (283215 => 283216)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-font-face-override-expected.txt  2021-09-29 14:38:48 UTC (rev 283215)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-font-face-override-expected.txt     2021-09-29 15:23:09 UTC (rev 283216)
</span><span class="lines">@@ -1,7 +1,7 @@
</span><span class="cx"> Test
</span><span class="cx"> 
</span><del>-FAIL @font-face layered overrides unlayered assert_equals: expected "80px" but got "38.515625px"
-FAIL @font-face override between layers assert_equals: expected "80px" but got "38.515625px"
-FAIL @font-face override update with appended sheet 1 assert_equals: expected "80px" but got "38.515625px"
</del><ins>+PASS @font-face layered overrides unlayered
+PASS @font-face override between layers
+PASS @font-face override update with appended sheet 1
</ins><span class="cx"> PASS @font-face override update with appended sheet 2
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3cwebplatformtestscsscsscascadelayerkeyframesoverrideexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-keyframes-override-expected.txt (283215 => 283216)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-keyframes-override-expected.txt  2021-09-29 14:38:48 UTC (rev 283215)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/css/css-cascade/layer-keyframes-override-expected.txt     2021-09-29 15:23:09 UTC (rev 283216)
</span><span class="lines">@@ -1,6 +1,6 @@
</span><span class="cx"> 
</span><del>-FAIL @keyframes layered overrides unlayered assert_equals: expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL @keyframes override between layers assert_equals: expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL @keyframes override update with appended sheet 1 assert_equals: expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL @keyframes override update with appended sheet 2 assert_equals: expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
</del><ins>+PASS @keyframes layered overrides unlayered
+PASS @keyframes override between layers
+PASS @keyframes override update with appended sheet 1
+PASS @keyframes override update with appended sheet 2
</ins><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (283215 => 283216)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2021-09-29 14:38:48 UTC (rev 283215)
+++ trunk/Source/WebCore/ChangeLog      2021-09-29 15:23:09 UTC (rev 283216)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2021-09-29  Antti Koivisto  <antti@apple.com>
+
+        [CSS Cascade Layers] Add fonts and keyframes in cascade layer order
+        https://bugs.webkit.org/show_bug.cgi?id=230947
+
+        Reviewed by Simon Fraser.
+
+        Ensure font and keyframe rules respect cascade layer ordering.
+
+        * style/RuleSet.cpp:
+        (WebCore::Style::RuleSet::Builder::addChildRules):
+
+        Collect resolver mutating rules instead of processing them immediately.
+
+        (WebCore::Style::RuleSet::Builder::~Builder):
+        (WebCore::Style::RuleSet::Builder::addMutatingRulesToResolver):
+
+        Process the rules in correct order after cascade layer order has been established.
+
+        In additive case we need to also re-process rules added from earlier stylesheets as they might
+        have higher priority than the newly added ones.
+
+        (WebCore::Style::RuleSet::shrinkToFit):
+        * style/RuleSet.h:
+        (WebCore::Style::RuleSet::cascadeLayerOrderForIdentifier const):
+        (WebCore::Style::RuleSet::cascadeLayerOrderFor const):
+
</ins><span class="cx"> 2021-09-28  Myles C. Maxfield  <mmaxfield@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Factor out rare members of FontCreationContext into FontCreationContextRareData
</span></span></pre></div>
<a id="trunkSourceWebCorestyleRuleSetcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/RuleSet.cpp (283215 => 283216)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/RuleSet.cpp   2021-09-29 14:38:48 UTC (rev 283215)
+++ trunk/Source/WebCore/style/RuleSet.cpp      2021-09-29 15:23:09 UTC (rev 283216)
</span><span class="lines">@@ -346,27 +346,13 @@
</span><span class="cx">             popCascadeLayer(layerRule.name());
</span><span class="cx">             continue;
</span><span class="cx">         }
</span><del>-        if (is<StyleRuleFontFace>(*rule)) {
-            // Add this font face to our set.
-            if (resolver) {
-                resolver->document().fontSelector().addFontFaceRule(downcast<StyleRuleFontFace>(*rule.get()), false);
-                resolver->invalidateMatchedDeclarationsCache();
</del><ins>+        if (is<StyleRuleFontFace>(*rule) || is<StyleRuleFontPaletteValues>(*rule) || is<StyleRuleKeyframes>(*rule)) {
+            if (mode == Mode::ResolverMutationScan) {
+                mediaQueryCollector.didMutateResolver();
+                continue;
</ins><span class="cx">             }
</span><del>-            mediaQueryCollector.didMutateResolver();
-            continue;
-        }
-        if (is<StyleRuleFontPaletteValues>(*rule)) {
-            if (resolver) {
-                resolver->document().fontSelector().addFontPaletteValuesRule(downcast<StyleRuleFontPaletteValues>(*rule.get()));
-                resolver->invalidateMatchedDeclarationsCache();
-            }
-            mediaQueryCollector.didMutateResolver();
-            continue;
-        }
-        if (is<StyleRuleKeyframes>(*rule)) {
</del><span class="cx">             if (resolver)
</span><del>-                resolver->addKeyframeStyle(downcast<StyleRuleKeyframes>(*rule));
-            mediaQueryCollector.didMutateResolver();
</del><ins>+                collectedResolverMutatingRules.append({ *rule, currentCascadeLayerIdentifier });
</ins><span class="cx">             continue;
</span><span class="cx">         }
</span><span class="cx">         if (is<StyleRuleSupports>(*rule) && downcast<StyleRuleSupports>(*rule).conditionIsSupported()) {
</span><span class="lines">@@ -403,8 +389,11 @@
</span><span class="cx"> 
</span><span class="cx"> RuleSet::Builder::~Builder()
</span><span class="cx"> {
</span><del>-    if (mode == Mode::Normal && !cascadeLayerIdentifierMap.isEmpty())
-        updateCascadeLayerOrder();
</del><ins>+    if (mode == Mode::ResolverMutationScan)
+        return;
+
+    updateCascadeLayerOrder();
+    addMutatingRulesToResolver();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void RuleSet::Builder::addStyleRule(const StyleRule& rule)
</span><span class="lines">@@ -470,6 +459,9 @@
</span><span class="cx"> 
</span><span class="cx"> void RuleSet::Builder::updateCascadeLayerOrder()
</span><span class="cx"> {
</span><ins>+    if (cascadeLayerIdentifierMap.isEmpty())
+        return;
+
</ins><span class="cx">     auto compare = [&](CascadeLayerIdentifier a, CascadeLayerIdentifier b) {
</span><span class="cx">         while (a && b) {
</span><span class="cx">             // Identifiers are in parse order which almost corresponds to the layer priority order.
</span><span class="lines">@@ -499,6 +491,46 @@
</span><span class="cx">         ruleSet->cascadeLayerForIdentifier(orderVector[i]).order = i + 1;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void RuleSet::Builder::addMutatingRulesToResolver()
+{
+    if (!resolver)
+        return;
+
+    auto compareLayers = [&](const ResolverMutatingRule& a, const ResolverMutatingRule& b) {
+        auto aOrder = ruleSet->cascadeLayerOrderForIdentifier(a.layerIdentifier);
+        auto bOrder = ruleSet->cascadeLayerOrderForIdentifier(b.layerIdentifier);
+        return aOrder < bOrder;
+    };
+
+    // The order may change so we need to reprocess resolver mutating rules from earlier stylesheets.
+    auto rulesToAdd = std::exchange(ruleSet->m_resolverMutatingRulesInLayers, { });
+    rulesToAdd.appendVector(WTFMove(collectedResolverMutatingRules));
+
+    if (!cascadeLayerIdentifierMap.isEmpty())
+        std::stable_sort(rulesToAdd.begin(), rulesToAdd.end(), compareLayers);
+
+    for (auto& collectedRule : rulesToAdd) {
+        if (collectedRule.layerIdentifier)
+            ruleSet->m_resolverMutatingRulesInLayers.append(collectedRule);
+
+        auto& rule = collectedRule.rule;
+        if (is<StyleRuleFontFace>(rule)) {
+            resolver->document().fontSelector().addFontFaceRule(downcast<StyleRuleFontFace>(rule.get()), false);
+            resolver->invalidateMatchedDeclarationsCache();
+            continue;
+        }
+        if (is<StyleRuleFontPaletteValues>(rule)) {
+            resolver->document().fontSelector().addFontPaletteValuesRule(downcast<StyleRuleFontPaletteValues>(rule.get()));
+            resolver->invalidateMatchedDeclarationsCache();
+            continue;
+        }
+        if (is<StyleRuleKeyframes>(rule)) {
+            resolver->addKeyframeStyle(downcast<StyleRuleKeyframes>(rule.get()));
+            continue;
+        }
+    }
+}
+
</ins><span class="cx"> template<typename Function>
</span><span class="cx"> void RuleSet::traverseRuleDatas(Function&& function)
</span><span class="cx"> {
</span><span class="lines">@@ -635,6 +667,7 @@
</span><span class="cx"> 
</span><span class="cx">     m_cascadeLayers.shrinkToFit();
</span><span class="cx">     m_cascadeLayerIdentifierForRulePosition.shrinkToFit();
</span><ins>+    m_resolverMutatingRulesInLayers.shrinkToFit();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> RuleSet::MediaQueryCollector::~MediaQueryCollector() = default;
</span></span></pre></div>
<a id="trunkSourceWebCorestyleRuleSeth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/style/RuleSet.h (283215 => 283216)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/style/RuleSet.h     2021-09-29 14:38:48 UTC (rev 283215)
+++ trunk/Source/WebCore/style/RuleSet.h        2021-09-29 15:23:09 UTC (rev 283216)
</span><span class="lines">@@ -152,6 +152,11 @@
</span><span class="cx"> 
</span><span class="cx">     using CascadeLayerIdentifier = unsigned;
</span><span class="cx"> 
</span><ins>+    struct ResolverMutatingRule {
+        Ref<StyleRuleBase> rule;
+        CascadeLayerIdentifier layerIdentifier;
+    };
+
</ins><span class="cx">     struct Builder {
</span><span class="cx">         enum class Mode { Normal, ResolverMutationScan };
</span><span class="cx"> 
</span><span class="lines">@@ -162,6 +167,7 @@
</span><span class="cx">         CascadeLayerName resolvedCascadeLayerName { };
</span><span class="cx">         HashMap<CascadeLayerName, CascadeLayerIdentifier> cascadeLayerIdentifierMap { };
</span><span class="cx">         CascadeLayerIdentifier currentCascadeLayerIdentifier { 0 };
</span><ins>+        Vector<ResolverMutatingRule> collectedResolverMutatingRules { };
</ins><span class="cx"> 
</span><span class="cx">         void addRulesFromSheet(const StyleSheetContents&);
</span><span class="cx"> 
</span><span class="lines">@@ -175,6 +181,7 @@
</span><span class="cx">         void pushCascadeLayer(const CascadeLayerName&);
</span><span class="cx">         void popCascadeLayer(const CascadeLayerName&);
</span><span class="cx">         void updateCascadeLayerOrder();
</span><ins>+        void addMutatingRulesToResolver();
</ins><span class="cx">     };
</span><span class="cx"> 
</span><span class="cx">     struct CollectedMediaQueryChanges {
</span><span class="lines">@@ -193,6 +200,7 @@
</span><span class="cx">     };
</span><span class="cx">     CascadeLayer& cascadeLayerForIdentifier(CascadeLayerIdentifier identifier) { return m_cascadeLayers[identifier - 1]; }
</span><span class="cx">     const CascadeLayer& cascadeLayerForIdentifier(CascadeLayerIdentifier identifier) const { return m_cascadeLayers[identifier - 1]; }
</span><ins>+    unsigned cascadeLayerOrderForIdentifier(CascadeLayerIdentifier) const;
</ins><span class="cx"> 
</span><span class="cx">     AtomRuleMap m_idRules;
</span><span class="cx">     AtomRuleMap m_classRules;
</span><span class="lines">@@ -218,6 +226,8 @@
</span><span class="cx">     // This is a side vector to hold layer identifiers without bloating RuleData.
</span><span class="cx">     Vector<CascadeLayerIdentifier> m_cascadeLayerIdentifierForRulePosition;
</span><span class="cx"> 
</span><ins>+    Vector<ResolverMutatingRule> m_resolverMutatingRulesInLayers;
+
</ins><span class="cx">     bool m_hasHostPseudoClassRulesMatchingInShadowTree { false };
</span><span class="cx">     bool m_autoShrinkToFitEnabled { true };
</span><span class="cx">     bool m_hasViewportDependentMediaQueries { false };
</span><span class="lines">@@ -233,14 +243,19 @@
</span><span class="cx">     return tagRules->get(key);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline unsigned RuleSet::cascadeLayerOrderForIdentifier(CascadeLayerIdentifier identifier) const
+{
+    if (!identifier)
+        return 0;
+    return cascadeLayerForIdentifier(identifier).order;
+}
+
</ins><span class="cx"> inline unsigned RuleSet::cascadeLayerOrderFor(const RuleData& ruleData) const
</span><span class="cx"> {
</span><span class="cx">     if (m_cascadeLayerIdentifierForRulePosition.size() <= ruleData.position())
</span><span class="cx">         return 0;
</span><span class="cx">     auto identifier = m_cascadeLayerIdentifierForRulePosition[ruleData.position()];
</span><del>-    if (!identifier)
-        return 0;
-    return cascadeLayerForIdentifier(identifier).order;
</del><ins>+    return cascadeLayerOrderForIdentifier(identifier);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> } // namespace Style
</span></span></pre>
</div>
</div>

</body>
</html>