morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com) |
| 4 | * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com) |
| 5 | * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. |
| 6 | * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org> |
| 7 | * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org> |
| 8 | * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/) |
| 9 | * Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 10 | * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
| 11 | * Copyright (C) 2012 Google Inc. All rights reserved. |
| 12 | * |
| 13 | * This library is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU Library General Public |
| 15 | * License as published by the Free Software Foundation; either |
| 16 | * version 2 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * This library is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * Library General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU Library General Public License |
| 24 | * along with this library; see the file COPYING.LIB. If not, write to |
| 25 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 26 | * Boston, MA 02110-1301, USA. |
| 27 | */ |
| 28 | |
| 29 | #include "config.h" |
| 30 | #include "RuleSet.h" |
| 31 | |
| 32 | #include "CSSFontSelector.h" |
| 33 | #include "CSSSelector.h" |
| 34 | #include "CSSSelectorList.h" |
| 35 | #include "HTMLNames.h" |
| 36 | #include "MediaQueryEvaluator.h" |
| 37 | #include "SecurityOrigin.h" |
| 38 | #include "SelectorChecker.h" |
dglazkov@chromium.org | 4e8caa3 | 2012-12-24 05:05:13 +0000 | [diff] [blame] | 39 | #include "SelectorFilter.h" |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 40 | #include "StyleResolver.h" |
| 41 | #include "StyleRule.h" |
| 42 | #include "StyleRuleImport.h" |
| 43 | #include "StyleSheetContents.h" |
| 44 | #include "WebCoreMemoryInstrumentation.h" |
| 45 | #include "WebKitCSSKeyframesRule.h" |
| 46 | #include <wtf/MemoryInstrumentationHashMap.h> |
| 47 | #include <wtf/MemoryInstrumentationHashSet.h> |
| 48 | #include <wtf/MemoryInstrumentationVector.h> |
| 49 | |
commit-queue@webkit.org | 0bcebbe | 2013-01-23 05:50:15 +0000 | [diff] [blame^] | 50 | #if ENABLE(VIDEO_TRACK) |
| 51 | #include "TextTrackCue.h" |
| 52 | #endif |
| 53 | |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 54 | namespace WebCore { |
| 55 | |
| 56 | using namespace HTMLNames; |
| 57 | |
| 58 | // ----------------------------------------------------------------- |
| 59 | |
| 60 | static inline bool isSelectorMatchingHTMLBasedOnRuleHash(const CSSSelector* selector) |
| 61 | { |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 62 | ASSERT(selector); |
| 63 | if (selector->m_match == CSSSelector::Tag) { |
| 64 | const AtomicString& selectorNamespace = selector->tagQName().namespaceURI(); |
| 65 | if (selectorNamespace != starAtom && selectorNamespace != xhtmlNamespaceURI) |
| 66 | return false; |
| 67 | if (selector->relation() == CSSSelector::SubSelector) |
| 68 | return isSelectorMatchingHTMLBasedOnRuleHash(selector->tagHistory()); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 69 | return true; |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 70 | } |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 71 | if (SelectorChecker::isCommonPseudoClassSelector(selector)) |
| 72 | return true; |
| 73 | return selector->m_match == CSSSelector::Id || selector->m_match == CSSSelector::Class; |
| 74 | } |
| 75 | |
| 76 | static inline bool selectorListContainsUncommonAttributeSelector(const CSSSelector* selector) |
| 77 | { |
| 78 | CSSSelectorList* selectorList = selector->selectorList(); |
| 79 | if (!selectorList) |
| 80 | return false; |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 81 | for (CSSSelector* selector = selectorList->first(); selector; selector = CSSSelectorList::next(selector)) { |
| 82 | for (CSSSelector* component = selector; component; component = component->tagHistory()) { |
| 83 | if (component->isAttributeSelector()) |
| 84 | return true; |
| 85 | } |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | static inline bool isCommonAttributeSelectorAttribute(const QualifiedName& attribute) |
| 91 | { |
| 92 | // These are explicitly tested for equality in canShareStyleWithElement. |
| 93 | return attribute == typeAttr || attribute == readonlyAttr; |
| 94 | } |
| 95 | |
| 96 | static inline bool containsUncommonAttributeSelector(const CSSSelector* selector) |
| 97 | { |
| 98 | for (; selector; selector = selector->tagHistory()) { |
| 99 | // Allow certain common attributes (used in the default style) in the selectors that match the current element. |
| 100 | if (selector->isAttributeSelector() && !isCommonAttributeSelectorAttribute(selector->attribute())) |
| 101 | return true; |
| 102 | if (selectorListContainsUncommonAttributeSelector(selector)) |
| 103 | return true; |
| 104 | if (selector->relation() != CSSSelector::SubSelector) { |
| 105 | selector = selector->tagHistory(); |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | for (; selector; selector = selector->tagHistory()) { |
| 111 | if (selector->isAttributeSelector()) |
| 112 | return true; |
| 113 | if (selectorListContainsUncommonAttributeSelector(selector)) |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
commit-queue@webkit.org | 085528c | 2013-01-18 18:22:46 +0000 | [diff] [blame] | 119 | static inline PropertyWhitelistType determinePropertyWhitelistType(const AddRuleFlags addRuleFlags, const CSSSelector* selector) |
| 120 | { |
| 121 | if (addRuleFlags & RuleIsInRegionRule) |
| 122 | return PropertyWhitelistRegion; |
| 123 | #if ENABLE(VIDEO_TRACK) |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 124 | for (const CSSSelector* component = selector; component; component = component->tagHistory()) { |
commit-queue@webkit.org | 0bcebbe | 2013-01-23 05:50:15 +0000 | [diff] [blame^] | 125 | if (component->pseudoType() == CSSSelector::PseudoCue || (component->m_match != CSSSelector::Tag && component->value() == TextTrackCue::cueShadowPseudoId())) |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 126 | return PropertyWhitelistCue; |
| 127 | } |
commit-queue@webkit.org | 0bcebbe | 2013-01-23 05:50:15 +0000 | [diff] [blame^] | 128 | #else |
| 129 | UNUSED_PARAM(selector); |
commit-queue@webkit.org | 085528c | 2013-01-18 18:22:46 +0000 | [diff] [blame] | 130 | #endif |
| 131 | return PropertyWhitelistNone; |
| 132 | } |
| 133 | |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 134 | RuleData::RuleData(StyleRule* rule, unsigned selectorIndex, unsigned position, AddRuleFlags addRuleFlags) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 135 | : m_rule(rule) |
| 136 | , m_selectorIndex(selectorIndex) |
| 137 | , m_position(position) |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 138 | , m_hasFastCheckableSelector((addRuleFlags & RuleCanUseFastCheckSelector) && SelectorChecker::isFastCheckableSelector(selector())) |
commit-queue@webkit.org | 085528c | 2013-01-18 18:22:46 +0000 | [diff] [blame] | 139 | , m_specificity(selector()->specificity()) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 140 | , m_hasMultipartSelector(!!selector()->tagHistory()) |
| 141 | , m_hasRightmostSelectorMatchingHTMLBasedOnRuleHash(isSelectorMatchingHTMLBasedOnRuleHash(selector())) |
| 142 | , m_containsUncommonAttributeSelector(WebCore::containsUncommonAttributeSelector(selector())) |
| 143 | , m_linkMatchType(SelectorChecker::determineLinkMatchType(selector())) |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 144 | , m_hasDocumentSecurityOrigin(addRuleFlags & RuleHasDocumentSecurityOrigin) |
commit-queue@webkit.org | 085528c | 2013-01-18 18:22:46 +0000 | [diff] [blame] | 145 | , m_propertyWhitelistType(determinePropertyWhitelistType(addRuleFlags, selector())) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 146 | { |
| 147 | ASSERT(m_position == position); |
| 148 | ASSERT(m_selectorIndex == selectorIndex); |
dglazkov@chromium.org | 4e8caa3 | 2012-12-24 05:05:13 +0000 | [diff] [blame] | 149 | SelectorFilter::collectIdentifierHashes(selector(), m_descendantSelectorIdentifierHashes, maximumIdentifierCount); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void RuleData::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const |
| 153 | { |
| 154 | MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS); |
loislo@chromium.org | 771ab1f | 2013-01-14 08:21:49 +0000 | [diff] [blame] | 155 | info.addMember(m_rule); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void reportAtomRuleMap(MemoryClassInfo* info, const RuleSet::AtomRuleMap& atomicRuleMap) |
| 159 | { |
| 160 | info->addMember(atomicRuleMap); |
| 161 | for (RuleSet::AtomRuleMap::const_iterator it = atomicRuleMap.begin(); it != atomicRuleMap.end(); ++it) |
benjamin@webkit.org | ee55405 | 2012-10-07 23:12:07 +0000 | [diff] [blame] | 162 | info->addMember(*it->value); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void RuleSet::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const |
| 166 | { |
| 167 | MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS); |
| 168 | reportAtomRuleMap(&info, m_idRules); |
| 169 | reportAtomRuleMap(&info, m_classRules); |
| 170 | reportAtomRuleMap(&info, m_tagRules); |
| 171 | reportAtomRuleMap(&info, m_shadowPseudoElementRules); |
| 172 | info.addMember(m_linkPseudoClassRules); |
commit-queue@webkit.org | 7db073d | 2012-12-18 00:01:39 +0000 | [diff] [blame] | 173 | #if ENABLE(VIDEO_TRACK) |
| 174 | info.addMember(m_cuePseudoRules); |
| 175 | #endif |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 176 | info.addMember(m_focusPseudoClassRules); |
| 177 | info.addMember(m_universalRules); |
| 178 | info.addMember(m_pageRules); |
| 179 | info.addMember(m_regionSelectorsAndRuleSets); |
loislo@chromium.org | 771ab1f | 2013-01-14 08:21:49 +0000 | [diff] [blame] | 180 | info.addMember(m_features); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void RuleSet::RuleSetSelectorPair::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const |
| 184 | { |
| 185 | MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CSS); |
| 186 | info.addMember(ruleSet); |
loislo@chromium.org | 771ab1f | 2013-01-14 08:21:49 +0000 | [diff] [blame] | 187 | info.addMember(selector); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 188 | } |
| 189 | |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 190 | static void collectFeaturesFromRuleData(RuleFeatureSet& features, const RuleData& ruleData) |
| 191 | { |
| 192 | bool foundSiblingSelector = false; |
| 193 | for (CSSSelector* selector = ruleData.selector(); selector; selector = selector->tagHistory()) { |
shinyak@chromium.org | ade81c3 | 2012-11-09 06:17:31 +0000 | [diff] [blame] | 194 | features.collectFeaturesFromSelector(selector); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 195 | |
| 196 | if (CSSSelectorList* selectorList = selector->selectorList()) { |
| 197 | for (CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) { |
| 198 | if (!foundSiblingSelector && selector->isSiblingSelector()) |
| 199 | foundSiblingSelector = true; |
shinyak@chromium.org | ade81c3 | 2012-11-09 06:17:31 +0000 | [diff] [blame] | 200 | features.collectFeaturesFromSelector(subSelector); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 201 | } |
| 202 | } else if (!foundSiblingSelector && selector->isSiblingSelector()) |
| 203 | foundSiblingSelector = true; |
| 204 | } |
| 205 | if (foundSiblingSelector) |
| 206 | features.siblingRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.hasDocumentSecurityOrigin())); |
| 207 | if (ruleData.containsUncommonAttributeSelector()) |
| 208 | features.uncommonAttributeRules.append(RuleFeature(ruleData.rule(), ruleData.selectorIndex(), ruleData.hasDocumentSecurityOrigin())); |
| 209 | } |
| 210 | |
| 211 | void RuleSet::addToRuleSet(AtomicStringImpl* key, AtomRuleMap& map, const RuleData& ruleData) |
| 212 | { |
| 213 | if (!key) |
| 214 | return; |
benjamin@webkit.org | ee55405 | 2012-10-07 23:12:07 +0000 | [diff] [blame] | 215 | OwnPtr<Vector<RuleData> >& rules = map.add(key, nullptr).iterator->value; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 216 | if (!rules) |
| 217 | rules = adoptPtr(new Vector<RuleData>); |
| 218 | rules->append(ruleData); |
| 219 | } |
| 220 | |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 221 | bool RuleSet::findBestRuleSetAndAdd(const CSSSelector* component, RuleData& ruleData) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 222 | { |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 223 | if (component->m_match == CSSSelector::Id) { |
| 224 | addToRuleSet(component->value().impl(), m_idRules, ruleData); |
| 225 | return true; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 226 | } |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 227 | if (component->m_match == CSSSelector::Class) { |
| 228 | addToRuleSet(component->value().impl(), m_classRules, ruleData); |
| 229 | return true; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 230 | } |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 231 | if (component->isCustomPseudoElement()) { |
| 232 | addToRuleSet(component->value().impl(), m_shadowPseudoElementRules, ruleData); |
| 233 | return true; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 234 | } |
commit-queue@webkit.org | 7db073d | 2012-12-18 00:01:39 +0000 | [diff] [blame] | 235 | #if ENABLE(VIDEO_TRACK) |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 236 | if (component->pseudoType() == CSSSelector::PseudoCue) { |
commit-queue@webkit.org | 7db073d | 2012-12-18 00:01:39 +0000 | [diff] [blame] | 237 | m_cuePseudoRules.append(ruleData); |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 238 | return true; |
commit-queue@webkit.org | 7db073d | 2012-12-18 00:01:39 +0000 | [diff] [blame] | 239 | } |
| 240 | #endif |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 241 | if (SelectorChecker::isCommonPseudoClassSelector(component)) { |
| 242 | switch (component->pseudoType()) { |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 243 | case CSSSelector::PseudoLink: |
| 244 | case CSSSelector::PseudoVisited: |
| 245 | case CSSSelector::PseudoAnyLink: |
| 246 | m_linkPseudoClassRules.append(ruleData); |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 247 | return true; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 248 | case CSSSelector::PseudoFocus: |
| 249 | m_focusPseudoClassRules.append(ruleData); |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 250 | return true; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 251 | default: |
| 252 | ASSERT_NOT_REACHED(); |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 253 | return true; |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 254 | } |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 255 | } |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 256 | |
| 257 | if (component->m_match == CSSSelector::Tag) { |
| 258 | if (component->tagQName().localName() != starAtom) { |
| 259 | // If this is part of a subselector chain, recurse ahead to find a narrower set (ID/class.) |
| 260 | if (component->relation() == CSSSelector::SubSelector |
| 261 | && (component->tagHistory()->m_match == CSSSelector::Class || component->tagHistory()->m_match == CSSSelector::Id || SelectorChecker::isCommonPseudoClassSelector(component->tagHistory())) |
| 262 | && findBestRuleSetAndAdd(component->tagHistory(), ruleData)) |
| 263 | return true; |
| 264 | |
| 265 | addToRuleSet(component->tagQName().localName().impl(), m_tagRules, ruleData); |
| 266 | return true; |
| 267 | } |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 268 | } |
akling@apple.com | 48d5604 | 2013-01-22 00:45:14 +0000 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | void RuleSet::addRule(StyleRule* rule, unsigned selectorIndex, AddRuleFlags addRuleFlags) |
| 273 | { |
| 274 | RuleData ruleData(rule, selectorIndex, m_ruleCount++, addRuleFlags); |
| 275 | collectFeaturesFromRuleData(m_features, ruleData); |
| 276 | |
| 277 | if (!findBestRuleSetAndAdd(ruleData.selector(), ruleData)) { |
| 278 | // If we didn't find a specialized map to stick it in, file under universal rules. |
| 279 | m_universalRules.append(ruleData); |
| 280 | } |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void RuleSet::addPageRule(StyleRulePage* rule) |
| 284 | { |
| 285 | m_pageRules.append(rule); |
| 286 | } |
| 287 | |
| 288 | void RuleSet::addRegionRule(StyleRuleRegion* regionRule, bool hasDocumentSecurityOrigin) |
| 289 | { |
| 290 | OwnPtr<RuleSet> regionRuleSet = RuleSet::create(); |
| 291 | // The region rule set should take into account the position inside the parent rule set. |
| 292 | // Otherwise, the rules inside region block might be incorrectly positioned before other similar rules from |
| 293 | // the stylesheet that contains the region block. |
| 294 | regionRuleSet->m_ruleCount = m_ruleCount; |
| 295 | |
| 296 | // Collect the region rules into a rule set |
commit-queue@webkit.org | 104b5ab | 2013-01-14 09:49:56 +0000 | [diff] [blame] | 297 | // FIXME: Should this add other types of rules? (i.e. use addChildRules() directly?) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 298 | const Vector<RefPtr<StyleRuleBase> >& childRules = regionRule->childRules(); |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 299 | AddRuleFlags addRuleFlags = hasDocumentSecurityOrigin ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState; |
| 300 | addRuleFlags = static_cast<AddRuleFlags>(addRuleFlags | RuleCanUseFastCheckSelector | RuleIsInRegionRule); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 301 | for (unsigned i = 0; i < childRules.size(); ++i) { |
| 302 | StyleRuleBase* regionStylingRule = childRules[i].get(); |
| 303 | if (regionStylingRule->isStyleRule()) |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 304 | regionRuleSet->addStyleRule(static_cast<StyleRule*>(regionStylingRule), addRuleFlags); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 305 | } |
| 306 | // Update the "global" rule count so that proper order is maintained |
| 307 | m_ruleCount = regionRuleSet->m_ruleCount; |
| 308 | |
| 309 | m_regionSelectorsAndRuleSets.append(RuleSetSelectorPair(regionRule->selectorList().first(), regionRuleSet.release())); |
| 310 | } |
| 311 | |
commit-queue@webkit.org | 104b5ab | 2013-01-14 09:49:56 +0000 | [diff] [blame] | 312 | void RuleSet::addChildRules(const Vector<RefPtr<StyleRuleBase> >& rules, const MediaQueryEvaluator& medium, StyleResolver* resolver, const ContainerNode* scope, bool hasDocumentSecurityOrigin, AddRuleFlags addRuleFlags) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 313 | { |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 314 | for (unsigned i = 0; i < rules.size(); ++i) { |
| 315 | StyleRuleBase* rule = rules[i].get(); |
| 316 | |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 317 | if (rule->isStyleRule()) |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 318 | addStyleRule(static_cast<StyleRule*>(rule), addRuleFlags); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 319 | else if (rule->isPageRule()) |
| 320 | addPageRule(static_cast<StyleRulePage*>(rule)); |
| 321 | else if (rule->isMediaRule()) { |
| 322 | StyleRuleMedia* mediaRule = static_cast<StyleRuleMedia*>(rule); |
commit-queue@webkit.org | 104b5ab | 2013-01-14 09:49:56 +0000 | [diff] [blame] | 323 | if ((!mediaRule->mediaQueries() || medium.eval(mediaRule->mediaQueries(), resolver))) |
| 324 | addChildRules(mediaRule->childRules(), medium, resolver, scope, hasDocumentSecurityOrigin, addRuleFlags); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 325 | } else if (rule->isFontFaceRule() && resolver) { |
| 326 | // Add this font face to our set. |
| 327 | // FIXME(BUG 72461): We don't add @font-face rules of scoped style sheets for the moment. |
| 328 | if (scope) |
| 329 | continue; |
| 330 | const StyleRuleFontFace* fontFaceRule = static_cast<StyleRuleFontFace*>(rule); |
| 331 | resolver->fontSelector()->addFontFaceRule(fontFaceRule); |
| 332 | resolver->invalidateMatchedPropertiesCache(); |
| 333 | } else if (rule->isKeyframesRule() && resolver) { |
| 334 | // FIXME (BUG 72462): We don't add @keyframe rules of scoped style sheets for the moment. |
| 335 | if (scope) |
| 336 | continue; |
| 337 | resolver->addKeyframeStyle(static_cast<StyleRuleKeyframes*>(rule)); |
| 338 | } |
| 339 | #if ENABLE(CSS_REGIONS) |
| 340 | else if (rule->isRegionRule() && resolver) { |
| 341 | // FIXME (BUG 72472): We don't add @-webkit-region rules of scoped style sheets for the moment. |
| 342 | if (scope) |
| 343 | continue; |
| 344 | addRegionRule(static_cast<StyleRuleRegion*>(rule), hasDocumentSecurityOrigin); |
| 345 | } |
| 346 | #endif |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 347 | #if ENABLE(SHADOW_DOM) |
| 348 | else if (rule->isHostRule()) |
| 349 | resolver->addHostRule(static_cast<StyleRuleHost*>(rule), hasDocumentSecurityOrigin, scope); |
| 350 | #endif |
commit-queue@webkit.org | daa81a4 | 2012-11-19 16:37:01 +0000 | [diff] [blame] | 351 | #if ENABLE(CSS_DEVICE_ADAPTATION) |
| 352 | else if (rule->isViewportRule() && resolver) { |
| 353 | // @viewport should not be scoped. |
| 354 | if (scope) |
| 355 | continue; |
| 356 | resolver->viewportStyleResolver()->addViewportRule(static_cast<StyleRuleViewport*>(rule)); |
| 357 | } |
| 358 | #endif |
commit-queue@webkit.org | 03dd24f | 2013-01-16 11:30:50 +0000 | [diff] [blame] | 359 | #if ENABLE(CSS3_CONDITIONAL_RULES) |
| 360 | else if (rule->isSupportsRule() && static_cast<StyleRuleSupports*>(rule)->conditionIsSupported()) |
| 361 | addChildRules(static_cast<StyleRuleSupports*>(rule)->childRules(), medium, resolver, scope, hasDocumentSecurityOrigin, addRuleFlags); |
| 362 | #endif |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 363 | } |
commit-queue@webkit.org | 104b5ab | 2013-01-14 09:49:56 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void RuleSet::addRulesFromSheet(StyleSheetContents* sheet, const MediaQueryEvaluator& medium, StyleResolver* resolver, const ContainerNode* scope) |
| 367 | { |
| 368 | ASSERT(sheet); |
| 369 | |
| 370 | const Vector<RefPtr<StyleRuleImport> >& importRules = sheet->importRules(); |
| 371 | for (unsigned i = 0; i < importRules.size(); ++i) { |
| 372 | StyleRuleImport* importRule = importRules[i].get(); |
| 373 | if (importRule->styleSheet() && (!importRule->mediaQueries() || medium.eval(importRule->mediaQueries(), resolver))) |
| 374 | addRulesFromSheet(importRule->styleSheet(), medium, resolver, scope); |
| 375 | } |
| 376 | |
| 377 | bool hasDocumentSecurityOrigin = resolver && resolver->document()->securityOrigin()->canRequest(sheet->baseURL()); |
| 378 | AddRuleFlags addRuleFlags = static_cast<AddRuleFlags>((hasDocumentSecurityOrigin ? RuleHasDocumentSecurityOrigin : 0) | (!scope ? RuleCanUseFastCheckSelector : 0)); |
| 379 | |
| 380 | addChildRules(sheet->childRules(), medium, resolver, scope, hasDocumentSecurityOrigin, addRuleFlags); |
| 381 | |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 382 | if (m_autoShrinkToFitEnabled) |
| 383 | shrinkToFit(); |
| 384 | } |
| 385 | |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 386 | void RuleSet::addStyleRule(StyleRule* rule, AddRuleFlags addRuleFlags) |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 387 | { |
| 388 | for (size_t selectorIndex = 0; selectorIndex != notFound; selectorIndex = rule->selectorList().indexOfNextSelectorAfter(selectorIndex)) |
tasak@google.com | 4a7bee5 | 2012-10-26 11:33:22 +0000 | [diff] [blame] | 389 | addRule(rule, selectorIndex, addRuleFlags); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static inline void shrinkMapVectorsToFit(RuleSet::AtomRuleMap& map) |
| 393 | { |
| 394 | RuleSet::AtomRuleMap::iterator end = map.end(); |
| 395 | for (RuleSet::AtomRuleMap::iterator it = map.begin(); it != end; ++it) |
benjamin@webkit.org | ee55405 | 2012-10-07 23:12:07 +0000 | [diff] [blame] | 396 | it->value->shrinkToFit(); |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void RuleSet::shrinkToFit() |
| 400 | { |
| 401 | shrinkMapVectorsToFit(m_idRules); |
| 402 | shrinkMapVectorsToFit(m_classRules); |
| 403 | shrinkMapVectorsToFit(m_tagRules); |
| 404 | shrinkMapVectorsToFit(m_shadowPseudoElementRules); |
| 405 | m_linkPseudoClassRules.shrinkToFit(); |
commit-queue@webkit.org | 7db073d | 2012-12-18 00:01:39 +0000 | [diff] [blame] | 406 | #if ENABLE(VIDEO_TRACK) |
| 407 | m_cuePseudoRules.shrinkToFit(); |
| 408 | #endif |
morrita@google.com | 3b321b5 | 2012-10-05 06:15:20 +0000 | [diff] [blame] | 409 | m_focusPseudoClassRules.shrinkToFit(); |
| 410 | m_universalRules.shrinkToFit(); |
| 411 | m_pageRules.shrinkToFit(); |
| 412 | } |
| 413 | |
| 414 | } // namespace WebCore |