2 * This file is part of the CSS implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef CSSStyleSelector_h
25 #define CSSStyleSelector_h
27 #include "CSSFontSelector.h"
28 #include "DeprecatedString.h"
29 #include "RenderStyle.h"
30 #include <wtf/HashSet.h>
31 #include <wtf/Vector.h>
32 #include <wtf/RefPtr.h>
36 class CSSMutableStyleDeclaration;
37 class CSSPrimitiveValue;
40 class CSSFontFaceRule;
42 class CSSRuleDataList;
54 class MediaQueryEvaluator;
62 * this class selects a RenderStyle for a given Element based on the
63 * collection of styleshets it contains. This is just a vrtual base class
64 * for specific implementations of the Selector. At the moment only CSSStyleSelector
65 * exists, but someone may wish to implement XSL.
80 * the StyleSelector implementation for CSS.
82 class CSSStyleSelector : public StyleSelector
85 CSSStyleSelector(Document*, const String& userStyleSheet, StyleSheetList *styleSheets, CSSStyleSheet* mappedElementsSheet, bool strictParsing, bool matchAuthorAndUserStyles);
88 static void loadDefaultStyle();
90 void initElementAndPseudoState(Element* e);
91 void initForStyleResolve(Element* e, RenderStyle* parentStyle);
92 RenderStyle *styleForElement(Element*, RenderStyle* parentStyle=0, bool allowSharing=true, bool resolveForRootDefault=false);
93 RenderStyle* pseudoStyleForElement(RenderStyle::PseudoId, Element*, RenderStyle* parentStyle=0);
95 RenderStyle* locateSharedStyle();
96 Node* locateCousinList(Element* parent, unsigned depth = 1);
97 bool canShareStyleWithElement(Node* n);
99 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
100 RefPtr<CSSRuleList> styleRulesForElement(Element* e, bool authorOnly);
101 RefPtr<CSSRuleList> pseudoStyleRulesForElement(Element* e, StringImpl* pseudoStyle, bool authorOnly);
106 DeprecatedString host; //also contains protocol
107 DeprecatedString path;
108 DeprecatedString file;
110 void setEncodedURL(const KURL& url);
112 // Given a CSS keyword in the range (xx-small to -webkit-xxx-large), this function will return
113 // the correct font size scaled relative to the user's default (medium).
114 float fontSizeForKeyword(int keyword, bool quirksMode, bool monospace) const;
116 // When the CSS keyword "larger" is used, this function will attempt to match within the keyword
117 // table, and failing that, will simply multiply by 1.2.
118 float largerFontSize(float size, bool quirksMode) const;
120 // Like the previous function, but for the keyword "smaller".
121 float smallerFontSize(float size, bool quirksMode) const;
123 void setFontSize(FontDescription& FontDescription, float size);
124 float getComputedSizeFromSpecifiedSize(bool isAbsoluteSize, float specifiedSize);
126 Color getColorFromPrimitiveValue(CSSPrimitiveValue* primitiveValue);
128 bool hasSelectorForAttribute(const AtomicString &attrname);
130 CSSFontSelector* fontSelector() { return m_fontSelector.get(); }
131 CSSFontSelector* ensureFontSelector();
135 /* checks if a compound selector (which can consist of multiple simple selectors)
136 matches the given Element */
137 bool checkSelector(CSSSelector* selector);
138 enum SelectorMatch { SelectorMatches=0, SelectorFailsLocally, SelectorFailsCompletely};
139 SelectorMatch checkSelector(CSSSelector* selector, Element *e, bool isAncestor, bool isSubSelector);
141 /* checks if the selector matches the given Element */
142 bool checkOneSelector(CSSSelector*, Element*, bool isAncestor, bool isSubSelector = false);
144 /* This function fixes up the default font size if it detects that the
145 current generic font family has changed. -dwh */
146 void checkForGenericFamilyChange(RenderStyle* aStyle, RenderStyle* aParentStyle);
147 void checkForTextSizeAdjust();
149 void adjustRenderStyle(RenderStyle* style, Element *e);
151 void addMatchedRule(CSSRuleData* rule) { m_matchedRules.append(rule); }
152 void addMatchedDeclaration(CSSMutableStyleDeclaration* decl) { m_matchedDecls.append(decl); }
154 void matchRules(CSSRuleSet* rules, int& firstRuleIndex, int& lastRuleIndex);
155 void matchRulesForList(CSSRuleDataList* rules, int& firstRuleIndex, int& lastRuleIndex);
156 void sortMatchedRules(unsigned start, unsigned end);
158 void applyDeclarations(bool firstPass, bool important, int startIndex, int endIndex);
160 static CSSStyleSheet* defaultSheet;
161 static CSSStyleSheet* quirksSheet;
162 static CSSStyleSheet* viewSourceSheet;
164 static CSSStyleSheet* svgSheet;
167 static CSSRuleSet* defaultStyle;
168 static CSSRuleSet* defaultQuirksStyle;
169 static CSSRuleSet* defaultPrintStyle;
170 static CSSRuleSet* defaultViewSourceStyle;
172 CSSRuleSet* m_authorStyle;
173 CSSRuleSet* m_userStyle;
174 RefPtr<CSSStyleSheet> m_userSheet;
176 bool m_hasUAAppearance;
177 BorderData m_borderData;
178 BackgroundLayer m_backgroundData;
179 Color m_backgroundColor;
182 static RenderStyle* styleNotYetAvailable;
187 void matchUARules(int& firstUARule, int& lastUARule);
189 void cacheBorderAndBackground();
191 void mapBackgroundAttachment(BackgroundLayer* layer, CSSValue* value);
192 void mapBackgroundClip(BackgroundLayer* layer, CSSValue* value);
193 void mapBackgroundComposite(BackgroundLayer* layer, CSSValue* value);
194 void mapBackgroundOrigin(BackgroundLayer* layer, CSSValue* value);
195 void mapBackgroundImage(BackgroundLayer* layer, CSSValue* value);
196 void mapBackgroundRepeat(BackgroundLayer* layer, CSSValue* value);
197 void mapBackgroundSize(BackgroundLayer* layer, CSSValue* value);
198 void mapBackgroundXPosition(BackgroundLayer* layer, CSSValue* value);
199 void mapBackgroundYPosition(BackgroundLayer* layer, CSSValue* value);
201 void mapTransitionDuration(Transition*, CSSValue*);
202 void mapTransitionRepeatCount(Transition*, CSSValue*);
203 void mapTransitionTimingFunction(Transition*, CSSValue*);
204 void mapTransitionProperty(Transition*, CSSValue*);
206 // We collect the set of decls that match in |m_matchedDecls|. We then walk the
207 // set of matched decls four times, once for those properties that others depend on (like font-size),
208 // and then a second time for all the remaining properties. We then do the same two passes
209 // for any !important rules.
210 Vector<CSSMutableStyleDeclaration*> m_matchedDecls;
212 // A buffer used to hold the set of matched rules for an element, and a temporary buffer used for
214 Vector<CSSRuleData*> m_matchedRules;
216 CSSRuleList* m_ruleList;
217 bool m_collectRulesOnly;
219 MediaQueryEvaluator* m_medium;
220 RenderStyle* m_rootDefaultStyle;
222 RenderStyle::PseudoId dynamicPseudo;
224 Document* m_document; // back pointer to owner document
226 RenderStyle *parentStyle;
228 StyledElement *styledElement;
230 RenderStyle::PseudoId pseudoStyle;
231 CSSValue* m_lineHeightValue;
234 bool m_matchAuthorAndUserStyles;
236 RefPtr<CSSFontSelector> m_fontSelector;
238 HashSet<AtomicStringImpl*> m_selectorAttrs;
240 void applyProperty(int id, CSSValue *value);
242 void applySVGProperty(int id, CSSValue *value);
245 friend class CSSRuleSet;
250 CSSRuleData(unsigned pos, CSSStyleRule* r, CSSSelector* sel, CSSRuleData* prev = 0)
251 :m_position(pos), m_rule(r), m_selector(sel), m_next(0) { if (prev) prev->m_next = this; }
252 ~CSSRuleData() { delete m_next; }
254 unsigned position() { return m_position; }
255 CSSStyleRule* rule() { return m_rule; }
256 CSSSelector* selector() { return m_selector; }
257 CSSRuleData* next() { return m_next; }
261 CSSStyleRule* m_rule;
262 CSSSelector* m_selector;
266 class CSSRuleDataList {
268 CSSRuleDataList(unsigned pos, CSSStyleRule* rule, CSSSelector* sel)
269 { m_first = m_last = new CSSRuleData(pos, rule, sel); }
270 ~CSSRuleDataList() { delete m_first; }
272 CSSRuleData* first() { return m_first; }
273 CSSRuleData* last() { return m_last; }
275 void append(unsigned pos, CSSStyleRule* rule, CSSSelector* sel) {
276 m_last = new CSSRuleData(pos, rule, sel, m_last);
280 CSSRuleData* m_first;
286 #endif // CSSStyleSelector_h