2 * This file is part of the CSS implementation for KDE.
4 * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
5 * Copyright (C) 2003, 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.
23 #ifndef _CSS_cssstyleselector_h_
24 #define _CSS_cssstyleselector_h_
26 #include "rendering/render_style.h"
27 #include "dom/dom_string.h"
28 #include "xml/dom_restyler.h"
29 #include "css/css_mediaquery.h"
30 #include <QtCore/QVarLengthArray>
31 #include <QtCore/QList>
32 #include <wtf/HashMap.h>
33 #include <wtf/Vector.h>
45 class CSSStyleRuleImpl
;
46 class CSSStyleSheetImpl
;
48 class CSSStyleDeclarationImpl
;
50 class StyleSheetListImpl
;
56 class CSSStyleSelectorList
;
58 class CSSOrderedPropertyList
;
60 class MediaQueryEvaluator
;
63 * to remember the source where a rule came from. Differentiates between
64 * important and not important rules. This is ordered in the order they have to be applied
79 * List of properties that get applied to the Element. We need to collect them first
80 * and then apply them one by one, because we have to change the apply order.
81 * Some properties depend on other one already being applied (for example all properties specifying
82 * some length need to have already the correct font size. Same applies to color
84 * While sorting them, we have to take care not to mix up the original order.
86 class CSSOrderedProperty
89 CSSOrderedProperty(DOM::CSSProperty
*_prop
, uint _selector
,
90 bool first
, Source source
, unsigned int specificity
,
91 unsigned int _position
)
92 : prop ( _prop
), pseudoId( RenderStyle::NOPSEUDO
), selector( _selector
),
95 priority
= (!first
<< 30) | (source
<< 24) | specificity
;
97 CSSOrderedProperty(): prop( 0 ), pseudoId( RenderStyle::NOPSEUDO
), selector(0),
103 bool operator < ( const CSSOrderedProperty
&other
) const {
104 if (priority
< other
.priority
) return true;
105 if (priority
> other
.priority
) return false;
106 if (position
< other
.position
) return true;
110 DOM::CSSProperty
*prop
;
111 RenderStyle::PseudoId pseudoId
;
112 unsigned int selector
;
113 unsigned int position
;
118 class MediaQueryResult
121 MediaQueryResult(const MediaQueryExp
& expr
, bool result
)
126 MediaQueryExp m_expression
;
131 * this class selects a RenderStyle for a given Element based on the
132 * collection of stylesheets it contains. This is just a virtual base class
133 * for specific implementations of the Selector. At the moment only CSSStyleSelector
134 * exists, but someone may wish to implement XSL...
141 /* as nobody has implemented a second style selector up to now comment out
142 the virtual methods until then, so the class has no vptr.
144 // virtual ~StyleSelector() {}
145 // virtual RenderStyle *styleForElement(DOM::ElementImpl *e) = 0;
157 * the StyleSelector implementation for CSS.
159 class CSSStyleSelector
: public StyleSelector
163 * creates a new StyleSelector for a Document.
164 * goes through all StyleSheets defined in the document and
165 * creates a list of rules it needs to apply to objects
167 * Also takes into account special cases for HTML documents,
168 * including the defaultStyle (which is html only)
170 CSSStyleSelector( DOM::DocumentImpl
* doc
, QString userStyleSheet
, DOM::StyleSheetListImpl
*styleSheets
, const KUrl
&url
,
171 bool _strictParsing
);
173 * same as above but for a single stylesheet.
175 CSSStyleSelector( DOM::CSSStyleSheetImpl
*sheet
);
179 void addSheet( DOM::CSSStyleSheetImpl
*sheet
);
180 KHTML_EXPORT
static void clear();
181 static void reparseConfiguration();
183 static void loadDefaultStyle(const KHTMLSettings
*s
, DOM::DocumentImpl
*doc
);
185 // fallbackParentStyle will be inheritted from if the parent doesn't have style info
186 RenderStyle
*styleForElement(DOM::ElementImpl
*e
, RenderStyle
* fallbackParentStyle
= 0);
188 QVector
<int> fontSizes() const { return m_fontSizes
; }
189 QVector
<int> fixedFontSizes() const { return m_fixedFontSizes
; }
193 QString host
; //also contains protocol
198 // called from KHTMLView::print()
199 void computeFontSizes(int logicalDpiY
, int zoomFactor
);
200 void computeFontSizesFor(int logicalDpiY
, int zoomFactor
, QVector
<int>& fontSizes
, bool isFixed
);
202 static void precomputeAttributeDependencies(DOM::DocumentImpl
* doc
, DOM::CSSSelector
* sel
);
203 void addViewportDependentMediaQueryResult(const MediaQueryExp
*, bool result
);
204 bool affectedByViewportChange() const;
207 /* checks if the complete selector (which can be build up from a few CSSSelector's
208 with given relationships matches the given Element */
209 void checkSelector(int selector
, DOM::ElementImpl
*e
);
210 /* checks if the selector matches the given Element */
211 bool checkSimpleSelector(DOM::CSSSelector
*selector
, DOM::ElementImpl
*e
, bool isAncestor
, bool isSubSelector
= false);
213 enum SelectorMatch
{SelectorMatches
= 0, SelectorFailsLocal
, SelectorFails
};
214 SelectorMatch
checkSelector(DOM::CSSSelector
*sel
, DOM::ElementImpl
*e
, bool isAncestor
, bool isSubSelector
= false);
216 void addDependency(StructuralDependencyType dependencyType
, DOM::ElementImpl
* dependency
);
217 void setupDefaultRootStyle(DOM::DocumentImpl
*d
=0);
219 /* This function fixes up the default font size if it detects that the
220 current generic font family has changed. -dwh */
221 void checkForGenericFamilyChange(RenderStyle
* aStyle
, RenderStyle
* aParentStyle
);
224 /* builds up the selectors and properties lists from the CSSStyleSelectorList's */
228 void adjustRenderStyle(RenderStyle
* style
, DOM::ElementImpl
*e
);
230 unsigned int addInlineDeclarations(DOM::ElementImpl
* e
, unsigned int numProps
);
232 static DOM::CSSStyleSheetImpl
*s_defaultSheet
;
233 static DOM::CSSStyleSheetImpl
*s_defaultNonCSSHintsSheet
;
234 static DOM::CSSStyleSheetImpl
*s_quirksSheet
;
235 static CSSStyleSelectorList
*s_defaultStyle
;
236 static CSSStyleSelectorList
*s_defaultQuirksStyle
;
237 static CSSStyleSelectorList
*s_defaultPrintStyle
;
238 static CSSStyleSelectorList
*s_defaultNonCSSHintsStyle
;
239 static RenderStyle
* styleNotYetAvailable
;
241 CSSStyleSelectorList
*defaultStyle
;
242 CSSStyleSelectorList
*defaultQuirksStyle
;
243 CSSStyleSelectorList
*defaultNonCSSHintsStyle
;
244 CSSStyleSelectorList
*defaultPrintStyle
;
246 CSSStyleSelectorList
*authorStyle
;
247 CSSStyleSelectorList
*implicitStyle
;
248 CSSStyleSelectorList
*userStyle
;
249 DOM::CSSStyleSheetImpl
*userSheet
;
254 void init(const KHTMLSettings
* settings
, DOM::DocumentImpl
* doc
);
256 void mapBackgroundAttachment(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
257 void mapBackgroundClip(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
258 void mapBackgroundOrigin(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
259 void mapBackgroundImage(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
260 void mapBackgroundRepeat(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
261 void mapBackgroundSize(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
262 void mapBackgroundXPosition(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
263 void mapBackgroundYPosition(BackgroundLayer
* layer
, DOM::CSSValueImpl
* value
);
265 public: // we need to make the enum public for SelectorCache
286 struct SelectorCache
{
288 unsigned int props_size
;
292 unsigned int selectors_size
;
293 DOM::CSSSelector
**selectors
;
294 SelectorCache
*selectorCache
;
295 unsigned int properties_size
;
296 CSSOrderedProperty
**properties
;
297 QVarLengthArray
<CSSOrderedProperty
> inlineProps
;
298 MediaQueryEvaluator
* m_medium
;
299 CSSOrderedProperty
**propsToApply
;
300 CSSOrderedProperty
**pseudoProps
;
301 unsigned int propsToApplySize
;
302 unsigned int pseudoPropsSize
;
304 // hashes for faster styleForElement
305 WTF::HashMap
< unsigned long, WTF::Vector
<int> > classSelectors
, idSelectors
;
306 WTF::HashMap
< unsigned, WTF::Vector
<int> > tagSelectors
;
307 WTF::Vector
<int> otherSelectors
;
309 RenderStyle::PseudoId dynamicPseudo
;
312 RenderStyle
*parentStyle
;
313 DOM::ElementImpl
*element
;
314 DOM::NodeImpl
*parentNode
;
317 const KHTMLSettings
*settings
;
319 RenderStyle
* m_rootDefaultStyle
;
320 QVector
<int> m_fontSizes
;
321 QVector
<int> m_fixedFontSizes
;
324 QList
<MediaQueryResult
*> m_viewportDependentMediaQueryResults
;
326 void applyRule(int id
, DOM::CSSValueImpl
*value
);
327 void applySVGRule(int id
, DOM::CSSValueImpl
*value
);
331 * This is the list we will collect all properties we need to apply in.
332 * It will get sorted once before applying.
334 class CSSOrderedPropertyList
: public QList
<CSSOrderedProperty
*>
337 static bool compareItems(const CSSOrderedProperty
* i1
, const CSSOrderedProperty
* i2
);
338 void append(DOM::CSSStyleDeclarationImpl
*decl
, uint selector
, uint specificity
,
339 Source regular
, Source important
);
345 CSSOrderedRule(DOM::CSSStyleRuleImpl
*r
, DOM::CSSSelector
*s
, int _index
);
348 DOM::CSSSelector
*selector
;
349 DOM::CSSStyleRuleImpl
*rule
;
353 class CSSStyleSelectorList
: public QList
<CSSOrderedRule
*>
356 CSSStyleSelectorList();
357 virtual ~CSSStyleSelectorList();
359 void append( DOM::CSSStyleSheetImpl
*sheet
,
360 MediaQueryEvaluator
*medium
, CSSStyleSelector
* styleSelector
);
362 void collect( QList
<DOM::CSSSelector
*> *selectorList
, CSSOrderedPropertyList
*propList
,
363 Source regular
, Source important
);