Title: [201073] trunk
Revision
201073
Author
an...@apple.com
Date
2016-05-18 01:41:50 -0700 (Wed, 18 May 2016)

Log Message

Cascading order for !important properties in ::slotted and ::host rules is incorrect
https://bugs.webkit.org/show_bug.cgi?id=157789
<rdar://problem/26318781>

Reviewed by Andreas Kling.

LayoutTests/imported/w3c:

* csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt: This now passes.

Source/WebCore:

https://drafts.csswg.org/css-scoping-1/#shadow-cascading

"When comparing two declarations that have different tree contexts, then for normal rules the declaration earlier
in the shadow-including tree order wins, and for important rules the declaration coming later in the shadow-including
tree order wins."

* css/ElementRuleCollector.cpp:
(WebCore::MatchRequest::MatchRequest):
(WebCore::ElementRuleCollector::ElementRuleCollector):
(WebCore::ElementRuleCollector::matchedRuleList):
(WebCore::ElementRuleCollector::addMatchedRule):
(WebCore::ElementRuleCollector::clearMatchedRules):
(WebCore::ElementRuleCollector::addElementStyleProperties):
(WebCore::ElementRuleCollector::sortAndTransferMatchedRules):

    Pass the tree context ordinal onwards so we can implement the order reversal semantics for !important properties.

(WebCore::ElementRuleCollector::matchAuthorRules):
(WebCore::ElementRuleCollector::matchHostPseudoClassRules):
(WebCore::ElementRuleCollector::matchSlottedPseudoElementRules):

    Don't flush the matched rules immediately to get the right ordering. Instead compute tree context ordinal
    that is then used as a sorting criteria.

(WebCore::ElementRuleCollector::collectSlottedPseudoElementRulesForSlot):
(WebCore::ElementRuleCollector::collectMatchingRulesForList):
(WebCore::compareRules):

    Sort regular rules so that earlier shadow trees win.

(WebCore::ElementRuleCollector::sortMatchedRules):
* css/ElementRuleCollector.h:
* css/StyleResolver.cpp:
(WebCore::StyleResolver::State::clear):
(WebCore::StyleResolver::MatchResult::addMatchedProperties):
(WebCore::StyleResolver::CascadedProperties::addMatch): Added.

    Add a helper.

(WebCore::StyleResolver::CascadedProperties::addNormalMatches):

    Split normal and important to different functions for clarity.

(WebCore::StyleResolver::CascadedProperties::addImportantMatches):

    For !important properties a later shadow tree wins. Do multiple passes to apply in correct order if needed.

* css/StyleResolver.h:
(WebCore::StyleResolver::MatchResult::matchedProperties):

Modified Paths

Diff

Modified: trunk/LayoutTests/imported/w3c/ChangeLog (201072 => 201073)


--- trunk/LayoutTests/imported/w3c/ChangeLog	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/LayoutTests/imported/w3c/ChangeLog	2016-05-18 08:41:50 UTC (rev 201073)
@@ -1,3 +1,13 @@
+2016-05-18  Antti Koivisto  <an...@apple.com>
+
+        Cascading order for !important properties in ::slotted and ::host rules is incorrect
+        https://bugs.webkit.org/show_bug.cgi?id=157789
+        <rdar://problem/26318781>
+
+        Reviewed by Andreas Kling.
+
+        * csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt: This now passes.
+
 2016-05-11  Brady Eidson  <beid...@apple.com>
 
         Modern IDB: WebWorker support.

Modified: trunk/LayoutTests/imported/w3c/csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt (201072 => 201073)


--- trunk/LayoutTests/imported/w3c/csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/LayoutTests/imported/w3c/csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt	2016-05-18 08:41:50 UTC (rev 201073)
@@ -17,14 +17,14 @@
 PASS C4. ::slotted vs :host with !important, later in tree-of-trees rule should win for open mode. 
 PASS C5. ::slotted with !important vs inline, ::slotted rule should win for open mode. 
 PASS C6. :host with !important vs inline, :host rule should win for open mode. 
-FAIL D1. document vs ::slotted both with !important, ::slotted rule should win for open mode. assert_equals: D1. document vs ::slotted both with !important, ::slotted rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL D2. document vs :host both with !important, :host rule should win for open mode. assert_equals: D2. document vs :host both with !important, :host rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
+PASS D1. document vs ::slotted both with !important, ::slotted rule should win for open mode. 
+PASS D2. document vs :host both with !important, :host rule should win for open mode. 
 PASS D3. document vs inline both with !important, inline rule should win for open mode. 
-FAIL D4. ::slotted vs :host both with !important, later in tree-of-trees rule should win for open mode. assert_equals: D4. ::slotted vs :host both with !important, later in tree-of-trees rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL D5. ::slotted vs inline both with !important, ::slotted rule should win for open mode. assert_equals: D5. ::slotted vs inline both with !important, ::slotted rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL D6. :host vs inline both with !important, :host rule should win for open mode. assert_equals: D6. :host vs inline both with !important, :host rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
+PASS D4. ::slotted vs :host both with !important, later in tree-of-trees rule should win for open mode. 
+PASS D5. ::slotted vs inline both with !important, ::slotted rule should win for open mode. 
+PASS D6. :host vs inline both with !important, :host rule should win for open mode. 
 PASS E1. all style applied, inline rule should win for open mode. 
-FAIL E2. all styles with !important applied, rule in the last tree-of-trees should win for open mode. assert_equals: E2. all styles with !important applied, rule in the last tree-of-trees should win expected "rgb(0, 128, 0)" but got "rgb(255, 255, 0)"
+PASS E2. all styles with !important applied, rule in the last tree-of-trees should win for open mode. 
 PASS A1. document vs ::slotted, document rule should win for closed mode. 
 PASS A2. document vs :host, document rule should win for closed mode. 
 PASS A3. document vs inline, inline rule should win for closed mode. 
@@ -43,24 +43,24 @@
 PASS C4. ::slotted vs :host with !important, later in tree-of-trees rule should win for closed mode. 
 PASS C5. ::slotted with !important vs inline, ::slotted rule should win for closed mode. 
 PASS C6. :host with !important vs inline, :host rule should win for closed mode. 
-FAIL D1. document vs ::slotted both with !important, ::slotted rule should win for closed mode. assert_equals: D1. document vs ::slotted both with !important, ::slotted rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL D2. document vs :host both with !important, :host rule should win for closed mode. assert_equals: D2. document vs :host both with !important, :host rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
+PASS D1. document vs ::slotted both with !important, ::slotted rule should win for closed mode. 
+PASS D2. document vs :host both with !important, :host rule should win for closed mode. 
 PASS D3. document vs inline both with !important, inline rule should win for closed mode. 
-FAIL D4. ::slotted vs :host both with !important, later in tree-of-trees rule should win for closed mode. assert_equals: D4. ::slotted vs :host both with !important, later in tree-of-trees rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL D5. ::slotted vs inline both with !important, ::slotted rule should win for closed mode. assert_equals: D5. ::slotted vs inline both with !important, ::slotted rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
-FAIL D6. :host vs inline both with !important, :host rule should win for closed mode. assert_equals: D6. :host vs inline both with !important, :host rule should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
+PASS D4. ::slotted vs :host both with !important, later in tree-of-trees rule should win for closed mode. 
+PASS D5. ::slotted vs inline both with !important, ::slotted rule should win for closed mode. 
+PASS D6. :host vs inline both with !important, :host rule should win for closed mode. 
 PASS E1. all style applied, inline rule should win for closed mode. 
-FAIL E2. all styles with !important applied, rule in the last tree-of-trees should win for closed mode. assert_equals: E2. all styles with !important applied, rule in the last tree-of-trees should win expected "rgb(0, 128, 0)" but got "rgb(255, 255, 0)"
+PASS E2. all styles with !important applied, rule in the last tree-of-trees should win for closed mode. 
 PASS F1. document vs others, document (the first rule in tree-of-trees order) rule should win for open mode. 
 PASS F2. document with !important vs others, document rule should win for open mode. 
 PASS F3. document vs ::slotted with !important, important rule should win for open mode. 
 PASS F4. document vs ::slotted with !important, important rule should win for open mode. 
 PASS F5. document vs :host with !important, important rule should win for open mode. 
-FAIL F6. all rules with !important, the last rule in tree-of-trees should win for open mode. assert_equals: F6. all rules with !important, the last rule in tree-of-trees should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
+PASS F6. all rules with !important, the last rule in tree-of-trees should win for open mode. 
 PASS F1. document vs others, document (the first rule in tree-of-trees order) rule should win for closed mode. 
 PASS F2. document with !important vs others, document rule should win for closed mode. 
 PASS F3. document vs ::slotted with !important, important rule should win for closed mode. 
 PASS F4. document vs ::slotted with !important, important rule should win for closed mode. 
 PASS F5. document vs :host with !important, important rule should win for closed mode. 
-FAIL F6. all rules with !important, the last rule in tree-of-trees should win for closed mode. assert_equals: F6. all rules with !important, the last rule in tree-of-trees should win expected "rgb(0, 128, 0)" but got "rgb(255, 0, 0)"
+PASS F6. all rules with !important, the last rule in tree-of-trees should win for closed mode. 
 

Modified: trunk/Source/WebCore/ChangeLog (201072 => 201073)


--- trunk/Source/WebCore/ChangeLog	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/Source/WebCore/ChangeLog	2016-05-18 08:41:50 UTC (rev 201073)
@@ -1,3 +1,61 @@
+2016-05-17  Antti Koivisto  <an...@apple.com>
+
+        Cascading order for !important properties in ::slotted and ::host rules is incorrect
+        https://bugs.webkit.org/show_bug.cgi?id=157789
+        <rdar://problem/26318781>
+
+        Reviewed by Andreas Kling.
+
+        https://drafts.csswg.org/css-scoping-1/#shadow-cascading
+
+        "When comparing two declarations that have different tree contexts, then for normal rules the declaration earlier
+        in the shadow-including tree order wins, and for important rules the declaration coming later in the shadow-including
+        tree order wins."
+
+        * css/ElementRuleCollector.cpp:
+        (WebCore::MatchRequest::MatchRequest):
+        (WebCore::ElementRuleCollector::ElementRuleCollector):
+        (WebCore::ElementRuleCollector::matchedRuleList):
+        (WebCore::ElementRuleCollector::addMatchedRule):
+        (WebCore::ElementRuleCollector::clearMatchedRules):
+        (WebCore::ElementRuleCollector::addElementStyleProperties):
+        (WebCore::ElementRuleCollector::sortAndTransferMatchedRules):
+
+            Pass the tree context ordinal onwards so we can implement the order reversal semantics for !important properties.
+
+        (WebCore::ElementRuleCollector::matchAuthorRules):
+        (WebCore::ElementRuleCollector::matchHostPseudoClassRules):
+        (WebCore::ElementRuleCollector::matchSlottedPseudoElementRules):
+
+            Don't flush the matched rules immediately to get the right ordering. Instead compute tree context ordinal
+            that is then used as a sorting criteria.
+
+        (WebCore::ElementRuleCollector::collectSlottedPseudoElementRulesForSlot):
+        (WebCore::ElementRuleCollector::collectMatchingRulesForList):
+        (WebCore::compareRules):
+
+            Sort regular rules so that earlier shadow trees win.
+
+        (WebCore::ElementRuleCollector::sortMatchedRules):
+        * css/ElementRuleCollector.h:
+        * css/StyleResolver.cpp:
+        (WebCore::StyleResolver::State::clear):
+        (WebCore::StyleResolver::MatchResult::addMatchedProperties):
+        (WebCore::StyleResolver::CascadedProperties::addMatch): Added.
+
+            Add a helper.
+
+        (WebCore::StyleResolver::CascadedProperties::addNormalMatches):
+
+            Split normal and important to different functions for clarity.
+
+        (WebCore::StyleResolver::CascadedProperties::addImportantMatches):
+
+            For !important properties a later shadow tree wins. Do multiple passes to apply in correct order if needed.
+
+        * css/StyleResolver.h:
+        (WebCore::StyleResolver::MatchResult::matchedProperties):
+
 2016-05-18  Joanmarie Diggs  <jdi...@igalia.com>
 
         AX: [ATK] Use WebCore Accessibility's AccessibilityText for AtkObject name and description

Modified: trunk/Source/WebCore/css/ElementRuleCollector.cpp (201072 => 201073)


--- trunk/Source/WebCore/css/ElementRuleCollector.cpp	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/Source/WebCore/css/ElementRuleCollector.cpp	2016-05-18 08:41:50 UTC (rev 201073)
@@ -69,13 +69,15 @@
 
 class MatchRequest {
 public:
-    MatchRequest(const RuleSet* ruleSet, bool includeEmptyRules = false)
+    MatchRequest(const RuleSet* ruleSet, bool includeEmptyRules = false, unsigned treeContextOrdinal = 0)
         : ruleSet(ruleSet)
         , includeEmptyRules(includeEmptyRules)
+        , treeContextOrdinal(treeContextOrdinal)
     {
     }
     const RuleSet* ruleSet;
     const bool includeEmptyRules;
+    unsigned treeContextOrdinal;
 };
 
 ElementRuleCollector::ElementRuleCollector(const Element& element, const DocumentRuleSets& ruleSets, const SelectorFilter* selectorFilter)
@@ -107,19 +109,23 @@
     return m_matchedRuleList;
 }
 
-inline void ElementRuleCollector::addMatchedRule(const RuleData& ruleData, unsigned specificity, StyleResolver::RuleRange& ruleRange)
+inline void ElementRuleCollector::addMatchedRule(const RuleData& ruleData, unsigned specificity, unsigned treeContextOrdinal, StyleResolver::RuleRange& ruleRange)
 {
     // Update our first/last rule indices in the matched rules array.
     ++ruleRange.lastRuleIndex;
     if (ruleRange.firstRuleIndex == -1)
         ruleRange.firstRuleIndex = ruleRange.lastRuleIndex;
 
-    m_matchedRules.append({ &ruleData, specificity });
+    m_matchedRules.append({ &ruleData, specificity, treeContextOrdinal });
 }
 
 void ElementRuleCollector::clearMatchedRules()
 {
     m_matchedRules.clear();
+#if ENABLE(SHADOW_DOM)
+    m_keepAliveSlottedPseudoElementRules.clear();
+#endif
+
 }
 
 inline void ElementRuleCollector::addElementStyleProperties(const StyleProperties* propertySet, bool isCacheable)
@@ -199,74 +205,70 @@
     }
 
     for (const MatchedRule& matchedRule : m_matchedRules) {
-        m_result.addMatchedProperties(matchedRule.ruleData->rule()->properties(), matchedRule.ruleData->rule(), matchedRule.ruleData->linkMatchType(), matchedRule.ruleData->propertyWhitelistType());
+        m_result.addMatchedProperties(matchedRule.ruleData->rule()->properties(), matchedRule.ruleData->rule(), matchedRule.ruleData->linkMatchType(), matchedRule.ruleData->propertyWhitelistType(), matchedRule.treeContextOrdinal);
     }
 }
 
 void ElementRuleCollector::matchAuthorRules(bool includeEmptyRules)
 {
-#if ENABLE(SHADOW_DOM)
-    if (m_element.shadowRoot())
-        matchHostPseudoClassRules(includeEmptyRules);
-
-    auto* parent = m_element.parentNode();
-    if (parent && parent->shadowRoot())
-        matchSlottedPseudoElementRules(includeEmptyRules);
-#endif
-
     clearMatchedRules();
+
     m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
+    StyleResolver::RuleRange ruleRange = m_result.ranges.authorRuleRange();
 
     // Match global author rules.
     MatchRequest matchRequest(&m_authorStyle, includeEmptyRules);
-    StyleResolver::RuleRange ruleRange = m_result.ranges.authorRuleRange();
     collectMatchingRules(matchRequest, ruleRange);
     collectMatchingRulesForRegion(matchRequest, ruleRange);
 
+#if ENABLE(SHADOW_DOM)
+    auto* parent = m_element.parentElement();
+    if (parent && parent->shadowRoot())
+        matchSlottedPseudoElementRules(matchRequest, ruleRange);
+
+    if (m_element.shadowRoot())
+        matchHostPseudoClassRules(matchRequest, ruleRange);
+#endif
+
     sortAndTransferMatchedRules();
 }
 
 #if ENABLE(SHADOW_DOM)
-void ElementRuleCollector::matchHostPseudoClassRules(bool includeEmptyRules)
+void ElementRuleCollector::matchHostPseudoClassRules(MatchRequest& matchRequest, StyleResolver::RuleRange& ruleRange)
 {
     ASSERT(m_element.shadowRoot());
+
+    matchRequest.treeContextOrdinal++;
+
     auto& shadowAuthorStyle = *m_element.shadowRoot()->styleResolver().ruleSets().authorStyle();
     auto& shadowHostRules = shadowAuthorStyle.hostPseudoClassRules();
     if (shadowHostRules.isEmpty())
         return;
 
-    clearMatchedRules();
-    m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
-
     SelectorChecker::CheckingContext context(m_mode);
     SelectorChecker selectorChecker(m_element.document());
 
-    auto ruleRange = m_result.ranges.authorRuleRange();
     for (auto& ruleData : shadowHostRules) {
-        if (ruleData.rule()->properties().isEmpty() && !includeEmptyRules)
+        if (ruleData.rule()->properties().isEmpty() && !matchRequest.includeEmptyRules)
             continue;
         auto& selector = *ruleData.selector();
         unsigned specificity = 0;
         if (!selectorChecker.matchHostPseudoClass(selector, m_element, context, specificity))
             continue;
-        addMatchedRule(ruleData, specificity, ruleRange);
+        addMatchedRule(ruleData, specificity, matchRequest.treeContextOrdinal, ruleRange);
     }
-
-    // We just sort the host rules before other author rules. This matches the current vague spec language
-    // but is not necessarily exactly what is needed.
-    // FIXME: Match the spec when it is finalized.
-    sortAndTransferMatchedRules();
 }
 
-void ElementRuleCollector::matchSlottedPseudoElementRules(bool includeEmptyRules)
+void ElementRuleCollector::matchSlottedPseudoElementRules(MatchRequest& matchRequest, StyleResolver::RuleRange& ruleRange)
 {
-    RuleSet::RuleDataVector slottedPseudoElementRules;
-
     auto* maybeSlotted = &m_element;
     for (auto* hostShadowRoot = m_element.parentNode()->shadowRoot(); hostShadowRoot; hostShadowRoot = maybeSlotted->parentNode()->shadowRoot()) {
         auto* slot = hostShadowRoot->findAssignedSlot(*maybeSlotted);
         if (!slot)
-            break;
+            return;
+
+        matchRequest.treeContextOrdinal++;
+
         // In nested case the slot may itself be assigned to a slot. Collect ::slotted rules from all the nested trees.
         maybeSlotted = slot;
         auto* shadowAuthorStyle = hostShadowRoot->styleResolver().ruleSets().authorStyle();
@@ -275,29 +277,20 @@
         // Find out if there are any ::slotted rules in the shadow tree matching the current slot.
         // FIXME: This is really part of the slot style and could be cached when resolving it.
         ElementRuleCollector collector(*slot, *shadowAuthorStyle, nullptr);
-        slottedPseudoElementRules.appendVector(collector.collectSlottedPseudoElementRulesForSlot(includeEmptyRules));
-    }
-
-    if (slottedPseudoElementRules.isEmpty())
-        return;
-
-    clearMatchedRules();
-    m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
-
-    {
+        auto slottedPseudoElementRules = collector.collectSlottedPseudoElementRulesForSlot(matchRequest.includeEmptyRules);
+        if (!slottedPseudoElementRules)
+            continue;
         // Match in the current scope.
         TemporaryChange<bool> change(m_isMatchingSlottedPseudoElements, true);
 
-        MatchRequest matchRequest(nullptr, includeEmptyRules);
-        auto ruleRange = m_result.ranges.authorRuleRange();
-        collectMatchingRulesForList(&slottedPseudoElementRules, matchRequest, ruleRange);
-    }
+        MatchRequest scopeMatchRequest(nullptr, matchRequest.includeEmptyRules, matchRequest.treeContextOrdinal);
+        collectMatchingRulesForList(slottedPseudoElementRules.get(), scopeMatchRequest, ruleRange);
 
-    // FIXME: What is the correct order?
-    sortAndTransferMatchedRules();
+        m_keepAliveSlottedPseudoElementRules.append(WTFMove(slottedPseudoElementRules));
+    }
 }
 
-RuleSet::RuleDataVector ElementRuleCollector::collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules)
+std::unique_ptr<RuleSet::RuleDataVector> ElementRuleCollector::collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules)
 {
     ASSERT(is<HTMLSlotElement>(m_element));
 
@@ -313,10 +306,11 @@
     if (m_matchedRules.isEmpty())
         return { };
 
-    RuleSet::RuleDataVector ruleDataVector;
-    ruleDataVector.reserveInitialCapacity(m_matchedRules.size());
+    auto ruleDataVector = std::make_unique<RuleSet::RuleDataVector>();
+    ruleDataVector->reserveInitialCapacity(m_matchedRules.size());
     for (auto& matchedRule : m_matchedRules)
-        ruleDataVector.uncheckedAppend(*matchedRule.ruleData);
+        ruleDataVector->uncheckedAppend(*matchedRule.ruleData);
+
     return ruleDataVector;
 }
 #endif
@@ -503,15 +497,20 @@
 
         unsigned specificity;
         if (ruleMatches(ruleData, specificity))
-            addMatchedRule(ruleData, specificity, ruleRange);
+            addMatchedRule(ruleData, specificity, matchRequest.treeContextOrdinal, ruleRange);
     }
 }
 
 static inline bool compareRules(MatchedRule r1, MatchedRule r2)
 {
-    unsigned specificity1 = r1.specificity;
-    unsigned specificity2 = r2.specificity;
-    return (specificity1 == specificity2) ? r1.ruleData->position() < r2.ruleData->position() : specificity1 < specificity2;
+    // For normal properties the earlier tree wins. This may be reversed by !important which is handled when resolving cascade.
+    if (r1.treeContextOrdinal != r2.treeContextOrdinal)
+        return r1.treeContextOrdinal > r2.treeContextOrdinal;
+
+    if (r1.specificity != r2.specificity)
+        return r1.specificity < r2.specificity;
+
+    return r1.ruleData->position() < r2.ruleData->position();
 }
 
 void ElementRuleCollector::sortMatchedRules()

Modified: trunk/Source/WebCore/css/ElementRuleCollector.h (201072 => 201073)


--- trunk/Source/WebCore/css/ElementRuleCollector.h	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/Source/WebCore/css/ElementRuleCollector.h	2016-05-18 08:41:50 UTC (rev 201073)
@@ -40,7 +40,8 @@
 
 struct MatchedRule {
     const RuleData* ruleData;
-    unsigned specificity;
+    unsigned specificity;   
+    unsigned treeContextOrdinal;
 };
 
 class ElementRuleCollector {
@@ -76,9 +77,9 @@
 
     void matchUARules(RuleSet*);
 #if ENABLE(SHADOW_DOM)
-    void matchHostPseudoClassRules(bool includeEmptyRules);
-    void matchSlottedPseudoElementRules(bool includeEmptyRules);
-    RuleSet::RuleDataVector collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules);
+    void matchHostPseudoClassRules(MatchRequest&, StyleResolver::RuleRange&);
+    void matchSlottedPseudoElementRules(MatchRequest&, StyleResolver::RuleRange&);
+    std::unique_ptr<RuleSet::RuleDataVector> collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules);
 #endif
 
     void collectMatchingRules(const MatchRequest&, StyleResolver::RuleRange&);
@@ -89,7 +90,7 @@
     void sortMatchedRules();
     void sortAndTransferMatchedRules();
 
-    void addMatchedRule(const RuleData&, unsigned specificity, StyleResolver::RuleRange&);
+    void addMatchedRule(const RuleData&, unsigned specificity, unsigned treeContextOrdinal, StyleResolver::RuleRange&);
 
     const Element& m_element;
     const RuleSet& m_authorStyle;
@@ -103,6 +104,7 @@
     SelectorChecker::Mode m_mode { SelectorChecker::Mode::ResolvingStyle };
 #if ENABLE(SHADOW_DOM)
     bool m_isMatchingSlottedPseudoElements { false };
+    Vector<std::unique_ptr<RuleSet::RuleDataVector>> m_keepAliveSlottedPseudoElementRules;
 #endif
 
     Vector<MatchedRule, 64> m_matchedRules;

Modified: trunk/Source/WebCore/css/StyleResolver.cpp (201072 => 201073)


--- trunk/Source/WebCore/css/StyleResolver.cpp	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/Source/WebCore/css/StyleResolver.cpp	2016-05-18 08:41:50 UTC (rev 201073)
@@ -197,15 +197,20 @@
     m_cssToLengthConversionData = CSSToLengthConversionData();
 }
 
-void StyleResolver::MatchResult::addMatchedProperties(const StyleProperties& properties, StyleRule* rule, unsigned linkMatchType, PropertyWhitelistType propertyWhitelistType)
+void StyleResolver::MatchResult::addMatchedProperties(const StyleProperties& properties, StyleRule* rule, unsigned linkMatchType, PropertyWhitelistType propertyWhitelistType, unsigned treeContextOrdinal)
 {
     m_matchedProperties.grow(m_matchedProperties.size() + 1);
     StyleResolver::MatchedProperties& newProperties = m_matchedProperties.last();
     newProperties.properties = const_cast<StyleProperties*>(&properties);
     newProperties.linkMatchType = linkMatchType;
     newProperties.whitelistType = propertyWhitelistType;
+    newProperties.treeContextOrdinal = treeContextOrdinal;
     matchedRules.append(rule);
 
+    // Ordinal is relative to the currently matched element
+    if (treeContextOrdinal)
+        isCacheable = false;
+
     if (isCacheable) {
         for (unsigned i = 0, count = properties.propertyCount(); i < count; ++i) {
             // Currently the property cache only copy the non-inherited values and resolve
@@ -463,7 +468,7 @@
     // We don't need to bother with !important. Since there is only ever one
     // decl, there's nothing to override. So just add the first properties.
     CascadedProperties cascade(direction, writingMode);
-    cascade.addMatches(result, false, 0, result.matchedProperties().size() - 1);
+    cascade.addNormalMatches(result, 0, result.matchedProperties().size() - 1);
     
     // Resolve custom properties first.
     applyCascadedProperties(cascade, CSSPropertyCustom, CSSPropertyCustom, &result);
@@ -631,7 +636,7 @@
     extractDirectionAndWritingMode(*m_state.style(), result, direction, writingMode);
 
     CascadedProperties cascade(direction, writingMode);
-    cascade.addMatches(result, false, 0, result.matchedProperties().size() - 1);
+    cascade.addNormalMatches(result, 0, result.matchedProperties().size() - 1);
 
     // Resolve custom properties first.
     applyCascadedProperties(cascade, CSSPropertyCustom, CSSPropertyCustom, &result);
@@ -1324,8 +1329,8 @@
         // If so, we cache the border and background styles so that RenderTheme::adjustStyle()
         // can look at them later to figure out if this is a styled form control or not.
         CascadedProperties cascade(direction, writingMode);
-        cascade.addMatches(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
-        cascade.addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
+        cascade.addNormalMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
+        cascade.addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
 
         applyCascadedProperties(cascade, CSSPropertyWebkitRubyPosition, CSSPropertyWebkitRubyPosition, &matchResult);
         adjustStyleForInterCharacterRuby();
@@ -1343,10 +1348,10 @@
     }
 
     CascadedProperties cascade(direction, writingMode);
-    cascade.addMatches(matchResult, false, 0, matchResult.matchedProperties().size() - 1, applyInheritedOnly);
-    cascade.addMatches(matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
-    cascade.addMatches(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
-    cascade.addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
+    cascade.addNormalMatches(matchResult, 0, matchResult.matchedProperties().size() - 1, applyInheritedOnly);
+    cascade.addImportantMatches(matchResult, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
+    cascade.addImportantMatches(matchResult, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
+    cascade.addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
     
     // Resolve custom properties first.
     applyCascadedProperties(cascade, CSSPropertyCustom, CSSPropertyCustom, &matchResult);
@@ -1522,10 +1527,10 @@
         auto newAuthorRollback(std::make_unique<CascadedProperties>(direction, writingMode));
         
         // This special rollback cascade contains UA rules and user rules but no author rules.
-        newAuthorRollback->addMatches(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
-        newAuthorRollback->addMatches(matchResult, false, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
-        newAuthorRollback->addMatches(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
-        newAuthorRollback->addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
+        newAuthorRollback->addNormalMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
+        newAuthorRollback->addNormalMatches(matchResult, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
+        newAuthorRollback->addImportantMatches(matchResult, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
+        newAuthorRollback->addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
     
         state().setAuthorRollback(newAuthorRollback);
         return state().authorRollback();
@@ -1539,8 +1544,8 @@
         auto newUserRollback(std::make_unique<CascadedProperties>(direction, writingMode));
         
         // This special rollback cascade contains only UA rules.
-        newUserRollback->addMatches(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
-        newUserRollback->addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
+        newUserRollback->addNormalMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
+        newUserRollback->addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
     
         state().setUserRollback(newUserRollback);
         return state().userRollback();
@@ -2380,18 +2385,59 @@
     return AuthorLevel;
 }
 
-void StyleResolver::CascadedProperties::addMatches(const MatchResult& matchResult, bool important, int startIndex, int endIndex, bool inheritedOnly)
+void StyleResolver::CascadedProperties::addMatch(const MatchResult& matchResult, unsigned index, bool isImportant, bool inheritedOnly)
 {
+    const MatchedProperties& matchedProperties = matchResult.matchedProperties()[index];
+
+    auto propertyWhitelistType = static_cast<PropertyWhitelistType>(matchedProperties.whitelistType);
+    auto cascadeLevel = cascadeLevelForIndex(matchResult, index);
+
+    addStyleProperties(*matchedProperties.properties, *matchResult.matchedRules[index], isImportant, inheritedOnly, propertyWhitelistType, matchedProperties.linkMatchType, cascadeLevel);
+}
+
+void StyleResolver::CascadedProperties::addNormalMatches(const MatchResult& matchResult, int startIndex, int endIndex, bool inheritedOnly)
+{
     if (startIndex == -1)
         return;
 
-    for (int i = startIndex; i <= endIndex; ++i) {
-        const MatchedProperties& matchedProperties = matchResult.matchedProperties()[i];
-        addStyleProperties(*matchedProperties.properties, *matchResult.matchedRules[i], important, inheritedOnly, static_cast<PropertyWhitelistType>(matchedProperties.whitelistType), matchedProperties.linkMatchType,
-            cascadeLevelForIndex(matchResult, i));
+    for (int i = startIndex; i <= endIndex; ++i)
+        addMatch(matchResult, i, false, inheritedOnly);
+}
+
+static bool hasImportantProperties(const StyleProperties& properties)
+{
+    for (unsigned i = 0, count = properties.propertyCount(); i < count; ++i) {
+        if (properties.propertyAt(i).isImportant())
+            return true;
     }
+    return false;
 }
 
+void StyleResolver::CascadedProperties::addImportantMatches(const MatchResult& matchResult, int startIndex, int endIndex, bool inheritedOnly)
+{
+    if (startIndex == -1)
+        return;
+
+    unsigned highestTreeContextOrdinal = 0;
+    for (unsigned treeContextPass = 0; treeContextPass <= highestTreeContextOrdinal; ++treeContextPass) {
+        for (int i = startIndex; i <= endIndex; ++i) {
+            const MatchedProperties& matchedProperties = matchResult.matchedProperties()[i];
+
+            if (!hasImportantProperties(*matchedProperties.properties))
+                continue;
+
+            // For !important properties a later shadow tree wins. Do multiple passes to apply in correct order if needed.
+            // Matched properties are sorted in reverse tree context order so this is not needed for normal properties.
+            if (matchedProperties.treeContextOrdinal != treeContextPass) {
+                highestTreeContextOrdinal = std::max(matchedProperties.treeContextOrdinal, highestTreeContextOrdinal);
+                continue;
+            }
+
+            addMatch(matchResult, i, true, inheritedOnly);
+        }
+    }
+}
+
 void StyleResolver::CascadedProperties::applyDeferredProperties(StyleResolver& resolver, const MatchResult* matchResult)
 {
     for (auto& property : m_deferredProperties)

Modified: trunk/Source/WebCore/css/StyleResolver.h (201072 => 201073)


--- trunk/Source/WebCore/css/StyleResolver.h	2016-05-18 07:12:09 UTC (rev 201072)
+++ trunk/Source/WebCore/css/StyleResolver.h	2016-05-18 08:41:50 UTC (rev 201073)
@@ -257,6 +257,7 @@
             struct {
                 unsigned linkMatchType : 2;
                 unsigned whitelistType : 2;
+                unsigned treeContextOrdinal : 28;
             };
             // Used to make sure all memory is zero-initialized since we compute the hash over the bytes of this object.
             void* possiblyPaddedMember;
@@ -271,7 +272,7 @@
 
         const Vector<MatchedProperties, 64>& matchedProperties() const { return m_matchedProperties; }
 
-        void addMatchedProperties(const StyleProperties&, StyleRule* = nullptr, unsigned linkMatchType = SelectorChecker::MatchAll, PropertyWhitelistType = PropertyWhitelistNone);
+        void addMatchedProperties(const StyleProperties&, StyleRule* = nullptr, unsigned linkMatchType = SelectorChecker::MatchAll, PropertyWhitelistType = PropertyWhitelistNone, unsigned treeContextOrdinal = 0);
     private:
         Vector<MatchedProperties, 64> m_matchedProperties;
     };
@@ -291,8 +292,10 @@
 
         bool hasProperty(CSSPropertyID) const;
         Property& property(CSSPropertyID);
-        void addMatches(const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly = false);
 
+        void addNormalMatches(const MatchResult&, int startIndex, int endIndex, bool inheritedOnly = false);
+        void addImportantMatches(const MatchResult&, int startIndex, int endIndex, bool inheritedOnly = false);
+
         void set(CSSPropertyID, CSSValue&, unsigned linkMatchType, CascadeLevel);
         void setDeferred(CSSPropertyID, CSSValue&, unsigned linkMatchType, CascadeLevel);
 
@@ -303,6 +306,7 @@
         Property customProperty(const String&) const;
         
     private:
+        void addMatch(const MatchResult&, unsigned index, bool isImportant, bool inheritedOnly);
         void addStyleProperties(const StyleProperties&, StyleRule&, bool isImportant, bool inheritedOnly, PropertyWhitelistType, unsigned linkMatchType, CascadeLevel);
         static void setPropertyInternal(Property&, CSSPropertyID, CSSValue&, unsigned linkMatchType, CascadeLevel);
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to