2
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
24
#include "CSSSelector.h"
25
#include "ElementRuleCollector.h"
26
#include "InspectorCSSOMWrappers.h"
27
#include "MatchedDeclarationsCache.h"
28
#include "MediaQueryEvaluator.h"
29
#include "RenderStyle.h"
31
#include "StyleBuilderState.h"
32
#include "StyleScopeRuleSets.h"
34
#include <wtf/HashMap.h>
35
#include <wtf/RefPtr.h>
36
#include <wtf/Vector.h>
37
#include <wtf/text/AtomStringHash.h>
38
#include <wtf/text/StringHash.h>
51
class StyleRuleKeyframe;
52
class StyleProperties;
54
class StyleRuleKeyframes;
58
class ViewportStyleResolver;
60
// MatchOnlyUserAgentRules is used in media queries, where relative units
61
// are interpreted according to the document root element style, and styled only
62
// from the User Agent Stylesheet rules.
64
enum class RuleMatchingBehavior: uint8_t {
66
MatchAllRulesExcludingSMIL,
67
MatchOnlyUserAgentRules,
73
ElementStyle(std::unique_ptr<RenderStyle> renderStyle, std::unique_ptr<Relations> relations = { })
74
: renderStyle(WTFMove(renderStyle))
75
, relations(WTFMove(relations))
78
std::unique_ptr<RenderStyle> renderStyle;
79
std::unique_ptr<Relations> relations;
83
WTF_MAKE_NONCOPYABLE(Resolver); WTF_MAKE_FAST_ALLOCATED;
88
ElementStyle styleForElement(const Element&, const RenderStyle* parentStyle, const RenderStyle* parentBoxStyle = nullptr, RuleMatchingBehavior = RuleMatchingBehavior::MatchAllRules, const SelectorFilter* = nullptr);
90
void keyframeStylesForAnimation(const Element&, const RenderStyle*, KeyframeList&);
92
WEBCORE_EXPORT std::unique_ptr<RenderStyle> pseudoStyleForElement(const Element&, const PseudoElementRequest&, const RenderStyle& parentStyle, const RenderStyle* parentBoxStyle = nullptr, const SelectorFilter* = nullptr);
94
std::unique_ptr<RenderStyle> styleForPage(int pageIndex);
95
std::unique_ptr<RenderStyle> defaultStyleForElement(const Element*);
97
Document& document() { return m_document; }
98
const Document& document() const { return m_document; }
99
const Settings& settings() const { return m_document.settings(); }
101
void appendAuthorStyleSheets(const Vector<RefPtr<CSSStyleSheet>>&);
103
ScopeRuleSets& ruleSets() { return m_ruleSets; }
104
const ScopeRuleSets& ruleSets() const { return m_ruleSets; }
106
const MediaQueryEvaluator& mediaQueryEvaluator() const { return m_mediaQueryEvaluator; }
108
RenderStyle* overrideDocumentElementStyle() const { return m_overrideDocumentElementStyle; }
109
void setOverrideDocumentElementStyle(RenderStyle* style) { m_overrideDocumentElementStyle = style; }
111
void addCurrentSVGFontFaceRules();
113
std::unique_ptr<RenderStyle> styleForKeyframe(const Element&, const RenderStyle*, const StyleRuleKeyframe*, KeyframeValue&);
114
bool isAnimationNameValid(const String&);
116
// These methods will give back the set of rules that matched for a given element (or a pseudo-element).
118
UAAndUserCSSRules = 1 << 1,
119
AuthorCSSRules = 1 << 2,
120
EmptyCSSRules = 1 << 3,
121
AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules,
122
AllCSSRules = AllButEmptyCSSRules | EmptyCSSRules,
124
Vector<RefPtr<const StyleRule>> styleRulesForElement(const Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
125
Vector<RefPtr<const StyleRule>> pseudoStyleRulesForElement(const Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
127
bool hasSelectorForId(const AtomString&) const;
128
bool hasSelectorForAttribute(const Element&, const AtomString&) const;
130
#if ENABLE(CSS_DEVICE_ADAPTATION)
131
ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
134
bool hasViewportDependentMediaQueries() const;
135
Optional<DynamicMediaQueryEvaluationChanges> evaluateDynamicMediaQueries();
137
void addKeyframeStyle(Ref<StyleRuleKeyframes>&&);
139
bool usesFirstLineRules() const { return m_ruleSets.features().usesFirstLineRules; }
140
bool usesFirstLetterRules() const { return m_ruleSets.features().usesFirstLetterRules; }
142
void invalidateMatchedDeclarationsCache();
143
void clearCachedDeclarationsAffectedByViewportUnits();
145
InspectorCSSOMWrappers& inspectorCSSOMWrappers() { return m_inspectorCSSOMWrappers; }
148
friend class PageRuleCollector;
153
State(const Element&, const RenderStyle* parentStyle, const RenderStyle* documentElementStyle = nullptr);
156
const Element* element() const { return m_element; }
158
void setStyle(std::unique_ptr<RenderStyle>);
159
RenderStyle* style() const { return m_style.get(); }
160
std::unique_ptr<RenderStyle> takeStyle() { return WTFMove(m_style); }
162
void setParentStyle(std::unique_ptr<RenderStyle>);
163
const RenderStyle* parentStyle() const { return m_parentStyle; }
164
const RenderStyle* rootElementStyle() const { return m_rootElementStyle; }
166
const RenderStyle* userAgentAppearanceStyle() const { return m_userAgentAppearanceStyle.get(); }
167
void setUserAgentAppearanceStyle(std::unique_ptr<RenderStyle> style) { m_userAgentAppearanceStyle = WTFMove(style); }
170
const Element* m_element { nullptr };
171
std::unique_ptr<RenderStyle> m_style;
172
const RenderStyle* m_parentStyle { nullptr };
173
std::unique_ptr<const RenderStyle> m_ownedParentStyle;
174
const RenderStyle* m_rootElementStyle { nullptr };
176
std::unique_ptr<RenderStyle> m_userAgentAppearanceStyle;
179
BuilderContext builderContext(const State&);
181
enum class UseMatchedDeclarationsCache { Yes, No };
182
void applyMatchedProperties(State&, const MatchResult&, UseMatchedDeclarationsCache = UseMatchedDeclarationsCache::Yes);
184
ScopeRuleSets m_ruleSets;
186
typedef HashMap<AtomStringImpl*, RefPtr<StyleRuleKeyframes>> KeyframesRuleMap;
187
KeyframesRuleMap m_keyframesRuleMap;
189
MediaQueryEvaluator m_mediaQueryEvaluator;
190
std::unique_ptr<RenderStyle> m_rootDefaultStyle;
192
Document& m_document;
194
RenderStyle* m_overrideDocumentElementStyle { nullptr };
196
#if ENABLE(CSS_DEVICE_ADAPTATION)
197
RefPtr<ViewportStyleResolver> m_viewportStyleResolver;
200
InspectorCSSOMWrappers m_inspectorCSSOMWrappers;
202
MatchedDeclarationsCache m_matchedDeclarationsCache;
204
bool m_matchAuthorAndUserStyles { true };
205
// See if we still have crashes where Resolver gets deleted early.
206
bool m_isDeleted { false };
209
inline bool Resolver::hasSelectorForAttribute(const Element& element, const AtomString &attributeName) const
211
ASSERT(!attributeName.isEmpty());
212
if (element.isHTMLElement())
213
return m_ruleSets.features().attributeCanonicalLocalNamesInRules.contains(attributeName);
214
return m_ruleSets.features().attributeLocalNamesInRules.contains(attributeName);
217
inline bool Resolver::hasSelectorForId(const AtomString& idValue) const
219
ASSERT(!idValue.isEmpty());
220
return m_ruleSets.features().idsInRules.contains(idValue);
224
} // namespace WebCore