CSSStyleSelector.h 10.1 KB
Newer Older
1
/*
kocienda's avatar
kocienda committed
2
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
darin@apple.com's avatar
darin@apple.com committed
3
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
kocienda's avatar
kocienda committed
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
17
18
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
kocienda's avatar
kocienda committed
19
20
21
 *
 */

weinig's avatar
weinig committed
22
23
#ifndef CSSStyleSelector_h
#define CSSStyleSelector_h
darin's avatar
darin committed
24

25
#include "CSSFontSelector.h"
26
#include "MediaQueryExp.h"
darin's avatar
darin committed
27
#include "RenderStyle.h"
darin@apple.com's avatar
darin@apple.com committed
28
#include "StringHash.h"
darin's avatar
darin committed
29
#include <wtf/HashSet.h>
darin@apple.com's avatar
darin@apple.com committed
30
#include <wtf/HashMap.h>
31
#include <wtf/Vector.h>
32
#include <wtf/RefPtr.h>
kocienda's avatar
kocienda committed
33

darin's avatar
darin committed
34
namespace WebCore {
kocienda's avatar
kocienda committed
35

36
class CSSMutableStyleDeclaration;
37
class CSSPrimitiveValue;
darin's avatar
darin committed
38
class CSSProperty;
39
40
class CSSFontFace;
class CSSFontFaceRule;
darin's avatar
darin committed
41
42
class CSSRuleData;
class CSSRuleDataList;
43
class CSSRuleList;
darin's avatar
darin committed
44
45
class CSSRuleSet;
class CSSSelector;
46
class CSSStyleRule;
darin's avatar
darin committed
47
48
49
50
class CSSStyleSheet;
class CSSValue;
class Document;
class Element;
darin's avatar
darin committed
51
52
class Frame;
class FrameView;
darin's avatar
darin committed
53
54
class KURL;
class MediaQueryEvaluator;
darin's avatar
darin committed
55
class Node;
darin's avatar
darin committed
56
class Settings;
57
class StyleImage;
darin's avatar
darin committed
58
59
60
class StyleSheet;
class StyleSheetList;
class StyledElement;
kocienda's avatar
kocienda committed
61

darin@apple.com's avatar
darin@apple.com committed
62
class MediaQueryResult {
63
64
public:
    MediaQueryResult(const MediaQueryExp& expr, bool result)
darin@apple.com's avatar
darin@apple.com committed
65
66
67
68
        : m_expression(expr)
        , m_result(result)
    {
    }
69
70
71
72
73

    MediaQueryExp m_expression;
    bool m_result;
};

darin@apple.com's avatar
darin@apple.com committed
74
75
    // This class selects a RenderStyle for a given element based on a collection of stylesheets.
    class CSSStyleSelector : Noncopyable {
kocienda's avatar
kocienda committed
76
    public:
77
        CSSStyleSelector(Document*, const String& userStyleSheet, StyleSheetList*, CSSStyleSheet*, bool strictParsing, bool matchAuthorAndUserStyles);
darin's avatar
darin committed
78
        ~CSSStyleSelector();
79

80
81
82
        void initElementAndPseudoState(Element*);
        void initForStyleResolve(Element*, RenderStyle* parentStyle);
        RenderStyle* styleForElement(Element*, RenderStyle* parentStyle = 0, bool allowSharing = true, bool resolveForRootDefault = false);
darin@apple.com's avatar
darin@apple.com committed
83

84
        RenderStyle* pseudoStyleForElement(RenderStyle::PseudoId, Element*, RenderStyle* parentStyle = 0);
85

darin@apple.com's avatar
darin@apple.com committed
86
    private:
87
        RenderStyle* locateSharedStyle();
88
        Node* locateCousinList(Element* parent, unsigned depth = 1);
darin@apple.com's avatar
darin@apple.com committed
89
        bool canShareStyleWithElement(Node*);
90

darin@apple.com's avatar
darin@apple.com committed
91
    public:
92
        // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
93
        RefPtr<CSSRuleList> styleRulesForElement(Element*, bool authorOnly);
darin@apple.com's avatar
darin@apple.com committed
94
        RefPtr<CSSRuleList> pseudoStyleRulesForElement(Element*, const String& pseudoStyle, bool authorOnly);
95

96
        // Given a CSS keyword in the range (xx-small to -webkit-xxx-large), this function will return
97
        // the correct font size scaled relative to the user's default (medium).
98
        float fontSizeForKeyword(int keyword, bool quirksMode, bool monospace) const;
99

darin@apple.com's avatar
darin@apple.com committed
100
    private:
101
102
103
        // When the CSS keyword "larger" is used, this function will attempt to match within the keyword
        // table, and failing that, will simply multiply by 1.2.
        float largerFontSize(float size, bool quirksMode) const;
104

105
106
        // Like the previous function, but for the keyword "smaller".
        float smallerFontSize(float size, bool quirksMode) const;
107

darin@apple.com's avatar
darin@apple.com committed
108
    public:
109
        void setStyle(RenderStyle* s) { m_style = s; } // Used by the document when setting up its root style.
110
        void setFontSize(FontDescription&, float size);
darin@apple.com's avatar
darin@apple.com committed
111
112

    private:
113
        float getComputedSizeFromSpecifiedSize(bool isAbsoluteSize, float specifiedSize);
114

darin@apple.com's avatar
darin@apple.com committed
115
    public:
116
117
118
        Color getColorFromPrimitiveValue(CSSPrimitiveValue*);

        bool hasSelectorForAttribute(const AtomicString&);
darin's avatar
darin committed
119
 
120
121
        CSSFontSelector* fontSelector() { return m_fontSelector.get(); }

darin@apple.com's avatar
darin@apple.com committed
122
        // Checks if a compound selector (which can consist of multiple simple selectors) matches the current element.
dsmith@webkit.org's avatar
dsmith@webkit.org committed
123
124
        bool checkSelector(CSSSelector*);

125
        void addViewportDependentMediaQueryResult(const MediaQueryExp*, bool result);
darin@apple.com's avatar
darin@apple.com committed
126

127
        bool affectedByViewportChange() const;
128

darin@apple.com's avatar
darin@apple.com committed
129
130
131
132
133
134
        void allVisitedStateChanged();
        void visitedStateChanged(unsigned visitedHash);

    private:
        enum SelectorMatch { SelectorMatches, SelectorFailsLocally, SelectorFailsCompletely };
        SelectorMatch checkSelector(CSSSelector*, Element*, bool isAncestor, bool isSubSelector);
135

darin@apple.com's avatar
darin@apple.com committed
136
        // Checks if the selector matches the given Element.
137
        bool checkOneSelector(CSSSelector*, Element*, bool isAncestor, bool isSubSelector = false);
kocienda's avatar
kocienda committed
138

darin@apple.com's avatar
darin@apple.com committed
139
140
        // This function fixes up the default font size if it detects that the current generic font family has changed. -dwh
        void checkForGenericFamilyChange(RenderStyle*, RenderStyle* parentStyle);
141
        void checkForTextSizeAdjust();
142

143
144
        void adjustRenderStyle(RenderStyle*, Element*);

145
146
147
        void addMatchedRule(CSSRuleData* rule) { m_matchedRules.append(rule); }
        void addMatchedDeclaration(CSSMutableStyleDeclaration* decl) { m_matchedDecls.append(decl); }

148
        void matchRules(CSSRuleSet*, int& firstRuleIndex, int& lastRuleIndex);
darin@apple.com's avatar
darin@apple.com committed
149
        void matchRulesForList(CSSRuleDataList*, int& firstRuleIndex, int& lastRuleIndex);
150
151
        void sortMatchedRules(unsigned start, unsigned end);

152
        void applyDeclarations(bool firstPass, bool important, int startIndex, int endIndex);
153

darin@apple.com's avatar
darin@apple.com committed
154
        bool m_strictParsing;
155

darin's avatar
darin committed
156
        CSSRuleSet* m_authorStyle;
157
        CSSRuleSet* m_userStyle;
158
        RefPtr<CSSStyleSheet> m_userSheet;
159

160
        bool m_hasUAAppearance;
161
162
        BorderData m_borderData;
        BackgroundLayer m_backgroundData;
163
        Color m_backgroundColor;
164

165
    public:
darin@apple.com's avatar
darin@apple.com committed
166
        static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
167

darin's avatar
darin committed
168
    private:
darin@apple.com's avatar
darin@apple.com committed
169
170
        static RenderStyle* s_styleNotYetAvailable;

darin's avatar
darin committed
171
        void init();
172
173
174
175

        void matchUARules(int& firstUARule, int& lastUARule);
        void updateFont();
        void cacheBorderAndBackground();
176
177
178
179
180
181
182
183
184
185
186

        void mapBackgroundAttachment(BackgroundLayer*, CSSValue*);
        void mapBackgroundClip(BackgroundLayer*, CSSValue*);
        void mapBackgroundComposite(BackgroundLayer*, CSSValue*);
        void mapBackgroundOrigin(BackgroundLayer*, CSSValue*);
        void mapBackgroundImage(BackgroundLayer*, CSSValue*);
        void mapBackgroundRepeat(BackgroundLayer*, CSSValue*);
        void mapBackgroundSize(BackgroundLayer*, CSSValue*);
        void mapBackgroundXPosition(BackgroundLayer*, CSSValue*);
        void mapBackgroundYPosition(BackgroundLayer*, CSSValue*);

187
188
189
190
        void mapTransitionDuration(Transition*, CSSValue*);
        void mapTransitionRepeatCount(Transition*, CSSValue*);
        void mapTransitionTimingFunction(Transition*, CSSValue*);
        void mapTransitionProperty(Transition*, CSSValue*);
191

darin@apple.com's avatar
darin@apple.com committed
192
193
194
195
196
        void applyProperty(int id, CSSValue*);
#if ENABLE(SVG)
        void applySVGProperty(int id, CSSValue*);
#endif

197
        PassRefPtr<StyleImage> createStyleImage(CSSValue* value);
198
        
darin@apple.com's avatar
darin@apple.com committed
199
200
        PseudoState checkPseudoState(Element*, bool checkVisited = true);

201
202
203
204
        // We collect the set of decls that match in |m_matchedDecls|.  We then walk the
        // set of matched decls four times, once for those properties that others depend on (like font-size),
        // and then a second time for all the remaining properties.  We then do the same two passes
        // for any !important rules.
205
        Vector<CSSMutableStyleDeclaration*> m_matchedDecls;
206

207
208
        // A buffer used to hold the set of matched rules for an element, and a temporary buffer used for
        // merge sorting.
209
        Vector<CSSRuleData*> m_matchedRules;
210

darin's avatar
darin committed
211
        CSSRuleList* m_ruleList;
212
213
        bool m_collectRulesOnly;

214
215
        MediaQueryEvaluator* m_medium;
        RenderStyle* m_rootDefaultStyle;
mjs's avatar
mjs committed
216

darin's avatar
darin committed
217
        RenderStyle::PseudoId dynamicPseudo;
218

ggaren's avatar
ggaren committed
219
        Document* m_document; // back pointer to owner document
220
221
222
223
224
225
        RenderStyle* m_style;
        RenderStyle* m_parentStyle;
        Element* m_element;
        StyledElement* m_styledElement;
        Node* m_parentNode;
        RenderStyle::PseudoId m_pseudoStyle;
rwlbuis's avatar
rwlbuis committed
226
        CSSValue* m_lineHeightValue;
227
228
        bool m_fontDirty;
        bool m_isXMLDoc;
timothy@apple.com's avatar
timothy@apple.com committed
229
230
        bool m_matchAuthorAndUserStyles;

231
232
        RefPtr<CSSFontSelector> m_fontSelector;
        HashSet<AtomicStringImpl*> m_selectorAttrs;
hyatt@apple.com's avatar
hyatt@apple.com committed
233
        Vector<CSSMutableStyleDeclaration*> m_additionalAttributeStyleDecls;
234
235
        Vector<MediaQueryResult*> m_viewportDependentMediaQueryResults;

darin@apple.com's avatar
darin@apple.com committed
236
        HashSet<unsigned, AlreadyHashed> m_linksCheckedForVisitedState;
kocienda's avatar
kocienda committed
237
238
    };

mjs's avatar
mjs committed
239
    class CSSRuleData {
240
    public:
darin's avatar
darin committed
241
        CSSRuleData(unsigned pos, CSSStyleRule* r, CSSSelector* sel, CSSRuleData* prev = 0)
242
243
244
245
246
247
248
249
250
            : m_position(pos)
            , m_rule(r)
            , m_selector(sel)
            , m_next(0)
        {
            if (prev)
                prev->m_next = this;
        }

251
252
        ~CSSRuleData() { delete m_next; }

darin's avatar
darin committed
253
254
        unsigned position() { return m_position; }
        CSSStyleRule* rule() { return m_rule; }
darin's avatar
darin committed
255
        CSSSelector* selector() { return m_selector; }
256
        CSSRuleData* next() { return m_next; }
257

258
    private:
darin's avatar
darin committed
259
260
        unsigned m_position;
        CSSStyleRule* m_rule;
darin's avatar
darin committed
261
        CSSSelector* m_selector;
262
        CSSRuleData* m_next;
kocienda's avatar
kocienda committed
263
264
    };

mjs's avatar
mjs committed
265
    class CSSRuleDataList {
kocienda's avatar
kocienda committed
266
    public:
darin's avatar
darin committed
267
        CSSRuleDataList(unsigned pos, CSSStyleRule* rule, CSSSelector* sel)
268
269
270
271
272
            : m_first(new CSSRuleData(pos, rule, sel))
            , m_last(m_first)
        {
        }

273
        ~CSSRuleDataList() { delete m_first; }
kocienda's avatar
kocienda committed
274

275
276
        CSSRuleData* first() { return m_first; }
        CSSRuleData* last() { return m_last; }
277
278
279

        void append(unsigned pos, CSSStyleRule* rule, CSSSelector* sel) { m_last = new CSSRuleData(pos, rule, sel, m_last); }

280
281
282
283
284
    private:
        CSSRuleData* m_first;
        CSSRuleData* m_last;
    };
    
285
} // namespace WebCore
darin's avatar
darin committed
286

weinig's avatar
weinig committed
287
#endif // CSSStyleSelector_h