Home | History | Annotate | Download | only in resolver
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) 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.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      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.
     14  *
     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.
     25  */
     26 
     27 #include "config.h"
     28 #include "core/css/resolver/ScopedStyleTree.h"
     29 
     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"
     34 
     35 namespace WebCore {
     36 
     37 class StyleSheetContents;
     38 
     39 ScopedStyleResolver* ScopedStyleTree::ensureScopedStyleResolver(ContainerNode& scopingNode)
     40 {
     41     bool isNewEntry;
     42     ScopedStyleResolver* scopedStyleResolver = addScopedStyleResolver(scopingNode, isNewEntry);
     43     if (isNewEntry)
     44         setupScopedStylesTree(scopedStyleResolver);
     45     return scopedStyleResolver;
     46 }
     47 
     48 ScopedStyleResolver* ScopedStyleTree::scopedStyleResolverFor(const ContainerNode& scopingNode)
     49 {
     50     if (!isShadowHost(&scopingNode)
     51         && !scopingNode.isDocumentNode()
     52         && !scopingNode.isShadowRoot())
     53         return 0;
     54     return lookupScopedStyleResolverFor(&scopingNode);
     55 }
     56 
     57 ScopedStyleResolver* ScopedStyleTree::addScopedStyleResolver(ContainerNode& scopingNode, bool& isNewEntry)
     58 {
     59     HashMap<const ContainerNode*, OwnPtr<ScopedStyleResolver> >::AddResult addResult = m_authorStyles.add(&scopingNode, nullptr);
     60 
     61     if (addResult.isNewEntry) {
     62         addResult.storedValue->value = ScopedStyleResolver::create(scopingNode);
     63         if (scopingNode.isDocumentNode())
     64             m_scopedResolverForDocument = addResult.storedValue->value.get();
     65     }
     66     isNewEntry = addResult.isNewEntry;
     67     return addResult.storedValue->value.get();
     68 }
     69 
     70 void ScopedStyleTree::setupScopedStylesTree(ScopedStyleResolver* target)
     71 {
     72     ASSERT(target);
     73 
     74     const ContainerNode& scopingNode = target->scopingNode();
     75 
     76     // Since StyleResolver creates RuleSets according to styles' document
     77     // order, a parent of the given ScopedRuleData has been already
     78     // prepared.
     79     for (ContainerNode* node = scopingNode.parentOrShadowHostNode(); node; node = node->parentOrShadowHostNode()) {
     80         if (ScopedStyleResolver* scopedResolver = scopedStyleResolverFor(*node)) {
     81             target->setParent(scopedResolver);
     82             break;
     83         }
     84         if (node->isDocumentNode()) {
     85             bool dummy;
     86             ScopedStyleResolver* scopedResolver = addScopedStyleResolver(*node, dummy);
     87             target->setParent(scopedResolver);
     88             setupScopedStylesTree(scopedResolver);
     89             break;
     90         }
     91     }
     92 
     93     if (m_buildInDocumentOrder)
     94         return;
     95 
     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)
     99             continue;
    100         ASSERT(it->key->inDocument());
    101         if (it->value->parent() == target->parent() && scopingNode.containsIncludingShadowDOM(it->key))
    102             it->value->setParent(target);
    103     }
    104 }
    105 
    106 void ScopedStyleTree::clear()
    107 {
    108     m_authorStyles.clear();
    109     m_scopedResolverForDocument = 0;
    110     m_cache.clear();
    111 }
    112 
    113 void ScopedStyleTree::resolveScopedStyles(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
    114 {
    115     for (ScopedStyleResolver* scopedResolver = scopedResolverFor(element); scopedResolver; scopedResolver = scopedResolver->parent())
    116         resolvers.append(scopedResolver);
    117 }
    118 
    119 void ScopedStyleTree::collectScopedResolversForHostedShadowTrees(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
    120 {
    121     ElementShadow* shadow = element->shadow();
    122     if (!shadow)
    123         return;
    124 
    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);
    130         }
    131     }
    132 }
    133 
    134 void ScopedStyleTree::resolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
    135 {
    136     Document& document = element->document();
    137     TreeScope& treeScope = element->treeScope();
    138     bool applyAuthorStyles = treeScope.applyAuthorStyles();
    139 
    140     // Add resolvers for shadow roots hosted by the given element.
    141     collectScopedResolversForHostedShadowTrees(element, resolvers);
    142 
    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);
    147     }
    148 }
    149 
    150 inline ScopedStyleResolver* ScopedStyleTree::enclosingScopedStyleResolverFor(const ContainerNode* scopingNode)
    151 {
    152     for (; scopingNode; scopingNode = scopingNode->parentOrShadowHostNode()) {
    153         if (ScopedStyleResolver* scopedStyleResolver = scopedStyleResolverFor(*scopingNode))
    154             return scopedStyleResolver;
    155     }
    156     return 0;
    157 }
    158 
    159 void ScopedStyleTree::resolveStyleCache(const ContainerNode* scopingNode)
    160 {
    161     m_cache.scopedResolver = enclosingScopedStyleResolverFor(scopingNode);
    162     m_cache.nodeForScopedStyles = scopingNode;
    163 }
    164 
    165 void ScopedStyleTree::pushStyleCache(const ContainerNode& scopingNode, const ContainerNode* parent)
    166 {
    167     if (m_authorStyles.isEmpty())
    168         return;
    169 
    170     if (!cacheIsValid(parent)) {
    171         resolveStyleCache(&scopingNode);
    172         return;
    173     }
    174 
    175     ScopedStyleResolver* scopedResolver = scopedStyleResolverFor(scopingNode);
    176     if (scopedResolver)
    177         m_cache.scopedResolver = scopedResolver;
    178     m_cache.nodeForScopedStyles = &scopingNode;
    179 }
    180 
    181 void ScopedStyleTree::popStyleCache(const ContainerNode& scopingNode)
    182 {
    183     if (!cacheIsValid(&scopingNode))
    184         return;
    185 
    186     if (m_cache.scopedResolver && m_cache.scopedResolver->scopingNode() == scopingNode)
    187         m_cache.scopedResolver = m_cache.scopedResolver->parent();
    188     m_cache.nodeForScopedStyles = scopingNode.parentOrShadowHostNode();
    189 }
    190 
    191 void ScopedStyleTree::collectFeaturesTo(RuleFeatureSet& features)
    192 {
    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);
    196 }
    197 
    198 inline void ScopedStyleTree::reparentNodes(const ScopedStyleResolver* oldParent, ScopedStyleResolver* newParent)
    199 {
    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);
    204     }
    205 }
    206 
    207 void ScopedStyleTree::remove(const ContainerNode* scopingNode)
    208 {
    209     if (!scopingNode || scopingNode->isDocumentNode())
    210         return;
    211 
    212     ScopedStyleResolver* resolverRemoved = lookupScopedStyleResolverFor(scopingNode);
    213     if (!resolverRemoved)
    214         return;
    215 
    216     reparentNodes(resolverRemoved, resolverRemoved->parent());
    217     if (m_cache.scopedResolver == resolverRemoved)
    218         m_cache.clear();
    219 
    220     m_authorStyles.remove(scopingNode);
    221 }
    222 
    223 } // namespace WebCore
    224