<!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>[163592] trunk/Source/WebCore</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/163592">163592</a></dd>
<dt>Author</dt> <dd>antti@apple.com</dd>
<dt>Date</dt> <dd>2014-02-06 17:58:40 -0800 (Thu, 06 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Check selectors exactly when invalidating style
https://bugs.webkit.org/show_bug.cgi?id=128321

Reviewed by Andreas Kling.
        
Selectors are now really fast to match with the JIT. Take advantage of this by invalidating
the document style exactly when a new stylesheet arrives (instead of using heuristics).
        
This reduces need for large style recalculations in some common cases.

* css/ElementRuleCollector.cpp:
(WebCore::ElementRuleCollector::clearMatchedRules):
(WebCore::ElementRuleCollector::collectMatchingRulesForList):
* css/ElementRuleCollector.h:
(WebCore::ElementRuleCollector::hasMatchedRules):
* css/RuleSet.h:
(WebCore::RuleSet::hasShadowPseudoElementRules):
* css/SelectorChecker.cpp:
(WebCore::SelectorChecker::matchRecursively):
* css/SelectorChecker.h:
        
    Add new mode where all pseudo elements match so we can invalidate their element.

* css/StyleInvalidationAnalysis.cpp:
(WebCore::shouldDirtyAllStyle):
(WebCore::StyleInvalidationAnalysis::StyleInvalidationAnalysis):
(WebCore::invalidateStyleRecursively):
(WebCore::StyleInvalidationAnalysis::invalidateStyle):
        
    Switch to real selector checker.

* css/StyleInvalidationAnalysis.h:
* css/StyleResolver.cpp:
(WebCore::StyleResolver::MatchedProperties::~MatchedProperties):
* css/StyleResolver.h:
(WebCore::StyleResolver::mediaQueryEvaluator):
* dom/DocumentStyleSheetCollection.cpp:
(WebCore::DocumentStyleSheetCollection::analyzeStyleSheetChange):</pre>

<h3>Modified Paths</h3>
<ul>
<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="#trunkSourceWebCorecssRuleSeth">trunk/Source/WebCore/css/RuleSet.h</a></li>
<li><a href="#trunkSourceWebCorecssSelectorCheckercpp">trunk/Source/WebCore/css/SelectorChecker.cpp</a></li>
<li><a href="#trunkSourceWebCorecssSelectorCheckerh">trunk/Source/WebCore/css/SelectorChecker.h</a></li>
<li><a href="#trunkSourceWebCorecssStyleInvalidationAnalysiscpp">trunk/Source/WebCore/css/StyleInvalidationAnalysis.cpp</a></li>
<li><a href="#trunkSourceWebCorecssStyleInvalidationAnalysish">trunk/Source/WebCore/css/StyleInvalidationAnalysis.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>
<li><a href="#trunkSourceWebCoredomDocumentStyleSheetCollectioncpp">trunk/Source/WebCore/dom/DocumentStyleSheetCollection.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/ChangeLog        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -1,3 +1,44 @@
</span><ins>+2014-02-06  Antti Koivisto  &lt;antti@apple.com&gt;
+
+        Check selectors exactly when invalidating style
+        https://bugs.webkit.org/show_bug.cgi?id=128321
+
+        Reviewed by Andreas Kling.
+        
+        Selectors are now really fast to match with the JIT. Take advantage of this by invalidating
+        the document style exactly when a new stylesheet arrives (instead of using heuristics).
+        
+        This reduces need for large style recalculations in some common cases.
+
+        * css/ElementRuleCollector.cpp:
+        (WebCore::ElementRuleCollector::clearMatchedRules):
+        (WebCore::ElementRuleCollector::collectMatchingRulesForList):
+        * css/ElementRuleCollector.h:
+        (WebCore::ElementRuleCollector::hasMatchedRules):
+        * css/RuleSet.h:
+        (WebCore::RuleSet::hasShadowPseudoElementRules):
+        * css/SelectorChecker.cpp:
+        (WebCore::SelectorChecker::matchRecursively):
+        * css/SelectorChecker.h:
+        
+            Add new mode where all pseudo elements match so we can invalidate their element.
+
+        * css/StyleInvalidationAnalysis.cpp:
+        (WebCore::shouldDirtyAllStyle):
+        (WebCore::StyleInvalidationAnalysis::StyleInvalidationAnalysis):
+        (WebCore::invalidateStyleRecursively):
+        (WebCore::StyleInvalidationAnalysis::invalidateStyle):
+        
+            Switch to real selector checker.
+
+        * css/StyleInvalidationAnalysis.h:
+        * css/StyleResolver.cpp:
+        (WebCore::StyleResolver::MatchedProperties::~MatchedProperties):
+        * css/StyleResolver.h:
+        (WebCore::StyleResolver::mediaQueryEvaluator):
+        * dom/DocumentStyleSheetCollection.cpp:
+        (WebCore::DocumentStyleSheetCollection::analyzeStyleSheetChange):
+
</ins><span class="cx"> 2014-02-06  Gavin Barraclough  &lt;barraclough@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Remove ChildProcess::m_activeTasks
</span></span></pre></div>
<a id="trunkSourceWebCorecssElementRuleCollectorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/ElementRuleCollector.cpp (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/ElementRuleCollector.cpp        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/ElementRuleCollector.cpp        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -94,7 +94,7 @@
</span><span class="cx">     m_matchedRules-&gt;append(rule);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline void ElementRuleCollector::clearMatchedRules()
</del><ins>+void ElementRuleCollector::clearMatchedRules()
</ins><span class="cx"> {
</span><span class="cx">     if (!m_matchedRules)
</span><span class="cx">         return;
</span><span class="lines">@@ -359,7 +359,7 @@
</span><span class="cx">         PseudoId dynamicPseudo = NOPSEUDO;
</span><span class="cx">         if (ruleMatches(ruleData, dynamicPseudo)) {
</span><span class="cx">             // For SharingRules testing, any match is good enough, we don't care what is matched.
</span><del>-            if (m_mode == SelectorChecker::SharingRules) {
</del><ins>+            if (m_mode == SelectorChecker::SharingRules || m_mode == SelectorChecker::StyleInvalidation) {
</ins><span class="cx">                 addMatchedRule(&amp;ruleData);
</span><span class="cx">                 break;
</span><span class="cx">             }
</span></span></pre></div>
<a id="trunkSourceWebCorecssElementRuleCollectorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/ElementRuleCollector.h (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/ElementRuleCollector.h        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/ElementRuleCollector.h        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -70,6 +70,9 @@
</span><span class="cx">     StyleResolver::MatchResult&amp; matchedResult();
</span><span class="cx">     const Vector&lt;RefPtr&lt;StyleRuleBase&gt;&gt;&amp; matchedRuleList() const;
</span><span class="cx"> 
</span><ins>+    bool hasMatchedRules() const { return m_matchedRules &amp;&amp; !m_matchedRules-&gt;isEmpty(); }
+    void clearMatchedRules();
+
</ins><span class="cx"> private:
</span><span class="cx">     void addElementStyleProperties(const StyleProperties*, bool isCacheable = true);
</span><span class="cx"> 
</span><span class="lines">@@ -84,7 +87,6 @@
</span><span class="cx">     void sortAndTransferMatchedRules();
</span><span class="cx"> 
</span><span class="cx">     void addMatchedRule(const RuleData*);
</span><del>-    void clearMatchedRules();
</del><span class="cx"> 
</span><span class="cx">     Element&amp; m_element;
</span><span class="cx">     RenderStyle* m_style;
</span></span></pre></div>
<a id="trunkSourceWebCorecssRuleSeth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/RuleSet.h (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/RuleSet.h        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/RuleSet.h        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -166,6 +166,8 @@
</span><span class="cx"> 
</span><span class="cx">     unsigned ruleCount() const { return m_ruleCount; }
</span><span class="cx"> 
</span><ins>+    bool hasShadowPseudoElementRules() const { return !m_shadowPseudoElementRules.isEmpty(); }
+
</ins><span class="cx"> private:
</span><span class="cx">     void addChildRules(const Vector&lt;RefPtr&lt;StyleRuleBase&gt;&gt;&amp;, const MediaQueryEvaluator&amp; medium, StyleResolver*, bool hasDocumentSecurityOrigin, AddRuleFlags);
</span><span class="cx">     bool findBestRuleSetAndAdd(const CSSSelector*, RuleData&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCorecssSelectorCheckercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/SelectorChecker.cpp (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/SelectorChecker.cpp        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/SelectorChecker.cpp        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -2,7 +2,7 @@
</span><span class="cx">  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
</span><span class="cx">  *           (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
</span><span class="cx">  * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
</span><del>- * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Apple Inc. All rights reserved.
</ins><span class="cx">  * Copyright (C) 2007 Alexey Proskuryakov &lt;ap@webkit.org&gt;
</span><span class="cx">  * Copyright (C) 2007, 2008 Eric Seidel &lt;eric@webkit.org&gt;
</span><span class="cx">  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
</span><span class="lines">@@ -162,13 +162,14 @@
</span><span class="cx"> 
</span><span class="cx">                 if (context.selector-&gt;pseudoType() == CSSSelector::PseudoWebKitCustomElement &amp;&amp; root-&gt;type() != ShadowRoot::UserAgentShadowRoot)
</span><span class="cx">                     return SelectorFailsLocally;
</span><del>-            } else
</del><ins>+            } else if (m_mode != StyleInvalidation)
</ins><span class="cx">                 return SelectorFailsLocally;
</span><span class="cx">         } else {
</span><span class="cx">             if ((!context.elementStyle &amp;&amp; m_mode == ResolvingStyle) || m_mode == QueryingRules)
</span><span class="cx">                 return SelectorFailsLocally;
</span><span class="cx"> 
</span><del>-            PseudoId pseudoId = CSSSelector::pseudoId(context.selector-&gt;pseudoType());
</del><ins>+            // When invalidating style all pseudo elements need to match.
+            PseudoId pseudoId = m_mode == StyleInvalidation ? NOPSEUDO : CSSSelector::pseudoId(context.selector-&gt;pseudoType());
</ins><span class="cx">             if (pseudoId == FIRST_LETTER)
</span><span class="cx">                 context.element-&gt;document().styleSheetCollection().setUsesFirstLetterRules(true);
</span><span class="cx">             if (pseudoId != NOPSEUDO)
</span></span></pre></div>
<a id="trunkSourceWebCorecssSelectorCheckerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/SelectorChecker.h (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/SelectorChecker.h        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/SelectorChecker.h        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -47,7 +47,7 @@
</span><span class="cx"> 
</span><span class="cx"> public:
</span><span class="cx">     enum VisitedMatchType { VisitedMatchDisabled, VisitedMatchEnabled };
</span><del>-    enum Mode { ResolvingStyle = 0, CollectingRules, QueryingRules, SharingRules };
</del><ins>+    enum Mode { ResolvingStyle = 0, CollectingRules, QueryingRules, SharingRules, StyleInvalidation };
</ins><span class="cx"> 
</span><span class="cx">     SelectorChecker(Document&amp;, Mode);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleInvalidationAnalysiscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleInvalidationAnalysis.cpp (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleInvalidationAnalysis.cpp        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/StyleInvalidationAnalysis.cpp        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2012 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2012, 2014 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -29,106 +29,100 @@
</span><span class="cx"> #include &quot;CSSSelectorList.h&quot;
</span><span class="cx"> #include &quot;Document.h&quot;
</span><span class="cx"> #include &quot;ElementIterator.h&quot;
</span><ins>+#include &quot;ElementRuleCollector.h&quot;
+#include &quot;SelectorFilter.h&quot;
</ins><span class="cx"> #include &quot;StyleRuleImport.h&quot;
</span><span class="cx"> #include &quot;StyleSheetContents.h&quot;
</span><del>-#include &quot;StyledElement.h&quot;
</del><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-StyleInvalidationAnalysis::StyleInvalidationAnalysis(const Vector&lt;StyleSheetContents*&gt;&amp; sheets)
-    : m_dirtiesAllStyle(false)
</del><ins>+static bool shouldDirtyAllStyle(const Vector&lt;RefPtr&lt;StyleRuleBase&gt;&gt; rules)
</ins><span class="cx"> {
</span><del>-    for (unsigned i = 0; i &lt; sheets.size() &amp;&amp; !m_dirtiesAllStyle; ++i)
-        analyzeStyleSheet(sheets[i]);
-}
-
-static bool determineSelectorScopes(const CSSSelectorList&amp; selectorList, HashSet&lt;AtomicStringImpl*&gt;&amp; idScopes, HashSet&lt;AtomicStringImpl*&gt;&amp; classScopes)
-{
-    for (const CSSSelector* selector = selectorList.first(); selector; selector = CSSSelectorList::next(selector)) {
-        const CSSSelector* scopeSelector = 0;
-        // This picks the widest scope, not the narrowest, to minimize the number of found scopes.
-        for (const CSSSelector* current = selector; current; current = current-&gt;tagHistory()) {
-            // Prefer ids over classes.
-            if (current-&gt;m_match == CSSSelector::Id)
-                scopeSelector = current;
-            else if (current-&gt;m_match == CSSSelector::Class &amp;&amp; (!scopeSelector || scopeSelector-&gt;m_match != CSSSelector::Id))
-                scopeSelector = current;
-            CSSSelector::Relation relation = current-&gt;relation();
-            if (relation != CSSSelector::Descendant &amp;&amp; relation != CSSSelector::Child &amp;&amp; relation != CSSSelector::SubSelector)
-                break;
</del><ins>+    for (auto&amp; rule : rules) {
+        if (rule-&gt;isMediaRule()) {
+            if (shouldDirtyAllStyle(static_cast&lt;StyleRuleMedia&amp;&gt;(*rule).childRules()))
+                return true;
+            continue;
</ins><span class="cx">         }
</span><del>-        if (!scopeSelector)
-            return false;
-        ASSERT(scopeSelector-&gt;m_match == CSSSelector::Class || scopeSelector-&gt;m_match == CSSSelector::Id);
-        if (scopeSelector-&gt;m_match == CSSSelector::Id)
-            idScopes.add(scopeSelector-&gt;value().impl());
-        else
-            classScopes.add(scopeSelector-&gt;value().impl());
</del><ins>+        // FIXME: At least font faces don't need full recalc in all cases.
+        if (!rule-&gt;isStyleRule())
+            return true;
</ins><span class="cx">     }
</span><del>-    return true;
</del><ins>+    return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void StyleInvalidationAnalysis::analyzeStyleSheet(StyleSheetContents* styleSheetContents)
</del><ins>+static bool shouldDirtyAllStyle(const StyleSheetContents&amp; sheet)
</ins><span class="cx"> {
</span><del>-    ASSERT(!styleSheetContents-&gt;isLoading());
-
-    // See if all rules on the sheet are scoped to some specific ids or classes.
-    // Then test if we actually have any of those in the tree at the moment.
-    const Vector&lt;RefPtr&lt;StyleRuleImport&gt;&gt;&amp; importRules = styleSheetContents-&gt;importRules();
-    for (unsigned i = 0; i &lt; importRules.size(); ++i) {
-        if (!importRules[i]-&gt;styleSheet())
</del><ins>+    for (auto&amp; import : sheet.importRules()) {
+        if (!import-&gt;styleSheet())
</ins><span class="cx">             continue;
</span><del>-        analyzeStyleSheet(importRules[i]-&gt;styleSheet());
-        if (m_dirtiesAllStyle)
-            return;
</del><ins>+        if (shouldDirtyAllStyle(*import-&gt;styleSheet()))
+            return true;
</ins><span class="cx">     }
</span><del>-    const Vector&lt;RefPtr&lt;StyleRuleBase&gt;&gt;&amp; rules = styleSheetContents-&gt;childRules();
-    for (unsigned i = 0; i &lt; rules.size(); i++) {
-        StyleRuleBase* rule = rules[i].get();
-        if (!rule-&gt;isStyleRule()) {
-            // FIXME: Media rules and maybe some others could be allowed.
-            m_dirtiesAllStyle = true;
-            return;
-        }
-        StyleRule* styleRule = static_cast&lt;StyleRule*&gt;(rule);
-        if (!determineSelectorScopes(styleRule-&gt;selectorList(), m_idScopes, m_classScopes)) {
-            m_dirtiesAllStyle = true;
-            return;
-        }
-    }
</del><ins>+    if (shouldDirtyAllStyle(sheet.childRules()))
+        return true;
+    return false;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-static bool elementMatchesSelectorScopes(const Element&amp; element, const HashSet&lt;AtomicStringImpl*&gt;&amp; idScopes, const HashSet&lt;AtomicStringImpl*&gt;&amp; classScopes)
</del><ins>+static bool shouldDirtyAllStyle(const Vector&lt;StyleSheetContents*&gt;&amp; sheets)
</ins><span class="cx"> {
</span><del>-    if (!idScopes.isEmpty() &amp;&amp; element.hasID() &amp;&amp; idScopes.contains(element.idForStyleResolution().impl()))
-        return true;
-    if (classScopes.isEmpty() || !element.hasClass())
-        return false;
-    const SpaceSplitString&amp; classNames = element.classNames();
-    for (unsigned i = 0; i &lt; classNames.size(); ++i) {
-        if (classScopes.contains(classNames[i].impl()))
</del><ins>+    for (auto&amp; sheet : sheets) {
+        if (shouldDirtyAllStyle(*sheet))
</ins><span class="cx">             return true;
</span><span class="cx">     }
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+StyleInvalidationAnalysis::StyleInvalidationAnalysis(const Vector&lt;StyleSheetContents*&gt;&amp; sheets, const MediaQueryEvaluator&amp; mediaQueryEvaluator)
+    : m_dirtiesAllStyle(shouldDirtyAllStyle(sheets))
+{
+    if (m_dirtiesAllStyle)
+        return;
+
+    m_ruleSets.resetAuthorStyle();
+    for (auto&amp; sheet : sheets)
+        m_ruleSets.authorStyle()-&gt;addRulesFromSheet(sheet, mediaQueryEvaluator);
+
+    // FIXME: We don't descent into shadow trees or otherwise handle shadow pseudo elements.
+    if (m_ruleSets.authorStyle()-&gt;hasShadowPseudoElementRules())
+        m_dirtiesAllStyle = true;
+}
+
+static void invalidateStyleRecursively(Element&amp; element, SelectorFilter&amp; filter, const DocumentRuleSets&amp; ruleSets)
+{
+    if (element.styleChangeType() &gt; InlineStyleChange)
+        return;
+    if (element.styleChangeType() == NoStyleChange) {
+        ElementRuleCollector ruleCollector(element, nullptr, ruleSets, filter);
+        ruleCollector.setMode(SelectorChecker::StyleInvalidation);
+        ruleCollector.matchAuthorRules(false);
+
+        if (ruleCollector.hasMatchedRules())
+            element.setNeedsStyleRecalc(InlineStyleChange);
+    }
+
+    auto children = childrenOfType&lt;Element&gt;(element);
+    if (!children.first())
+        return;
+    filter.pushParent(&amp;element);
+    for (auto&amp; child : children)
+        invalidateStyleRecursively(child, filter, ruleSets);
+    filter.popParent();
+}
+
</ins><span class="cx"> void StyleInvalidationAnalysis::invalidateStyle(Document&amp; document)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!m_dirtiesAllStyle);
</span><del>-    if (m_idScopes.isEmpty() &amp;&amp; m_classScopes.isEmpty())
</del><ins>+    if (!m_ruleSets.authorStyle())
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    auto it = descendantsOfType&lt;Element&gt;(document).begin();
-    auto end = descendantsOfType&lt;Element&gt;(document).end();
-    while (it != end) {
-        if (elementMatchesSelectorScopes(*it, m_idScopes, m_classScopes)) {
-            it-&gt;setNeedsStyleRecalc();
-            // The whole subtree is now invalidated, we can skip to the next sibling.
-            it.traverseNextSkippingChildren();
-            continue;
-        }
-        ++it;
-    }
</del><ins>+    Element* documentElement = document.documentElement();
+    if (!documentElement)
+        return;
+
+    SelectorFilter filter;
+    filter.setupParentStack(documentElement);
+    invalidateStyleRecursively(*documentElement, filter, m_ruleSets);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleInvalidationAnalysish"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleInvalidationAnalysis.h (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleInvalidationAnalysis.h        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/StyleInvalidationAnalysis.h        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2012 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2012, 2014 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #ifndef StyleInvalidationAnalysis_h
</span><span class="cx"> #define StyleInvalidationAnalysis_h
</span><span class="cx"> 
</span><ins>+#include &quot;DocumentRuleSets.h&quot;
</ins><span class="cx"> #include &lt;wtf/HashSet.h&gt;
</span><span class="cx"> #include &lt;wtf/text/AtomicStringImpl.h&gt;
</span><span class="cx"> 
</span><span class="lines">@@ -36,18 +37,14 @@
</span><span class="cx"> 
</span><span class="cx"> class StyleInvalidationAnalysis {
</span><span class="cx"> public:
</span><del>-    StyleInvalidationAnalysis(const Vector&lt;StyleSheetContents*&gt;&amp;);
</del><ins>+    StyleInvalidationAnalysis(const Vector&lt;StyleSheetContents*&gt;&amp;, const MediaQueryEvaluator&amp;);
</ins><span class="cx"> 
</span><span class="cx">     bool dirtiesAllStyle() const { return m_dirtiesAllStyle; }
</span><span class="cx">     void invalidateStyle(Document&amp;);
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-
-    void analyzeStyleSheet(StyleSheetContents*);
-
</del><span class="cx">     bool m_dirtiesAllStyle;
</span><del>-    HashSet&lt;AtomicStringImpl*&gt; m_idScopes;
-    HashSet&lt;AtomicStringImpl*&gt; m_classScopes;
</del><ins>+    DocumentRuleSets m_ruleSets;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleResolvercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleResolver.cpp (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleResolver.cpp        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/StyleResolver.cpp        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -3657,7 +3657,7 @@
</span><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline StyleResolver::MatchedProperties::~MatchedProperties()
</del><ins>+StyleResolver::MatchedProperties::~MatchedProperties()
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleResolverh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleResolver.h (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleResolver.h        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/css/StyleResolver.h        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -168,6 +168,8 @@
</span><span class="cx">     const DocumentRuleSets&amp; ruleSets() const { return m_ruleSets; }
</span><span class="cx">     SelectorFilter&amp; selectorFilter() { return m_selectorFilter; }
</span><span class="cx"> 
</span><ins>+    const MediaQueryEvaluator&amp; mediaQueryEvaluator() const { return *m_medium; }
+
</ins><span class="cx"> private:
</span><span class="cx">     void initElement(Element*);
</span><span class="cx">     RenderStyle* locateSharedStyle();
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumentStyleSheetCollectioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/DocumentStyleSheetCollection.cpp (163591 => 163592)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/DocumentStyleSheetCollection.cpp        2014-02-07 01:57:37 UTC (rev 163591)
+++ trunk/Source/WebCore/dom/DocumentStyleSheetCollection.cpp        2014-02-07 01:58:40 UTC (rev 163592)
</span><span class="lines">@@ -366,6 +366,7 @@
</span><span class="cx">         return;
</span><span class="cx">     if (!m_document.styleResolverIfExists())
</span><span class="cx">         return;
</span><ins>+    StyleResolver&amp; styleResolver = *m_document.styleResolverIfExists();
</ins><span class="cx"> 
</span><span class="cx">     // Find out which stylesheets are new.
</span><span class="cx">     unsigned oldStylesheetCount = m_activeAuthorStyleSheets.size();
</span><span class="lines">@@ -396,7 +397,7 @@
</span><span class="cx">     // If we are already parsing the body and so may have significant amount of elements, put some effort into trying to avoid style recalcs.
</span><span class="cx">     if (!m_document.body() || m_document.hasNodesWithPlaceholderStyle())
</span><span class="cx">         return;
</span><del>-    StyleInvalidationAnalysis invalidationAnalysis(addedSheets);
</del><ins>+    StyleInvalidationAnalysis invalidationAnalysis(addedSheets, styleResolver.mediaQueryEvaluator());
</ins><span class="cx">     if (invalidationAnalysis.dirtiesAllStyle())
</span><span class="cx">         return;
</span><span class="cx">     invalidationAnalysis.invalidateStyle(m_document);
</span></span></pre>
</div>
</div>

</body>
</html>