~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebCore/style/StyleResolver.h

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
20
 */
 
21
 
 
22
#pragma once
 
23
 
 
24
#include "CSSSelector.h"
 
25
#include "ElementRuleCollector.h"
 
26
#include "InspectorCSSOMWrappers.h"
 
27
#include "MatchedDeclarationsCache.h"
 
28
#include "MediaQueryEvaluator.h"
 
29
#include "RenderStyle.h"
 
30
#include "RuleSet.h"
 
31
#include "StyleBuilderState.h"
 
32
#include "StyleScopeRuleSets.h"
 
33
#include <memory>
 
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>
 
39
 
 
40
namespace WebCore {
 
41
 
 
42
class CSSStyleSheet;
 
43
class Document;
 
44
class Element;
 
45
class KeyframeList;
 
46
class KeyframeValue;
 
47
class RuleData;
 
48
class RuleSet;
 
49
class SelectorFilter;
 
50
class Settings;
 
51
class StyleRuleKeyframe;
 
52
class StyleProperties;
 
53
class StyleRule;
 
54
class StyleRuleKeyframes;
 
55
class StyleRulePage;
 
56
class StyleSheet;
 
57
class StyleSheetList;
 
58
class ViewportStyleResolver;
 
59
 
 
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.
 
63
 
 
64
enum class RuleMatchingBehavior: uint8_t {
 
65
    MatchAllRules,
 
66
    MatchAllRulesExcludingSMIL,
 
67
    MatchOnlyUserAgentRules,
 
68
};
 
69
 
 
70
namespace Style {
 
71
 
 
72
struct ElementStyle {
 
73
    ElementStyle(std::unique_ptr<RenderStyle> renderStyle, std::unique_ptr<Relations> relations = { })
 
74
        : renderStyle(WTFMove(renderStyle))
 
75
        , relations(WTFMove(relations))
 
76
    { }
 
77
 
 
78
    std::unique_ptr<RenderStyle> renderStyle;
 
79
    std::unique_ptr<Relations> relations;
 
80
};
 
81
 
 
82
class Resolver {
 
83
    WTF_MAKE_NONCOPYABLE(Resolver); WTF_MAKE_FAST_ALLOCATED;
 
84
public:
 
85
    Resolver(Document&);
 
86
    ~Resolver();
 
87
 
 
88
    ElementStyle styleForElement(const Element&, const RenderStyle* parentStyle, const RenderStyle* parentBoxStyle = nullptr, RuleMatchingBehavior = RuleMatchingBehavior::MatchAllRules, const SelectorFilter* = nullptr);
 
89
 
 
90
    void keyframeStylesForAnimation(const Element&, const RenderStyle*, KeyframeList&);
 
91
 
 
92
    WEBCORE_EXPORT std::unique_ptr<RenderStyle> pseudoStyleForElement(const Element&, const PseudoElementRequest&, const RenderStyle& parentStyle, const RenderStyle* parentBoxStyle = nullptr, const SelectorFilter* = nullptr);
 
93
 
 
94
    std::unique_ptr<RenderStyle> styleForPage(int pageIndex);
 
95
    std::unique_ptr<RenderStyle> defaultStyleForElement(const Element*);
 
96
 
 
97
    Document& document() { return m_document; }
 
98
    const Document& document() const { return m_document; }
 
99
    const Settings& settings() const { return m_document.settings(); }
 
100
 
 
101
    void appendAuthorStyleSheets(const Vector<RefPtr<CSSStyleSheet>>&);
 
102
 
 
103
    ScopeRuleSets& ruleSets() { return m_ruleSets; }
 
104
    const ScopeRuleSets& ruleSets() const { return m_ruleSets; }
 
105
 
 
106
    const MediaQueryEvaluator& mediaQueryEvaluator() const { return m_mediaQueryEvaluator; }
 
107
 
 
108
    RenderStyle* overrideDocumentElementStyle() const { return m_overrideDocumentElementStyle; }
 
109
    void setOverrideDocumentElementStyle(RenderStyle* style) { m_overrideDocumentElementStyle = style; }
 
110
 
 
111
    void addCurrentSVGFontFaceRules();
 
112
 
 
113
    std::unique_ptr<RenderStyle> styleForKeyframe(const Element&, const RenderStyle*, const StyleRuleKeyframe*, KeyframeValue&);
 
114
    bool isAnimationNameValid(const String&);
 
115
 
 
116
    // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
 
117
    enum CSSRuleFilter {
 
118
        UAAndUserCSSRules   = 1 << 1,
 
119
        AuthorCSSRules      = 1 << 2,
 
120
        EmptyCSSRules       = 1 << 3,
 
121
        AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules,
 
122
        AllCSSRules         = AllButEmptyCSSRules | EmptyCSSRules,
 
123
    };
 
124
    Vector<RefPtr<const StyleRule>> styleRulesForElement(const Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
 
125
    Vector<RefPtr<const StyleRule>> pseudoStyleRulesForElement(const Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
 
126
 
 
127
    bool hasSelectorForId(const AtomString&) const;
 
128
    bool hasSelectorForAttribute(const Element&, const AtomString&) const;
 
129
 
 
130
#if ENABLE(CSS_DEVICE_ADAPTATION)
 
131
    ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
 
132
#endif
 
133
 
 
134
    bool hasViewportDependentMediaQueries() const;
 
135
    Optional<DynamicMediaQueryEvaluationChanges> evaluateDynamicMediaQueries();
 
136
 
 
137
    void addKeyframeStyle(Ref<StyleRuleKeyframes>&&);
 
138
 
 
139
    bool usesFirstLineRules() const { return m_ruleSets.features().usesFirstLineRules; }
 
140
    bool usesFirstLetterRules() const { return m_ruleSets.features().usesFirstLetterRules; }
 
141
    
 
142
    void invalidateMatchedDeclarationsCache();
 
143
    void clearCachedDeclarationsAffectedByViewportUnits();
 
144
 
 
145
    InspectorCSSOMWrappers& inspectorCSSOMWrappers() { return m_inspectorCSSOMWrappers; }
 
146
 
 
147
private:
 
148
    friend class PageRuleCollector;
 
149
 
 
150
    class State {
 
151
    public:
 
152
        State() { }
 
153
        State(const Element&, const RenderStyle* parentStyle, const RenderStyle* documentElementStyle = nullptr);
 
154
 
 
155
    public:
 
156
        const Element* element() const { return m_element; }
 
157
 
 
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); }
 
161
 
 
162
        void setParentStyle(std::unique_ptr<RenderStyle>);
 
163
        const RenderStyle* parentStyle() const { return m_parentStyle; }
 
164
        const RenderStyle* rootElementStyle() const { return m_rootElementStyle; }
 
165
 
 
166
        const RenderStyle* userAgentAppearanceStyle() const { return m_userAgentAppearanceStyle.get(); }
 
167
        void setUserAgentAppearanceStyle(std::unique_ptr<RenderStyle> style) { m_userAgentAppearanceStyle = WTFMove(style); }
 
168
 
 
169
    private:
 
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 };
 
175
 
 
176
        std::unique_ptr<RenderStyle> m_userAgentAppearanceStyle;
 
177
    };
 
178
 
 
179
    BuilderContext builderContext(const State&);
 
180
 
 
181
    enum class UseMatchedDeclarationsCache { Yes, No };
 
182
    void applyMatchedProperties(State&, const MatchResult&, UseMatchedDeclarationsCache = UseMatchedDeclarationsCache::Yes);
 
183
 
 
184
    ScopeRuleSets m_ruleSets;
 
185
 
 
186
    typedef HashMap<AtomStringImpl*, RefPtr<StyleRuleKeyframes>> KeyframesRuleMap;
 
187
    KeyframesRuleMap m_keyframesRuleMap;
 
188
 
 
189
    MediaQueryEvaluator m_mediaQueryEvaluator;
 
190
    std::unique_ptr<RenderStyle> m_rootDefaultStyle;
 
191
 
 
192
    Document& m_document;
 
193
 
 
194
    RenderStyle* m_overrideDocumentElementStyle { nullptr };
 
195
 
 
196
#if ENABLE(CSS_DEVICE_ADAPTATION)
 
197
    RefPtr<ViewportStyleResolver> m_viewportStyleResolver;
 
198
#endif
 
199
 
 
200
    InspectorCSSOMWrappers m_inspectorCSSOMWrappers;
 
201
 
 
202
    MatchedDeclarationsCache m_matchedDeclarationsCache;
 
203
 
 
204
    bool m_matchAuthorAndUserStyles { true };
 
205
    // See if we still have crashes where Resolver gets deleted early.
 
206
    bool m_isDeleted { false };
 
207
};
 
208
 
 
209
inline bool Resolver::hasSelectorForAttribute(const Element& element, const AtomString &attributeName) const
 
210
{
 
211
    ASSERT(!attributeName.isEmpty());
 
212
    if (element.isHTMLElement())
 
213
        return m_ruleSets.features().attributeCanonicalLocalNamesInRules.contains(attributeName);
 
214
    return m_ruleSets.features().attributeLocalNamesInRules.contains(attributeName);
 
215
}
 
216
 
 
217
inline bool Resolver::hasSelectorForId(const AtomString& idValue) const
 
218
{
 
219
    ASSERT(!idValue.isEmpty());
 
220
    return m_ruleSets.features().idsInRules.contains(idValue);
 
221
}
 
222
 
 
223
} // namespace Style
 
224
} // namespace WebCore