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(); }
134 /* checks if a compound selector (which can consist of multiple simple selectors)
135 matches the given Element */
136 bool checkSelector(CSSSelector* selector);
137 enum SelectorMatch { SelectorMatches=0, SelectorFailsLocally, SelectorFailsCompletely};
138 SelectorMatch checkSelector(CSSSelector* selector, Element *e, bool isAncestor, bool isSubSelector);
140 /* checks if the selector matches the given Element */
141 bool checkOneSelector(CSSSelector*, Element*, bool isAncestor, bool isSubSelector = false);
143 /* This function fixes up the default font size if it detects that the
144 current generic font family has changed. -dwh */
145 void checkForGenericFamilyChange(RenderStyle* aStyle, RenderStyle* aParentStyle);
146 void checkForTextSizeAdjust();
148 void adjustRenderStyle(RenderStyle* style, Element *e);
150 void addMatchedRule(CSSRuleData* rule) { m_matchedRules.append(rule); }
151 void addMatchedDeclaration(CSSMutableStyleDeclaration* decl) { m_matchedDecls.append(decl); }
153 void matchRules(CSSRuleSet* rules, int& firstRuleIndex, int& lastRuleIndex);
154 void matchRulesForList(CSSRuleDataList* rules, int& firstRuleIndex, int& lastRuleIndex);
155 void sortMatchedRules(unsigned start, unsigned end);
157 void applyDeclarations(bool firstPass, bool important, int startIndex, int endIndex);
159 static CSSStyleSheet* defaultSheet;
160 static CSSStyleSheet* quirksSheet;
161 static CSSStyleSheet* viewSourceSheet;
163 static CSSStyleSheet* svgSheet;
166 static CSSRuleSet* defaultStyle;
167 static CSSRuleSet* defaultQuirksStyle;
168 static CSSRuleSet* defaultPrintStyle;
169 static CSSRuleSet* defaultViewSourceStyle;
171 CSSRuleSet* m_authorStyle;
172 CSSRuleSet* m_userStyle;
173 RefPtr<CSSStyleSheet> m_userSheet;
175 bool m_hasUAAppearance;
176 BorderData m_borderData;
177 BackgroundLayer m_backgroundData;
178 Color m_backgroundColor;
181 static RenderStyle* styleNotYetAvailable;
186 void matchUARules(int& firstUARule, int& lastUARule);
188 void cacheBorderAndBackground();
190 void mapBackgroundAttachment(BackgroundLayer* layer, CSSValue* value);
191 void mapBackgroundClip(BackgroundLayer* layer, CSSValue* value);
192 void mapBackgroundComposite(BackgroundLayer* layer, CSSValue* value);
193 void mapBackgroundOrigin(BackgroundLayer* layer, CSSValue* value);
194 void mapBackgroundImage(BackgroundLayer* layer, CSSValue* value);
195 void mapBackgroundRepeat(BackgroundLayer* layer, CSSValue* value);
196 void mapBackgroundSize(BackgroundLayer* layer, CSSValue* value);
197 void mapBackgroundXPosition(BackgroundLayer* layer, CSSValue* value);
198 void mapBackgroundYPosition(BackgroundLayer* layer, CSSValue* value);
200 void mapTransitionDuration(Transition*, CSSValue*);
201 void mapTransitionRepeatCount(Transition*, CSSValue*);
202 void mapTransitionTimingFunction(Transition*, CSSValue*);
203 void mapTransitionProperty(Transition*, CSSValue*);
205 // We collect the set of decls that match in |m_matchedDecls|. We then walk the
206 // set of matched decls four times, once for those properties that others depend on (like font-size),
207 // and then a second time for all the remaining properties. We then do the same two passes
208 // for any !important rules.
209 Vector<CSSMutableStyleDeclaration*> m_matchedDecls;
211 // A buffer used to hold the set of matched rules for an element, and a temporary buffer used for
213 Vector<CSSRuleData*> m_matchedRules;
215 CSSRuleList* m_ruleList;
216 bool m_collectRulesOnly;
218 MediaQueryEvaluator* m_medium;
219 RenderStyle* m_rootDefaultStyle;
221 RenderStyle::PseudoId dynamicPseudo;
223 Document* m_document; // back pointer to owner document
225 RenderStyle *parentStyle;
227 StyledElement *styledElement;
229 RenderStyle::PseudoId pseudoStyle;
230 CSSValue* m_lineHeightValue;
233 bool m_matchAuthorAndUserStyles;
235 RefPtr<CSSFontSelector> m_fontSelector;
237 HashSet<AtomicStringImpl*> m_selectorAttrs;
239 void applyProperty(int id, CSSValue *value);
241 void applySVGProperty(int id, CSSValue *value);
244 friend class CSSRuleSet;
249 CSSRuleData(unsigned pos, CSSStyleRule* r, CSSSelector* sel, CSSRuleData* prev = 0)
250 :m_position(pos), m_rule(r), m_selector(sel), m_next(0) { if (prev) prev->m_next = this; }
251 ~CSSRuleData() { delete m_next; }
253 unsigned position() { return m_position; }
254 CSSStyleRule* rule() { return m_rule; }
255 CSSSelector* selector() { return m_selector; }
256 CSSRuleData* next() { return m_next; }
260 CSSStyleRule* m_rule;
261 CSSSelector* m_selector;
265 class CSSRuleDataList {
267 CSSRuleDataList(unsigned pos, CSSStyleRule* rule, CSSSelector* sel)
268 { m_first = m_last = new CSSRuleData(pos, rule, sel); }
269 ~CSSRuleDataList() { delete m_first; }
271 CSSRuleData* first() { return m_first; }
272 CSSRuleData* last() { return m_last; }
274 void append(unsigned pos, CSSStyleRule* rule, CSSSelector* sel) {
275 m_last = new CSSRuleData(pos, rule, sel, m_last);
279 CSSRuleData* m_first;
285 #endif // CSSStyleSelector_h