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/ScopedStyleResolver.h"
30 #include "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"
45 ContainerNode* ScopedStyleResolver::scopingNodeFor(Document& document, const CSSStyleSheet* sheet)
49 Document* sheetDocument = sheet->ownerDocument();
52 Node* ownerNode = sheet->ownerNode();
53 if (!ownerNode || !ownerNode->hasTagName(HTMLNames::styleTag))
56 HTMLStyleElement* styleElement = toHTMLStyleElement(ownerNode);
57 if (!styleElement->scoped()) {
58 if (styleElement->isInShadowTree())
59 return styleElement->containingShadowRoot();
63 ContainerNode* parent = styleElement->parentNode();
67 return (parent->isElementNode() || parent->isShadowRoot()) ? parent : 0;
70 void ScopedStyleResolver::addRulesFromSheet(CSSStyleSheet* cssSheet, const MediaQueryEvaluator& medium, StyleResolver* resolver)
72 m_authorStyleSheets.append(cssSheet);
73 StyleSheetContents* sheet = cssSheet->contents();
75 AddRuleFlags addRuleFlags = resolver->document().securityOrigin()->canRequest(sheet->baseURL()) ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState;
76 const RuleSet& ruleSet = sheet->ensureRuleSet(medium, addRuleFlags);
77 resolver->addMediaQueryResults(ruleSet.viewportDependentMediaQueryResults());
78 resolver->processScopedRules(ruleSet, sheet->baseURL(), &m_scopingNode);
81 void ScopedStyleResolver::collectFeaturesTo(RuleFeatureSet& features)
83 for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
84 features.add(m_authorStyleSheets[i]->contents()->ruleSet().features());
87 void ScopedStyleResolver::resetAuthorStyle()
89 m_authorStyleSheets.clear();
90 m_keyframesRuleMap.clear();
93 const StyleRuleKeyframes* ScopedStyleResolver::keyframeStylesForAnimation(const StringImpl* animationName)
95 if (m_keyframesRuleMap.isEmpty())
98 KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(animationName);
99 if (it == m_keyframesRuleMap.end())
102 return it->value.get();
105 void ScopedStyleResolver::addKeyframeStyle(PassRefPtr<StyleRuleKeyframes> rule)
107 AtomicString s(rule->name());
108 if (rule->isVendorPrefixed()) {
109 KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(rule->name().impl());
110 if (it == m_keyframesRuleMap.end())
111 m_keyframesRuleMap.set(s.impl(), rule);
112 else if (it->value->isVendorPrefixed())
113 m_keyframesRuleMap.set(s.impl(), rule);
115 m_keyframesRuleMap.set(s.impl(), rule);
119 void ScopedStyleResolver::collectMatchingAuthorRules(ElementRuleCollector& collector, bool includeEmptyRules, bool applyAuthorStyles, CascadeScope cascadeScope, CascadeOrder cascadeOrder)
121 const ContainerNode* scopingNode = &m_scopingNode;
122 unsigned behaviorAtBoundary = SelectorChecker::DoesNotCrossBoundary;
124 if (!applyAuthorStyles)
125 behaviorAtBoundary |= SelectorChecker::ScopeContainsLastMatchedElement;
127 if (m_scopingNode.isShadowRoot()) {
128 scopingNode = toShadowRoot(m_scopingNode).host();
129 behaviorAtBoundary |= SelectorChecker::ScopeIsShadowHost;
132 RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
133 for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
134 MatchRequest matchRequest(&m_authorStyleSheets[i]->contents()->ruleSet(), includeEmptyRules, scopingNode, applyAuthorStyles, i, m_authorStyleSheets[i]);
135 collector.collectMatchingRules(matchRequest, ruleRange, static_cast<SelectorChecker::BehaviorAtBoundary>(behaviorAtBoundary), cascadeScope, cascadeOrder);
139 void ScopedStyleResolver::matchPageRules(PageRuleCollector& collector)
141 // Only consider the global author RuleSet for @page rules, as per the HTML5 spec.
142 ASSERT(m_scopingNode.isDocumentNode());
143 for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
144 collector.matchPageRules(&m_authorStyleSheets[i]->contents()->ruleSet());
147 void ScopedStyleResolver::collectViewportRulesTo(StyleResolver* resolver) const
149 if (!m_scopingNode.isDocumentNode())
151 for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
152 resolver->viewportStyleResolver()->collectViewportRules(&m_authorStyleSheets[i]->contents()->ruleSet(), ViewportStyleResolver::AuthorOrigin);
155 } // namespace WebCore