<!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>[201073] 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/201073">201073</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2016-05-18 01:41:50 -0700 (Wed, 18 May 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Cascading order for !important properties in ::slotted and ::host rules is incorrect
https://bugs.webkit.org/show_bug.cgi?id=157789
&lt;rdar://problem/26318781&gt;

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

&quot;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.&quot;

* 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):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsimportedw3cChangeLog">trunk/LayoutTests/imported/w3c/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsimportedw3ccsswgtestcssscoping1shadowcascadeorder001expectedtxt">trunk/LayoutTests/imported/w3c/csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorecssElementRuleCollectorcpp">trunk/Source/WebCore/css/ElementRuleCollector.cpp</a></li>
<li><a href="#trunkSourceWebCorecssElementRuleCollectorh">trunk/Source/WebCore/css/ElementRuleCollector.h</a></li>
<li><a href="#trunkSourceWebCorecssStyleResolvercpp">trunk/Source/WebCore/css/StyleResolver.cpp</a></li>
<li><a href="#trunkSourceWebCorecssStyleResolverh">trunk/Source/WebCore/css/StyleResolver.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 (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2016-05-18  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Cascading order for !important properties in ::slotted and ::host rules is incorrect
+        https://bugs.webkit.org/show_bug.cgi?id=157789
+        &lt;rdar://problem/26318781&gt;
+
+        Reviewed by Andreas Kling.
+
+        * csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt: This now passes.
+
</ins><span class="cx"> 2016-05-11  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Modern IDB: WebWorker support.
</span></span></pre></div>
<a id="trunkLayoutTestsimportedw3ccsswgtestcssscoping1shadowcascadeorder001expectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/imported/w3c/csswg-test/css-scoping-1/shadow-cascade-order-001-expected.txt (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -17,14 +17,14 @@
</span><span class="cx"> PASS C4. ::slotted vs :host with !important, later in tree-of-trees rule should win for open mode. 
</span><span class="cx"> PASS C5. ::slotted with !important vs inline, ::slotted rule should win for open mode. 
</span><span class="cx"> PASS C6. :host with !important vs inline, :host rule should win for open mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
</del><ins>+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. 
</ins><span class="cx"> PASS D3. document vs inline both with !important, inline rule should win for open mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
</del><ins>+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. 
</ins><span class="cx"> PASS E1. all style applied, inline rule should win for open mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 255, 0)&quot;
</del><ins>+PASS E2. all styles with !important applied, rule in the last tree-of-trees should win for open mode. 
</ins><span class="cx"> PASS A1. document vs ::slotted, document rule should win for closed mode. 
</span><span class="cx"> PASS A2. document vs :host, document rule should win for closed mode. 
</span><span class="cx"> PASS A3. document vs inline, inline rule should win for closed mode. 
</span><span class="lines">@@ -43,24 +43,24 @@
</span><span class="cx"> PASS C4. ::slotted vs :host with !important, later in tree-of-trees rule should win for closed mode. 
</span><span class="cx"> PASS C5. ::slotted with !important vs inline, ::slotted rule should win for closed mode. 
</span><span class="cx"> PASS C6. :host with !important vs inline, :host rule should win for closed mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
</del><ins>+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. 
</ins><span class="cx"> PASS D3. document vs inline both with !important, inline rule should win for closed mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
</del><ins>+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. 
</ins><span class="cx"> PASS E1. all style applied, inline rule should win for closed mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 255, 0)&quot;
</del><ins>+PASS E2. all styles with !important applied, rule in the last tree-of-trees should win for closed mode. 
</ins><span class="cx"> PASS F1. document vs others, document (the first rule in tree-of-trees order) rule should win for open mode. 
</span><span class="cx"> PASS F2. document with !important vs others, document rule should win for open mode. 
</span><span class="cx"> PASS F3. document vs ::slotted with !important, important rule should win for open mode. 
</span><span class="cx"> PASS F4. document vs ::slotted with !important, important rule should win for open mode. 
</span><span class="cx"> PASS F5. document vs :host with !important, important rule should win for open mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
</del><ins>+PASS F6. all rules with !important, the last rule in tree-of-trees should win for open mode. 
</ins><span class="cx"> PASS F1. document vs others, document (the first rule in tree-of-trees order) rule should win for closed mode. 
</span><span class="cx"> PASS F2. document with !important vs others, document rule should win for closed mode. 
</span><span class="cx"> PASS F3. document vs ::slotted with !important, important rule should win for closed mode. 
</span><span class="cx"> PASS F4. document vs ::slotted with !important, important rule should win for closed mode. 
</span><span class="cx"> PASS F5. document vs :host with !important, important rule should win for closed mode. 
</span><del>-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 &quot;rgb(0, 128, 0)&quot; but got &quot;rgb(255, 0, 0)&quot;
</del><ins>+PASS F6. all rules with !important, the last rule in tree-of-trees should win for closed mode. 
</ins><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,3 +1,61 @@
</span><ins>+2016-05-17  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Cascading order for !important properties in ::slotted and ::host rules is incorrect
+        https://bugs.webkit.org/show_bug.cgi?id=157789
+        &lt;rdar://problem/26318781&gt;
+
+        Reviewed by Andreas Kling.
+
+        https://drafts.csswg.org/css-scoping-1/#shadow-cascading
+
+        &quot;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.&quot;
+
+        * 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):
+
</ins><span class="cx"> 2016-05-18  Joanmarie Diggs  &lt;jdiggs@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         AX: [ATK] Use WebCore Accessibility's AccessibilityText for AtkObject name and description
</span></span></pre></div>
<a id="trunkSourceWebCorecssElementRuleCollectorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/ElementRuleCollector.cpp (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -69,13 +69,15 @@
</span><span class="cx"> 
</span><span class="cx"> class MatchRequest {
</span><span class="cx"> public:
</span><del>-    MatchRequest(const RuleSet* ruleSet, bool includeEmptyRules = false)
</del><ins>+    MatchRequest(const RuleSet* ruleSet, bool includeEmptyRules = false, unsigned treeContextOrdinal = 0)
</ins><span class="cx">         : ruleSet(ruleSet)
</span><span class="cx">         , includeEmptyRules(includeEmptyRules)
</span><ins>+        , treeContextOrdinal(treeContextOrdinal)
</ins><span class="cx">     {
</span><span class="cx">     }
</span><span class="cx">     const RuleSet* ruleSet;
</span><span class="cx">     const bool includeEmptyRules;
</span><ins>+    unsigned treeContextOrdinal;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> ElementRuleCollector::ElementRuleCollector(const Element&amp; element, const DocumentRuleSets&amp; ruleSets, const SelectorFilter* selectorFilter)
</span><span class="lines">@@ -107,19 +109,23 @@
</span><span class="cx">     return m_matchedRuleList;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline void ElementRuleCollector::addMatchedRule(const RuleData&amp; ruleData, unsigned specificity, StyleResolver::RuleRange&amp; ruleRange)
</del><ins>+inline void ElementRuleCollector::addMatchedRule(const RuleData&amp; ruleData, unsigned specificity, unsigned treeContextOrdinal, StyleResolver::RuleRange&amp; ruleRange)
</ins><span class="cx"> {
</span><span class="cx">     // Update our first/last rule indices in the matched rules array.
</span><span class="cx">     ++ruleRange.lastRuleIndex;
</span><span class="cx">     if (ruleRange.firstRuleIndex == -1)
</span><span class="cx">         ruleRange.firstRuleIndex = ruleRange.lastRuleIndex;
</span><span class="cx"> 
</span><del>-    m_matchedRules.append({ &amp;ruleData, specificity });
</del><ins>+    m_matchedRules.append({ &amp;ruleData, specificity, treeContextOrdinal });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ElementRuleCollector::clearMatchedRules()
</span><span class="cx"> {
</span><span class="cx">     m_matchedRules.clear();
</span><ins>+#if ENABLE(SHADOW_DOM)
+    m_keepAliveSlottedPseudoElementRules.clear();
+#endif
+
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> inline void ElementRuleCollector::addElementStyleProperties(const StyleProperties* propertySet, bool isCacheable)
</span><span class="lines">@@ -199,74 +205,70 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     for (const MatchedRule&amp; matchedRule : m_matchedRules) {
</span><del>-        m_result.addMatchedProperties(matchedRule.ruleData-&gt;rule()-&gt;properties(), matchedRule.ruleData-&gt;rule(), matchedRule.ruleData-&gt;linkMatchType(), matchedRule.ruleData-&gt;propertyWhitelistType());
</del><ins>+        m_result.addMatchedProperties(matchedRule.ruleData-&gt;rule()-&gt;properties(), matchedRule.ruleData-&gt;rule(), matchedRule.ruleData-&gt;linkMatchType(), matchedRule.ruleData-&gt;propertyWhitelistType(), matchedRule.treeContextOrdinal);
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ElementRuleCollector::matchAuthorRules(bool includeEmptyRules)
</span><span class="cx"> {
</span><del>-#if ENABLE(SHADOW_DOM)
-    if (m_element.shadowRoot())
-        matchHostPseudoClassRules(includeEmptyRules);
-
-    auto* parent = m_element.parentNode();
-    if (parent &amp;&amp; parent-&gt;shadowRoot())
-        matchSlottedPseudoElementRules(includeEmptyRules);
-#endif
-
</del><span class="cx">     clearMatchedRules();
</span><ins>+
</ins><span class="cx">     m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
</span><ins>+    StyleResolver::RuleRange ruleRange = m_result.ranges.authorRuleRange();
</ins><span class="cx"> 
</span><span class="cx">     // Match global author rules.
</span><span class="cx">     MatchRequest matchRequest(&amp;m_authorStyle, includeEmptyRules);
</span><del>-    StyleResolver::RuleRange ruleRange = m_result.ranges.authorRuleRange();
</del><span class="cx">     collectMatchingRules(matchRequest, ruleRange);
</span><span class="cx">     collectMatchingRulesForRegion(matchRequest, ruleRange);
</span><span class="cx"> 
</span><ins>+#if ENABLE(SHADOW_DOM)
+    auto* parent = m_element.parentElement();
+    if (parent &amp;&amp; parent-&gt;shadowRoot())
+        matchSlottedPseudoElementRules(matchRequest, ruleRange);
+
+    if (m_element.shadowRoot())
+        matchHostPseudoClassRules(matchRequest, ruleRange);
+#endif
+
</ins><span class="cx">     sortAndTransferMatchedRules();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(SHADOW_DOM)
</span><del>-void ElementRuleCollector::matchHostPseudoClassRules(bool includeEmptyRules)
</del><ins>+void ElementRuleCollector::matchHostPseudoClassRules(MatchRequest&amp; matchRequest, StyleResolver::RuleRange&amp; ruleRange)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_element.shadowRoot());
</span><ins>+
+    matchRequest.treeContextOrdinal++;
+
</ins><span class="cx">     auto&amp; shadowAuthorStyle = *m_element.shadowRoot()-&gt;styleResolver().ruleSets().authorStyle();
</span><span class="cx">     auto&amp; shadowHostRules = shadowAuthorStyle.hostPseudoClassRules();
</span><span class="cx">     if (shadowHostRules.isEmpty())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    clearMatchedRules();
-    m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
-
</del><span class="cx">     SelectorChecker::CheckingContext context(m_mode);
</span><span class="cx">     SelectorChecker selectorChecker(m_element.document());
</span><span class="cx"> 
</span><del>-    auto ruleRange = m_result.ranges.authorRuleRange();
</del><span class="cx">     for (auto&amp; ruleData : shadowHostRules) {
</span><del>-        if (ruleData.rule()-&gt;properties().isEmpty() &amp;&amp; !includeEmptyRules)
</del><ins>+        if (ruleData.rule()-&gt;properties().isEmpty() &amp;&amp; !matchRequest.includeEmptyRules)
</ins><span class="cx">             continue;
</span><span class="cx">         auto&amp; selector = *ruleData.selector();
</span><span class="cx">         unsigned specificity = 0;
</span><span class="cx">         if (!selectorChecker.matchHostPseudoClass(selector, m_element, context, specificity))
</span><span class="cx">             continue;
</span><del>-        addMatchedRule(ruleData, specificity, ruleRange);
</del><ins>+        addMatchedRule(ruleData, specificity, matchRequest.treeContextOrdinal, ruleRange);
</ins><span class="cx">     }
</span><del>-
-    // 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();
</del><span class="cx"> }
</span><span class="cx"> 
</span><del>-void ElementRuleCollector::matchSlottedPseudoElementRules(bool includeEmptyRules)
</del><ins>+void ElementRuleCollector::matchSlottedPseudoElementRules(MatchRequest&amp; matchRequest, StyleResolver::RuleRange&amp; ruleRange)
</ins><span class="cx"> {
</span><del>-    RuleSet::RuleDataVector slottedPseudoElementRules;
-
</del><span class="cx">     auto* maybeSlotted = &amp;m_element;
</span><span class="cx">     for (auto* hostShadowRoot = m_element.parentNode()-&gt;shadowRoot(); hostShadowRoot; hostShadowRoot = maybeSlotted-&gt;parentNode()-&gt;shadowRoot()) {
</span><span class="cx">         auto* slot = hostShadowRoot-&gt;findAssignedSlot(*maybeSlotted);
</span><span class="cx">         if (!slot)
</span><del>-            break;
</del><ins>+            return;
+
+        matchRequest.treeContextOrdinal++;
+
</ins><span class="cx">         // In nested case the slot may itself be assigned to a slot. Collect ::slotted rules from all the nested trees.
</span><span class="cx">         maybeSlotted = slot;
</span><span class="cx">         auto* shadowAuthorStyle = hostShadowRoot-&gt;styleResolver().ruleSets().authorStyle();
</span><span class="lines">@@ -275,29 +277,20 @@
</span><span class="cx">         // Find out if there are any ::slotted rules in the shadow tree matching the current slot.
</span><span class="cx">         // FIXME: This is really part of the slot style and could be cached when resolving it.
</span><span class="cx">         ElementRuleCollector collector(*slot, *shadowAuthorStyle, nullptr);
</span><del>-        slottedPseudoElementRules.appendVector(collector.collectSlottedPseudoElementRulesForSlot(includeEmptyRules));
-    }
-
-    if (slottedPseudoElementRules.isEmpty())
-        return;
-
-    clearMatchedRules();
-    m_result.ranges.lastAuthorRule = m_result.matchedProperties().size() - 1;
-
-    {
</del><ins>+        auto slottedPseudoElementRules = collector.collectSlottedPseudoElementRulesForSlot(matchRequest.includeEmptyRules);
+        if (!slottedPseudoElementRules)
+            continue;
</ins><span class="cx">         // Match in the current scope.
</span><span class="cx">         TemporaryChange&lt;bool&gt; change(m_isMatchingSlottedPseudoElements, true);
</span><span class="cx"> 
</span><del>-        MatchRequest matchRequest(nullptr, includeEmptyRules);
-        auto ruleRange = m_result.ranges.authorRuleRange();
-        collectMatchingRulesForList(&amp;slottedPseudoElementRules, matchRequest, ruleRange);
-    }
</del><ins>+        MatchRequest scopeMatchRequest(nullptr, matchRequest.includeEmptyRules, matchRequest.treeContextOrdinal);
+        collectMatchingRulesForList(slottedPseudoElementRules.get(), scopeMatchRequest, ruleRange);
</ins><span class="cx"> 
</span><del>-    // FIXME: What is the correct order?
-    sortAndTransferMatchedRules();
</del><ins>+        m_keepAliveSlottedPseudoElementRules.append(WTFMove(slottedPseudoElementRules));
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-RuleSet::RuleDataVector ElementRuleCollector::collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules)
</del><ins>+std::unique_ptr&lt;RuleSet::RuleDataVector&gt; ElementRuleCollector::collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(is&lt;HTMLSlotElement&gt;(m_element));
</span><span class="cx"> 
</span><span class="lines">@@ -313,10 +306,11 @@
</span><span class="cx">     if (m_matchedRules.isEmpty())
</span><span class="cx">         return { };
</span><span class="cx"> 
</span><del>-    RuleSet::RuleDataVector ruleDataVector;
-    ruleDataVector.reserveInitialCapacity(m_matchedRules.size());
</del><ins>+    auto ruleDataVector = std::make_unique&lt;RuleSet::RuleDataVector&gt;();
+    ruleDataVector-&gt;reserveInitialCapacity(m_matchedRules.size());
</ins><span class="cx">     for (auto&amp; matchedRule : m_matchedRules)
</span><del>-        ruleDataVector.uncheckedAppend(*matchedRule.ruleData);
</del><ins>+        ruleDataVector-&gt;uncheckedAppend(*matchedRule.ruleData);
+
</ins><span class="cx">     return ruleDataVector;
</span><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="lines">@@ -503,15 +497,20 @@
</span><span class="cx"> 
</span><span class="cx">         unsigned specificity;
</span><span class="cx">         if (ruleMatches(ruleData, specificity))
</span><del>-            addMatchedRule(ruleData, specificity, ruleRange);
</del><ins>+            addMatchedRule(ruleData, specificity, matchRequest.treeContextOrdinal, ruleRange);
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static inline bool compareRules(MatchedRule r1, MatchedRule r2)
</span><span class="cx"> {
</span><del>-    unsigned specificity1 = r1.specificity;
-    unsigned specificity2 = r2.specificity;
-    return (specificity1 == specificity2) ? r1.ruleData-&gt;position() &lt; r2.ruleData-&gt;position() : specificity1 &lt; specificity2;
</del><ins>+    // 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 &gt; r2.treeContextOrdinal;
+
+    if (r1.specificity != r2.specificity)
+        return r1.specificity &lt; r2.specificity;
+
+    return r1.ruleData-&gt;position() &lt; r2.ruleData-&gt;position();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ElementRuleCollector::sortMatchedRules()
</span></span></pre></div>
<a id="trunkSourceWebCorecssElementRuleCollectorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/ElementRuleCollector.h (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -40,7 +40,8 @@
</span><span class="cx"> 
</span><span class="cx"> struct MatchedRule {
</span><span class="cx">     const RuleData* ruleData;
</span><del>-    unsigned specificity;
</del><ins>+    unsigned specificity;   
+    unsigned treeContextOrdinal;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> class ElementRuleCollector {
</span><span class="lines">@@ -76,9 +77,9 @@
</span><span class="cx"> 
</span><span class="cx">     void matchUARules(RuleSet*);
</span><span class="cx"> #if ENABLE(SHADOW_DOM)
</span><del>-    void matchHostPseudoClassRules(bool includeEmptyRules);
-    void matchSlottedPseudoElementRules(bool includeEmptyRules);
-    RuleSet::RuleDataVector collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules);
</del><ins>+    void matchHostPseudoClassRules(MatchRequest&amp;, StyleResolver::RuleRange&amp;);
+    void matchSlottedPseudoElementRules(MatchRequest&amp;, StyleResolver::RuleRange&amp;);
+    std::unique_ptr&lt;RuleSet::RuleDataVector&gt; collectSlottedPseudoElementRulesForSlot(bool includeEmptyRules);
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     void collectMatchingRules(const MatchRequest&amp;, StyleResolver::RuleRange&amp;);
</span><span class="lines">@@ -89,7 +90,7 @@
</span><span class="cx">     void sortMatchedRules();
</span><span class="cx">     void sortAndTransferMatchedRules();
</span><span class="cx"> 
</span><del>-    void addMatchedRule(const RuleData&amp;, unsigned specificity, StyleResolver::RuleRange&amp;);
</del><ins>+    void addMatchedRule(const RuleData&amp;, unsigned specificity, unsigned treeContextOrdinal, StyleResolver::RuleRange&amp;);
</ins><span class="cx"> 
</span><span class="cx">     const Element&amp; m_element;
</span><span class="cx">     const RuleSet&amp; m_authorStyle;
</span><span class="lines">@@ -103,6 +104,7 @@
</span><span class="cx">     SelectorChecker::Mode m_mode { SelectorChecker::Mode::ResolvingStyle };
</span><span class="cx"> #if ENABLE(SHADOW_DOM)
</span><span class="cx">     bool m_isMatchingSlottedPseudoElements { false };
</span><ins>+    Vector&lt;std::unique_ptr&lt;RuleSet::RuleDataVector&gt;&gt; m_keepAliveSlottedPseudoElementRules;
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     Vector&lt;MatchedRule, 64&gt; m_matchedRules;
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleResolvercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleResolver.cpp (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -197,15 +197,20 @@
</span><span class="cx">     m_cssToLengthConversionData = CSSToLengthConversionData();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void StyleResolver::MatchResult::addMatchedProperties(const StyleProperties&amp; properties, StyleRule* rule, unsigned linkMatchType, PropertyWhitelistType propertyWhitelistType)
</del><ins>+void StyleResolver::MatchResult::addMatchedProperties(const StyleProperties&amp; properties, StyleRule* rule, unsigned linkMatchType, PropertyWhitelistType propertyWhitelistType, unsigned treeContextOrdinal)
</ins><span class="cx"> {
</span><span class="cx">     m_matchedProperties.grow(m_matchedProperties.size() + 1);
</span><span class="cx">     StyleResolver::MatchedProperties&amp; newProperties = m_matchedProperties.last();
</span><span class="cx">     newProperties.properties = const_cast&lt;StyleProperties*&gt;(&amp;properties);
</span><span class="cx">     newProperties.linkMatchType = linkMatchType;
</span><span class="cx">     newProperties.whitelistType = propertyWhitelistType;
</span><ins>+    newProperties.treeContextOrdinal = treeContextOrdinal;
</ins><span class="cx">     matchedRules.append(rule);
</span><span class="cx"> 
</span><ins>+    // Ordinal is relative to the currently matched element
+    if (treeContextOrdinal)
+        isCacheable = false;
+
</ins><span class="cx">     if (isCacheable) {
</span><span class="cx">         for (unsigned i = 0, count = properties.propertyCount(); i &lt; count; ++i) {
</span><span class="cx">             // Currently the property cache only copy the non-inherited values and resolve
</span><span class="lines">@@ -463,7 +468,7 @@
</span><span class="cx">     // We don't need to bother with !important. Since there is only ever one
</span><span class="cx">     // decl, there's nothing to override. So just add the first properties.
</span><span class="cx">     CascadedProperties cascade(direction, writingMode);
</span><del>-    cascade.addMatches(result, false, 0, result.matchedProperties().size() - 1);
</del><ins>+    cascade.addNormalMatches(result, 0, result.matchedProperties().size() - 1);
</ins><span class="cx">     
</span><span class="cx">     // Resolve custom properties first.
</span><span class="cx">     applyCascadedProperties(cascade, CSSPropertyCustom, CSSPropertyCustom, &amp;result);
</span><span class="lines">@@ -631,7 +636,7 @@
</span><span class="cx">     extractDirectionAndWritingMode(*m_state.style(), result, direction, writingMode);
</span><span class="cx"> 
</span><span class="cx">     CascadedProperties cascade(direction, writingMode);
</span><del>-    cascade.addMatches(result, false, 0, result.matchedProperties().size() - 1);
</del><ins>+    cascade.addNormalMatches(result, 0, result.matchedProperties().size() - 1);
</ins><span class="cx"> 
</span><span class="cx">     // Resolve custom properties first.
</span><span class="cx">     applyCascadedProperties(cascade, CSSPropertyCustom, CSSPropertyCustom, &amp;result);
</span><span class="lines">@@ -1324,8 +1329,8 @@
</span><span class="cx">         // If so, we cache the border and background styles so that RenderTheme::adjustStyle()
</span><span class="cx">         // can look at them later to figure out if this is a styled form control or not.
</span><span class="cx">         CascadedProperties cascade(direction, writingMode);
</span><del>-        cascade.addMatches(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
-        cascade.addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
</del><ins>+        cascade.addNormalMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
+        cascade.addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
</ins><span class="cx"> 
</span><span class="cx">         applyCascadedProperties(cascade, CSSPropertyWebkitRubyPosition, CSSPropertyWebkitRubyPosition, &amp;matchResult);
</span><span class="cx">         adjustStyleForInterCharacterRuby();
</span><span class="lines">@@ -1343,10 +1348,10 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     CascadedProperties cascade(direction, writingMode);
</span><del>-    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);
</del><ins>+    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);
</ins><span class="cx">     
</span><span class="cx">     // Resolve custom properties first.
</span><span class="cx">     applyCascadedProperties(cascade, CSSPropertyCustom, CSSPropertyCustom, &amp;matchResult);
</span><span class="lines">@@ -1522,10 +1527,10 @@
</span><span class="cx">         auto newAuthorRollback(std::make_unique&lt;CascadedProperties&gt;(direction, writingMode));
</span><span class="cx">         
</span><span class="cx">         // This special rollback cascade contains UA rules and user rules but no author rules.
</span><del>-        newAuthorRollback-&gt;addMatches(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
-        newAuthorRollback-&gt;addMatches(matchResult, false, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
-        newAuthorRollback-&gt;addMatches(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
-        newAuthorRollback-&gt;addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
</del><ins>+        newAuthorRollback-&gt;addNormalMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
+        newAuthorRollback-&gt;addNormalMatches(matchResult, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
+        newAuthorRollback-&gt;addImportantMatches(matchResult, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, false);
+        newAuthorRollback-&gt;addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
</ins><span class="cx">     
</span><span class="cx">         state().setAuthorRollback(newAuthorRollback);
</span><span class="cx">         return state().authorRollback();
</span><span class="lines">@@ -1539,8 +1544,8 @@
</span><span class="cx">         auto newUserRollback(std::make_unique&lt;CascadedProperties&gt;(direction, writingMode));
</span><span class="cx">         
</span><span class="cx">         // This special rollback cascade contains only UA rules.
</span><del>-        newUserRollback-&gt;addMatches(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
-        newUserRollback-&gt;addMatches(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
</del><ins>+        newUserRollback-&gt;addNormalMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
+        newUserRollback-&gt;addImportantMatches(matchResult, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, false);
</ins><span class="cx">     
</span><span class="cx">         state().setUserRollback(newUserRollback);
</span><span class="cx">         return state().userRollback();
</span><span class="lines">@@ -2380,18 +2385,59 @@
</span><span class="cx">     return AuthorLevel;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void StyleResolver::CascadedProperties::addMatches(const MatchResult&amp; matchResult, bool important, int startIndex, int endIndex, bool inheritedOnly)
</del><ins>+void StyleResolver::CascadedProperties::addMatch(const MatchResult&amp; matchResult, unsigned index, bool isImportant, bool inheritedOnly)
</ins><span class="cx"> {
</span><ins>+    const MatchedProperties&amp; matchedProperties = matchResult.matchedProperties()[index];
+
+    auto propertyWhitelistType = static_cast&lt;PropertyWhitelistType&gt;(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&amp; matchResult, int startIndex, int endIndex, bool inheritedOnly)
+{
</ins><span class="cx">     if (startIndex == -1)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    for (int i = startIndex; i &lt;= endIndex; ++i) {
-        const MatchedProperties&amp; matchedProperties = matchResult.matchedProperties()[i];
-        addStyleProperties(*matchedProperties.properties, *matchResult.matchedRules[i], important, inheritedOnly, static_cast&lt;PropertyWhitelistType&gt;(matchedProperties.whitelistType), matchedProperties.linkMatchType,
-            cascadeLevelForIndex(matchResult, i));
</del><ins>+    for (int i = startIndex; i &lt;= endIndex; ++i)
+        addMatch(matchResult, i, false, inheritedOnly);
+}
+
+static bool hasImportantProperties(const StyleProperties&amp; properties)
+{
+    for (unsigned i = 0, count = properties.propertyCount(); i &lt; count; ++i) {
+        if (properties.propertyAt(i).isImportant())
+            return true;
</ins><span class="cx">     }
</span><ins>+    return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void StyleResolver::CascadedProperties::addImportantMatches(const MatchResult&amp; matchResult, int startIndex, int endIndex, bool inheritedOnly)
+{
+    if (startIndex == -1)
+        return;
+
+    unsigned highestTreeContextOrdinal = 0;
+    for (unsigned treeContextPass = 0; treeContextPass &lt;= highestTreeContextOrdinal; ++treeContextPass) {
+        for (int i = startIndex; i &lt;= endIndex; ++i) {
+            const MatchedProperties&amp; 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);
+        }
+    }
+}
+
</ins><span class="cx"> void StyleResolver::CascadedProperties::applyDeferredProperties(StyleResolver&amp; resolver, const MatchResult* matchResult)
</span><span class="cx"> {
</span><span class="cx">     for (auto&amp; property : m_deferredProperties)
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleResolverh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleResolver.h (201072 => 201073)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -257,6 +257,7 @@
</span><span class="cx">             struct {
</span><span class="cx">                 unsigned linkMatchType : 2;
</span><span class="cx">                 unsigned whitelistType : 2;
</span><ins>+                unsigned treeContextOrdinal : 28;
</ins><span class="cx">             };
</span><span class="cx">             // Used to make sure all memory is zero-initialized since we compute the hash over the bytes of this object.
</span><span class="cx">             void* possiblyPaddedMember;
</span><span class="lines">@@ -271,7 +272,7 @@
</span><span class="cx"> 
</span><span class="cx">         const Vector&lt;MatchedProperties, 64&gt;&amp; matchedProperties() const { return m_matchedProperties; }
</span><span class="cx"> 
</span><del>-        void addMatchedProperties(const StyleProperties&amp;, StyleRule* = nullptr, unsigned linkMatchType = SelectorChecker::MatchAll, PropertyWhitelistType = PropertyWhitelistNone);
</del><ins>+        void addMatchedProperties(const StyleProperties&amp;, StyleRule* = nullptr, unsigned linkMatchType = SelectorChecker::MatchAll, PropertyWhitelistType = PropertyWhitelistNone, unsigned treeContextOrdinal = 0);
</ins><span class="cx">     private:
</span><span class="cx">         Vector&lt;MatchedProperties, 64&gt; m_matchedProperties;
</span><span class="cx">     };
</span><span class="lines">@@ -291,8 +292,10 @@
</span><span class="cx"> 
</span><span class="cx">         bool hasProperty(CSSPropertyID) const;
</span><span class="cx">         Property&amp; property(CSSPropertyID);
</span><del>-        void addMatches(const MatchResult&amp;, bool important, int startIndex, int endIndex, bool inheritedOnly = false);
</del><span class="cx"> 
</span><ins>+        void addNormalMatches(const MatchResult&amp;, int startIndex, int endIndex, bool inheritedOnly = false);
+        void addImportantMatches(const MatchResult&amp;, int startIndex, int endIndex, bool inheritedOnly = false);
+
</ins><span class="cx">         void set(CSSPropertyID, CSSValue&amp;, unsigned linkMatchType, CascadeLevel);
</span><span class="cx">         void setDeferred(CSSPropertyID, CSSValue&amp;, unsigned linkMatchType, CascadeLevel);
</span><span class="cx"> 
</span><span class="lines">@@ -303,6 +306,7 @@
</span><span class="cx">         Property customProperty(const String&amp;) const;
</span><span class="cx">         
</span><span class="cx">     private:
</span><ins>+        void addMatch(const MatchResult&amp;, unsigned index, bool isImportant, bool inheritedOnly);
</ins><span class="cx">         void addStyleProperties(const StyleProperties&amp;, StyleRule&amp;, bool isImportant, bool inheritedOnly, PropertyWhitelistType, unsigned linkMatchType, CascadeLevel);
</span><span class="cx">         static void setPropertyInternal(Property&amp;, CSSPropertyID, CSSValue&amp;, unsigned linkMatchType, CascadeLevel);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>