KHtml

cssstyleselector.h
1 /*
2  * This file is part of the CSS implementation for KDE.
3  *
4  * Copyright (C) 1999-2003 Lars Knoll ([email protected])
5  * Copyright (C) 2003, 2005, 2006 Apple Computer, Inc.
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23 #ifndef _CSS_cssstyleselector_h_
24 #define _CSS_cssstyleselector_h_
25 
26 #include "rendering/render_style.h"
27 #include "dom/dom_string.h"
28 #include "css/css_mediaquery.h"
29 #include <QVarLengthArray>
30 #include <QList>
31 #include <wtf/HashMap.h>
32 #include <wtf/Vector.h>
33 #include <wtf/HashSet.h>
34 
35 class KHTMLSettings;
36 class KHTMLView;
37 class KHTMLPart;
38 class QUrl;
39 
40 namespace DOM
41 {
42 class DocumentImpl;
43 class NodeImpl;
44 class ElementImpl;
45 class StyleSheetImpl;
46 class CSSStyleRuleImpl;
47 class CSSStyleSheetImpl;
48 class CSSSelector;
49 class CSSStyleDeclarationImpl;
50 class CSSProperty;
51 class StyleSheetListImpl;
52 class CSSValueImpl;
53 class CSSFontSelector;
54 }
55 
56 namespace khtml
57 {
58 class CSSStyleSelectorList;
59 class CSSOrderedRule;
60 class CSSOrderedPropertyList;
61 class RenderStyle;
62 class MediaQueryEvaluator;
63 
64 /*
65  * to remember the source where a rule came from. Differentiates between
66  * important and not important rules. This is ordered in the order they have to be applied
67  * to the RenderStyle.
68  */
69 enum Source {
70  Default = 0,
71  User = 1,
72  NonCSSHint = 2,
73  Author = 3,
74  Inline = 4,
75  AuthorImportant = 5,
76  InlineImportant = 6,
77  UserImportant = 7
78 };
79 
80 /*
81  * List of properties that get applied to the Element. We need to collect them first
82  * and then apply them one by one, because we have to change the apply order.
83  * Some properties depend on other one already being applied (for example all properties specifying
84  * some length need to have already the correct font size. Same applies to color
85  *
86  * While sorting them, we have to take care not to mix up the original order.
87  */
88 class CSSOrderedProperty
89 {
90 public:
91  CSSOrderedProperty(DOM::CSSProperty *_prop, uint _selector,
92  bool first, Source source, unsigned int specificity,
93  unsigned int _position)
94  : prop(_prop), pseudoId(RenderStyle::NOPSEUDO), selector(_selector),
95  position(_position)
96  {
97  priority = (!first << 30) | (source << 24) | specificity;
98  }
99  CSSOrderedProperty(): prop(nullptr), pseudoId(RenderStyle::NOPSEUDO), selector(0),
100  position(0)
101  {
102 
103  }
104 
105  bool operator < (const CSSOrderedProperty &other) const
106  {
107  if (priority < other.priority) {
108  return true;
109  }
110  if (priority > other.priority) {
111  return false;
112  }
113  if (position < other.position) {
114  return true;
115  }
116  return false;
117  }
118 
119  DOM::CSSProperty *prop;
120  RenderStyle::PseudoId pseudoId;
121  unsigned int selector;
122  unsigned int position;
123 
124  quint32 priority;
125 };
126 
127 class MediaQueryResult
128 {
129 public:
130  MediaQueryResult(const MediaQueryExp &expr, bool result)
131  : m_expression(expr)
132  , m_result(result)
133  {}
134 
135  MediaQueryExp m_expression;
136  bool m_result;
137 };
138 
146 {
147 public:
148  StyleSelector() {}
149 
150  /* as nobody has implemented a second style selector up to now comment out
151  the virtual methods until then, so the class has no vptr.
152  */
153 // virtual ~StyleSelector() {}
154 // virtual RenderStyle *styleForElement(DOM::ElementImpl *e) = 0;
155 
156  enum State {
157  None = 0x00,
158  Hover = 0x01,
159  Focus = 0x02,
160  Active = 0x04
161  };
162 };
163 
168 {
169 public:
178  CSSStyleSelector(DOM::DocumentImpl *doc, QString userStyleSheet, DOM::StyleSheetListImpl *styleSheets, const QUrl &url,
179  bool _strictParsing);
183  CSSStyleSelector(DOM::CSSStyleSheetImpl *sheet);
184 
185  ~CSSStyleSelector();
186 
187  void addSheet(DOM::CSSStyleSheetImpl *sheet);
188  KHTML_EXPORT static void clear();
189  static void reparseConfiguration();
190 
191  static void loadDefaultStyle(const KHTMLSettings *s, DOM::DocumentImpl *doc);
192 
193  // fallbackParentStyle will be inheritted from if the parent doesn't have style info
194  RenderStyle *styleForElement(DOM::ElementImpl *e, RenderStyle *fallbackParentStyle = nullptr);
195 
196  bool isMatchedByAnySelector(DOM::ElementImpl *e, const QList<DOM::CSSSelector *> &sels);
197 
198  QVector<int> fontSizes() const
199  {
200  return m_fontSizes;
201  }
202  QVector<int> fixedFontSizes() const
203  {
204  return m_fixedFontSizes;
205  }
206 
207  bool strictParsing;
208  struct Encodedurl {
209  QString host; //also contains protocol
210  QString path;
211  QString file;
212  } encodedurl;
213 
214  // called from KHTMLView::print()
215  void computeFontSizes(int logicalDpiY, int zoomFactor);
216  void computeFontSizesFor(int logicalDpiY, int zoomFactor, QVector<int> &fontSizes, bool isFixed);
217 
218  static void precomputeAttributeDependencies(DOM::DocumentImpl *doc, DOM::CSSSelector *sel);
219  void addViewportDependentMediaQueryResult(const MediaQueryExp *, bool result);
220  bool affectedByViewportChange() const;
221  DOM::CSSFontSelector *fontSelector() const
222  {
223  return m_fontSelector;
224  }
225 protected:
226  /* checks if the complete selector (which can be build up from a few CSSSelector's
227  with given relationships matches the given Element */
228  void checkSelector(int selector, DOM::ElementImpl *e);
229  /* checks if the selector matches the given Element */
230  bool checkSimpleSelector(DOM::CSSSelector *selector, DOM::ElementImpl *e, bool isAncestor, bool isSubSelector = false);
231 
232  enum SelectorMatch {SelectorMatches = 0, SelectorFailsLocal, SelectorFails};
233  SelectorMatch checkSelector(DOM::CSSSelector *sel, DOM::ElementImpl *e, bool isAncestor, bool isSubSelector = false);
234 
235  // computes various summaries of the documents' properties, which are used in
236  // the various checkSelector methods for optimizations
237  void prepareToMatchElement(DOM::ElementImpl *e, bool withDeps);
238 
239  void addDependency(int dependencyType, DOM::ElementImpl *dependency);
240  void setupDefaultRootStyle(DOM::DocumentImpl *d = nullptr);
241 #ifdef APPLE_CHANGES
242  /* This function fixes up the default font size if it detects that the
243  current generic font family has changed. -dwh */
244  void checkForGenericFamilyChange(RenderStyle *aStyle, RenderStyle *aParentStyle);
245 #endif
246  // locates potentially similar style
247  RenderStyle *locateSimilarStyle();
248 
249  /* builds up the selectors and properties lists from the CSSStyleSelectorList's */
250  void buildLists();
251  void clearLists();
252 
253  void adjustRenderStyle(RenderStyle *style, DOM::ElementImpl *e);
254 
255  void addInlineDeclarations(DOM::ElementImpl *e);
256 
257  static DOM::CSSStyleSheetImpl *s_defaultSheet;
258  static DOM::CSSStyleSheetImpl *s_defaultNonCSSHintsSheet;
259  static DOM::CSSStyleSheetImpl *s_quirksSheet;
260  static CSSStyleSelectorList *s_defaultStyle;
261  static CSSStyleSelectorList *s_defaultQuirksStyle;
262  static CSSStyleSelectorList *s_defaultPrintStyle;
263  static CSSStyleSelectorList *s_defaultNonCSSHintsStyle;
264  static RenderStyle *styleNotYetAvailable;
265 
266  CSSStyleSelectorList *defaultStyle;
267  CSSStyleSelectorList *defaultQuirksStyle;
268  CSSStyleSelectorList *defaultNonCSSHintsStyle;
269  CSSStyleSelectorList *defaultPrintStyle;
270 
271  CSSStyleSelectorList *authorStyle;
272  CSSStyleSelectorList *implicitStyle;
273  CSSStyleSelectorList *userStyle;
274  DOM::CSSStyleSheetImpl *userSheet;
275 
276 public:
277 
278 private:
279  void init(const KHTMLSettings *settings, DOM::DocumentImpl *doc);
280 
281  void mapBackgroundAttachment(BackgroundLayer *layer, DOM::CSSValueImpl *value);
282  void mapBackgroundClip(BackgroundLayer *layer, DOM::CSSValueImpl *value);
283  void mapBackgroundOrigin(BackgroundLayer *layer, DOM::CSSValueImpl *value);
284  void mapBackgroundImage(BackgroundLayer *layer, DOM::CSSValueImpl *value);
285  void mapBackgroundRepeat(BackgroundLayer *layer, DOM::CSSValueImpl *value);
286  void mapBackgroundSize(BackgroundLayer *layer, DOM::CSSValueImpl *value);
287  void mapBackgroundXPosition(BackgroundLayer *layer, DOM::CSSValueImpl *value);
288  void mapBackgroundYPosition(BackgroundLayer *layer, DOM::CSSValueImpl *value);
289 
290 public: // we need to make the enum public for SelectorCache
291  enum SelectorState {
292  Unknown = 0,
293  Applies,
294  AppliesPseudo,
295  Invalid
296  };
297 
298  enum SelectorMedia {
299  MediaAural = 1,
300  MediaBraille,
301  MediaEmboss,
302  MediaHandheld,
303  MediaPrint,
304  MediaProjection,
305  MediaScreen,
306  MediaTTY,
307  MediaTV
308  };
309 protected:
310 
311  struct SelectorCache {
312  SelectorState state;
313  unsigned firstPropertyIndex;
314  };
315 
316  unsigned int selectors_size;
317  DOM::CSSSelector **selectors;
318  SelectorCache *selectorCache;
319  unsigned *nextPropertyIndexes;
320  unsigned int properties_size;
321  unsigned *nextSimilarSelector;
322  CSSOrderedProperty *propertiesBuffer;
324  MediaQueryEvaluator *m_medium;
325  WTF::Vector<CSSOrderedProperty *> propsToApply;
326  WTF::Vector<CSSOrderedProperty *> pseudoProps;
327 
328  // hashes for faster styleForElement
329  WTF::HashMap<quintptr, int> classSelector, idSelector;
330  WTF::HashMap<unsigned, int> tagSelector;
331  int otherSelector;
332 
333  WTF::HashSet<unsigned> tagCache;
334  WTF::HashSet<quintptr> classCache, idCache;
335 
336  RenderStyle::PseudoId dynamicPseudo;
337 
338  RenderStyle *style;
339  RenderStyle *parentStyle;
340  RenderStyle *m_rootStyle; // needed to compute 'rem' lengths
341  DOM::ElementImpl *element;
342  DOM::NodeImpl *parentNode;
343  KHTMLView *view;
344  KHTMLPart *part;
345  const KHTMLSettings *settings;
346  bool rememberDependencies;
347  int logicalDpiY;
348  RenderStyle *m_rootDefaultStyle;
349  QVector<int> m_fontSizes;
350  QVector<int> m_fixedFontSizes;
351  int m_minFontSize;
352 
353  bool fontDirty;
354  DOM::CSSFontSelector *m_fontSelector;
355  QList<MediaQueryResult *> m_viewportDependentMediaQueryResults;
356 
357  void applyRule(int id, DOM::CSSValueImpl *value);
358  void applySVGRule(int id, DOM::CSSValueImpl *value);
359 };
360 
361 /*
362  * This is the list we will collect all properties we need to apply in.
363  * It will get sorted once before applying.
364  */
365 class CSSOrderedPropertyList : public QVector<CSSOrderedProperty>
366 {
367 public:
368  void append(DOM::CSSStyleDeclarationImpl *decl, uint selector, uint specificity,
369  Source regular, Source important);
370 };
371 
372 class CSSOrderedRule
373 {
374 public:
375  CSSOrderedRule(DOM::CSSStyleRuleImpl *r, DOM::CSSSelector *s, int _index);
376  ~CSSOrderedRule();
377 
378  DOM::CSSSelector *selector;
379  DOM::CSSStyleRuleImpl *rule;
380  int index;
381 };
382 
383 class CSSStyleSelectorList : public QList<CSSOrderedRule *>
384 {
385 public:
386  CSSStyleSelectorList();
387  virtual ~CSSStyleSelectorList();
388 
389  void append(DOM::CSSStyleSheetImpl *sheet,
390  MediaQueryEvaluator *medium, CSSStyleSelector *styleSelector);
391 
392  void collect(WTF::HashMap<DOM::CSSSelector *, int> *selectorsCache, QList<DOM::CSSSelector *> *selectorList,
393  CSSOrderedPropertyList *propList, Source regular, Source important);
394 };
395 
396 }
397 #endif
Settings for the HTML view.
The CSSNamespaceRule interface represents an.
Definition: css_rule.h:455
This file is part of the HTML rendering engine for KDE.
This class is khtml&#39;s main class.
Definition: khtml_part.h:208
Renders and displays HTML in a QScrollArea.
Definition: khtmlview.h:97
Author
the StyleSelector implementation for CSS.
QCA_EXPORT void init()
This library provides a full-featured HTML parser and widget.
Class that evaluates css media queries as defined in CSS3 Module "Media Queries" (https://www.w3.org/TR/css3-mediaqueries/) Special constructors are needed, if simple media queries are to be evaluated without knowledge of the medium features.
this class selects a RenderStyle for a given Element based on the collection of stylesheets it contai...
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 13 2020 22:45:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.