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 ScopedStyleResolver* ScopedStyleTree::ensureScopedStyleResolver(ContainerNode& scopingNode)
40 ScopedStyleResolver* scopedStyleResolver = addScopedStyleResolver(scopingNode, isNewEntry);
42 setupScopedStylesTree(scopedStyleResolver);
43 return scopedStyleResolver;
46 ScopedStyleResolver* ScopedStyleTree::scopedStyleResolverFor(const ContainerNode& scopingNode)
48 if (!scopingNode.hasScopedHTMLStyleChild()
49 && !isShadowHost(&scopingNode)
50 && !scopingNode.isDocumentNode()
51 && !scopingNode.isShadowRoot())
53 return lookupScopedStyleResolverFor(&scopingNode);
56 ScopedStyleResolver* ScopedStyleTree::addScopedStyleResolver(ContainerNode& scopingNode, bool& isNewEntry)
58 HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::AddResult addResult = m_authorStyles.add(&scopingNode, nullptr);
60 if (addResult.isNewEntry) {
61 addResult.iterator->value = ScopedStyleResolver::create(scopingNode);
62 if (scopingNode.isDocumentNode())
63 m_scopedResolverForDocument = addResult.iterator->value.get();
65 isNewEntry = addResult.isNewEntry;
66 return addResult.iterator->value.get();
69 void ScopedStyleTree::setupScopedStylesTree(ScopedStyleResolver* target)
73 const ContainerNode& scopingNode = target->scopingNode();
75 // Since StyleResolver creates RuleSets according to styles' document
76 // order, a parent of the given ScopedRuleData has been already
78 for (ContainerNode* node = scopingNode.parentOrShadowHostNode(); node; node = node->parentOrShadowHostNode()) {
79 if (ScopedStyleResolver* scopedResolver = scopedStyleResolverFor(*node)) {
80 target->setParent(scopedResolver);
83 if (node->isDocumentNode()) {
85 ScopedStyleResolver* scopedResolver = addScopedStyleResolver(*node, dummy);
86 target->setParent(scopedResolver);
87 setupScopedStylesTree(scopedResolver);
92 if (m_buildInDocumentOrder)
95 // Reparent all nodes whose scoping node is contained by target's one.
96 for (HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::iterator it = m_authorStyles.begin(); it != m_authorStyles.end(); ++it) {
97 if (it->value == target)
99 ASSERT(it->key->inDocument());
100 if (it->value->parent() == target->parent() && scopingNode.containsIncludingShadowDOM(it->key))
101 it->value->setParent(target);
105 void ScopedStyleTree::clear()
107 m_authorStyles.clear();
108 m_scopedResolverForDocument = 0;
112 void ScopedStyleTree::resolveScopedStyles(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
114 for (ScopedStyleResolver* scopedResolver = scopedResolverFor(element); scopedResolver; scopedResolver = scopedResolver->parent())
115 resolvers.append(scopedResolver);
118 void ScopedStyleTree::collectScopedResolversForHostedShadowTrees(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
120 ElementShadow* shadow = element->shadow();
124 // Adding scoped resolver for active shadow roots for shadow host styling.
125 for (ShadowRoot* shadowRoot = shadow->youngestShadowRoot(); shadowRoot; shadowRoot = shadowRoot->olderShadowRoot()) {
126 if (shadowRoot->hasScopedHTMLStyleChild()) {
127 if (ScopedStyleResolver* resolver = scopedStyleResolverFor(*shadowRoot))
128 resolvers.append(resolver);
130 if (!shadowRoot->containsShadowElements())
135 void ScopedStyleTree::resolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
137 Document& document = element->document();
138 TreeScope& treeScope = element->treeScope();
139 bool applyAuthorStyles = treeScope.applyAuthorStyles();
141 // Add resolvers for shadow roots hosted by the given element.
142 collectScopedResolversForHostedShadowTrees(element, resolvers);
144 // Add resolvers while walking up DOM tree from the given element.
145 for (ScopedStyleResolver* scopedResolver = scopedResolverFor(element); scopedResolver; scopedResolver = scopedResolver->parent()) {
146 if (scopedResolver->treeScope() == treeScope || (applyAuthorStyles && scopedResolver->treeScope() == document))
147 resolvers.append(scopedResolver);
151 inline ScopedStyleResolver* ScopedStyleTree::enclosingScopedStyleResolverFor(const ContainerNode* scopingNode)
153 for (; scopingNode; scopingNode = scopingNode->parentOrShadowHostNode()) {
154 if (ScopedStyleResolver* scopedStyleResolver = scopedStyleResolverFor(*scopingNode))
155 return scopedStyleResolver;
160 void ScopedStyleTree::resolveStyleCache(const ContainerNode* scopingNode)
162 m_cache.scopedResolver = enclosingScopedStyleResolverFor(scopingNode);
163 m_cache.nodeForScopedStyles = scopingNode;
166 void ScopedStyleTree::pushStyleCache(const ContainerNode& scopingNode, const ContainerNode* parent)
168 if (m_authorStyles.isEmpty())
171 if (!cacheIsValid(parent)) {
172 resolveStyleCache(&scopingNode);
176 ScopedStyleResolver* scopedResolver = scopedStyleResolverFor(scopingNode);
178 m_cache.scopedResolver = scopedResolver;
179 m_cache.nodeForScopedStyles = &scopingNode;
182 void ScopedStyleTree::popStyleCache(const ContainerNode& scopingNode)
184 if (!cacheIsValid(&scopingNode))
187 if (m_cache.scopedResolver && m_cache.scopedResolver->scopingNode() == scopingNode)
188 m_cache.scopedResolver = m_cache.scopedResolver->parent();
189 m_cache.nodeForScopedStyles = scopingNode.parentOrShadowHostNode();
192 void ScopedStyleTree::collectFeaturesTo(RuleFeatureSet& features)
194 for (HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::iterator it = m_authorStyles.begin(); it != m_authorStyles.end(); ++it)
195 it->value->collectFeaturesTo(features);
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