2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Copyright (C) 2012 Google Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/css/resolver/ScopedStyleTree.h"
30 #include "core/css/RuleFeature.h"
31 #include "core/dom/Document.h"
32 #include "core/dom/shadow/ElementShadow.h"
33 #include "core/dom/shadow/ShadowRoot.h"
37 class StyleSheetContents;
39 ScopedStyleResolver* ScopedStyleTree::ensureScopedStyleResolver(ContainerNode& scopingNode)
42 ScopedStyleResolver* scopedStyleResolver = addScopedStyleResolver(scopingNode, isNewEntry);
44 setupScopedStylesTree(scopedStyleResolver);
45 return scopedStyleResolver;
48 ScopedStyleResolver* ScopedStyleTree::scopedStyleResolverFor(const ContainerNode& scopingNode)
50 if (!isShadowHost(&scopingNode)
51 && !scopingNode.isDocumentNode()
52 && !scopingNode.isShadowRoot())
54 return lookupScopedStyleResolverFor(&scopingNode);
57 ScopedStyleResolver* ScopedStyleTree::addScopedStyleResolver(ContainerNode& scopingNode, bool& isNewEntry)
59 HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::AddResult addResult = m_authorStyles.add(&scopingNode, nullptr);
61 if (addResult.isNewEntry) {
62 addResult.storedValue->value = ScopedStyleResolver::create(scopingNode);
63 if (scopingNode.isDocumentNode())
64 m_scopedResolverForDocument = addResult.storedValue->value.get();
66 isNewEntry = addResult.isNewEntry;
67 return addResult.storedValue->value.get();
70 void ScopedStyleTree::setupScopedStylesTree(ScopedStyleResolver* target)
74 const ContainerNode& scopingNode = target->scopingNode();
76 // Since StyleResolver creates RuleSets according to styles' document
77 // order, a parent of the given ScopedRuleData has been already
79 for (ContainerNode* node = scopingNode.parentOrShadowHostNode(); node; node = node->parentOrShadowHostNode()) {
80 if (ScopedStyleResolver* scopedResolver = scopedStyleResolverFor(*node)) {
81 target->setParent(scopedResolver);
84 if (node->isDocumentNode()) {
86 ScopedStyleResolver* scopedResolver = addScopedStyleResolver(*node, dummy);
87 target->setParent(scopedResolver);
88 setupScopedStylesTree(scopedResolver);
93 if (m_buildInDocumentOrder)
96 // Reparent all nodes whose scoping node is contained by target's one.
97 for (HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::iterator it = m_authorStyles.begin(); it != m_authorStyles.end(); ++it) {
98 if (it->value == target)
100 ASSERT(it->key->inDocument());
101 if (it->value->parent() == target->parent() && scopingNode.containsIncludingShadowDOM(it->key))
102 it->value->setParent(target);
106 void ScopedStyleTree::clear()
108 m_authorStyles.clear();
109 m_scopedResolverForDocument = 0;
113 void ScopedStyleTree::resolveScopedStyles(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
115 for (ScopedStyleResolver* scopedResolver = scopedResolverFor(element); scopedResolver; scopedResolver = scopedResolver->parent())
116 resolvers.append(scopedResolver);
119 void ScopedStyleTree::collectScopedResolversForHostedShadowTrees(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
121 ElementShadow* shadow = element->shadow();
125 // Adding scoped resolver for active shadow roots for shadow host styling.
126 for (ShadowRoot* shadowRoot = shadow->youngestShadowRoot(); shadowRoot; shadowRoot = shadowRoot->olderShadowRoot()) {
127 if (shadowRoot->numberOfStyles() > 0) {
128 if (ScopedStyleResolver* resolver = scopedStyleResolverFor(*shadowRoot))
129 resolvers.append(resolver);
134 void ScopedStyleTree::resolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
136 Document& document = element->document();
137 TreeScope& treeScope = element->treeScope();
138 bool applyAuthorStyles = treeScope.applyAuthorStyles();
140 // Add resolvers for shadow roots hosted by the given element.
141 collectScopedResolversForHostedShadowTrees(element, resolvers);
143 // Add resolvers while walking up DOM tree from the given element.
144 for (ScopedStyleResolver* scopedResolver = scopedResolverFor(element); scopedResolver; scopedResolver = scopedResolver->parent()) {
145 if (scopedResolver->treeScope() == treeScope || (applyAuthorStyles && scopedResolver->treeScope() == document))
146 resolvers.append(scopedResolver);
150 inline ScopedStyleResolver* ScopedStyleTree::enclosingScopedStyleResolverFor(const ContainerNode* scopingNode)
152 for (; scopingNode; scopingNode = scopingNode->parentOrShadowHostNode()) {
153 if (ScopedStyleResolver* scopedStyleResolver = scopedStyleResolverFor(*scopingNode))
154 return scopedStyleResolver;
159 void ScopedStyleTree::resolveStyleCache(const ContainerNode* scopingNode)
161 m_cache.scopedResolver = enclosingScopedStyleResolverFor(scopingNode);
162 m_cache.nodeForScopedStyles = scopingNode;
165 void ScopedStyleTree::pushStyleCache(const ContainerNode& scopingNode, const ContainerNode* parent)
167 if (m_authorStyles.isEmpty())
170 if (!cacheIsValid(parent)) {
171 resolveStyleCache(&scopingNode);
175 ScopedStyleResolver* scopedResolver = scopedStyleResolverFor(scopingNode);
177 m_cache.scopedResolver = scopedResolver;
178 m_cache.nodeForScopedStyles = &scopingNode;
181 void ScopedStyleTree::popStyleCache(const ContainerNode& scopingNode)
183 if (!cacheIsValid(&scopingNode))
186 if (m_cache.scopedResolver && m_cache.scopedResolver->scopingNode() == scopingNode)
187 m_cache.scopedResolver = m_cache.scopedResolver->parent();
188 m_cache.nodeForScopedStyles = scopingNode.parentOrShadowHostNode();
191 void ScopedStyleTree::collectFeaturesTo(RuleFeatureSet& features)
193 HashSet<const StyleSheetContents*> visitedSharedStyleSheetContents;
194 for (HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::iterator it = m_authorStyles.begin(); it != m_authorStyles.end(); ++it)
195 it->value->collectFeaturesTo(features, visitedSharedStyleSheetContents);
198 inline void ScopedStyleTree::reparentNodes(const ScopedStyleResolver* oldParent, ScopedStyleResolver* newParent)
200 // FIXME: this takes O(N) (N = number of all scoping nodes).
201 for (HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::iterator it = m_authorStyles.begin(); it != m_authorStyles.end(); ++it) {
202 if (it->value->parent() == oldParent)
203 it->value->setParent(newParent);
207 void ScopedStyleTree::remove(const ContainerNode* scopingNode)
209 if (!scopingNode || scopingNode->isDocumentNode())
212 ScopedStyleResolver* resolverRemoved = lookupScopedStyleResolverFor(scopingNode);
213 if (!resolverRemoved)
216 reparentNodes(resolverRemoved, resolverRemoved->parent());
217 if (m_cache.scopedResolver == resolverRemoved)
220 m_authorStyles.remove(scopingNode);
223 } // namespace WebCore