720cc2e1d06417ae08d6aef7f8dd3fee64420744
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / resolver / ScopedStyleResolver.cpp
1 /*
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.
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 blink {
44
45 TreeScope* ScopedStyleResolver::treeScopeFor(Document& document, const CSSStyleSheet* sheet)
46 {
47     ASSERT(sheet);
48
49     if (!sheet->ownerDocument())
50         return 0;
51
52     Node* ownerNode = sheet->ownerNode();
53     if (!isHTMLStyleElement(ownerNode))
54         return &document;
55
56     HTMLStyleElement& styleElement = toHTMLStyleElement(*ownerNode);
57     if (styleElement.isInShadowTree())
58         return styleElement.containingShadowRoot();
59     return &document;
60 }
61
62 ScopedStyleResolver* ScopedStyleResolver::parent() const
63 {
64     for (TreeScope* scope = treeScope().parentTreeScope(); scope; scope = scope->parentTreeScope()) {
65         if (ScopedStyleResolver* resolver = scope->scopedStyleResolver())
66             return resolver;
67     }
68     return 0;
69 }
70
71 void ScopedStyleResolver::addRulesFromSheet(CSSStyleSheet* cssSheet, const MediaQueryEvaluator& medium, StyleResolver* resolver)
72 {
73     m_authorStyleSheets.append(cssSheet);
74     unsigned index = m_authorStyleSheets.size() - 1;
75     StyleSheetContents* sheet = cssSheet->contents();
76
77     AddRuleFlags addRuleFlags = resolver->document().securityOrigin()->canRequest(sheet->baseURL()) ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState;
78     const RuleSet& ruleSet = sheet->ensureRuleSet(medium, addRuleFlags);
79     resolver->addMediaQueryResults(ruleSet.viewportDependentMediaQueryResults());
80     resolver->processScopedRules(ruleSet, cssSheet, index, treeScope().rootNode());
81 }
82
83 void ScopedStyleResolver::collectFeaturesTo(RuleFeatureSet& features, HashSet<const StyleSheetContents*>& visitedSharedStyleSheetContents) const
84 {
85     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
86         StyleSheetContents* contents = m_authorStyleSheets[i]->contents();
87         if (contents->hasOneClient() || visitedSharedStyleSheetContents.add(contents).isNewEntry)
88             features.add(contents->ruleSet().features());
89     }
90 }
91
92 void ScopedStyleResolver::resetAuthorStyle()
93 {
94     m_authorStyleSheets.clear();
95     m_keyframesRuleMap.clear();
96 }
97
98 const StyleRuleKeyframes* ScopedStyleResolver::keyframeStylesForAnimation(const StringImpl* animationName)
99 {
100     if (m_keyframesRuleMap.isEmpty())
101         return 0;
102
103     KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(animationName);
104     if (it == m_keyframesRuleMap.end())
105         return 0;
106
107     return it->value.get();
108 }
109
110 void ScopedStyleResolver::addKeyframeStyle(PassRefPtrWillBeRawPtr<StyleRuleKeyframes> rule)
111 {
112     AtomicString s(rule->name());
113     if (rule->isVendorPrefixed()) {
114         KeyframesRuleMap::iterator it = m_keyframesRuleMap.find(rule->name().impl());
115         if (it == m_keyframesRuleMap.end())
116             m_keyframesRuleMap.set(s.impl(), rule);
117         else if (it->value->isVendorPrefixed())
118             m_keyframesRuleMap.set(s.impl(), rule);
119     } else {
120         m_keyframesRuleMap.set(s.impl(), rule);
121     }
122 }
123
124 void ScopedStyleResolver::collectMatchingAuthorRules(ElementRuleCollector& collector, bool includeEmptyRules, CascadeScope cascadeScope, CascadeOrder cascadeOrder)
125 {
126     unsigned contextFlags = SelectorChecker::DefaultBehavior;
127
128     // FIXME: This is always true now.
129     contextFlags |= SelectorChecker::ScopeContainsLastMatchedElement;
130
131     RuleRange ruleRange = collector.matchedResult().ranges.authorRuleRange();
132     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i) {
133         MatchRequest matchRequest(&m_authorStyleSheets[i]->contents()->ruleSet(), includeEmptyRules, &m_scope->rootNode(), m_authorStyleSheets[i], i);
134         collector.collectMatchingRules(matchRequest, ruleRange, static_cast<SelectorChecker::ContextFlags>(contextFlags), cascadeScope, cascadeOrder);
135     }
136 }
137
138 void ScopedStyleResolver::matchPageRules(PageRuleCollector& collector)
139 {
140     // Only consider the global author RuleSet for @page rules, as per the HTML5 spec.
141     ASSERT(m_scope->rootNode().isDocumentNode());
142     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
143         collector.matchPageRules(&m_authorStyleSheets[i]->contents()->ruleSet());
144 }
145
146 void ScopedStyleResolver::collectViewportRulesTo(StyleResolver* resolver) const
147 {
148     if (!m_scope->rootNode().isDocumentNode())
149         return;
150     for (size_t i = 0; i < m_authorStyleSheets.size(); ++i)
151         resolver->viewportStyleResolver()->collectViewportRules(&m_authorStyleSheets[i]->contents()->ruleSet(), ViewportStyleResolver::AuthorOrigin);
152 }
153
154 void ScopedStyleResolver::trace(Visitor* visitor)
155 {
156 #if ENABLE(OILPAN)
157     visitor->trace(m_scope);
158     visitor->trace(m_authorStyleSheets);
159     visitor->trace(m_keyframesRuleMap);
160 #endif
161 }
162
163 } // namespace blink