OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "core/css/analyzer/RuleSetAnalyzer.h" |
| 33 |
| 34 #include "core/css/CSSSelector.h" |
| 35 #include "core/css/CSSSelectorList.h" |
| 36 #include "core/css/RuleFeature.h" |
| 37 #include "core/css/RuleSet.h" |
| 38 #include "core/css/analyzer/DescendantInvalidationSet.h" |
| 39 #include "wtf/Forward.h" |
| 40 #include "wtf/HashSet.h" |
| 41 #include "wtf/text/AtomicString.h" |
| 42 #include "wtf/text/StringHash.h" |
| 43 |
| 44 |
| 45 namespace WebCore { |
| 46 |
| 47 static bool isSkippableComponentForInvalidation(const CSSSelector* selector) |
| 48 { |
| 49 if (selector->matchesPseudoElement() || selector->pseudoType() == CSSSelecto
r::PseudoHost) |
| 50 return false; |
| 51 return true; |
| 52 } |
| 53 |
| 54 // This method is somewhat conservative in what it acceptss. |
| 55 static bool supportsClassDescendantInvalidation(const CSSSelector* selector) |
| 56 { |
| 57 bool foundDescendantRelation = false; |
| 58 bool foundAncestorIdent = false; |
| 59 bool foundIdent = false; |
| 60 for (const CSSSelector* component = selector; component; component = compone
nt->tagHistory()) { |
| 61 |
| 62 // FIXME: We should allow pseudo elements, but we need to change how the
y hook |
| 63 // into recalcStyle by moving them to recalcOwnStyle instead of recalcCh
ildStyle. |
| 64 |
| 65 if (component->m_match == CSSSelector::Tag |
| 66 || component->m_match == CSSSelector::Id |
| 67 || component->m_match == CSSSelector::Class) { |
| 68 if (!foundDescendantRelation) |
| 69 foundIdent = true; |
| 70 else |
| 71 foundAncestorIdent = true; |
| 72 } else if (!isSkippableComponentForInvalidation(component)) { |
| 73 return false; |
| 74 } |
| 75 // FIXME: We can probably support ChildTree and DescendantTree. |
| 76 switch (component->relation()) { |
| 77 case CSSSelector::Descendant: |
| 78 case CSSSelector::Child: |
| 79 foundDescendantRelation = true; |
| 80 // Fall through! |
| 81 case CSSSelector::SubSelector: |
| 82 continue; |
| 83 default: |
| 84 return false; |
| 85 } |
| 86 } |
| 87 return foundDescendantRelation && foundAncestorIdent && foundIdent; |
| 88 } |
| 89 |
| 90 void extractClassIdOrTag(const CSSSelector& selector, HashSet<AtomicString>& cla
sses, AtomicString& id, AtomicString& tagName) |
| 91 { |
| 92 if (selector.m_match == CSSSelector::Tag) |
| 93 tagName = selector.tagQName().localName(); |
| 94 else if (selector.m_match == CSSSelector::Id) |
| 95 id = selector.value(); |
| 96 else if (selector.m_match == CSSSelector::Class) |
| 97 classes.add(selector.value()); |
| 98 } |
| 99 |
| 100 RuleSetAnalyzer::RuleSetAnalyzer() |
| 101 { |
| 102 } |
| 103 |
| 104 bool RuleSetAnalyzer::updateClassInvalidationSets(const CSSSelector* selector) |
| 105 { |
| 106 if (!selector) |
| 107 return false; |
| 108 if (!supportsClassDescendantInvalidation(selector)) |
| 109 return false; |
| 110 |
| 111 HashSet<AtomicString> classes; |
| 112 AtomicString id; |
| 113 AtomicString tagName; |
| 114 |
| 115 const CSSSelector* lastSelector = selector; |
| 116 for (; lastSelector->relation() == CSSSelector::SubSelector; lastSelector =
lastSelector->tagHistory()) { |
| 117 extractClassIdOrTag(*selector, classes, id, tagName); |
| 118 } |
| 119 extractClassIdOrTag(*selector, classes, id, tagName); |
| 120 |
| 121 for ( ; selector; selector = selector->tagHistory()) { |
| 122 if (selector->m_match == CSSSelector::Class) { |
| 123 DescendantInvalidationSet* invalidationSet = ensureClassInvalidation
Set(selector->value()); |
| 124 if (!id.isEmpty()) |
| 125 invalidationSet->addId(id); |
| 126 if (!tagName.isEmpty()) |
| 127 invalidationSet->addTagName(tagName); |
| 128 for (HashSet<AtomicString>::const_iterator it = classes.begin(); it
!= classes.end(); ++it) { |
| 129 invalidationSet->addClass(*it); |
| 130 } |
| 131 } |
| 132 } |
| 133 return true; |
| 134 } |
| 135 |
| 136 PassRefPtr<RuleSetAnalyzer> RuleSetAnalyzer::create() |
| 137 { |
| 138 return adoptRef(new RuleSetAnalyzer); |
| 139 } |
| 140 |
| 141 void RuleSetAnalyzer::collectFeaturesFromRuleData(RuleFeatureSet& features, cons
t RuleData& ruleData) |
| 142 { |
| 143 bool foundSiblingSelector = false; |
| 144 unsigned maxDirectAdjacentSelectors = 0; |
| 145 bool selectorUsesClassInvalidationSet= updateClassInvalidationSets(ruleData.
selector()); |
| 146 for (const CSSSelector* selector = ruleData.selector(); selector; selector =
selector->tagHistory()) { |
| 147 features.collectFeaturesFromSelector(selector); |
| 148 |
| 149 if (const CSSSelectorList* selectorList = selector->selectorList()) { |
| 150 for (const CSSSelector* subSelector = selectorList->first(); subSele
ctor; subSelector = CSSSelectorList::next(subSelector)) { |
| 151 // FIXME: Shouldn't this be checking subSelector->isSiblingSelec
tor()? |
| 152 if (!foundSiblingSelector && selector->isSiblingSelector()) |
| 153 foundSiblingSelector = true; |
| 154 if (subSelector->isDirectAdjacentSelector()) |
| 155 maxDirectAdjacentSelectors++; |
| 156 features.collectFeaturesFromSelector(subSelector); |
| 157 } |
| 158 } else { |
| 159 if (!foundSiblingSelector && selector->isSiblingSelector()) |
| 160 foundSiblingSelector = true; |
| 161 if (selector->isDirectAdjacentSelector()) |
| 162 maxDirectAdjacentSelectors++; |
| 163 } |
| 164 } |
| 165 if (!selectorUsesClassInvalidationSet) { |
| 166 for (HashSet<AtomicString>::const_iterator it = features.classesInRules.
begin(); it != features.classesInRules.end(); ++it) { |
| 167 DescendantInvalidationSet* invalidationSet = ensureClassInvalidation
Set(*it); |
| 168 invalidationSet->setWholeSubtreeInvalid(); |
| 169 } |
| 170 } |
| 171 features.setMaxDirectAdjacentSelectors(maxDirectAdjacentSelectors); |
| 172 if (foundSiblingSelector) |
| 173 features.siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selec
torIndex(), ruleData.hasDocumentSecurityOrigin())); |
| 174 if (ruleData.containsUncommonAttributeSelector()) |
| 175 features.uncommonAttributeRules.append(RuleFeature(ruleData.rule(), rule
Data.selectorIndex(), ruleData.hasDocumentSecurityOrigin())); |
| 176 } |
| 177 |
| 178 DescendantInvalidationSet* RuleSetAnalyzer::ensureClassInvalidationSet(const Ato
micString& className) |
| 179 { |
| 180 InvalidationSetMap::AddResult addResult = classInvalidationSets.add(classNam
e, 0); |
| 181 if (addResult.isNewEntry) |
| 182 addResult.iterator->value = DescendantInvalidationSet::create(); |
| 183 return addResult.iterator->value.get(); |
| 184 } |
| 185 |
| 186 void RuleSetAnalyzer::combine(const RuleSetAnalyzer& other) |
| 187 { |
| 188 for (InvalidationSetMap::const_iterator it = other.classInvalidationSets.beg
in(); it != other.classInvalidationSets.end(); ++it) { |
| 189 ensureClassInvalidationSet(it->key)->combine(*it->value); |
| 190 } |
| 191 } |
| 192 |
| 193 } // namespace WebCore |
OLD | NEW |