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) 2013 Google Inc. All rights reserved.
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.
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.
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.
30 #include "core/css/TreeBoundaryCrossingRules.h"
32 #include "core/css/ElementRuleCollector.h"
33 #include "core/css/RuleFeature.h"
34 #include "core/dom/StyleEngine.h"
35 #include "core/dom/shadow/ShadowRoot.h"
39 static void addRules(RuleSet* ruleSet, const WillBeHeapVector<MinimalRuleData>& rules)
41 for (unsigned i = 0; i < rules.size(); ++i) {
42 const MinimalRuleData& info = rules[i];
43 ruleSet->addRule(info.m_rule, info.m_selectorIndex, info.m_flags);
47 void TreeBoundaryCrossingRules::addTreeBoundaryCrossingRules(const RuleSet& authorRules, CSSStyleSheet* parentStyleSheet, unsigned parentIndex, ContainerNode& scopingNode)
49 if (authorRules.treeBoundaryCrossingRules().isEmpty() && (scopingNode.isDocumentNode() || authorRules.shadowDistributedRules().isEmpty()))
51 OwnPtrWillBeRawPtr<RuleSet> ruleSetForScope = RuleSet::create();
52 addRules(ruleSetForScope.get(), authorRules.treeBoundaryCrossingRules());
53 if (!scopingNode.isDocumentNode())
54 addRules(ruleSetForScope.get(), authorRules.shadowDistributedRules());
56 if (!m_treeBoundaryCrossingRuleSetMap.contains(&scopingNode)) {
57 m_treeBoundaryCrossingRuleSetMap.add(&scopingNode, adoptPtrWillBeNoop(new CSSStyleSheetRuleSubSet()));
58 m_scopingNodes.add(&scopingNode);
60 CSSStyleSheetRuleSubSet* ruleSubSet = m_treeBoundaryCrossingRuleSetMap.get(&scopingNode);
61 ruleSubSet->append(RuleSubSet::create(parentStyleSheet, parentIndex, ruleSetForScope.release()));
64 void TreeBoundaryCrossingRules::collectTreeBoundaryCrossingRules(Element* element, ElementRuleCollector& collector, bool includeEmptyRules)
66 if (m_treeBoundaryCrossingRuleSetMap.isEmpty())
69 RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
71 // When comparing rules declared in outer treescopes, outer's rules win.
72 CascadeOrder outerCascadeOrder = size() + size();
73 // When comparing rules declared in inner treescopes, inner's rules win.
74 CascadeOrder innerCascadeOrder = size();
76 for (DocumentOrderedList::iterator it = m_scopingNodes.begin(); it != m_scopingNodes.end(); ++it) {
77 const ContainerNode* scopingNode = toContainerNode(*it);
78 CSSStyleSheetRuleSubSet* ruleSubSet = m_treeBoundaryCrossingRuleSetMap.get(scopingNode);
79 bool isInnerTreeScope = element->treeScope().isInclusiveAncestorOf(scopingNode->treeScope());
81 CascadeOrder cascadeOrder = isInnerTreeScope ? innerCascadeOrder : outerCascadeOrder;
82 for (CSSStyleSheetRuleSubSet::iterator it = ruleSubSet->begin(); it != ruleSubSet->end(); ++it) {
83 MatchRequest request((*it)->ruleSet.get(), includeEmptyRules, scopingNode, (*it)->parentStyleSheet, (*it)->parentIndex);
84 collector.collectMatchingRules(request, ruleRange, SelectorChecker::ScopeContainsLastMatchedElement, ignoreCascadeScope, cascadeOrder, true);
91 void TreeBoundaryCrossingRules::reset(const ContainerNode* scopingNode)
93 m_treeBoundaryCrossingRuleSetMap.remove(scopingNode);
94 m_scopingNodes.remove(scopingNode);
97 void TreeBoundaryCrossingRules::collectFeaturesFromRuleSubSet(CSSStyleSheetRuleSubSet* ruleSubSet, RuleFeatureSet& features)
99 for (CSSStyleSheetRuleSubSet::iterator it = ruleSubSet->begin(); it != ruleSubSet->end(); ++it)
100 features.add((*it)->ruleSet->features());
103 void TreeBoundaryCrossingRules::collectFeaturesTo(RuleFeatureSet& features)
105 for (TreeBoundaryCrossingRuleSetMap::iterator::Values it = m_treeBoundaryCrossingRuleSetMap.values().begin(); it != m_treeBoundaryCrossingRuleSetMap.values().end(); ++it)
106 collectFeaturesFromRuleSubSet(it->get(), features);
109 void TreeBoundaryCrossingRules::trace(Visitor* visitor)
112 visitor->trace(m_treeBoundaryCrossingRuleSetMap);
113 visitor->trace(m_scopingNodes);
117 void TreeBoundaryCrossingRules::RuleSubSet::trace(Visitor* visitor)
119 visitor->trace(ruleSet);