| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/css/RuleFeature.h" | 5 #include "core/css/RuleFeature.h" |
| 6 | 6 |
| 7 #include "core/css/CSSSelectorList.h" | 7 #include "core/css/CSSSelectorList.h" |
| 8 #include "core/css/RuleSet.h" | 8 #include "core/css/RuleSet.h" |
| 9 #include "core/css/StylePropertySet.h" | 9 #include "core/css/StylePropertySet.h" |
| 10 #include "core/css/StyleRule.h" | 10 #include "core/css/StyleRule.h" |
| 11 #include "core/css/invalidation/InvalidationSet.h" | 11 #include "core/css/invalidation/InvalidationSet.h" |
| 12 #include "core/css/parser/CSSParser.h" | 12 #include "core/css/parser/CSSParser.h" |
| 13 #include "core/dom/ElementTraversal.h" | 13 #include "core/dom/ElementTraversal.h" |
| 14 #include "core/html/HTMLBodyElement.h" | 14 #include "core/html/HTMLBodyElement.h" |
| 15 #include "core/html/HTMLDocument.h" | 15 #include "core/html/HTMLDocument.h" |
| 16 #include "core/html/HTMLElement.h" | 16 #include "core/html/HTMLElement.h" |
| 17 #include "core/html/HTMLHtmlElement.h" | 17 #include "core/html/HTMLHtmlElement.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 namespace blink { | 20 namespace blink { |
| 21 | 21 |
| 22 // TODO(sof): consider making these part object helper abstractions |
| 23 // available from platform/heap/. |
| 24 |
| 25 template<typename T> |
| 26 class HeapPartObject final : public GarbageCollectedFinalized<HeapPartObject<T>>
{ |
| 27 public: |
| 28 static HeapPartObject* create() |
| 29 { |
| 30 return new HeapPartObject; |
| 31 } |
| 32 |
| 33 T* get() { return &m_part; } |
| 34 |
| 35 DEFINE_INLINE_TRACE() |
| 36 { |
| 37 visitor->trace(m_part); |
| 38 } |
| 39 |
| 40 private: |
| 41 HeapPartObject() |
| 42 { |
| 43 } |
| 44 |
| 45 T m_part; |
| 46 }; |
| 47 |
| 48 template<typename T> |
| 49 class PersistentPartObject final { |
| 50 DISALLOW_NEW(); |
| 51 public: |
| 52 PersistentPartObject() |
| 53 : m_part(HeapPartObject<T>::create()) |
| 54 { |
| 55 } |
| 56 |
| 57 T* operator->() const { return (*m_part).get(); } |
| 58 |
| 59 private: |
| 60 Persistent<HeapPartObject<T>> m_part; |
| 61 }; |
| 62 |
| 22 class RuleFeatureSetTest : public ::testing::Test { | 63 class RuleFeatureSetTest : public ::testing::Test { |
| 23 public: | 64 public: |
| 24 RuleFeatureSetTest() | 65 RuleFeatureSetTest() |
| 25 { | 66 { |
| 26 } | 67 } |
| 27 | 68 |
| 28 void SetUp() | 69 void SetUp() |
| 29 { | 70 { |
| 30 m_document = HTMLDocument::create(); | 71 m_document = HTMLDocument::create(); |
| 31 HTMLHtmlElement* html = HTMLHtmlElement::create(*m_document); | 72 HTMLHtmlElement* html = HTMLHtmlElement::create(*m_document); |
| 32 html->appendChild(HTMLBodyElement::create(*m_document)); | 73 html->appendChild(HTMLBodyElement::create(*m_document)); |
| 33 m_document->appendChild(html); | 74 m_document->appendChild(html); |
| 34 | 75 |
| 35 m_document->body()->setInnerHTML("<b><i></i></b>", ASSERT_NO_EXCEPTION); | 76 m_document->body()->setInnerHTML("<b><i></i></b>", ASSERT_NO_EXCEPTION); |
| 36 } | 77 } |
| 37 | 78 |
| 38 RuleFeatureSet::SelectorPreMatch collectFeatures(const String& selectorText) | 79 RuleFeatureSet::SelectorPreMatch collectFeatures(const String& selectorText) |
| 39 { | 80 { |
| 40 CSSSelectorList selectorList = CSSParser::parseSelector(strictCSSParserC
ontext(), nullptr, selectorText); | 81 CSSSelectorList selectorList = CSSParser::parseSelector(strictCSSParserC
ontext(), nullptr, selectorText); |
| 41 | 82 |
| 42 StyleRule* styleRule = StyleRule::create(std::move(selectorList), Mutabl
eStylePropertySet::create(HTMLStandardMode)); | 83 StyleRule* styleRule = StyleRule::create(std::move(selectorList), Mutabl
eStylePropertySet::create(HTMLStandardMode)); |
| 43 RuleData ruleData(styleRule, 0, 0, RuleHasNoSpecialState); | 84 RuleData ruleData(styleRule, 0, 0, RuleHasNoSpecialState); |
| 44 return m_ruleFeatureSet.collectFeaturesFromRuleData(ruleData); | 85 return m_ruleFeatureSet->collectFeaturesFromRuleData(ruleData); |
| 45 } | 86 } |
| 46 | 87 |
| 47 void collectInvalidationSetsForClass(InvalidationLists& invalidationLists, c
onst AtomicString& className) const | 88 void collectInvalidationSetsForClass(InvalidationLists& invalidationLists, c
onst AtomicString& className) const |
| 48 { | 89 { |
| 49 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); | 90 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 50 m_ruleFeatureSet.collectInvalidationSetsForClass(invalidationLists, *ele
ment, className); | 91 m_ruleFeatureSet->collectInvalidationSetsForClass(invalidationLists, *el
ement, className); |
| 51 } | 92 } |
| 52 | 93 |
| 53 void collectInvalidationSetsForId(InvalidationLists& invalidationLists, cons
t AtomicString& id) const | 94 void collectInvalidationSetsForId(InvalidationLists& invalidationLists, cons
t AtomicString& id) const |
| 54 { | 95 { |
| 55 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); | 96 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 56 m_ruleFeatureSet.collectInvalidationSetsForId(invalidationLists, *elemen
t, id); | 97 m_ruleFeatureSet->collectInvalidationSetsForId(invalidationLists, *eleme
nt, id); |
| 57 } | 98 } |
| 58 | 99 |
| 59 void collectInvalidationSetsForAttribute(InvalidationLists& invalidationList
s, const QualifiedName& attributeName) const | 100 void collectInvalidationSetsForAttribute(InvalidationLists& invalidationList
s, const QualifiedName& attributeName) const |
| 60 { | 101 { |
| 61 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); | 102 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 62 m_ruleFeatureSet.collectInvalidationSetsForAttribute(invalidationLists,
*element, attributeName); | 103 m_ruleFeatureSet->collectInvalidationSetsForAttribute(invalidationLists,
*element, attributeName); |
| 63 } | 104 } |
| 64 | 105 |
| 65 void collectInvalidationSetsForPseudoClass(InvalidationLists& invalidationLi
sts, CSSSelector::PseudoType pseudo) const | 106 void collectInvalidationSetsForPseudoClass(InvalidationLists& invalidationLi
sts, CSSSelector::PseudoType pseudo) const |
| 66 { | 107 { |
| 67 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); | 108 Element* element = Traversal<HTMLElement>::firstChild(*Traversal<HTMLEle
ment>::firstChild(*m_document->body())); |
| 68 m_ruleFeatureSet.collectInvalidationSetsForPseudoClass(invalidationLists
, *element, pseudo); | 109 m_ruleFeatureSet->collectInvalidationSetsForPseudoClass(invalidationList
s, *element, pseudo); |
| 69 } | 110 } |
| 70 | 111 |
| 71 const HashSet<AtomicString>& classSet(const InvalidationSet& invalidationSet
) | 112 const HashSet<AtomicString>& classSet(const InvalidationSet& invalidationSet
) |
| 72 { | 113 { |
| 73 return invalidationSet.classSetForTesting(); | 114 return invalidationSet.classSetForTesting(); |
| 74 } | 115 } |
| 75 | 116 |
| 76 const HashSet<AtomicString>& idSet(const InvalidationSet& invalidationSet) | 117 const HashSet<AtomicString>& idSet(const InvalidationSet& invalidationSet) |
| 77 { | 118 { |
| 78 return invalidationSet.idSetForTesting(); | 119 return invalidationSet.idSetForTesting(); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 void expectAttributeInvalidation(const AtomicString& attribute, Invalidation
SetVector& invalidationSets) | 206 void expectAttributeInvalidation(const AtomicString& attribute, Invalidation
SetVector& invalidationSets) |
| 166 { | 207 { |
| 167 EXPECT_EQ(1u, invalidationSets.size()); | 208 EXPECT_EQ(1u, invalidationSets.size()); |
| 168 HashSet<AtomicString> attributes = attributeSet(*invalidationSets[0]); | 209 HashSet<AtomicString> attributes = attributeSet(*invalidationSets[0]); |
| 169 EXPECT_EQ(1u, attributes.size()); | 210 EXPECT_EQ(1u, attributes.size()); |
| 170 EXPECT_TRUE(attributes.contains(attribute)); | 211 EXPECT_TRUE(attributes.contains(attribute)); |
| 171 } | 212 } |
| 172 | 213 |
| 173 void expectSiblingRuleCount(unsigned count) | 214 void expectSiblingRuleCount(unsigned count) |
| 174 { | 215 { |
| 175 EXPECT_EQ(count, m_ruleFeatureSet.siblingRules.size()); | 216 EXPECT_EQ(count, m_ruleFeatureSet->siblingRules.size()); |
| 176 } | 217 } |
| 177 | 218 |
| 178 void expectUncommonAttributeRuleCount(unsigned count) | 219 void expectUncommonAttributeRuleCount(unsigned count) |
| 179 { | 220 { |
| 180 EXPECT_EQ(count, m_ruleFeatureSet.uncommonAttributeRules.size()); | 221 EXPECT_EQ(count, m_ruleFeatureSet->uncommonAttributeRules.size()); |
| 181 } | |
| 182 | |
| 183 DEFINE_INLINE_TRACE() | |
| 184 { | |
| 185 visitor->trace(m_ruleFeatureSet); | |
| 186 visitor->trace(m_document); | |
| 187 } | 222 } |
| 188 | 223 |
| 189 private: | 224 private: |
| 190 RuleFeatureSet m_ruleFeatureSet; | 225 PersistentPartObject<RuleFeatureSet> m_ruleFeatureSet; |
| 191 Persistent<Document> m_document; | 226 Persistent<Document> m_document; |
| 192 }; | 227 }; |
| 193 | 228 |
| 194 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling1) | 229 TEST_F(RuleFeatureSetTest, interleavedDescendantSibling1) |
| 195 { | 230 { |
| 196 EXPECT_EQ(RuleFeatureSet::SelectorMayMatch, collectFeatures(".p")); | 231 EXPECT_EQ(RuleFeatureSet::SelectorMayMatch, collectFeatures(".p")); |
| 197 | 232 |
| 198 InvalidationLists invalidationLists; | 233 InvalidationLists invalidationLists; |
| 199 collectInvalidationSetsForClass(invalidationLists, "p"); | 234 collectInvalidationSetsForClass(invalidationLists, "p"); |
| 200 expectSelfInvalidation(invalidationLists.descendants); | 235 expectSelfInvalidation(invalidationLists.descendants); |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 expectUncommonAttributeRuleCount(1); | 562 expectUncommonAttributeRuleCount(1); |
| 528 } | 563 } |
| 529 | 564 |
| 530 TEST_F(RuleFeatureSetTest, uncommonAttributeRulesAfterHostContextBeforePseudo) | 565 TEST_F(RuleFeatureSetTest, uncommonAttributeRulesAfterHostContextBeforePseudo) |
| 531 { | 566 { |
| 532 EXPECT_EQ(RuleFeatureSet::SelectorMayMatch, collectFeatures(":host-context([
attr])::before")); | 567 EXPECT_EQ(RuleFeatureSet::SelectorMayMatch, collectFeatures(":host-context([
attr])::before")); |
| 533 expectUncommonAttributeRuleCount(1); | 568 expectUncommonAttributeRuleCount(1); |
| 534 } | 569 } |
| 535 | 570 |
| 536 } // namespace blink | 571 } // namespace blink |
| OLD | NEW |