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/ScopedStyleResolver.h"
     29 
     30 #include "core/HTMLNames.h"
     31 #include "core/css/CSSStyleSheet.h"
     32 #include "core/css/PageRuleCollector.h"
     33 #include "core/css/RuleFeature.h"
     34 #include "core/css/StyleRule.h"
     35 #include "core/css/StyleSheetContents.h"
     36 #include "core/css/resolver/StyleResolver.h" // For MatchRequest.
     37 #include "core/css/resolver/ViewportStyleResolver.h"
     38 #include "core/dom/Document.h"
     39 #include "core/dom/shadow/ElementShadow.h"
     40 #include "core/dom/shadow/ShadowRoot.h"
     41 #include "core/html/HTMLStyleElement.h"
     42 
     43 namespace WebCore {
     44 
     45 ContainerNode* ScopedStyleResolver::scopingNodeFor(Document& document, const CSSStyleSheet* sheet)
     46 {
     47     ASSERT(sheet);
     48 
     49     Document* sheetDocument = sheet->ownerDocument();
     50     if (!sheetDocument)
     51         return 0;
     52 
     53     Node* ownerNode = sheet->ownerNode();
     54     if (!isHTMLStyleElement(ownerNode))
     55         return &document;
     56 
     57     HTMLStyleElement& styleElement = toHTMLStyleElement(*ownerNode);
     58     if (styleElement.isInShadowTree())
     59         return styleElement.containingShadowRoot();
     60     return &document;
     61 }
     62 
     63 void ScopedStyleResolver::addRulesFromSheet(CSSStyleSheet* cssSheet, const MediaQueryEvaluator& medium, StyleResolver* resolver)
     64 {
     65     m_authorStyleSheets.append(cssSheet);
     66     StyleSheetContents* sheet = cssSheet->contents();
     67 
     68     AddRuleFlags addRuleFlags = resolver->document().securityOrigin()->canRequest(sheet->baseURL()) ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState;
     69     const RuleSet& ruleSet = sheet->ensureRuleSet(medium, addRuleFlags);
     70     resolver->addMediaQueryResults(ruleSet.viewportDependentMediaQueryResults());
     71     resolver->processScopedRules(ruleSet, cssSheet, m_scopingNode);
     72 }
     73 
     74 void ScopedStyleResolver::collectFeaturesTo(RuleFeatureSet& features, HashSet<const StyleSheetContents*>& visitedSharedStyleSheetContents)
     75 {
     76     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
     77         StyleSheetContents* contents = m_authorStyleSheets[i]->contents();
     78         if (contents->hasOneClient() || visitedSharedStyleSheetContents.add(contents).isNewEntry)
     79             features.add(contents->ruleSet().features());
     80     }
     81 }
     82 
     83 void ScopedStyleResolver::resetAuthorStyle()
     84 {
     85     m_authorStyleSheets.clear();
     86     m_keyframesRuleMap.clear();
     87 }
     88 
     89 const StyleRuleKeyframes* ScopedStyleResolver::keyframeStylesForAnimation(const StringImpl* animationName)
     90 {
     91     if (m_keyframesRuleMap.isEmpty())
     92         return 0;
     93 
     94     KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(animationName);
     95     if (it == m_keyframesRuleMap.end())
     96         return 0;
     97 
     98     return it->value.get();
     99 }
    100 
    101 void ScopedStyleResolver::addKeyframeStyle(PassRefPtrWillBeRawPtr<StyleRuleKeyframes> rule)
    102 {
    103     AtomicString s(rule->name());
    104     if (rule->isVendorPrefixed()) {
    105         KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(rule->name().impl());
    106         if (it == m_keyframesRuleMap.end())
    107             m_keyframesRuleMap.set(s.impl(), rule);
    108         else if (it->value->isVendorPrefixed())
    109             m_keyframesRuleMap.set(s.impl(), rule);
    110     } else {
    111         m_keyframesRuleMap.set(s.impl(), rule);
    112     }
    113 }
    114 
    115 void ScopedStyleResolver::collectMatchingAuthorRules(ElementRuleCollector& collector, bool includeEmptyRules, bool applyAuthorStyles, CascadeScope cascadeScope, CascadeOrder cascadeOrder)
    116 {
    117     const ContainerNode* scopingNode = &m_scopingNode;
    118     unsigned behaviorAtBoundary = SelectorChecker::DoesNotCrossBoundary;
    119 
    120     if (!applyAuthorStyles)
    121         behaviorAtBoundary |= SelectorChecker::ScopeContainsLastMatchedElement;
    122 
    123     if (m_scopingNode.isShadowRoot())
    124         behaviorAtBoundary |= SelectorChecker::ScopeIsShadowRoot;
    125 
    126     RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
    127     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
    128         MatchRequest matchRequest(&m_authorStyleSheets[i]->contents()->ruleSet(), includeEmptyRules, scopingNode, m_authorStyleSheets[i], applyAuthorStyles, i);
    129         collector.collectMatchingRules(matchRequest, ruleRange, static_cast<SelectorChecker::BehaviorAtBoundary>(behaviorAtBoundary), cascadeScope, cascadeOrder);
    130     }
    131 }
    132 
    133 void ScopedStyleResolver::matchPageRules(PageRuleCollector& collector)
    134 {
    135     // Only consider the global author RuleSet for @page rules, as per the HTML5 spec.
    136     ASSERT(m_scopingNode.isDocumentNode());
    137     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
    138         collector.matchPageRules(&m_authorStyleSheets[i]->contents()->ruleSet());
    139 }
    140 
    141 void ScopedStyleResolver::collectViewportRulesTo(StyleResolver* resolver) const
    142 {
    143     if (!m_scopingNode.isDocumentNode())
    144         return;
    145     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
    146         resolver->viewportStyleResolver()->collectViewportRules(&m_authorStyleSheets[i]->contents()->ruleSet(), ViewportStyleResolver::AuthorOrigin);
    147 }
    148 
    149 } // namespace WebCore
    150