]> git.saurik.com Git - iphone-api.git/blob - WebCore/CSSStyleSelector.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / CSSStyleSelector.h
1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 #ifndef CSSStyleSelector_h
23 #define CSSStyleSelector_h
24
25 #include "CSSFontSelector.h"
26 #include "KeyframeList.h"
27 #include "LinkHash.h"
28 #include "MediaQueryExp.h"
29 #include "RenderStyle.h"
30 #include "StringHash.h"
31 #include <wtf/HashMap.h>
32 #include <wtf/HashSet.h>
33 #include <wtf/RefPtr.h>
34 #include <wtf/Vector.h>
35
36 namespace WebCore {
37
38 class CSSMutableStyleDeclaration;
39 class CSSPrimitiveValue;
40 class CSSProperty;
41 class CSSFontFace;
42 class CSSFontFaceRule;
43 class CSSRuleData;
44 class CSSRuleDataList;
45 class CSSRuleList;
46 class CSSRuleSet;
47 class CSSSelector;
48 class CSSStyleRule;
49 class CSSStyleSheet;
50 class CSSValue;
51 class CSSVariableDependentValue;
52 class CSSVariablesRule;
53 class Document;
54 class Element;
55 class Frame;
56 class FrameView;
57 class KURL;
58 class MediaQueryEvaluator;
59 class Node;
60 class Settings;
61 class StyleImage;
62 class StyleSheet;
63 class StyleSheetList;
64 class StyledElement;
65 class WebKitCSSKeyframesRule;
66
67 class MediaQueryResult {
68 public:
69 MediaQueryResult(const MediaQueryExp& expr, bool result)
70 : m_expression(expr)
71 , m_result(result)
72 {
73 }
74
75 MediaQueryExp m_expression;
76 bool m_result;
77 };
78
79 // This class selects a RenderStyle for a given element based on a collection of stylesheets.
80 class CSSStyleSelector : Noncopyable {
81 public:
82 CSSStyleSelector(Document*, const String& userStyleSheet, StyleSheetList*, CSSStyleSheet*, bool strictParsing, bool matchAuthorAndUserStyles);
83 ~CSSStyleSelector();
84
85 void initElementAndPseudoState(Element*);
86 void initForStyleResolve(Element*, RenderStyle* parentStyle = 0, RenderStyle::PseudoId = RenderStyle::NOPSEUDO);
87 PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, bool allowSharing = true, bool resolveForRootDefault = false);
88 void keyframeStylesForAnimation(Element*, const RenderStyle*, KeyframeList& list);
89
90 PassRefPtr<RenderStyle> pseudoStyleForElement(RenderStyle::PseudoId, Element*, RenderStyle* parentStyle = 0);
91
92 private:
93 RenderStyle* locateSharedStyle();
94 Node* locateCousinList(Element* parent, unsigned depth = 1);
95 bool canShareStyleWithElement(Node*);
96
97 RenderStyle* style() const { return m_style.get(); }
98
99 public:
100 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
101 PassRefPtr<CSSRuleList> styleRulesForElement(Element*, bool authorOnly);
102 PassRefPtr<CSSRuleList> pseudoStyleRulesForElement(Element*, const String& pseudoStyle, bool authorOnly);
103
104 // Given a CSS keyword in the range (xx-small to -webkit-xxx-large), this function will return
105 // the correct font size scaled relative to the user's default (medium).
106 float fontSizeForKeyword(int keyword, bool quirksMode, bool monospace) const;
107
108 private:
109 // When the CSS keyword "larger" is used, this function will attempt to match within the keyword
110 // table, and failing that, will simply multiply by 1.2.
111 float largerFontSize(float size, bool quirksMode) const;
112
113 // Like the previous function, but for the keyword "smaller".
114 float smallerFontSize(float size, bool quirksMode) const;
115
116 public:
117 void setStyle(PassRefPtr<RenderStyle> s) { m_style = s; } // Used by the document when setting up its root style.
118 void setFontSize(FontDescription&, float size);
119
120 void applyPropertyToStyle(int id, CSSValue*, RenderStyle*);
121
122 private:
123 float getComputedSizeFromSpecifiedSize(bool isAbsoluteSize, float specifiedSize);
124
125 public:
126 Color getColorFromPrimitiveValue(CSSPrimitiveValue*);
127
128 bool hasSelectorForAttribute(const AtomicString&);
129
130 CSSFontSelector* fontSelector() { return m_fontSelector.get(); }
131
132 // Checks if a compound selector (which can consist of multiple simple selectors) matches the current element.
133 bool checkSelector(CSSSelector*);
134
135 void addViewportDependentMediaQueryResult(const MediaQueryExp*, bool result);
136
137 bool affectedByViewportChange() const;
138
139 void allVisitedStateChanged() { m_checker.allVisitedStateChanged(); }
140 void visitedStateChanged(LinkHash visitedHash) { m_checker.visitedStateChanged(visitedHash); }
141
142 void addVariables(CSSVariablesRule* variables);
143 CSSValue* resolveVariableDependentValue(CSSVariableDependentValue*);
144 void resolveVariablesForDeclaration(CSSMutableStyleDeclaration* decl, CSSMutableStyleDeclaration* newDecl, HashSet<String>& usedBlockVariables);
145
146 void addKeyframeStyle(PassRefPtr<WebKitCSSKeyframesRule> rule);
147
148 static bool createTransformOperations(CSSValue* inValue, RenderStyle* inStyle, TransformOperations& outOperations);
149
150 private:
151 enum SelectorMatch { SelectorMatches, SelectorFailsLocally, SelectorFailsCompletely };
152
153 // This function fixes up the default font size if it detects that the current generic font family has changed. -dwh
154 void checkForGenericFamilyChange(RenderStyle*, RenderStyle* parentStyle);
155 void checkForZoomChange(RenderStyle*, RenderStyle* parentStyle);
156 void checkForTextSizeAdjust();
157
158 void adjustRenderStyle(RenderStyle*, Element*);
159
160 void addMatchedRule(CSSRuleData* rule) { m_matchedRules.append(rule); }
161 void addMatchedDeclaration(CSSMutableStyleDeclaration* decl);
162
163 void matchRules(CSSRuleSet*, int& firstRuleIndex, int& lastRuleIndex);
164 void matchRulesForList(CSSRuleDataList*, int& firstRuleIndex, int& lastRuleIndex);
165 void sortMatchedRules(unsigned start, unsigned end);
166
167 void applyDeclarations(bool firstPass, bool important, int startIndex, int endIndex);
168
169 CSSRuleSet* m_authorStyle;
170 CSSRuleSet* m_userStyle;
171 RefPtr<CSSStyleSheet> m_userSheet;
172
173 bool m_hasUAAppearance;
174 BorderData m_borderData;
175 FillLayer m_backgroundData;
176 Color m_backgroundColor;
177
178 typedef HashMap<AtomicStringImpl*, RefPtr<WebKitCSSKeyframesRule> > KeyframesRuleMap;
179 KeyframesRuleMap m_keyframesRuleMap;
180
181 public:
182 static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
183
184 class SelectorChecker : public Noncopyable {
185 public:
186 SelectorChecker(Document*, bool strictParsing);
187
188 bool checkSelector(CSSSelector*, Element*) const;
189 SelectorMatch checkSelector(CSSSelector*, Element*, HashSet<AtomicStringImpl*>* selectorAttrs, RenderStyle::PseudoId& dynamicPseudo, bool isAncestor, bool isSubSelector, RenderStyle* = 0, RenderStyle* elementParentStyle = 0) const;
190 bool checkOneSelector(CSSSelector*, Element*, HashSet<AtomicStringImpl*>* selectorAttrs, RenderStyle::PseudoId& dynamicPseudo, bool isAncestor, bool isSubSelector, RenderStyle*, RenderStyle* elementParentStyle) const;
191 PseudoState checkPseudoState(Element*, bool checkVisited = true) const;
192 bool checkScrollbarPseudoClass(CSSSelector*, RenderStyle::PseudoId& dynamicPseudo) const;
193
194 void allVisitedStateChanged();
195 void visitedStateChanged(LinkHash visitedHash);
196
197 Document* m_document;
198 bool m_strictParsing;
199 bool m_collectRulesOnly;
200 RenderStyle::PseudoId m_pseudoStyle;
201 bool m_documentIsHTML;
202 mutable HashSet<LinkHash, LinkHashHash> m_linksCheckedForVisitedState;
203 };
204
205 private:
206 static RenderStyle* s_styleNotYetAvailable;
207
208 void init();
209
210 void matchUARules(int& firstUARule, int& lastUARule);
211 void updateFont();
212 void cacheBorderAndBackground();
213
214 void mapFillAttachment(FillLayer*, CSSValue*);
215 void mapFillClip(FillLayer*, CSSValue*);
216 void mapFillComposite(FillLayer*, CSSValue*);
217 void mapFillOrigin(FillLayer*, CSSValue*);
218 void mapFillImage(FillLayer*, CSSValue*);
219 void mapFillRepeat(FillLayer*, CSSValue*);
220 void mapFillSize(FillLayer*, CSSValue*);
221 void mapFillXPosition(FillLayer*, CSSValue*);
222 void mapFillYPosition(FillLayer*, CSSValue*);
223
224 void mapAnimationDelay(Animation*, CSSValue*);
225 void mapAnimationDirection(Animation*, CSSValue*);
226 void mapAnimationDuration(Animation*, CSSValue*);
227 void mapAnimationIterationCount(Animation*, CSSValue*);
228 void mapAnimationName(Animation*, CSSValue*);
229 void mapAnimationPlayState(Animation*, CSSValue*);
230 void mapAnimationProperty(Animation*, CSSValue*);
231 void mapAnimationTimingFunction(Animation*, CSSValue*);
232
233 void mapNinePieceImage(CSSValue*, NinePieceImage&);
234
235 void applyProperty(int id, CSSValue*);
236 #if ENABLE(SVG)
237 void applySVGProperty(int id, CSSValue*);
238 #endif
239
240 StyleImage* styleImage(CSSValue* value);
241
242 // We collect the set of decls that match in |m_matchedDecls|. We then walk the
243 // set of matched decls four times, once for those properties that others depend on (like font-size),
244 // and then a second time for all the remaining properties. We then do the same two passes
245 // for any !important rules.
246 Vector<CSSMutableStyleDeclaration*, 64> m_matchedDecls;
247
248 // A buffer used to hold the set of matched rules for an element, and a temporary buffer used for
249 // merge sorting.
250 Vector<CSSRuleData*, 32> m_matchedRules;
251
252 RefPtr<CSSRuleList> m_ruleList;
253
254 MediaQueryEvaluator* m_medium;
255 RefPtr<RenderStyle> m_rootDefaultStyle;
256
257 RenderStyle::PseudoId m_dynamicPseudo;
258
259 SelectorChecker m_checker;
260
261 RefPtr<RenderStyle> m_style;
262 RenderStyle* m_parentStyle;
263 Element* m_element;
264 StyledElement* m_styledElement;
265 Node* m_parentNode;
266 CSSValue* m_lineHeightValue;
267 bool m_fontDirty;
268 bool m_matchAuthorAndUserStyles;
269
270 RefPtr<CSSFontSelector> m_fontSelector;
271 HashSet<AtomicStringImpl*> m_selectorAttrs;
272 Vector<CSSMutableStyleDeclaration*> m_additionalAttributeStyleDecls;
273 Vector<MediaQueryResult*> m_viewportDependentMediaQueryResults;
274
275 HashMap<String, CSSVariablesRule*> m_variablesMap;
276 HashMap<CSSMutableStyleDeclaration*, RefPtr<CSSMutableStyleDeclaration> > m_resolvedVariablesDeclarations;
277 };
278
279 class CSSRuleData {
280 public:
281 CSSRuleData(unsigned pos, CSSStyleRule* r, CSSSelector* sel, CSSRuleData* prev = 0)
282 : m_position(pos)
283 , m_rule(r)
284 , m_selector(sel)
285 , m_next(0)
286 {
287 if (prev)
288 prev->m_next = this;
289 }
290
291 ~CSSRuleData() { delete m_next; }
292
293 unsigned position() { return m_position; }
294 CSSStyleRule* rule() { return m_rule; }
295 CSSSelector* selector() { return m_selector; }
296 CSSRuleData* next() { return m_next; }
297
298 private:
299 unsigned m_position;
300 CSSStyleRule* m_rule;
301 CSSSelector* m_selector;
302 CSSRuleData* m_next;
303 };
304
305 class CSSRuleDataList {
306 public:
307 CSSRuleDataList(unsigned pos, CSSStyleRule* rule, CSSSelector* sel)
308 : m_first(new CSSRuleData(pos, rule, sel))
309 , m_last(m_first)
310 {
311 }
312
313 ~CSSRuleDataList() { delete m_first; }
314
315 CSSRuleData* first() { return m_first; }
316 CSSRuleData* last() { return m_last; }
317
318 void append(unsigned pos, CSSStyleRule* rule, CSSSelector* sel) { m_last = new CSSRuleData(pos, rule, sel, m_last); }
319
320 private:
321 CSSRuleData* m_first;
322 CSSRuleData* m_last;
323 };
324
325 } // namespace WebCore
326
327 #endif // CSSStyleSelector_h