<!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>[160806] 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/160806">160806</a></dd>
<dt>Author</dt> <dd>akling@apple.com</dd>
<dt>Date</dt> <dd>2013-12-18 16:33:25 -0800 (Wed, 18 Dec 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>CSS: Add a property cascading pass to style application.
&lt;https://webkit.org/b/125213&gt;

Add an intermediate pass to style application where we cascade all
style properties to figure out their final values before starting
to build RenderStyles.

This opens up various opportunities for further optimization.

Reviewed by Antti Koivisto.

* css/StyleResolver.cpp:
(WebCore::StyleResolver::CascadedProperties::Property::Property):
(WebCore::StyleResolver::CascadedProperties::CascadedProperties):
(WebCore::StyleResolver::CascadedProperties::property):
(WebCore::StyleResolver::CascadedProperties::set):
(WebCore::StyleResolver::CascadedProperties::addStyleProperties):
(WebCore::StyleResolver::CascadedProperties::addMatches):
(WebCore::StyleResolver::CascadedProperties::Property::apply):

    Added. CascadedProperties is something of a container class
    that takes CSS property/value/linkMatchType as input and boils
    them down to the final values that will actually be used.

    Most properties are poked into an unfancy array where latest
    is greatest (unless !important, of course.) Some properties are
    queued up to be applied in parse order, more on that below.

(WebCore::StyleResolver::applyCascadedProperties):
(WebCore::StyleResolver::applyMatchedProperties):

    The brains of this patch. applyMatchedProperties() now creates
    a CascadedProperties and uses it to figure out the final values
    and uses applyCascadedProperties() to apply them. Deferred
    properties (parse order) are applied last.

    We may discover during property application that we won't be
    able to use a matched properties cache item. This happens if
    the effective zoom or font changes. If that happens, we start
    the process over, now with the cache disabled. This may need
    some optimization work.

(WebCore::extractDirectionAndWritingMode):

    Directional properties ending in e.g -before or -after depend on
    the direction and writing mode in effect, so we must begin with
    resolving those properties before doing the full cascade.

    This is done by simply walking the set of matched properties and
    manually applying '-webkit-writing-mode' and 'direction'.

    If this starts showing up in profiles, we can easily cache some
    of the information in e.g RuleData to avoid the traversal here.

(WebCore::elementTypeHasAppearanceFromUAStyle):

    To determine whether a form element is styled beyond the default
    UA style sheet, StyleResolver caches the border and background
    values from RenderStyle after applying the UA style sheet.

    Those values are then compared against after all style is applied
    and if some (platform-dependent) values differ, the element is
    considered &quot;styled.&quot;

    This really only affects elements with -webkit-appearance values
    in the default UA style sheet, so this function determines if an
    element should take the goofy slow path for this.

(WebCore::shouldApplyPropertyInParseOrder):
(WebCore::StyleResolver::CascadedProperties::setDeferred):
(WebCore::StyleResolver::CascadedProperties::applyDeferredProperties):

    Some CSS properties will write to the same RenderStyle fields when
    applied, so in order to maintain previous behavior, we must apply
    them in the order they were parsed.

    We accomplish this by keeping an ordered queue of such properties
    to apply separately after all the other properties.

(WebCore::StyleResolver::CascadedProperties::setPropertyInternal):

    Helper for poking values into a CascadedProperties::Property.

* css/StyleResolver.h:
(WebCore::StyleResolver::state):

    Expose the StyleResolver::State so CascadedProperties can access it.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</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="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (160805 => 160806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2013-12-19 00:10:15 UTC (rev 160805)
+++ trunk/Source/WebCore/ChangeLog        2013-12-19 00:33:25 UTC (rev 160806)
</span><span class="lines">@@ -1,3 +1,93 @@
</span><ins>+2013-12-18  Andreas Kling  &lt;akling@apple.com&gt;
+
+        CSS: Add a property cascading pass to style application.
+        &lt;https://webkit.org/b/125213&gt;
+
+        Add an intermediate pass to style application where we cascade all
+        style properties to figure out their final values before starting
+        to build RenderStyles.
+
+        This opens up various opportunities for further optimization.
+
+        Reviewed by Antti Koivisto.
+
+        * css/StyleResolver.cpp:
+        (WebCore::StyleResolver::CascadedProperties::Property::Property):
+        (WebCore::StyleResolver::CascadedProperties::CascadedProperties):
+        (WebCore::StyleResolver::CascadedProperties::property):
+        (WebCore::StyleResolver::CascadedProperties::set):
+        (WebCore::StyleResolver::CascadedProperties::addStyleProperties):
+        (WebCore::StyleResolver::CascadedProperties::addMatches):
+        (WebCore::StyleResolver::CascadedProperties::Property::apply):
+
+            Added. CascadedProperties is something of a container class
+            that takes CSS property/value/linkMatchType as input and boils
+            them down to the final values that will actually be used.
+
+            Most properties are poked into an unfancy array where latest
+            is greatest (unless !important, of course.) Some properties are
+            queued up to be applied in parse order, more on that below.
+
+        (WebCore::StyleResolver::applyCascadedProperties):
+        (WebCore::StyleResolver::applyMatchedProperties):
+
+            The brains of this patch. applyMatchedProperties() now creates
+            a CascadedProperties and uses it to figure out the final values
+            and uses applyCascadedProperties() to apply them. Deferred
+            properties (parse order) are applied last.
+
+            We may discover during property application that we won't be
+            able to use a matched properties cache item. This happens if
+            the effective zoom or font changes. If that happens, we start
+            the process over, now with the cache disabled. This may need
+            some optimization work.
+
+        (WebCore::extractDirectionAndWritingMode):
+
+            Directional properties ending in e.g -before or -after depend on
+            the direction and writing mode in effect, so we must begin with
+            resolving those properties before doing the full cascade.
+
+            This is done by simply walking the set of matched properties and
+            manually applying '-webkit-writing-mode' and 'direction'.
+
+            If this starts showing up in profiles, we can easily cache some
+            of the information in e.g RuleData to avoid the traversal here.
+
+        (WebCore::elementTypeHasAppearanceFromUAStyle):
+
+            To determine whether a form element is styled beyond the default
+            UA style sheet, StyleResolver caches the border and background
+            values from RenderStyle after applying the UA style sheet.
+
+            Those values are then compared against after all style is applied
+            and if some (platform-dependent) values differ, the element is
+            considered &quot;styled.&quot;
+
+            This really only affects elements with -webkit-appearance values
+            in the default UA style sheet, so this function determines if an
+            element should take the goofy slow path for this.
+
+        (WebCore::shouldApplyPropertyInParseOrder):
+        (WebCore::StyleResolver::CascadedProperties::setDeferred):
+        (WebCore::StyleResolver::CascadedProperties::applyDeferredProperties):
+
+            Some CSS properties will write to the same RenderStyle fields when
+            applied, so in order to maintain previous behavior, we must apply
+            them in the order they were parsed.
+
+            We accomplish this by keeping an ordered queue of such properties
+            to apply separately after all the other properties.
+
+        (WebCore::StyleResolver::CascadedProperties::setPropertyInternal):
+
+            Helper for poking values into a CascadedProperties::Property.
+
+        * css/StyleResolver.h:
+        (WebCore::StyleResolver::state):
+
+            Expose the StyleResolver::State so CascadedProperties can access it.
+
</ins><span class="cx"> 2013-12-16  Martin Robinson  &lt;mrobinson@igalia.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [GTK] [CMake] Add support for building WebKit1
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleResolvercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleResolver.cpp (160805 => 160806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleResolver.cpp        2013-12-19 00:10:15 UTC (rev 160805)
+++ trunk/Source/WebCore/css/StyleResolver.cpp        2013-12-19 00:33:25 UTC (rev 160806)
</span><span class="lines">@@ -191,6 +191,38 @@
</span><span class="cx"> 
</span><span class="cx"> using namespace HTMLNames;
</span><span class="cx"> 
</span><ins>+class StyleResolver::CascadedProperties {
+public:
+    CascadedProperties(TextDirection, WritingMode);
+
+    struct Property {
+        Property();
+        void apply(StyleResolver&amp;);
+
+        CSSPropertyID id;
+        bool isPresent;
+        CSSValue* cssValue[3];
+    };
+
+    Property&amp; property(CSSPropertyID);
+    void addMatches(const MatchResult&amp;, bool important, int startIndex, int endIndex, bool inheritedOnly);
+
+    void set(CSSPropertyID, CSSValue&amp;, unsigned linkMatchType);
+    void setDeferred(CSSPropertyID, CSSValue&amp;, unsigned linkMatchType);
+
+    void applyDeferredProperties(StyleResolver&amp;);
+
+private:
+    void addStyleProperties(const StyleProperties&amp;, StyleRule&amp;, bool isImportant, bool inheritedOnly, PropertyWhitelistType, unsigned linkMatchType);
+    static void setPropertyInternal(Property&amp;, CSSPropertyID, CSSValue&amp;, unsigned linkMatchType);
+
+    Property m_properties[numCSSProperties + 1];
+    Vector&lt;Property&gt; m_deferredProperties;
+
+    TextDirection m_direction;
+    WritingMode m_writingMode;
+};
+
</ins><span class="cx"> #define HANDLE_INHERIT(prop, Prop) \
</span><span class="cx"> if (isInherit) { \
</span><span class="cx">     m_state.style()-&gt;set##Prop(m_state.parentStyle()-&gt;prop()); \
</span><span class="lines">@@ -1568,6 +1600,44 @@
</span><span class="cx">     InspectorInstrumentation::didProcessRule(cookie);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static bool shouldApplyPropertyInParseOrder(CSSPropertyID propertyID)
+{
+    switch (propertyID) {
+    case CSSPropertyWebkitBorderImage:
+    case CSSPropertyBorderImage:
+    case CSSPropertyBorderImageSlice:
+    case CSSPropertyBorderImageSource:
+    case CSSPropertyBorderImageOutset:
+    case CSSPropertyBorderImageRepeat:
+    case CSSPropertyBorderImageWidth:
+#if ENABLE(CSS3_TEXT_DECORATION)
+    case CSSPropertyWebkitTextDecoration:
+    case CSSPropertyWebkitTextDecorationLine:
+    case CSSPropertyWebkitTextDecorationStyle:
+    case CSSPropertyWebkitTextDecorationColor:
+    case CSSPropertyWebkitTextDecorationSkip:
+    case CSSPropertyWebkitTextUnderlinePosition:
+#endif
+    case CSSPropertyTextDecoration:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static bool elementTypeHasAppearanceFromUAStyle(const Element&amp; element)
+{
+    // NOTE: This is just a hard-coded list of elements that have some -webkit-appearance value in html.css
+    const auto&amp; localName = element.localName();
+    return localName == HTMLNames::inputTag
+        || localName == HTMLNames::textareaTag
+        || localName == HTMLNames::buttonTag
+        || localName == HTMLNames::progressTag
+        || localName == HTMLNames::selectTag
+        || localName == HTMLNames::meterTag
+        || localName == HTMLNames::isindexTag;
+}
+
</ins><span class="cx"> template &lt;StyleResolver::StyleApplicationPass pass&gt;
</span><span class="cx"> void StyleResolver::applyMatchedProperties(const MatchResult&amp; matchResult, bool isImportant, int startIndex, int endIndex, bool inheritedOnly)
</span><span class="cx"> {
</span><span class="lines">@@ -1691,14 +1761,47 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void StyleResolver::applyMatchedProperties(const MatchResult&amp; matchResult, const Element* element)
</del><ins>+static void extractDirectionAndWritingMode(const RenderStyle&amp; style, const StyleResolver::MatchResult&amp; matchResult, TextDirection&amp; direction, WritingMode&amp; writingMode)
</ins><span class="cx"> {
</span><ins>+    direction = style.direction();
+    writingMode = style.writingMode();
+
+    bool hadImportantWebkitWritingMode = false;
+    bool hadImportantDirection = false;
+
+    for (auto&amp; matchedProperties : matchResult.matchedProperties) {
+        for (unsigned i = 0, count = matchedProperties.properties-&gt;propertyCount(); i &lt; count; ++i) {
+            auto property = matchedProperties.properties-&gt;propertyAt(i);
+            if (!property.value()-&gt;isPrimitiveValue())
+                continue;
+            switch (property.id()) {
+            case CSSPropertyWebkitWritingMode:
+                if (!hadImportantWebkitWritingMode || property.isImportant()) {
+                    writingMode = toCSSPrimitiveValue(*property.value());
+                    hadImportantWebkitWritingMode = property.isImportant();
+                }
+                break;
+            case CSSPropertyDirection:
+                if (!hadImportantDirection || property.isImportant()) {
+                    direction = toCSSPrimitiveValue(*property.value());
+                    hadImportantDirection = property.isImportant();
+                }
+                break;
+            default:
+                break;
+            }
+        }
+    }
+}
+
+void StyleResolver::applyMatchedProperties(const MatchResult&amp; matchResult, const Element* element, ShouldUseMatchedPropertiesCache shouldUseMatchedPropertiesCache)
+{
</ins><span class="cx">     ASSERT(element);
</span><span class="cx">     State&amp; state = m_state;
</span><span class="cx">     unsigned cacheHash = matchResult.isCacheable ? computeMatchedPropertiesHash(matchResult.matchedProperties.data(), matchResult.matchedProperties.size()) : 0;
</span><span class="cx">     bool applyInheritedOnly = false;
</span><span class="cx">     const MatchedPropertiesCacheItem* cacheItem = 0;
</span><del>-    if (cacheHash &amp;&amp; (cacheItem = findFromMatchedPropertiesCache(cacheHash, matchResult))) {
</del><ins>+    if (shouldUseMatchedPropertiesCache &amp;&amp; cacheHash &amp;&amp; (cacheItem = findFromMatchedPropertiesCache(cacheHash, matchResult))) {
</ins><span class="cx">         // We can build up the style by copying non-inherited properties from an earlier style object built using the same exact
</span><span class="cx">         // style declarations. We then only need to apply the inherited properties, if any, as their values can depend on the 
</span><span class="cx">         // element context. This is fast and saves memory by reusing the style data structures.
</span><span class="lines">@@ -1716,21 +1819,44 @@
</span><span class="cx">         applyInheritedOnly = true; 
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    // Now we have all of the matched rules in the appropriate order. Walk the rules and apply
-    // high-priority properties first, i.e., those properties that other properties depend on.
-    // The order is (1) high-priority not important, (2) high-priority important, (3) normal not important
-    // and (4) normal important.
-    state.setLineHeightValue(0);
-    applyMatchedProperties&lt;HighPriorityProperties&gt;(matchResult, false, 0, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
-    applyMatchedProperties&lt;HighPriorityProperties&gt;(matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
-    applyMatchedProperties&lt;HighPriorityProperties&gt;(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
-    applyMatchedProperties&lt;HighPriorityProperties&gt;(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
</del><ins>+    // Directional properties (*-before/after) are aliases that depend on the TextDirection and WritingMode.
+    // These must be resolved before we can begin the property cascade.
+    TextDirection direction;
+    WritingMode writingMode;
+    extractDirectionAndWritingMode(*state.style(), matchResult, direction, writingMode);
</ins><span class="cx"> 
</span><del>-    if (cacheItem &amp;&amp; cacheItem-&gt;renderStyle-&gt;effectiveZoom() != state.style()-&gt;effectiveZoom()) {
-        state.setFontDirty(true);
-        applyInheritedOnly = false;
</del><ins>+    if (elementTypeHasAppearanceFromUAStyle(*state.element())) {
+        // FIXME: This is such a hack.
+        // Find out if there's a -webkit-appearance property in effect from the UA sheet.
+        // 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.
+        state.setLineHeightValue(nullptr);
+        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);
+
+        applyCascadedProperties(cascade, firstCSSProperty, CSSPropertyLineHeight);
+        updateFont();
+        applyCascadedProperties(cascade, CSSPropertyBackground, lastCSSProperty);
+
+        state.cacheBorderAndBackground();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    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);
+
+    state.setLineHeightValue(nullptr);
+
+    // Start by applying properties that other properties may depend on.
+    applyCascadedProperties(cascade, firstCSSProperty, CSSPropertyLineHeight);
+
+    // If the effective zoom value changes, we can't use the matched properties cache. Start over.
+    if (cacheItem &amp;&amp; cacheItem-&gt;renderStyle-&gt;effectiveZoom() != state.style()-&gt;effectiveZoom())
+        return applyMatchedProperties(matchResult, element, DoNotUseMatchedPropertiesCache);
+
</ins><span class="cx">     // If our font got dirtied, go ahead and update it now.
</span><span class="cx">     updateFont();
</span><span class="cx"> 
</span><span class="lines">@@ -1738,22 +1864,18 @@
</span><span class="cx">     if (state.lineHeightValue())
</span><span class="cx">         applyProperty(CSSPropertyLineHeight, state.lineHeightValue());
</span><span class="cx"> 
</span><del>-    // Many properties depend on the font. If it changes we just apply all properties.
</del><ins>+    // If the font changed, we can't use the matched properties cache. Start over.
</ins><span class="cx">     if (cacheItem &amp;&amp; cacheItem-&gt;renderStyle-&gt;fontDescription() != state.style()-&gt;fontDescription())
</span><del>-        applyInheritedOnly = false;
</del><ins>+        return applyMatchedProperties(matchResult, element, DoNotUseMatchedPropertiesCache);
</ins><span class="cx"> 
</span><del>-    // Now do the normal priority UA properties.
-    applyMatchedProperties&lt;LowPriorityProperties&gt;(matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
-    
-    // Cache our border and background so that we can examine them later.
-    state.cacheBorderAndBackground();
-    
-    // Now do the author and user normal priority properties and all the !important properties.
-    applyMatchedProperties&lt;LowPriorityProperties&gt;(matchResult, false, matchResult.ranges.lastUARule + 1, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
-    applyMatchedProperties&lt;LowPriorityProperties&gt;(matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
-    applyMatchedProperties&lt;LowPriorityProperties&gt;(matchResult, true, matchResult.ranges.firstUserRule, matchResult.ranges.lastUserRule, applyInheritedOnly);
-    applyMatchedProperties&lt;LowPriorityProperties&gt;(matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
-   
</del><ins>+    // Apply properties that no other properties depend on.
+    applyCascadedProperties(cascade, CSSPropertyBackground, lastCSSProperty);
+
+    // Finally, some properties must be applied in the order they were parsed.
+    // There are some CSS properties that affect the same RenderStyle values,
+    // so to preserve behavior, we queue them up during cascade and flush here.
+    cascade.applyDeferredProperties(*this);
+
</ins><span class="cx">     // Start loading resources referenced by this style.
</span><span class="cx">     loadPendingResources();
</span><span class="cx">     
</span><span class="lines">@@ -4070,4 +4192,150 @@
</span><span class="cx">     return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+StyleResolver::CascadedProperties::Property::Property()
+    : id(CSSPropertyInvalid)
+    , isPresent(false)
+{
+    memset(cssValue, 0, sizeof(cssValue));
+}
+
+StyleResolver::CascadedProperties::CascadedProperties(TextDirection direction, WritingMode writingMode)
+    : m_direction(direction)
+    , m_writingMode(writingMode)
+{
+    memset(&amp;m_properties, 0, sizeof(m_properties));
+}
+
+inline StyleResolver::CascadedProperties::Property&amp; StyleResolver::CascadedProperties::property(CSSPropertyID id)
+{
+    return m_properties[id];
+}
+
+void StyleResolver::CascadedProperties::setPropertyInternal(Property&amp; property, CSSPropertyID id, CSSValue&amp; cssValue, unsigned linkMatchType)
+{
+    ASSERT(linkMatchType &lt;= SelectorChecker::MatchAll);
+    property.id = id;
+    property.isPresent = true;
+    if (linkMatchType == SelectorChecker::MatchAll) {
+        property.cssValue[0] = &amp;cssValue;
+        property.cssValue[SelectorChecker::MatchLink] = &amp;cssValue;
+        property.cssValue[SelectorChecker::MatchVisited] = &amp;cssValue;
+    } else
+        property.cssValue[linkMatchType] = &amp;cssValue;
+}
+
+void StyleResolver::CascadedProperties::set(CSSPropertyID id, CSSValue&amp; cssValue, unsigned linkMatchType)
+{
+    if (CSSProperty::isDirectionAwareProperty(id))
+        id = CSSProperty::resolveDirectionAwareProperty(id, m_direction, m_writingMode);
+
+    ASSERT(!shouldApplyPropertyInParseOrder(id));
+
+    auto&amp; property = m_properties[id];
+    setPropertyInternal(property, id, cssValue, linkMatchType);
+}
+
+void StyleResolver::CascadedProperties::setDeferred(CSSPropertyID id, CSSValue&amp; cssValue, unsigned linkMatchType)
+{
+    ASSERT(!CSSProperty::isDirectionAwareProperty(id));
+    ASSERT(shouldApplyPropertyInParseOrder(id));
+
+    Property property;
+    setPropertyInternal(property, id, cssValue, linkMatchType);
+    m_deferredProperties.append(property);
+}
+
+void StyleResolver::CascadedProperties::addStyleProperties(const StyleProperties&amp; properties, StyleRule&amp;, bool isImportant, bool inheritedOnly, PropertyWhitelistType propertyWhitelistType, unsigned linkMatchType)
+{
+    for (unsigned i = 0, count = properties.propertyCount(); i &lt; count; ++i) {
+        auto current = properties.propertyAt(i);
+        if (isImportant != current.isImportant())
+            continue;
+        if (inheritedOnly &amp;&amp; !current.isInherited()) {
+            // If the property value is explicitly inherited, we need to apply further non-inherited properties
+            // as they might override the value inherited here. For this reason we don't allow declarations with
+            // explicitly inherited properties to be cached.
+            ASSERT(!current.value()-&gt;isInheritedValue());
+            continue;
+        }
+        CSSPropertyID propertyID = current.id();
+
+        if (propertyWhitelistType == PropertyWhitelistRegion &amp;&amp; !StyleResolver::isValidRegionStyleProperty(propertyID))
+            continue;
+#if ENABLE(VIDEO_TRACK)
+        if (propertyWhitelistType == PropertyWhitelistCue &amp;&amp; !StyleResolver::isValidCueStyleProperty(propertyID))
+            continue;
+#endif
+
+        if (shouldApplyPropertyInParseOrder(propertyID))
+            setDeferred(propertyID, *current.value(), linkMatchType);
+        else
+            set(propertyID, *current.value(), linkMatchType);
+    }
+}
+
+void StyleResolver::CascadedProperties::addMatches(const MatchResult&amp; matchResult, bool important, int startIndex, int endIndex, bool inheritedOnly)
+{
+    if (startIndex == -1)
+        return;
+
+    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);
+    }
+}
+
+void StyleResolver::CascadedProperties::applyDeferredProperties(StyleResolver&amp; resolver)
+{
+    for (auto&amp; property : m_deferredProperties)
+        property.apply(resolver);
+}
+
+void StyleResolver::CascadedProperties::Property::apply(StyleResolver&amp; resolver)
+{
+    State&amp; state = resolver.state();
+
+    // FIXME: It would be nice if line-height were less of a special snowflake.
+    if (id == CSSPropertyLineHeight) {
+        if (cssValue[0])
+            state.setLineHeightValue(cssValue[0]);
+        return;
+    }
+
+    if (cssValue[0]) {
+        state.setApplyPropertyToRegularStyle(true);
+        state.setApplyPropertyToVisitedLinkStyle(false);
+        resolver.applyProperty(id, cssValue[0]);
+    }
+
+    if (state.style()-&gt;insideLink() == NotInsideLink)
+        return;
+
+    if (cssValue[SelectorChecker::MatchLink]) {
+        state.setApplyPropertyToRegularStyle(true);
+        state.setApplyPropertyToVisitedLinkStyle(false);
+        resolver.applyProperty(id, cssValue[SelectorChecker::MatchLink]);
+    }
+
+    if (cssValue[SelectorChecker::MatchVisited]) {
+        state.setApplyPropertyToRegularStyle(false);
+        state.setApplyPropertyToVisitedLinkStyle(true);
+        resolver.applyProperty(id, cssValue[SelectorChecker::MatchVisited]);
+    }
+
+    state.setApplyPropertyToRegularStyle(true);
+    state.setApplyPropertyToVisitedLinkStyle(false);
+}
+
+void StyleResolver::applyCascadedProperties(CascadedProperties&amp; cascade, int firstProperty, int lastProperty)
+{
+    for (int id = firstProperty; id &lt;= lastProperty; ++id) {
+        auto&amp; property = cascade.property(static_cast&lt;CSSPropertyID&gt;(id));
+        if (!property.isPresent)
+            continue;
+        ASSERT(!shouldApplyPropertyInParseOrder(property.id));
+        property.apply(*this);
+    }
+}
+
</ins><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorecssStyleResolverh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/css/StyleResolver.h (160805 => 160806)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/css/StyleResolver.h        2013-12-19 00:10:15 UTC (rev 160805)
+++ trunk/Source/WebCore/css/StyleResolver.h        2013-12-19 00:33:25 UTC (rev 160806)
</span><span class="lines">@@ -350,12 +350,19 @@
</span><span class="cx"> 
</span><span class="cx">     bool fastRejectSelector(const RuleData&amp;) const;
</span><span class="cx"> 
</span><del>-    void applyMatchedProperties(const MatchResult&amp;, const Element*);
</del><ins>+    enum ShouldUseMatchedPropertiesCache { DoNotUseMatchedPropertiesCache = 0, UseMatchedPropertiesCache };
+    void applyMatchedProperties(const MatchResult&amp;, const Element*, ShouldUseMatchedPropertiesCache = UseMatchedPropertiesCache);
</ins><span class="cx"> 
</span><span class="cx">     enum StyleApplicationPass {
</span><span class="cx">         HighPriorityProperties,
</span><span class="cx">         LowPriorityProperties
</span><span class="cx">     };
</span><ins>+
+    class CascadedProperties;
+
+    void applyCascadedProperties(CascadedProperties&amp;, int firstProperty, int lastProperty);
+    void cascadeMatches(CascadedProperties&amp;, const MatchResult&amp;, bool important, int startIndex, int endIndex, bool inheritedOnly);
+
</ins><span class="cx">     template &lt;StyleApplicationPass pass&gt;
</span><span class="cx">     void applyMatchedProperties(const MatchResult&amp;, bool important, int startIndex, int endIndex, bool inheritedOnly);
</span><span class="cx">     template &lt;StyleApplicationPass pass&gt;
</span><span class="lines">@@ -499,6 +506,8 @@
</span><span class="cx">         Color m_backgroundColor;
</span><span class="cx">     };
</span><span class="cx"> 
</span><ins>+    State&amp; state() { return m_state; }
+
</ins><span class="cx">     static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
</span><span class="cx"> 
</span><span class="cx">     PassRefPtr&lt;StyleImage&gt; styleImage(CSSPropertyID, CSSValue*);
</span></span></pre>
</div>
</div>

</body>
</html>