fix logic
[personal-kdelibs.git] / khtml / css / cssstyleselector.h
blob4184e31b3210b1a071ed66a62d3a1cc0a168d475
1 /*
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>
35 class KHTMLSettings;
36 class KHTMLView;
37 class KHTMLPart;
38 class KUrl;
40 namespace DOM {
41 class DocumentImpl;
42 class NodeImpl;
43 class ElementImpl;
44 class StyleSheetImpl;
45 class CSSStyleRuleImpl;
46 class CSSStyleSheetImpl;
47 class CSSSelector;
48 class CSSStyleDeclarationImpl;
49 class CSSProperty;
50 class StyleSheetListImpl;
51 class CSSValueImpl;
54 namespace khtml
56 class CSSStyleSelectorList;
57 class CSSOrderedRule;
58 class CSSOrderedPropertyList;
59 class RenderStyle;
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
65 * to the RenderStyle.
67 enum Source {
68 Default = 0,
69 User = 1,
70 NonCSSHint = 2,
71 Author = 3,
72 Inline = 4,
73 AuthorImportant = 5,
74 InlineImportant = 6,
75 UserImportant =7
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
88 public:
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 ),
93 position( _position )
95 priority = (!first << 30) | (source << 24) | specificity;
97 CSSOrderedProperty(): prop( 0 ), pseudoId( RenderStyle::NOPSEUDO ), selector(0),
98 position( 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;
107 return false;
110 DOM::CSSProperty *prop;
111 RenderStyle::PseudoId pseudoId;
112 unsigned int selector;
113 unsigned int position;
115 quint32 priority;
118 class MediaQueryResult
120 public:
121 MediaQueryResult(const MediaQueryExp& expr, bool result)
122 : m_expression(expr)
123 , m_result(result)
126 MediaQueryExp m_expression;
127 bool m_result;
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...
136 class StyleSelector
138 public:
139 StyleSelector() {}
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;
147 enum State {
148 None = 0x00,
149 Hover = 0x01,
150 Focus = 0x02,
151 Active = 0x04
157 * the StyleSelector implementation for CSS.
159 class CSSStyleSelector : public StyleSelector
161 public:
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 );
177 ~CSSStyleSelector();
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; }
191 bool strictParsing;
192 struct Encodedurl {
193 QString host; //also contains protocol
194 QString path;
195 QString file;
196 } encodedurl;
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;
206 protected:
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);
218 #ifdef APPLE_CHANGES
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);
222 #endif
224 /* builds up the selectors and properties lists from the CSSStyleSelectorList's */
225 void buildLists();
226 void clearLists();
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;
251 public:
253 private:
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
266 enum SelectorState {
267 Unknown = 0,
268 Applies,
269 AppliesPseudo,
270 Invalid
273 enum SelectorMedia {
274 MediaAural = 1,
275 MediaBraille,
276 MediaEmboss,
277 MediaHandheld,
278 MediaPrint,
279 MediaProjection,
280 MediaScreen,
281 MediaTTY,
282 MediaTV
284 protected:
286 struct SelectorCache {
287 SelectorState state;
288 unsigned int props_size;
289 int *props;
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;
311 RenderStyle *style;
312 RenderStyle *parentStyle;
313 DOM::ElementImpl *element;
314 DOM::NodeImpl *parentNode;
315 KHTMLView *view;
316 KHTMLPart *part;
317 const KHTMLSettings *settings;
318 int logicalDpiY;
319 RenderStyle* m_rootDefaultStyle;
320 QVector<int> m_fontSizes;
321 QVector<int> m_fixedFontSizes;
323 bool fontDirty;
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*>
336 public:
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 );
342 class CSSOrderedRule
344 public:
345 CSSOrderedRule(DOM::CSSStyleRuleImpl *r, DOM::CSSSelector *s, int _index);
346 ~CSSOrderedRule();
348 DOM::CSSSelector *selector;
349 DOM::CSSStyleRuleImpl *rule;
350 int index;
353 class CSSStyleSelectorList : public QList<CSSOrderedRule*>
355 public:
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 );
367 #endif