KHtml

cssparser.h
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright 2003 Lars Knoll ([email protected])
5  * Copyright (C) 2004, 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 #ifndef _CSS_cssparser_h_
23 #define _CSS_cssparser_h_
24 
25 #include <QString>
26 #include <QColor>
27 #include <QVector>
28 #include <dom/dom_string.h>
29 #include <misc/htmlnames.h>
30 #include <wtf/Vector.h>
31 #include <xml/dom_docimpl.h>
32 
33 namespace khtml
34 {
35 class MediaQuery;
36 }
37 
38 namespace DOM
39 {
40 class StyleListImpl;
41 class CSSStyleSheetImpl;
42 class CSSRuleImpl;
43 class CSSStyleRuleImpl;
44 class DocumentImpl;
45 class CSSValueImpl;
46 class CSSValueListImpl;
47 class CSSPrimitiveValueImpl;
48 class CSSStyleDeclarationImpl;
49 class CSSFontFaceRuleImpl;
50 class CSSProperty;
51 class MediaListImpl;
52 class CSSSelector;
53 
54 struct ParseString {
55  unsigned short *string;
56  int length;
57 };
58 
59 struct Value;
60 class ValueList;
61 
62 struct Function {
63  ParseString name;
64  ValueList *args;
65 };
66 
67 struct Value {
68  int id;
69  bool isInt;
70  union {
71  double fValue;
72  int iValue;
73  ParseString string;
74  struct Function *function;
75  };
76  enum {
77  Operator = 0x100000,
78  Function = 0x100001,
79  Q_EMS = 0x100002
80  };
81 
82  int unit;
83 };
84 
85 static inline QString qString(const ParseString &ps)
86 {
87  return QString((QChar *)ps.string, ps.length);
88 }
89 static inline DOMString domString(const ParseString &ps)
90 {
91  return DOMString((QChar *)ps.string, ps.length);
92 }
93 
94 class ValueList
95 {
96 public:
97  ValueList() : m_current(0) { }
98  ~ValueList();
99  void addValue(const Value &v)
100  {
101  m_values.append(v);
102  }
103  int size() const
104  {
105  return m_values.size();
106  }
107  Value *current()
108  {
109  return m_current < m_values.size() ? &m_values[m_current] : nullptr;
110  }
111  Value *next()
112  {
113  ++m_current;
114  return current();
115  }
116 private:
117  QVector<Value> m_values;
118  int m_current;
119 };
120 
121 class CSSParser
122 {
123 public:
124  CSSParser(bool strictParsing = true);
125  ~CSSParser();
126 
127  void parseSheet(DOM::CSSStyleSheetImpl *sheet, const DOM::DOMString &string);
128  DOM::CSSRuleImpl *parseRule(DOM::CSSStyleSheetImpl *sheet, const DOM::DOMString &string);
129  bool parseValue(DOM::CSSStyleDeclarationImpl *decls, int id, const DOM::DOMString &string,
130  bool _important);
131  bool parseDeclaration(DOM::CSSStyleDeclarationImpl *decls, const DOM::DOMString &string);
132  bool parseMediaQuery(DOM::MediaListImpl *queries, const DOM::DOMString &string);
133  QList<DOM::CSSSelector *> parseSelectorList(DOM::DocumentImpl *doc, const DOM::DOMString &string);
134  // Returns an empty list on parse error.
135 
136  static CSSParser *current()
137  {
138  return currentParser;
139  }
140 
141  unsigned int getLocalNameId(const DOMString &str)
142  {
143  LocalName localname;
144  DOM::DocumentImpl *doc = document();
145  if (doc && doc->isHTMLDocument()) {
146  localname = LocalName::fromString(str, khtml::IDS_NormalizeLower);
147  } else {
148  localname = LocalName::fromString(str);
149  }
150  boundLocalNames.append(localname);
151  return localname.id();
152  }
153 
154  DOM::DocumentImpl *document() const;
155 
156  unsigned int defaultNamespace();
157 
158  void addProperty(int propId, CSSValueImpl *value, bool important);
159  void rollbackParsedProperties(int toNumParsedProperties);
160  bool hasProperties() const
161  {
162  return numParsedProperties > 0;
163  }
164  CSSStyleDeclarationImpl *createStyleDeclaration(CSSStyleRuleImpl *rule);
165  CSSStyleDeclarationImpl *createFontFaceStyleDeclaration(CSSFontFaceRuleImpl *rule);
166  void clearProperties();
167 
168  bool parseValue(int propId, bool important);
169  bool parseSVGValue(int propId, bool important);
170  bool parseShortHand(int propId, const int *properties, const int numProperties, bool important);
171  bool parse4Values(int propId, const int *properties, bool important);
172  bool parseContent(int propId, bool important);
173 
174  CSSValueImpl *parseBackgroundColor();
175  CSSValueImpl *parseBackgroundImage(bool &didParse);
176 
177  enum BackgroundPosKind {
178  BgPos_X,
179  BgPos_Y,
180  BgPos_NonKW,
181  BgPos_Center
182  };
183 
184  CSSValueImpl *parseBackgroundPositionXY(BackgroundPosKind &kindOut);
185  void parseBackgroundPosition(CSSValueImpl *&value1, CSSValueImpl *&value2);
186  CSSValueImpl *parseBackgroundSize();
187 
188  bool parseBackgroundProperty(int propId, int &propId1, int &propId2, CSSValueImpl *&retValue1, CSSValueImpl *&retValue2);
189  bool parseBackgroundShorthand(bool important);
190 
191  void addBackgroundValue(CSSValueImpl *&lval, CSSValueImpl *rval);
192 
193  bool parseShape(int propId, bool important);
194  bool parseFontShorthand(bool important);
195  bool parseFontFaceSrc();
196  bool parseCounter(int propId, bool increment, bool important);
197  bool parseListStyleShorthand(bool important);
198 
199  // returns parsed font-weight's css value id if valid, otherwise 0 (CSS_VAL_INVALID)
200  int parseFontWeight(Value *val, bool strict);
201 
202  bool parseColorParameters(Value *, int *colorValues, bool parseAlpha);
203  bool parseHSLParameters(Value *, double *colorValues, bool parseAlpha);
204 
205  // returns the found property
206  // 0 if nothing found (or ok == false)
207  // @param forward if true, it parses the next in the list
208  CSSValueListImpl *parseFontFamily();
209  CSSPrimitiveValueImpl *parseColor();
210  CSSPrimitiveValueImpl *parseColorFromValue(Value *val);
211  CSSValueImpl *parseCounterContent(ValueList *args, bool counters);
212 
213  // CSS3 Parsing Routines (for properties specific to CSS3)
214  bool parseShadow(int propId, bool important);
215 
216  // SVG parsing:
217  CSSValueImpl *parseSVGStrokeDasharray();
218  CSSValueImpl *parseSVGPaint();
219  CSSValueImpl *parseSVGColor();
220 private:
221  // defines units allowed for a certain property, used in parseUnit
222  enum Units {
223  FUnknown = 0x0000,
224  FInteger = 0x0001,
225  FNumber = 0x0002, // Real Numbers
226  FPercent = 0x0004,
227  FLength = 0x0008,
228  FAngle = 0x0010,
229  FTime = 0x0020,
230  FFrequency = 0x0040,
231  FRelative = 0x0100,
232  FNonNeg = 0x0200
233  };
234 
235  bool parseBorderImage(int propId, bool important);
236  bool parseBorderRadius(bool important);
237 
238  static bool validUnit(Value *value, int unitflags, bool strict);
239 
240 public:
241  bool strict;
242  bool important;
243  unsigned int id;
244  DOM::StyleListImpl *styleElement;
245  mutable DOM::DocumentImpl *styleDocument; // cached document for styleElement,
246  // or manually set one for special parses
247 
248  // Outputs for specialized parses.
249  DOM::CSSRuleImpl *rule;
250  khtml::MediaQuery *mediaQuery;
251  QList<DOM::CSSSelector *> selectors;
252 
253  ValueList *valueList;
254  CSSProperty **parsedProperties;
255  int numParsedProperties;
256  int maxParsedProperties;
257 
258  int m_inParseShorthand;
259  int m_currentShorthand;
260  bool m_implicitShorthand;
261 
262  static CSSParser *currentParser;
263 
264  // tokenizer methods and data
265 public:
266  int lex(void *yylval);
267  int token()
268  {
269  return yyTok;
270  }
271  unsigned short *text(int *length);
272  int lex();
273 private:
274  int yyparse();
275  void runParser();
276  void setupParser(const char *prefix, const DOMString &string, const char *suffix);
277 
278  bool inShorthand() const
279  {
280  return m_inParseShorthand;
281  }
282 
283  unsigned short *data;
284  unsigned short *yytext;
285  unsigned short *yy_c_buf_p;
286  unsigned short yy_hold_char;
287  int yy_last_accepting_state;
288  unsigned short *yy_last_accepting_cpos;
289  int block_nesting;
290  int yyleng;
291  int yyTok;
292  int yy_start;
293  WTF::Vector<LocalName> boundLocalNames;
294 };
295 
296 } // namespace
297 #endif
298 
QString name(const QVariant &location)
The CSSNamespaceRule interface represents an.
Definition: css_rule.h:455
This file is part of the HTML rendering engine for KDE.
MESSAGECORE_EXPORT KMime::Content * next(KMime::Content *node, bool allowChildren=true)
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
This library provides a full-featured HTML parser and widget.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:51 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.