2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
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.
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.
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.
22 #ifndef CSSStyleSelector_h
23 #define CSSStyleSelector_h
25 #include "CSSFontSelector.h"
28 #include "MediaQueryExp.h"
29 #include "RenderStyle.h"
30 #include <wtf/HashMap.h>
31 #include <wtf/HashSet.h>
32 #include <wtf/RefPtr.h>
33 #include <wtf/Vector.h>
34 #include <wtf/text/StringHash.h>
38 class CSSMutableStyleDeclaration;
40 class CSSPrimitiveValue;
43 class CSSFontFaceRule;
59 class MediaQueryEvaluator;
68 class WebKitCSSKeyframeRule;
69 class WebKitCSSKeyframesRule;
71 class MediaQueryResult {
72 WTF_MAKE_NONCOPYABLE(MediaQueryResult); WTF_MAKE_FAST_ALLOCATED;
74 MediaQueryResult(const MediaQueryExp& expr, bool result)
80 MediaQueryExp m_expression;
84 // This class selects a RenderStyle for a given element based on a collection of stylesheets.
85 class CSSStyleSelector {
86 WTF_MAKE_NONCOPYABLE(CSSStyleSelector); WTF_MAKE_FAST_ALLOCATED;
88 CSSStyleSelector(Document*, StyleSheetList* authorSheets, CSSStyleSheet* mappedElementSheet,
89 CSSStyleSheet* pageUserSheet, const Vector<RefPtr<CSSStyleSheet> >* pageGroupUserSheets,
90 bool strictParsing, bool matchAuthorAndUserStyles);
93 PassRefPtr<RenderStyle> styleForElement(Element* e, RenderStyle* parentStyle = 0, bool allowSharing = true, bool resolveForRootDefault = false, bool matchVisitedPseudoClass = false);
95 void keyframeStylesForAnimation(Element*, const RenderStyle*, KeyframeList& list);
97 PassRefPtr<RenderStyle> pseudoStyleForElement(PseudoId pseudo, Element* e, RenderStyle* parentStyle = 0, bool matchVisitedPseudoClass = false);
99 PassRefPtr<RenderStyle> styleForPage(int pageIndex);
101 static PassRefPtr<RenderStyle> styleForDocument(Document*);
104 // Datagrid style computation (uses unique pseudo elements and structures)
105 PassRefPtr<RenderStyle> pseudoStyleForDataGridColumn(DataGridColumn*, RenderStyle* parentStyle);
106 PassRefPtr<RenderStyle> pseudoStyleForDataGridColumnHeader(DataGridColumn*, RenderStyle* parentStyle);
110 void initForStyleResolve(Element*, RenderStyle* parentStyle = 0, PseudoId = NOPSEUDO);
111 void initElement(Element*);
112 RenderStyle* locateSharedStyle();
113 bool matchesSiblingRules();
114 Node* locateCousinList(Element* parent, unsigned depth = 1) const;
115 Node* findSiblingForStyleSharing(Node*, unsigned& count) const;
116 bool canShareStyleWithElement(Node*) const;
118 RenderStyle* style() const { return m_style.get(); }
120 PassRefPtr<RenderStyle> styleForKeyframe(const RenderStyle*, const WebKitCSSKeyframeRule*, KeyframeValue&);
123 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
124 PassRefPtr<CSSRuleList> styleRulesForElement(Element*, bool authorOnly, bool includeEmptyRules = false, CSSRuleFilter filter = AllCSSRules);
125 PassRefPtr<CSSRuleList> pseudoStyleRulesForElement(Element*, PseudoId, bool authorOnly, bool includeEmptyRules = false, CSSRuleFilter filter = AllCSSRules);
127 // Given a CSS keyword in the range (xx-small to -webkit-xxx-large), this function will return
128 // the correct font size scaled relative to the user's default (medium).
129 static float fontSizeForKeyword(Document*, int keyword, bool shouldUseFixedDefaultSize);
131 // Given a font size in pixel, this function will return legacy font size between 1 and 7.
132 static int legacyFontSize(Document*, int pixelFontSize, bool shouldUseFixedDefaultSize);
136 // When the CSS keyword "larger" is used, this function will attempt to match within the keyword
137 // table, and failing that, will simply multiply by 1.2.
138 float largerFontSize(float size, bool quirksMode) const;
140 // Like the previous function, but for the keyword "smaller".
141 float smallerFontSize(float size, bool quirksMode) const;
144 void setStyle(PassRefPtr<RenderStyle> s) { m_style = s; } // Used by the document when setting up its root style.
146 void applyPropertyToStyle(int id, CSSValue*, RenderStyle*);
149 void setFontSize(FontDescription&, float size);
150 static float getComputedSizeFromSpecifiedSize(Document*, RenderStyle*, bool isAbsoluteSize, float specifiedSize, bool useSVGZoomRules);
153 Color getColorFromPrimitiveValue(CSSPrimitiveValue*) const;
155 bool hasSelectorForAttribute(const AtomicString&) const;
157 CSSFontSelector* fontSelector() const { return m_fontSelector.get(); }
159 // Checks if a compound selector (which can consist of multiple simple selectors) matches the current element.
160 bool checkSelector(CSSSelector*);
162 void addViewportDependentMediaQueryResult(const MediaQueryExp*, bool result);
164 bool affectedByViewportChange() const;
166 void allVisitedStateChanged() { m_checker.allVisitedStateChanged(); }
167 void visitedStateChanged(LinkHash visitedHash) { m_checker.visitedStateChanged(visitedHash); }
169 void addKeyframeStyle(PassRefPtr<WebKitCSSKeyframesRule> rule);
170 void addPageStyle(PassRefPtr<CSSPageRule>);
172 static bool createTransformOperations(CSSValue* inValue, RenderStyle* inStyle, RenderStyle* rootStyle, TransformOperations& outOperations);
175 enum SelectorMatch { SelectorMatches, SelectorFailsLocally, SelectorFailsCompletely };
177 // This function fixes up the default font size if it detects that the current generic font family has changed. -dwh
178 void checkForGenericFamilyChange(RenderStyle*, RenderStyle* parentStyle);
179 void checkForZoomChange(RenderStyle*, RenderStyle* parentStyle);
180 void checkForTextSizeAdjust();
182 void adjustRenderStyle(RenderStyle* styleToAdjust, RenderStyle* parentStyle, Element*);
184 void addMatchedRule(const RuleData* rule) { m_matchedRules.append(rule); }
185 void addMatchedDeclaration(CSSMutableStyleDeclaration* decl);
187 void matchRules(RuleSet*, int& firstRuleIndex, int& lastRuleIndex, bool includeEmptyRules);
188 void matchRulesForList(const Vector<RuleData>*, int& firstRuleIndex, int& lastRuleIndex, bool includeEmptyRules);
189 void sortMatchedRules(unsigned start, unsigned end);
191 template <bool firstPass>
192 void applyDeclarations(bool important, int startIndex, int endIndex);
194 void matchPageRules(RuleSet*, bool isLeftPage, bool isFirstPage, const String& pageName);
195 void matchPageRulesForList(const Vector<RuleData>*, bool isLeftPage, bool isFirstPage, const String& pageName);
196 bool isLeftPage(int pageIndex) const;
197 bool isRightPage(int pageIndex) const { return !isLeftPage(pageIndex); }
198 bool isFirstPage(int pageIndex) const;
199 String pageName(int pageIndex) const;
201 OwnPtr<RuleSet> m_authorStyle;
202 OwnPtr<RuleSet> m_userStyle;
204 OwnPtr<RuleSet> m_siblingRules;
205 HashSet<AtomicStringImpl*> m_idsInRules;
207 bool m_hasUAAppearance;
208 BorderData m_borderData;
209 FillLayer m_backgroundData;
210 Color m_backgroundColor;
212 typedef HashMap<AtomicStringImpl*, RefPtr<WebKitCSSKeyframesRule> > KeyframesRuleMap;
213 KeyframesRuleMap m_keyframesRuleMap;
216 static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
218 class SelectorChecker {
219 WTF_MAKE_NONCOPYABLE(SelectorChecker);
221 SelectorChecker(Document*, bool strictParsing);
223 bool checkSelector(CSSSelector*, Element*) const;
224 SelectorMatch checkSelector(CSSSelector*, Element*, HashSet<AtomicStringImpl*>* selectorAttrs, PseudoId& dynamicPseudo, bool isSubSelector, bool encounteredLink, RenderStyle* = 0, RenderStyle* elementParentStyle = 0) const;
225 bool checkOneSelector(CSSSelector*, Element*, HashSet<AtomicStringImpl*>* selectorAttrs, PseudoId& dynamicPseudo, bool isSubSelector, RenderStyle*, RenderStyle* elementParentStyle) const;
226 bool checkScrollbarPseudoClass(CSSSelector*, PseudoId& dynamicPseudo) const;
228 EInsideLink determineLinkState(Element* element) const;
229 EInsideLink determineLinkStateSlowCase(Element* element) const;
230 void allVisitedStateChanged();
231 void visitedStateChanged(LinkHash visitedHash);
233 Document* m_document;
234 bool m_strictParsing;
235 bool m_collectRulesOnly;
236 bool m_sameOriginOnly;
237 PseudoId m_pseudoStyle;
238 bool m_documentIsHTML;
239 mutable bool m_matchVisitedPseudoClass;
240 mutable HashSet<LinkHash, LinkHashHash> m_linksCheckedForVisitedState;
244 static RenderStyle* s_styleNotYetAvailable;
246 void matchUARules(int& firstUARule, int& lastUARule);
248 void cacheBorderAndBackground();
250 void mapFillAttachment(CSSPropertyID, FillLayer*, CSSValue*);
251 void mapFillClip(CSSPropertyID, FillLayer*, CSSValue*);
252 void mapFillComposite(CSSPropertyID, FillLayer*, CSSValue*);
253 void mapFillOrigin(CSSPropertyID, FillLayer*, CSSValue*);
254 void mapFillImage(CSSPropertyID, FillLayer*, CSSValue*);
255 void mapFillRepeatX(CSSPropertyID, FillLayer*, CSSValue*);
256 void mapFillRepeatY(CSSPropertyID, FillLayer*, CSSValue*);
257 void mapFillSize(CSSPropertyID, FillLayer*, CSSValue*);
258 void mapFillXPosition(CSSPropertyID, FillLayer*, CSSValue*);
259 void mapFillYPosition(CSSPropertyID, FillLayer*, CSSValue*);
261 void mapAnimationDelay(Animation*, CSSValue*);
262 void mapAnimationDirection(Animation*, CSSValue*);
263 void mapAnimationDuration(Animation*, CSSValue*);
264 void mapAnimationFillMode(Animation*, CSSValue*);
265 void mapAnimationIterationCount(Animation*, CSSValue*);
266 void mapAnimationName(Animation*, CSSValue*);
267 void mapAnimationPlayState(Animation*, CSSValue*);
268 void mapAnimationProperty(Animation*, CSSValue*);
269 void mapAnimationTimingFunction(Animation*, CSSValue*);
271 void mapNinePieceImage(CSSPropertyID, CSSValue*, NinePieceImage&);
273 void applyProperty(int id, CSSValue*);
274 void applyPageSizeProperty(CSSValue*);
275 bool pageSizeFromName(CSSPrimitiveValue*, CSSPrimitiveValue*, Length& width, Length& height);
276 Length mmLength(double mm) const;
277 Length inchLength(double inch) const;
279 void applySVGProperty(int id, CSSValue*);
282 void loadPendingImages();
284 StyleImage* styleImage(CSSPropertyID, CSSValue* value);
285 StyleImage* cachedOrPendingFromValue(CSSPropertyID property, CSSImageValue* value);
287 // We collect the set of decls that match in |m_matchedDecls|. We then walk the
288 // set of matched decls four times, once for those properties that others depend on (like font-size),
289 // and then a second time for all the remaining properties. We then do the same two passes
290 // for any !important rules.
291 Vector<CSSMutableStyleDeclaration*, 64> m_matchedDecls;
293 // A buffer used to hold the set of matched rules for an element, and a temporary buffer used for
295 Vector<const RuleData*, 32> m_matchedRules;
297 RefPtr<CSSRuleList> m_ruleList;
299 HashSet<int> m_pendingImageProperties; // Hash of CSSPropertyIDs
301 OwnPtr<MediaQueryEvaluator> m_medium;
302 RefPtr<RenderStyle> m_rootDefaultStyle;
304 PseudoId m_dynamicPseudo;
306 SelectorChecker m_checker;
308 RefPtr<RenderStyle> m_style;
309 RenderStyle* m_parentStyle;
310 RenderStyle* m_rootElementStyle;
312 StyledElement* m_styledElement;
313 EInsideLink m_elementLinkState;
314 ContainerNode* m_parentNode;
315 CSSValue* m_lineHeightValue;
317 bool m_matchAuthorAndUserStyles;
319 RefPtr<CSSFontSelector> m_fontSelector;
320 HashSet<AtomicStringImpl*> m_selectorAttrs;
321 Vector<CSSMutableStyleDeclaration*> m_additionalAttributeStyleDecls;
322 Vector<MediaQueryResult*> m_viewportDependentMediaQueryResults;
325 } // namespace WebCore
327 #endif // CSSStyleSelector_h