1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* 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
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#ifndef CSSStyleSelector_h
#define CSSStyleSelector_h
#include "CSSFontSelector.h"
#include "MediaQueryExp.h"
#include "RenderStyle.h"
#include <wtf/HashSet.h>
#include <wtf/Vector.h>
#include <wtf/RefPtr.h>
namespace WebCore {
class CSSMutableStyleDeclaration;
class CSSPrimitiveValue;
class CSSProperty;
class CSSFontFace;
class CSSFontFaceRule;
class CSSRuleData;
class CSSRuleDataList;
class CSSRuleList;
class CSSRuleSet;
class CSSSelector;
class CSSStyleRule;
class CSSStyleSheet;
class CSSValue;
class Document;
class Element;
class Frame;
class FrameView;
class KURL;
class MediaQueryEvaluator;
class Node;
class Settings;
class StyleSheet;
class StyleSheetList;
class StyledElement;
class MediaQueryResult
{
public:
MediaQueryResult(const MediaQueryExp& expr, bool result)
: m_expression(expr)
, m_result(result)
{}
MediaQueryExp m_expression;
bool m_result;
};
/**
* this class selects a RenderStyle for a given Element based on the
* collection of styleshets it contains. This is just a vrtual base class
* for specific implementations of the Selector. At the moment only CSSStyleSelector
* exists, but someone may wish to implement XSL.
*/
class StyleSelector {
public:
enum State {
None = 0x00,
Hover = 0x01,
Focus = 0x02,
Active = 0x04,
Drag = 0x08
};
};
/**
* the StyleSelector implementation for CSS.
*/
class CSSStyleSelector : public StyleSelector {
public:
CSSStyleSelector(Document*, const String& userStyleSheet, StyleSheetList*, CSSStyleSheet*, bool strictParsing, bool matchAuthorAndUserStyles);
~CSSStyleSelector();
static void loadDefaultStyle();
void initElementAndPseudoState(Element*);
void initForStyleResolve(Element*, RenderStyle* parentStyle);
RenderStyle* styleForElement(Element*, RenderStyle* parentStyle = 0, bool allowSharing = true, bool resolveForRootDefault = false);
RenderStyle* pseudoStyleForElement(RenderStyle::PseudoId, Element*, RenderStyle* parentStyle = 0);
RenderStyle* locateSharedStyle();
Node* locateCousinList(Element* parent, unsigned depth = 1);
bool canShareStyleWithElement(Node* n);
// These methods will give back the set of rules that matched for a given element (or a pseudo-element).
RefPtr<CSSRuleList> styleRulesForElement(Element*, bool authorOnly);
RefPtr<CSSRuleList> pseudoStyleRulesForElement(Element*, StringImpl* pseudoStyle, bool authorOnly);
bool strictParsing;
struct EncodedURL {
String prefix; // protocol, host, etc.
String path;
String file;
} m_encodedURL;
void setEncodedURL(const KURL& url);
// Given a CSS keyword in the range (xx-small to -webkit-xxx-large), this function will return
// the correct font size scaled relative to the user's default (medium).
float fontSizeForKeyword(int keyword, bool quirksMode, bool monospace) const;
// 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;
// Like the previous function, but for the keyword "smaller".
float smallerFontSize(float size, bool quirksMode) const;
void setFontSize(FontDescription&, float size);
float getComputedSizeFromSpecifiedSize(bool isAbsoluteSize, float specifiedSize);
Color getColorFromPrimitiveValue(CSSPrimitiveValue*);
bool hasSelectorForAttribute(const AtomicString&);
CSSFontSelector* fontSelector() { return m_fontSelector.get(); }
/* checks if a compound selector (which can consist of multiple simple selectors)
matches the given Element */
bool checkSelector(CSSSelector*);
void addViewportDependentMediaQueryResult(const MediaQueryExp*, bool result);
bool affectedByViewportChange() const;
protected:
enum SelectorMatch {
SelectorMatches = 0,
SelectorFailsLocally,
SelectorFailsCompletely
};
SelectorMatch checkSelector(CSSSelector*, Element *, bool isAncestor, bool isSubSelector);
/* checks if the selector matches the given Element */
bool checkOneSelector(CSSSelector*, Element*, bool isAncestor, bool isSubSelector = false);
/* This function fixes up the default font size if it detects that the
current generic font family has changed. -dwh */
void checkForGenericFamilyChange(RenderStyle* style, RenderStyle* parentStyle);
void checkForTextSizeAdjust();
void adjustRenderStyle(RenderStyle*, Element*);
void addMatchedRule(CSSRuleData* rule) { m_matchedRules.append(rule); }
void addMatchedDeclaration(CSSMutableStyleDeclaration* decl) { m_matchedDecls.append(decl); }
void matchRules(CSSRuleSet*, int& firstRuleIndex, int& lastRuleIndex);
void matchRulesForList(CSSRuleDataList* rules, int& firstRuleIndex, int& lastRuleIndex);
void sortMatchedRules(unsigned start, unsigned end);
void applyDeclarations(bool firstPass, bool important, int startIndex, int endIndex);
static CSSStyleSheet* m_defaultSheet;
static CSSStyleSheet* m_quirksSheet;
static CSSStyleSheet* m_viewSourceSheet;
#if ENABLE(SVG)
static CSSStyleSheet* m_svgSheet;
#endif
static CSSRuleSet* m_defaultStyle;
static CSSRuleSet* m_defaultQuirksStyle;
static CSSRuleSet* m_defaultPrintStyle;
static CSSRuleSet* m_defaultViewSourceStyle;
CSSRuleSet* m_authorStyle;
CSSRuleSet* m_userStyle;
RefPtr<CSSStyleSheet> m_userSheet;
bool m_hasUAAppearance;
BorderData m_borderData;
BackgroundLayer m_backgroundData;
Color m_backgroundColor;
public:
static RenderStyle* m_styleNotYetAvailable;
private:
void init();
void matchUARules(int& firstUARule, int& lastUARule);
void updateFont();
void cacheBorderAndBackground();
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*);
void mapTransitionDuration(Transition*, CSSValue*);
void mapTransitionRepeatCount(Transition*, CSSValue*);
void mapTransitionTimingFunction(Transition*, CSSValue*);
void mapTransitionProperty(Transition*, CSSValue*);
// 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.
Vector<CSSMutableStyleDeclaration*> m_matchedDecls;
// A buffer used to hold the set of matched rules for an element, and a temporary buffer used for
// merge sorting.
Vector<CSSRuleData*> m_matchedRules;
CSSRuleList* m_ruleList;
bool m_collectRulesOnly;
MediaQueryEvaluator* m_medium;
RenderStyle* m_rootDefaultStyle;
RenderStyle::PseudoId dynamicPseudo;
Document* m_document; // back pointer to owner document
RenderStyle* m_style;
RenderStyle* m_parentStyle;
Element* m_element;
StyledElement* m_styledElement;
Node* m_parentNode;
RenderStyle::PseudoId m_pseudoStyle;
CSSValue* m_lineHeightValue;
bool m_fontDirty;
bool m_isXMLDoc;
bool m_matchAuthorAndUserStyles;
RefPtr<CSSFontSelector> m_fontSelector;
HashSet<AtomicStringImpl*> m_selectorAttrs;
Vector<CSSMutableStyleDeclaration*> m_additionalAttributeStyleDecls;
Vector<MediaQueryResult*> m_viewportDependentMediaQueryResults;
void applyProperty(int id, CSSValue*);
#if ENABLE(SVG)
void applySVGProperty(int id, CSSValue*);
#endif
friend class CSSRuleSet;
friend class Node;
};
class CSSRuleData {
public:
CSSRuleData(unsigned pos, CSSStyleRule* r, CSSSelector* sel, CSSRuleData* prev = 0)
: m_position(pos)
, m_rule(r)
, m_selector(sel)
, m_next(0)
{
if (prev)
prev->m_next = this;
}
~CSSRuleData() { delete m_next; }
unsigned position() { return m_position; }
CSSStyleRule* rule() { return m_rule; }
CSSSelector* selector() { return m_selector; }
CSSRuleData* next() { return m_next; }
private:
unsigned m_position;
CSSStyleRule* m_rule;
CSSSelector* m_selector;
CSSRuleData* m_next;
};
class CSSRuleDataList {
public:
CSSRuleDataList(unsigned pos, CSSStyleRule* rule, CSSSelector* sel)
: m_first(new CSSRuleData(pos, rule, sel))
, m_last(m_first)
{
}
~CSSRuleDataList() { delete m_first; }
CSSRuleData* first() { return m_first; }
CSSRuleData* last() { return m_last; }
void append(unsigned pos, CSSStyleRule* rule, CSSSelector* sel) { m_last = new CSSRuleData(pos, rule, sel, m_last); }
private:
CSSRuleData* m_first;
CSSRuleData* m_last;
};
} // namespace WebCore
#endif // CSSStyleSelector_h
|