Eliminate RuleRange.

In Sky there's no UA rules so we don't need to keep track of the
range of UA vs Author rules for running applyMatchedProperties later.

R=ojan@chromium.org

Review URL: https://codereview.chromium.org/863883002
diff --git a/sky/engine/core/css/ElementRuleCollector.cpp b/sky/engine/core/css/ElementRuleCollector.cpp
index 3a8267e..c6b0c26 100644
--- a/sky/engine/core/css/ElementRuleCollector.cpp
+++ b/sky/engine/core/css/ElementRuleCollector.cpp
@@ -72,15 +72,12 @@
 {
     if (!propertySet)
         return;
-    m_result.ranges.lastAuthorRule = m_result.matchedProperties.size();
-    if (m_result.ranges.firstAuthorRule == -1)
-        m_result.ranges.firstAuthorRule = m_result.ranges.lastAuthorRule;
     m_result.addMatchedProperties(propertySet);
     if (!isCacheable)
         m_result.isCacheable = false;
 }
 
-void ElementRuleCollector::collectMatchingRules(const MatchRequest& matchRequest, RuleRange& ruleRange, CascadeOrder cascadeOrder)
+void ElementRuleCollector::collectMatchingRules(const MatchRequest& matchRequest, CascadeOrder cascadeOrder)
 {
     ASSERT(matchRequest.ruleSet);
     ASSERT(m_context.element());
@@ -90,19 +87,19 @@
     // We need to collect the rules for id, class, tag, and everything else into a buffer and
     // then sort the buffer.
     if (element.hasID())
-        collectMatchingRulesForList(matchRequest.ruleSet->idRules(element.idForStyleResolution()), cascadeOrder, matchRequest, ruleRange);
+        collectMatchingRulesForList(matchRequest.ruleSet->idRules(element.idForStyleResolution()), cascadeOrder, matchRequest);
     if (element.isStyledElement() && element.hasClass()) {
         for (size_t i = 0; i < element.classNames().size(); ++i)
-            collectMatchingRulesForList(matchRequest.ruleSet->classRules(element.classNames()[i]), cascadeOrder, matchRequest, ruleRange);
+            collectMatchingRulesForList(matchRequest.ruleSet->classRules(element.classNames()[i]), cascadeOrder, matchRequest);
     }
 
-    collectMatchingRulesForList(matchRequest.ruleSet->tagRules(element.localName()), cascadeOrder, matchRequest, ruleRange);
-    collectMatchingRulesForList(matchRequest.ruleSet->universalRules(), cascadeOrder, matchRequest, ruleRange);
+    collectMatchingRulesForList(matchRequest.ruleSet->tagRules(element.localName()), cascadeOrder, matchRequest);
+    collectMatchingRulesForList(matchRequest.ruleSet->universalRules(), cascadeOrder, matchRequest);
 }
 
-void ElementRuleCollector::collectMatchingHostRules(const MatchRequest& matchRequest, RuleRange& ruleRange, CascadeOrder cascadeOrder)
+void ElementRuleCollector::collectMatchingHostRules(const MatchRequest& matchRequest, CascadeOrder cascadeOrder)
 {
-    collectMatchingRulesForList(matchRequest.ruleSet->hostRules(), cascadeOrder, matchRequest, ruleRange);
+    collectMatchingRulesForList(matchRequest.ruleSet->hostRules(), cascadeOrder, matchRequest);
 }
 
 void ElementRuleCollector::sortAndTransferMatchedRules()
@@ -141,7 +138,7 @@
     return matched;
 }
 
-void ElementRuleCollector::collectRuleIfMatches(const RuleData& ruleData, CascadeOrder cascadeOrder, const MatchRequest& matchRequest, RuleRange& ruleRange)
+void ElementRuleCollector::collectRuleIfMatches(const RuleData& ruleData, CascadeOrder cascadeOrder, const MatchRequest& matchRequest)
 {
     StyleRule* rule = ruleData.rule();
     if (ruleMatches(ruleData)) {
@@ -150,11 +147,6 @@
         if (properties.isEmpty())
             return;
 
-        // Update our first/last rule indices in the matched rules array.
-        ++ruleRange.lastRuleIndex;
-        if (ruleRange.firstRuleIndex == -1)
-            ruleRange.firstRuleIndex = ruleRange.lastRuleIndex;
-
         // Add this rule to our list of matched rules.
         addMatchedRule(&ruleData, cascadeOrder, matchRequest.styleSheetIndex, matchRequest.styleSheet);
     }
diff --git a/sky/engine/core/css/ElementRuleCollector.h b/sky/engine/core/css/ElementRuleCollector.h
index 4da85d9..2156ffc 100644
--- a/sky/engine/core/css/ElementRuleCollector.h
+++ b/sky/engine/core/css/ElementRuleCollector.h
@@ -22,6 +22,7 @@
 #ifndef SKY_ENGINE_CORE_CSS_ELEMENTRULECOLLECTOR_H_
 #define SKY_ENGINE_CORE_CSS_ELEMENTRULECOLLECTOR_H_
 
+#include "sky/engine/core/css/RuleSet.h"
 #include "sky/engine/core/css/SelectorChecker.h"
 #include "sky/engine/core/css/resolver/ElementResolveContext.h"
 #include "sky/engine/core/css/resolver/MatchRequest.h"
@@ -32,8 +33,6 @@
 namespace blink {
 
 class CSSStyleSheet;
-class RuleData;
-class RuleSet;
 class ScopedStyleResolver;
 
 typedef unsigned CascadeOrder;
@@ -94,23 +93,23 @@
 
     MatchResult& matchedResult();
 
-    void collectMatchingRules(const MatchRequest&, RuleRange&, CascadeOrder = ignoreCascadeOrder);
-    void collectMatchingHostRules(const MatchRequest&, RuleRange&, CascadeOrder cascadeOrder = ignoreCascadeOrder);
+    void collectMatchingRules(const MatchRequest&, CascadeOrder = ignoreCascadeOrder);
+    void collectMatchingHostRules(const MatchRequest&, CascadeOrder cascadeOrder = ignoreCascadeOrder);
     void sortAndTransferMatchedRules();
     void clearMatchedRules();
     void addElementStyleProperties(const StylePropertySet*, bool isCacheable = true);
 
 private:
-    void collectRuleIfMatches(const RuleData&, CascadeOrder, const MatchRequest&, RuleRange&);
+    void collectRuleIfMatches(const RuleData&, CascadeOrder, const MatchRequest&);
 
     template<typename RuleDataListType>
-    void collectMatchingRulesForList(const RuleDataListType* rules, CascadeOrder cascadeOrder, const MatchRequest& matchRequest, RuleRange& ruleRange)
+    void collectMatchingRulesForList(const RuleDataListType* rules, CascadeOrder cascadeOrder, const MatchRequest& matchRequest)
     {
         if (!rules)
             return;
 
         for (typename RuleDataListType::const_iterator it = rules->begin(), end = rules->end(); it != end; ++it)
-            collectRuleIfMatches(*it, cascadeOrder, matchRequest, ruleRange);
+            collectRuleIfMatches(*it, cascadeOrder, matchRequest);
     }
 
     bool ruleMatches(const RuleData&);
diff --git a/sky/engine/core/css/resolver/MatchResult.cpp b/sky/engine/core/css/resolver/MatchResult.cpp
index 1abed72..2432b0f 100644
--- a/sky/engine/core/css/resolver/MatchResult.cpp
+++ b/sky/engine/core/css/resolver/MatchResult.cpp
@@ -34,19 +34,10 @@
 
 namespace blink {
 
-MatchedProperties::MatchedProperties()
-{
-}
-
-MatchedProperties::~MatchedProperties()
-{
-}
-
 void MatchResult::addMatchedProperties(const StylePropertySet* properties)
 {
     matchedProperties.grow(matchedProperties.size() + 1);
-    MatchedProperties& newProperties = matchedProperties.last();
-    newProperties.properties = const_cast<StylePropertySet*>(properties);
+    matchedProperties.last() = const_cast<StylePropertySet*>(properties);
 }
 
 } // namespace blink
diff --git a/sky/engine/core/css/resolver/MatchResult.h b/sky/engine/core/css/resolver/MatchResult.h
index c1694b3..10b8677 100644
--- a/sky/engine/core/css/resolver/MatchResult.h
+++ b/sky/engine/core/css/resolver/MatchResult.h
@@ -23,8 +23,6 @@
 #ifndef SKY_ENGINE_CORE_CSS_RESOLVER_MATCHRESULT_H_
 #define SKY_ENGINE_CORE_CSS_RESOLVER_MATCHRESULT_H_
 
-#include "sky/engine/core/css/RuleSet.h"
-#include "sky/engine/core/css/SelectorChecker.h"
 #include "sky/engine/platform/heap/Handle.h"
 #include "sky/engine/wtf/RefPtr.h"
 #include "sky/engine/wtf/Vector.h"
@@ -33,71 +31,16 @@
 
 class StylePropertySet;
 
-struct RuleRange {
-    RuleRange(int& firstRuleIndex, int& lastRuleIndex): firstRuleIndex(firstRuleIndex), lastRuleIndex(lastRuleIndex) { }
-    int& firstRuleIndex;
-    int& lastRuleIndex;
-};
-
-struct MatchRanges {
-    MatchRanges() : firstUARule(-1), lastUARule(-1), firstAuthorRule(-1), lastAuthorRule(-1) { }
-    int firstUARule;
-    int lastUARule;
-    int firstAuthorRule;
-    int lastAuthorRule;
-    RuleRange UARuleRange() { return RuleRange(firstUARule, lastUARule); }
-    RuleRange authorRuleRange() { return RuleRange(firstAuthorRule, lastAuthorRule); }
-};
-
-struct MatchedProperties {
-    ALLOW_ONLY_INLINE_ALLOCATION();
-public:
-    MatchedProperties();
-    ~MatchedProperties();
-
-    RefPtr<StylePropertySet> properties;
-};
-
-} // namespace blink
-
-WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::MatchedProperties);
-
-namespace blink {
-
 class MatchResult {
     STACK_ALLOCATED();
 public:
     MatchResult() : isCacheable(true) { }
-    Vector<MatchedProperties, 64> matchedProperties;
-    MatchRanges ranges;
+    Vector<RefPtr<StylePropertySet>, 64> matchedProperties;
     bool isCacheable;
 
-    void addMatchedProperties(const StylePropertySet* properties);
+    void addMatchedProperties(const StylePropertySet*);
 };
 
-inline bool operator==(const MatchRanges& a, const MatchRanges& b)
-{
-    return a.firstUARule == b.firstUARule
-        && a.lastUARule == b.lastUARule
-        && a.firstAuthorRule == b.firstAuthorRule
-        && a.lastAuthorRule == b.lastAuthorRule;
-}
-
-inline bool operator!=(const MatchRanges& a, const MatchRanges& b)
-{
-    return !(a == b);
-}
-
-inline bool operator==(const MatchedProperties& a, const MatchedProperties& b)
-{
-    return a.properties == b.properties;
-}
-
-inline bool operator!=(const MatchedProperties& a, const MatchedProperties& b)
-{
-    return !(a == b);
-}
-
 } // namespace blink
 
 #endif  // SKY_ENGINE_CORE_CSS_RESOLVER_MATCHRESULT_H_
diff --git a/sky/engine/core/css/resolver/MatchedPropertiesCache.cpp b/sky/engine/core/css/resolver/MatchedPropertiesCache.cpp
index 4f68638..edcf16f 100644
--- a/sky/engine/core/css/resolver/MatchedPropertiesCache.cpp
+++ b/sky/engine/core/css/resolver/MatchedPropertiesCache.cpp
@@ -38,7 +38,6 @@
 void CachedMatchedProperties::set(const RenderStyle* style, const RenderStyle* parentStyle, const MatchResult& matchResult)
 {
     matchedProperties.appendVector(matchResult.matchedProperties);
-    ranges = matchResult.ranges;
 
     // Note that we don't cache the original RenderStyle instance. It may be further modified.
     // The RenderStyle in the cache is really just a holder for the substructures and never used as-is.
@@ -76,8 +75,6 @@
         if (matchResult.matchedProperties[i] != cacheItem->matchedProperties[i])
             return 0;
     }
-    if (cacheItem->ranges != matchResult.ranges)
-        return 0;
     return cacheItem;
 }
 
@@ -126,9 +123,9 @@
     Cache::iterator end = m_cache.end();
     for (; it != end; ++it) {
         CachedMatchedProperties* cacheItem = it->value.get();
-        Vector<MatchedProperties>& matchedProperties = cacheItem->matchedProperties;
+        Vector<RefPtr<StylePropertySet>>& matchedProperties = cacheItem->matchedProperties;
         for (size_t i = 0; i < matchedProperties.size(); ++i) {
-            if (matchedProperties[i].properties->hasOneRef()) {
+            if (matchedProperties[i]->hasOneRef()) {
                 toRemove.append(it->key);
                 break;
             }
diff --git a/sky/engine/core/css/resolver/MatchedPropertiesCache.h b/sky/engine/core/css/resolver/MatchedPropertiesCache.h
index bbf056e..7ad4f93 100644
--- a/sky/engine/core/css/resolver/MatchedPropertiesCache.h
+++ b/sky/engine/core/css/resolver/MatchedPropertiesCache.h
@@ -37,10 +37,8 @@
 class StyleResolverState;
 
 class CachedMatchedProperties final {
-
 public:
-    Vector<MatchedProperties> matchedProperties;
-    MatchRanges ranges;
+    Vector<RefPtr<StylePropertySet>> matchedProperties;
     RefPtr<RenderStyle> renderStyle;
     RefPtr<RenderStyle> parentRenderStyle;
 
diff --git a/sky/engine/core/css/resolver/ScopedStyleResolver.cpp b/sky/engine/core/css/resolver/ScopedStyleResolver.cpp
index 7688fed..6655bf3 100644
--- a/sky/engine/core/css/resolver/ScopedStyleResolver.cpp
+++ b/sky/engine/core/css/resolver/ScopedStyleResolver.cpp
@@ -112,19 +112,17 @@
 
 void ScopedStyleResolver::collectMatchingAuthorRules(ElementRuleCollector& collector, CascadeOrder cascadeOrder)
 {
-    RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
         MatchRequest matchRequest(&m_authorStyleSheets[i]->contents()->ruleSet(), m_authorStyleSheets[i].get(), i);
-        collector.collectMatchingRules(matchRequest, ruleRange, cascadeOrder);
+        collector.collectMatchingRules(matchRequest, cascadeOrder);
     }
 }
 
 void ScopedStyleResolver::collectMatchingHostRules(ElementRuleCollector& collector, CascadeOrder cascadeOrder)
 {
-    RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
         MatchRequest matchRequest(&m_authorStyleSheets[i]->contents()->ruleSet(), m_authorStyleSheets[i].get(), i);
-        collector.collectMatchingHostRules(matchRequest, ruleRange, cascadeOrder);
+        collector.collectMatchingHostRules(matchRequest, cascadeOrder);
     }
 }
 
diff --git a/sky/engine/core/css/resolver/StyleResolver.cpp b/sky/engine/core/css/resolver/StyleResolver.cpp
index e9d22f5..c2e2b96 100644
--- a/sky/engine/core/css/resolver/StyleResolver.cpp
+++ b/sky/engine/core/css/resolver/StyleResolver.cpp
@@ -148,12 +148,10 @@
 void StyleResolver::matchRules(Element& element, ElementRuleCollector& collector)
 {
     collector.clearMatchedRules();
-    collector.matchedResult().ranges.lastAuthorRule = collector.matchedResult().matchedProperties.size() - 1;
 
     CascadeOrder cascadeOrder = 0;
 
-    RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
-    collector.collectMatchingRules(MatchRequest(&defaultStyles()), ruleRange, ++cascadeOrder);
+    collector.collectMatchingRules(MatchRequest(&defaultStyles()), ++cascadeOrder);
 
     if (ShadowRoot* shadowRoot = element.shadowRoot())
         shadowRoot->scopedStyleResolver().collectMatchingHostRules(collector, ++cascadeOrder);
@@ -279,7 +277,7 @@
     // relevant one is animation-timing-function and we special-case that in
     // CSSAnimations.cpp
     bool inheritedOnly = false;
-    applyMatchedProperties<HighPriorityProperties>(state, result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
+    applyMatchedProperties<HighPriorityProperties>(state, result, false, inheritedOnly);
 
     // If our font got dirtied, go ahead and update it now.
     updateFont(state);
@@ -289,7 +287,7 @@
         StyleBuilder::applyProperty(CSSPropertyLineHeight, state, state.lineHeightValue());
 
     // Now do rest of the properties.
-    applyMatchedProperties<LowPriorityProperties>(state, result, false, 0, result.matchedProperties.size() - 1, inheritedOnly);
+    applyMatchedProperties<LowPriorityProperties>(state, result, false, inheritedOnly);
 
     loadPendingResources(state);
 
@@ -522,19 +520,16 @@
 }
 
 template <StyleResolver::StyleApplicationPass pass>
-void StyleResolver::applyMatchedProperties(StyleResolverState& state, const MatchResult& matchResult, bool isImportant, int startIndex, int endIndex, bool inheritedOnly)
+void StyleResolver::applyMatchedProperties(StyleResolverState& state, const MatchResult& matchResult, bool isImportant, bool inheritedOnly)
 {
-    if (startIndex == -1)
-        return;
-    for (int i = startIndex; i <= endIndex; ++i) {
-        const MatchedProperties& matchedProperties = matchResult.matchedProperties[i];
-        applyProperties<pass>(state, matchedProperties.properties.get(), isImportant, inheritedOnly);
+    for (const RefPtr<StylePropertySet>& properties : matchResult.matchedProperties) {
+        applyProperties<pass>(state, properties.get(), isImportant, inheritedOnly);
     }
 }
 
-static unsigned computeMatchedPropertiesHash(const MatchedProperties* properties, unsigned size)
+static unsigned computeMatchedPropertiesHash(const RefPtr<StylePropertySet>* properties, unsigned size)
 {
-    return StringHasher::hashMemory(properties, sizeof(MatchedProperties) * size);
+    return StringHasher::hashMemory(properties, sizeof(*properties) * size);
 }
 
 void StyleResolver::invalidateMatchedPropertiesCache()
@@ -582,9 +577,8 @@
     // The order is (1) high-priority not important, (2) high-priority important, (3) normal not important
     // and (4) normal important.
     state.setLineHeightValue(0);
-    applyMatchedProperties<HighPriorityProperties>(state, matchResult, false, 0, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
-    applyMatchedProperties<HighPriorityProperties>(state, matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
-    applyMatchedProperties<HighPriorityProperties>(state, matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
+    applyMatchedProperties<HighPriorityProperties>(state, matchResult, false, applyInheritedOnly);
+    applyMatchedProperties<HighPriorityProperties>(state, matchResult, true, applyInheritedOnly);
 
     // If our font got dirtied, go ahead and update it now.
     updateFont(state);
@@ -597,13 +591,9 @@
     if (cachedMatchedProperties && cachedMatchedProperties->renderStyle->fontDescription() != state.style()->fontDescription())
         applyInheritedOnly = false;
 
-    // Now do the normal priority UA properties.
-    applyMatchedProperties<LowPriorityProperties>(state, matchResult, false, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
-
     // Now do the author and user normal priority properties and all the !important properties.
-    applyMatchedProperties<LowPriorityProperties>(state, matchResult, false, matchResult.ranges.lastUARule + 1, matchResult.matchedProperties.size() - 1, applyInheritedOnly);
-    applyMatchedProperties<LowPriorityProperties>(state, matchResult, true, matchResult.ranges.firstAuthorRule, matchResult.ranges.lastAuthorRule, applyInheritedOnly);
-    applyMatchedProperties<LowPriorityProperties>(state, matchResult, true, matchResult.ranges.firstUARule, matchResult.ranges.lastUARule, applyInheritedOnly);
+    applyMatchedProperties<LowPriorityProperties>(state, matchResult, false, applyInheritedOnly);
+    applyMatchedProperties<LowPriorityProperties>(state, matchResult, true, applyInheritedOnly);
 
     loadPendingResources(state);
 
diff --git a/sky/engine/core/css/resolver/StyleResolver.h b/sky/engine/core/css/resolver/StyleResolver.h
index 398da63..813b160 100644
--- a/sky/engine/core/css/resolver/StyleResolver.h
+++ b/sky/engine/core/css/resolver/StyleResolver.h
@@ -120,7 +120,7 @@
     template <StyleResolver::StyleApplicationPass pass>
     static inline bool isPropertyForPass(CSSPropertyID);
     template <StyleApplicationPass pass>
-    void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly);
+    void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, bool inheritedOnly);
     template <StyleApplicationPass pass>
     void applyProperties(StyleResolverState&, const StylePropertySet* properties, bool isImportant, bool inheritedOnly);
     template <StyleApplicationPass pass>