1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QCSSPARSER_P_H |
41 | #define QCSSPARSER_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtGui/private/qtguiglobal_p.h> |
55 | #include <QtCore/QStringList> |
56 | #include <QtCore/QList> |
57 | #include <QtCore/QVariant> |
58 | #include <QtCore/QPair> |
59 | #include <QtCore/QSize> |
60 | #include <QtCore/QMultiHash> |
61 | #include <QtGui/QFont> |
62 | #include <QtGui/QPalette> |
63 | #include <QtCore/QSharedData> |
64 | |
65 | QT_BEGIN_NAMESPACE |
66 | class QIcon; |
67 | QT_END_NAMESPACE |
68 | |
69 | #ifndef QT_NO_CSSPARSER |
70 | |
71 | // VxWorks defines NONE as (-1) "for times when NULL won't do" |
72 | #if defined(Q_OS_VXWORKS) && defined(NONE) |
73 | # undef NONE |
74 | #endif |
75 | #if defined(Q_OS_INTEGRITY) |
76 | # undef Value |
77 | #endif |
78 | // Hurd has #define TILDE 0x00080000 from <sys/ioctl.h> |
79 | #if defined(TILDE) |
80 | # undef TILDE |
81 | #endif |
82 | |
83 | #define QT_CSS_DECLARE_TYPEINFO(Class, Type) \ |
84 | } /* namespace QCss */ \ |
85 | Q_DECLARE_TYPEINFO(QCss:: Class, Type); \ |
86 | namespace QCss { |
87 | |
88 | QT_BEGIN_NAMESPACE |
89 | |
90 | namespace QCss |
91 | { |
92 | |
93 | enum Property { |
94 | UnknownProperty, |
95 | BackgroundColor, |
96 | Color, |
97 | Float, |
98 | Font, |
99 | FontFamily, |
100 | FontSize, |
101 | FontStyle, |
102 | FontWeight, |
103 | Margin, |
104 | MarginBottom, |
105 | MarginLeft, |
106 | MarginRight, |
107 | MarginTop, |
108 | QtBlockIndent, |
109 | QtListIndent, |
110 | QtParagraphType, |
111 | QtTableType, |
112 | QtUserState, |
113 | TextDecoration, |
114 | TextIndent, |
115 | TextUnderlineStyle, |
116 | VerticalAlignment, |
117 | Whitespace, |
118 | QtSelectionForeground, |
119 | QtSelectionBackground, |
120 | Border, |
121 | BorderLeft, |
122 | BorderRight, |
123 | BorderTop, |
124 | BorderBottom, |
125 | BorderCollapse, |
126 | Padding, |
127 | PaddingLeft, |
128 | PaddingRight, |
129 | PaddingTop, |
130 | PaddingBottom, |
131 | PageBreakBefore, |
132 | PageBreakAfter, |
133 | QtAlternateBackground, |
134 | BorderLeftStyle, |
135 | BorderRightStyle, |
136 | BorderTopStyle, |
137 | BorderBottomStyle, |
138 | BorderStyles, |
139 | BorderLeftColor, |
140 | BorderRightColor, |
141 | BorderTopColor, |
142 | BorderBottomColor, |
143 | BorderColor, |
144 | BorderLeftWidth, |
145 | BorderRightWidth, |
146 | BorderTopWidth, |
147 | BorderBottomWidth, |
148 | BorderWidth, |
149 | BorderTopLeftRadius, |
150 | BorderTopRightRadius, |
151 | BorderBottomLeftRadius, |
152 | BorderBottomRightRadius, |
153 | BorderRadius, |
154 | Background, |
155 | BackgroundOrigin, |
156 | BackgroundClip, |
157 | BackgroundRepeat, |
158 | BackgroundPosition, |
159 | BackgroundAttachment, |
160 | BackgroundImage, |
161 | BorderImage, |
162 | QtSpacing, |
163 | Width, |
164 | Height, |
165 | MinimumWidth, |
166 | MinimumHeight, |
167 | MaximumWidth, |
168 | MaximumHeight, |
169 | QtImage, |
170 | Left, |
171 | Right, |
172 | Top, |
173 | Bottom, |
174 | QtOrigin, |
175 | QtPosition, |
176 | Position, |
177 | QtStyleFeatures, |
178 | QtBackgroundRole, |
179 | ListStyleType, |
180 | ListStyle, |
181 | QtImageAlignment, |
182 | TextAlignment, |
183 | Outline, |
184 | OutlineOffset, |
185 | OutlineWidth, |
186 | OutlineColor, |
187 | OutlineStyle, |
188 | OutlineRadius, |
189 | OutlineTopLeftRadius, |
190 | OutlineTopRightRadius, |
191 | OutlineBottomLeftRadius, |
192 | OutlineBottomRightRadius, |
193 | FontVariant, |
194 | TextTransform, |
195 | QtListNumberPrefix, |
196 | QtListNumberSuffix, |
197 | LineHeight, |
198 | QtLineHeightType, |
199 | FontKerning, |
200 | QtForegroundTextureCacheKey, |
201 | QtIcon, |
202 | LetterSpacing, |
203 | WordSpacing, |
204 | NumProperties |
205 | }; |
206 | |
207 | enum KnownValue { |
208 | UnknownValue, |
209 | Value_Normal, |
210 | Value_Pre, |
211 | Value_NoWrap, |
212 | Value_PreLine, |
213 | Value_PreWrap, |
214 | Value_Small, |
215 | Value_Medium, |
216 | Value_Large, |
217 | Value_XLarge, |
218 | Value_XXLarge, |
219 | Value_Italic, |
220 | Value_Oblique, |
221 | Value_Bold, |
222 | Value_Underline, |
223 | Value_Overline, |
224 | Value_LineThrough, |
225 | Value_Sub, |
226 | Value_Super, |
227 | Value_Left, |
228 | Value_Right, |
229 | Value_Top, |
230 | Value_Bottom, |
231 | Value_Center, |
232 | Value_Native, |
233 | Value_Solid, |
234 | Value_Dotted, |
235 | Value_Dashed, |
236 | Value_DotDash, |
237 | Value_DotDotDash, |
238 | Value_Double, |
239 | Value_Groove, |
240 | Value_Ridge, |
241 | Value_Inset, |
242 | Value_Outset, |
243 | Value_Wave, |
244 | Value_Middle, |
245 | Value_Auto, |
246 | Value_Always, |
247 | Value_None, |
248 | Value_Transparent, |
249 | Value_Disc, |
250 | Value_Circle, |
251 | Value_Square, |
252 | Value_Decimal, |
253 | Value_LowerAlpha, |
254 | Value_UpperAlpha, |
255 | Value_LowerRoman, |
256 | Value_UpperRoman, |
257 | Value_SmallCaps, |
258 | Value_Uppercase, |
259 | Value_Lowercase, |
260 | |
261 | /* keep these in same order as QPalette::ColorRole */ |
262 | Value_FirstColorRole, |
263 | Value_WindowText = Value_FirstColorRole, |
264 | Value_Button, |
265 | Value_Light, |
266 | Value_Midlight, |
267 | Value_Dark, |
268 | Value_Mid, |
269 | Value_Text, |
270 | Value_BrightText, |
271 | Value_ButtonText, |
272 | Value_Base, |
273 | Value_Window, |
274 | Value_Shadow, |
275 | Value_Highlight, |
276 | Value_HighlightedText, |
277 | Value_Link, |
278 | Value_LinkVisited, |
279 | Value_AlternateBase, |
280 | Value_LastColorRole = Value_AlternateBase, |
281 | |
282 | Value_Disabled, |
283 | Value_Active, |
284 | Value_Selected, |
285 | Value_On, |
286 | Value_Off, |
287 | |
288 | NumKnownValues |
289 | }; |
290 | |
291 | enum BorderStyle { |
292 | BorderStyle_Unknown, |
293 | BorderStyle_None, |
294 | BorderStyle_Dotted, |
295 | BorderStyle_Dashed, |
296 | BorderStyle_Solid, |
297 | BorderStyle_Double, |
298 | BorderStyle_DotDash, |
299 | BorderStyle_DotDotDash, |
300 | BorderStyle_Groove, |
301 | BorderStyle_Ridge, |
302 | BorderStyle_Inset, |
303 | BorderStyle_Outset, |
304 | BorderStyle_Native, |
305 | NumKnownBorderStyles |
306 | }; |
307 | |
308 | enum Edge { |
309 | TopEdge, |
310 | RightEdge, |
311 | BottomEdge, |
312 | LeftEdge, |
313 | NumEdges |
314 | }; |
315 | |
316 | enum Corner { |
317 | TopLeftCorner, |
318 | TopRightCorner, |
319 | BottomLeftCorner, |
320 | BottomRightCorner |
321 | }; |
322 | |
323 | enum TileMode { |
324 | TileMode_Unknown, |
325 | TileMode_Round, |
326 | TileMode_Stretch, |
327 | TileMode_Repeat, |
328 | NumKnownTileModes |
329 | }; |
330 | |
331 | enum Repeat { |
332 | Repeat_Unknown, |
333 | Repeat_None, |
334 | Repeat_X, |
335 | Repeat_Y, |
336 | Repeat_XY, |
337 | NumKnownRepeats |
338 | }; |
339 | |
340 | enum Origin { |
341 | Origin_Unknown, |
342 | Origin_Padding, |
343 | Origin_Border, |
344 | Origin_Content, |
345 | Origin_Margin, |
346 | NumKnownOrigins |
347 | }; |
348 | |
349 | enum PositionMode { |
350 | PositionMode_Unknown, |
351 | PositionMode_Static, |
352 | PositionMode_Relative, |
353 | PositionMode_Absolute, |
354 | PositionMode_Fixed, |
355 | NumKnownPositionModes |
356 | }; |
357 | |
358 | enum Attachment { |
359 | Attachment_Unknown, |
360 | Attachment_Fixed, |
361 | Attachment_Scroll, |
362 | NumKnownAttachments |
363 | }; |
364 | |
365 | enum StyleFeature { |
366 | StyleFeature_None = 0, |
367 | StyleFeature_BackgroundColor = 1, |
368 | StyleFeature_BackgroundGradient = 2, |
369 | NumKnownStyleFeatures = 4 |
370 | }; |
371 | |
372 | struct Value |
373 | { |
374 | enum Type { |
375 | Unknown, |
376 | Number, |
377 | Percentage, |
378 | Length, |
379 | String, |
380 | Identifier, |
381 | KnownIdentifier, |
382 | Uri, |
383 | Color, |
384 | Function, |
385 | TermOperatorSlash, |
386 | TermOperatorComma |
387 | }; |
388 | inline Value() : type(Unknown) { } |
389 | Type type; |
390 | QVariant variant; |
391 | |
392 | Q_GUI_EXPORT QString toString() const; |
393 | }; |
394 | QT_CSS_DECLARE_TYPEINFO(Value, Q_MOVABLE_TYPE) |
395 | |
396 | struct ColorData { |
397 | ColorData() : role(QPalette::NoRole), type(Invalid) {} |
398 | ColorData(const QColor &col) : color(col), role(QPalette::NoRole), type(Color) {} |
399 | ColorData(QPalette::ColorRole r) : role(r), type(Role) {} |
400 | QColor color; |
401 | QPalette::ColorRole role; |
402 | enum { Invalid, Color, Role} type; |
403 | }; |
404 | QT_CSS_DECLARE_TYPEINFO(ColorData, Q_MOVABLE_TYPE) |
405 | |
406 | struct BrushData { |
407 | BrushData() : role(QPalette::NoRole), type(Invalid) {} |
408 | BrushData(const QBrush &br) : brush(br), role(QPalette::NoRole), type(Brush) {} |
409 | BrushData(QPalette::ColorRole r) : role(r), type(Role) {} |
410 | QBrush brush; |
411 | QPalette::ColorRole role; |
412 | enum { Invalid, Brush, Role, DependsOnThePalette } type; |
413 | }; |
414 | QT_CSS_DECLARE_TYPEINFO(BrushData, Q_MOVABLE_TYPE) |
415 | |
416 | struct BackgroundData { |
417 | BrushData brush; |
418 | QString image; |
419 | Repeat repeat; |
420 | Qt::Alignment alignment; |
421 | }; |
422 | QT_CSS_DECLARE_TYPEINFO(BackgroundData, Q_MOVABLE_TYPE) |
423 | |
424 | struct LengthData { |
425 | qreal number; |
426 | enum { None, Px, Ex, Em } unit; |
427 | }; |
428 | QT_CSS_DECLARE_TYPEINFO(LengthData, Q_PRIMITIVE_TYPE) |
429 | |
430 | struct BorderData { |
431 | LengthData width; |
432 | BorderStyle style; |
433 | BrushData color; |
434 | }; |
435 | QT_CSS_DECLARE_TYPEINFO(BorderData, Q_MOVABLE_TYPE) |
436 | |
437 | // 1. StyleRule - x:hover, y:clicked > z:checked { prop1: value1; prop2: value2; } |
438 | // 2. QList<Selector> - x:hover, y:clicked z:checked |
439 | // 3. QList<BasicSelector> - y:clicked z:checked |
440 | // 4. QList<Declaration> - { prop1: value1; prop2: value2; } |
441 | // 5. Declaration - prop1: value1; |
442 | |
443 | struct Q_GUI_EXPORT Declaration |
444 | { |
445 | struct DeclarationData : public QSharedData |
446 | { |
447 | inline DeclarationData() : propertyId(UnknownProperty), important(false), inheritable(false) {} |
448 | QString property; |
449 | Property propertyId; |
450 | QList<Value> values; |
451 | QVariant parsed; |
452 | bool important:1; |
453 | bool inheritable:1; |
454 | }; |
455 | QExplicitlySharedDataPointer<DeclarationData> d; |
456 | inline Declaration() : d(new DeclarationData()) {} |
457 | inline bool isEmpty() const { return d->property.isEmpty() && d->propertyId == UnknownProperty; } |
458 | |
459 | // helper functions |
460 | QColor colorValue(const QPalette & = QPalette()) const; |
461 | void colorValues(QColor *c, const QPalette & = QPalette()) const; |
462 | QBrush brushValue(const QPalette & = QPalette()) const; |
463 | void brushValues(QBrush *c, const QPalette & = QPalette()) const; |
464 | |
465 | BorderStyle styleValue() const; |
466 | void styleValues(BorderStyle *s) const; |
467 | |
468 | Origin originValue() const; |
469 | Repeat repeatValue() const; |
470 | Qt::Alignment alignmentValue() const; |
471 | PositionMode positionValue() const; |
472 | Attachment attachmentValue() const; |
473 | int styleFeaturesValue() const; |
474 | |
475 | bool intValue(int *i, const char *unit = nullptr) const; |
476 | bool realValue(qreal *r, const char *unit = nullptr) const; |
477 | |
478 | QSize sizeValue() const; |
479 | QRect rectValue() const; |
480 | QString uriValue() const; |
481 | QIcon iconValue() const; |
482 | |
483 | void borderImageValue(QString *image, int *cuts, TileMode *h, TileMode *v) const; |
484 | bool borderCollapseValue() const; |
485 | }; |
486 | QT_CSS_DECLARE_TYPEINFO(Declaration, Q_MOVABLE_TYPE) |
487 | |
488 | const quint64 PseudoClass_Unknown = Q_UINT64_C(0x0000000000000000); |
489 | const quint64 PseudoClass_Enabled = Q_UINT64_C(0x0000000000000001); |
490 | const quint64 PseudoClass_Disabled = Q_UINT64_C(0x0000000000000002); |
491 | const quint64 PseudoClass_Pressed = Q_UINT64_C(0x0000000000000004); |
492 | const quint64 PseudoClass_Focus = Q_UINT64_C(0x0000000000000008); |
493 | const quint64 PseudoClass_Hover = Q_UINT64_C(0x0000000000000010); |
494 | const quint64 PseudoClass_Checked = Q_UINT64_C(0x0000000000000020); |
495 | const quint64 PseudoClass_Unchecked = Q_UINT64_C(0x0000000000000040); |
496 | const quint64 PseudoClass_Indeterminate = Q_UINT64_C(0x0000000000000080); |
497 | const quint64 PseudoClass_Unspecified = Q_UINT64_C(0x0000000000000100); |
498 | const quint64 PseudoClass_Selected = Q_UINT64_C(0x0000000000000200); |
499 | const quint64 PseudoClass_Horizontal = Q_UINT64_C(0x0000000000000400); |
500 | const quint64 PseudoClass_Vertical = Q_UINT64_C(0x0000000000000800); |
501 | const quint64 PseudoClass_Window = Q_UINT64_C(0x0000000000001000); |
502 | const quint64 PseudoClass_Children = Q_UINT64_C(0x0000000000002000); |
503 | const quint64 PseudoClass_Sibling = Q_UINT64_C(0x0000000000004000); |
504 | const quint64 PseudoClass_Default = Q_UINT64_C(0x0000000000008000); |
505 | const quint64 PseudoClass_First = Q_UINT64_C(0x0000000000010000); |
506 | const quint64 PseudoClass_Last = Q_UINT64_C(0x0000000000020000); |
507 | const quint64 PseudoClass_Middle = Q_UINT64_C(0x0000000000040000); |
508 | const quint64 PseudoClass_OnlyOne = Q_UINT64_C(0x0000000000080000); |
509 | const quint64 PseudoClass_PreviousSelected = Q_UINT64_C(0x0000000000100000); |
510 | const quint64 PseudoClass_NextSelected = Q_UINT64_C(0x0000000000200000); |
511 | const quint64 PseudoClass_Flat = Q_UINT64_C(0x0000000000400000); |
512 | const quint64 PseudoClass_Left = Q_UINT64_C(0x0000000000800000); |
513 | const quint64 PseudoClass_Right = Q_UINT64_C(0x0000000001000000); |
514 | const quint64 PseudoClass_Top = Q_UINT64_C(0x0000000002000000); |
515 | const quint64 PseudoClass_Bottom = Q_UINT64_C(0x0000000004000000); |
516 | const quint64 PseudoClass_Exclusive = Q_UINT64_C(0x0000000008000000); |
517 | const quint64 PseudoClass_NonExclusive = Q_UINT64_C(0x0000000010000000); |
518 | const quint64 PseudoClass_Frameless = Q_UINT64_C(0x0000000020000000); |
519 | const quint64 PseudoClass_ReadOnly = Q_UINT64_C(0x0000000040000000); |
520 | const quint64 PseudoClass_Active = Q_UINT64_C(0x0000000080000000); |
521 | const quint64 PseudoClass_Closable = Q_UINT64_C(0x0000000100000000); |
522 | const quint64 PseudoClass_Movable = Q_UINT64_C(0x0000000200000000); |
523 | const quint64 PseudoClass_Floatable = Q_UINT64_C(0x0000000400000000); |
524 | const quint64 PseudoClass_Minimized = Q_UINT64_C(0x0000000800000000); |
525 | const quint64 PseudoClass_Maximized = Q_UINT64_C(0x0000001000000000); |
526 | const quint64 PseudoClass_On = Q_UINT64_C(0x0000002000000000); |
527 | const quint64 PseudoClass_Off = Q_UINT64_C(0x0000004000000000); |
528 | const quint64 PseudoClass_Editable = Q_UINT64_C(0x0000008000000000); |
529 | const quint64 PseudoClass_Item = Q_UINT64_C(0x0000010000000000); |
530 | const quint64 PseudoClass_Closed = Q_UINT64_C(0x0000020000000000); |
531 | const quint64 PseudoClass_Open = Q_UINT64_C(0x0000040000000000); |
532 | const quint64 PseudoClass_EditFocus = Q_UINT64_C(0x0000080000000000); |
533 | const quint64 PseudoClass_Alternate = Q_UINT64_C(0x0000100000000000); |
534 | // The Any specifier is never generated, but can be used as a wildcard in searches. |
535 | const quint64 PseudoClass_Any = Q_UINT64_C(0x0000ffffffffffff); |
536 | const int NumPseudos = 45; |
537 | |
538 | struct Pseudo |
539 | { |
540 | Pseudo() : type(0), negated(false) { } |
541 | quint64 type; |
542 | QString name; |
543 | QString function; |
544 | bool negated; |
545 | }; |
546 | QT_CSS_DECLARE_TYPEINFO(Pseudo, Q_MOVABLE_TYPE) |
547 | |
548 | struct AttributeSelector |
549 | { |
550 | enum ValueMatchType { |
551 | NoMatch, |
552 | MatchEqual, |
553 | MatchIncludes, |
554 | MatchDashMatch, |
555 | MatchBeginsWith, |
556 | MatchEndsWith, |
557 | MatchContains |
558 | }; |
559 | inline AttributeSelector() : valueMatchCriterium(NoMatch) {} |
560 | |
561 | QString name; |
562 | QString value; |
563 | ValueMatchType valueMatchCriterium; |
564 | }; |
565 | QT_CSS_DECLARE_TYPEINFO(AttributeSelector, Q_MOVABLE_TYPE) |
566 | |
567 | struct BasicSelector |
568 | { |
569 | inline BasicSelector() : relationToNext(NoRelation) {} |
570 | |
571 | enum Relation { |
572 | NoRelation, |
573 | MatchNextSelectorIfAncestor, |
574 | MatchNextSelectorIfParent, |
575 | MatchNextSelectorIfDirectAdjecent, |
576 | MatchNextSelectorIfIndirectAdjecent, |
577 | }; |
578 | |
579 | QString elementName; |
580 | |
581 | QStringList ids; |
582 | QList<Pseudo> pseudos; |
583 | QList<AttributeSelector> attributeSelectors; |
584 | |
585 | Relation relationToNext; |
586 | }; |
587 | QT_CSS_DECLARE_TYPEINFO(BasicSelector, Q_MOVABLE_TYPE) |
588 | |
589 | struct Q_GUI_EXPORT Selector |
590 | { |
591 | QList<BasicSelector> basicSelectors; |
592 | int specificity() const; |
593 | quint64 pseudoClass(quint64 *negated = nullptr) const; |
594 | QString pseudoElement() const; |
595 | }; |
596 | QT_CSS_DECLARE_TYPEINFO(Selector, Q_MOVABLE_TYPE) |
597 | |
598 | struct StyleRule |
599 | { |
600 | StyleRule() : order(0) { } |
601 | QList<Selector> selectors; |
602 | QList<Declaration> declarations; |
603 | int order; |
604 | }; |
605 | QT_CSS_DECLARE_TYPEINFO(StyleRule, Q_MOVABLE_TYPE) |
606 | |
607 | struct MediaRule |
608 | { |
609 | QStringList media; |
610 | QList<StyleRule> styleRules; |
611 | }; |
612 | QT_CSS_DECLARE_TYPEINFO(MediaRule, Q_MOVABLE_TYPE) |
613 | |
614 | struct |
615 | { |
616 | QString ; |
617 | QList<Declaration> ; |
618 | }; |
619 | QT_CSS_DECLARE_TYPEINFO(PageRule, Q_MOVABLE_TYPE) |
620 | |
621 | struct ImportRule |
622 | { |
623 | QString href; |
624 | QStringList media; |
625 | }; |
626 | QT_CSS_DECLARE_TYPEINFO(ImportRule, Q_MOVABLE_TYPE) |
627 | |
628 | enum StyleSheetOrigin { |
629 | StyleSheetOrigin_Unspecified, |
630 | StyleSheetOrigin_UserAgent, |
631 | StyleSheetOrigin_User, |
632 | StyleSheetOrigin_Author, |
633 | StyleSheetOrigin_Inline |
634 | }; |
635 | |
636 | struct StyleSheet |
637 | { |
638 | StyleSheet() : origin(StyleSheetOrigin_Unspecified), depth(0) { } |
639 | QList<StyleRule> styleRules; // only contains rules that are not indexed |
640 | QList<MediaRule> mediaRules; |
641 | QList<PageRule> ; |
642 | QList<ImportRule> importRules; |
643 | StyleSheetOrigin origin; |
644 | int depth; // applicable only for inline style sheets |
645 | QMultiHash<QString, StyleRule> nameIndex; |
646 | QMultiHash<QString, StyleRule> idIndex; |
647 | |
648 | Q_GUI_EXPORT void buildIndexes(Qt::CaseSensitivity nameCaseSensitivity = Qt::CaseSensitive); |
649 | }; |
650 | QT_CSS_DECLARE_TYPEINFO(StyleSheet, Q_MOVABLE_TYPE) |
651 | |
652 | |
653 | class Q_GUI_EXPORT StyleSelector |
654 | { |
655 | public: |
656 | StyleSelector() : nameCaseSensitivity(Qt::CaseSensitive) {} |
657 | virtual ~StyleSelector(); |
658 | |
659 | union NodePtr { |
660 | void *ptr; |
661 | int id; |
662 | }; |
663 | |
664 | QList<StyleRule> styleRulesForNode(NodePtr node); |
665 | QList<Declaration> declarationsForNode(NodePtr node, const char * = nullptr); |
666 | |
667 | virtual bool nodeNameEquals(NodePtr node, const QString& nodeName) const; |
668 | virtual QString attribute(NodePtr node, const QString &name) const = 0; |
669 | virtual bool hasAttributes(NodePtr node) const = 0; |
670 | virtual QStringList nodeIds(NodePtr node) const; |
671 | virtual QStringList nodeNames(NodePtr node) const = 0; |
672 | virtual bool isNullNode(NodePtr node) const = 0; |
673 | virtual NodePtr parentNode(NodePtr node) const = 0; |
674 | virtual NodePtr previousSiblingNode(NodePtr node) const = 0; |
675 | virtual NodePtr duplicateNode(NodePtr node) const = 0; |
676 | virtual void freeNode(NodePtr node) const = 0; |
677 | |
678 | QList<StyleSheet> styleSheets; |
679 | QString medium; |
680 | Qt::CaseSensitivity nameCaseSensitivity; |
681 | private: |
682 | void matchRule(NodePtr node, const StyleRule &rules, StyleSheetOrigin origin, |
683 | int depth, QMultiMap<uint, StyleRule> *weightedRules); |
684 | bool selectorMatches(const Selector &rule, NodePtr node); |
685 | bool basicSelectorMatches(const BasicSelector &rule, NodePtr node); |
686 | }; |
687 | |
688 | enum TokenType { |
689 | NONE, |
690 | |
691 | S, |
692 | |
693 | CDO, |
694 | CDC, |
695 | INCLUDES, |
696 | DASHMATCH, |
697 | BEGINSWITH, |
698 | ENDSWITH, |
699 | CONTAINS, |
700 | |
701 | LBRACE, |
702 | PLUS, |
703 | GREATER, |
704 | COMMA, |
705 | TILDE, |
706 | |
707 | STRING, |
708 | INVALID, |
709 | |
710 | IDENT, |
711 | |
712 | HASH, |
713 | |
714 | ATKEYWORD_SYM, |
715 | |
716 | EXCLAMATION_SYM, |
717 | |
718 | LENGTH, |
719 | |
720 | PERCENTAGE, |
721 | NUMBER, |
722 | |
723 | FUNCTION, |
724 | |
725 | COLON, |
726 | SEMICOLON, |
727 | RBRACE, |
728 | SLASH, |
729 | MINUS, |
730 | DOT, |
731 | STAR, |
732 | LBRACKET, |
733 | RBRACKET, |
734 | EQUAL, |
735 | LPAREN, |
736 | RPAREN, |
737 | OR |
738 | }; |
739 | |
740 | struct Symbol |
741 | { |
742 | inline Symbol() : token(NONE), start(0), len(-1) {} |
743 | TokenType token; |
744 | QString text; |
745 | int start, len; |
746 | Q_GUI_EXPORT QString lexem() const; |
747 | }; |
748 | QT_CSS_DECLARE_TYPEINFO(Symbol, Q_MOVABLE_TYPE) |
749 | |
750 | class Q_GUI_EXPORT Scanner |
751 | { |
752 | public: |
753 | static QString preprocess(const QString &input, bool *hasEscapeSequences = nullptr); |
754 | static void scan(const QString &preprocessedInput, QList<Symbol> *symbols); |
755 | }; |
756 | |
757 | class Q_GUI_EXPORT Parser |
758 | { |
759 | public: |
760 | Parser(); |
761 | explicit Parser(const QString &css, bool file = false); |
762 | |
763 | void init(const QString &css, bool file = false); |
764 | bool parse(StyleSheet *styleSheet, Qt::CaseSensitivity nameCaseSensitivity = Qt::CaseSensitive); |
765 | Symbol errorSymbol(); |
766 | |
767 | bool parseImport(ImportRule *importRule); |
768 | bool parseMedia(MediaRule *mediaRule); |
769 | bool parseMedium(QStringList *media); |
770 | bool (PageRule *); |
771 | bool parsePseudoPage(QString *selector); |
772 | bool parseNextOperator(Value *value); |
773 | bool parseCombinator(BasicSelector::Relation *relation); |
774 | bool parseProperty(Declaration *decl); |
775 | bool parseRuleset(StyleRule *styleRule); |
776 | bool parseSelector(Selector *sel); |
777 | bool parseSimpleSelector(BasicSelector *basicSel); |
778 | bool parseClass(QString *name); |
779 | bool parseElementName(QString *name); |
780 | bool parseAttrib(AttributeSelector *attr); |
781 | bool parsePseudo(Pseudo *pseudo); |
782 | bool parseNextDeclaration(Declaration *declaration); |
783 | bool parsePrio(Declaration *declaration); |
784 | bool parseExpr(QList<Value> *values); |
785 | bool parseTerm(Value *value); |
786 | bool parseFunction(QString *name, QString *args); |
787 | bool parseHexColor(QColor *col); |
788 | bool testAndParseUri(QString *uri); |
789 | |
790 | inline bool testRuleset() { return testSelector(); } |
791 | inline bool testSelector() { return testSimpleSelector(); } |
792 | inline bool parseNextSelector(Selector *sel) { if (!testSelector()) return recordError(); return parseSelector(sel); } |
793 | bool testSimpleSelector(); |
794 | inline bool parseNextSimpleSelector(BasicSelector *basicSel) { if (!testSimpleSelector()) return recordError(); return parseSimpleSelector(basicSel); } |
795 | inline bool testElementName() { return test(IDENT) || test(STAR); } |
796 | inline bool testClass() { return test(DOT); } |
797 | inline bool testAttrib() { return test(LBRACKET); } |
798 | inline bool testPseudo() { return test(COLON); } |
799 | inline bool testMedium() { return test(IDENT); } |
800 | inline bool parseNextMedium(QStringList *media) { if (!testMedium()) return recordError(); return parseMedium(media); } |
801 | inline bool testPseudoPage() { return test(COLON); } |
802 | inline bool testImport() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("import" )); } |
803 | inline bool testMedia() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("media" )); } |
804 | inline bool testPage() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("page" )); } |
805 | inline bool testCombinator() { return test(PLUS) || test(GREATER) || test(TILDE) || test(S); } |
806 | inline bool testProperty() { return test(IDENT); } |
807 | bool testTerm(); |
808 | inline bool testExpr() { return testTerm(); } |
809 | inline bool parseNextExpr(QList<Value> *values) |
810 | { |
811 | if (!testExpr()) |
812 | return recordError(); |
813 | return parseExpr(values); |
814 | } |
815 | bool testPrio(); |
816 | inline bool testHexColor() { return test(HASH); } |
817 | inline bool testFunction() { return test(FUNCTION); } |
818 | inline bool parseNextFunction(QString *name, QString *args) { if (!testFunction()) return recordError(); return parseFunction(name, args); } |
819 | |
820 | inline bool lookupElementName() const { return lookup() == IDENT || lookup() == STAR; } |
821 | |
822 | inline void skipSpace() { while (test(S)) {}; } |
823 | |
824 | inline bool hasNext() const { return index < symbols.count(); } |
825 | inline TokenType next() { return symbols.at(index++).token; } |
826 | bool next(TokenType t); |
827 | bool test(TokenType t); |
828 | inline void prev() { index--; } |
829 | inline const Symbol &symbol() const { return symbols.at(index - 1); } |
830 | inline QString lexem() const { return symbol().lexem(); } |
831 | QString unquotedLexem() const; |
832 | QString lexemUntil(TokenType t); |
833 | bool until(TokenType target, TokenType target2 = NONE); |
834 | inline TokenType lookup() const { |
835 | return (index - 1) < symbols.count() ? symbols.at(index - 1).token : NONE; |
836 | } |
837 | |
838 | bool testTokenAndEndsWith(TokenType t, QLatin1String str); |
839 | |
840 | inline bool recordError() { errorIndex = index; return false; } |
841 | |
842 | QList<Symbol> symbols; |
843 | int index; |
844 | int errorIndex; |
845 | bool hasEscapeSequences; |
846 | QString sourcePath; |
847 | }; |
848 | |
849 | struct Q_GUI_EXPORT |
850 | { |
851 | (const QList<Declaration> &declarations, const QPalette & = QPalette()); |
852 | |
853 | bool (QFont *font, int *fontSizeAdjustment); |
854 | bool (QBrush *, QString *, Repeat *, Qt::Alignment *, QCss::Origin *, QCss::Attachment *, |
855 | QCss::Origin *); |
856 | bool (int *w, int *h, int *minw, int *minh, int *maxw, int *maxh); |
857 | bool (int *l, int *t, int *r, int *b, QCss::Origin *, Qt::Alignment *, |
858 | QCss::PositionMode *, Qt::Alignment *); |
859 | bool (int *margins, int *paddings, int *spacing = nullptr); |
860 | bool (int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii); |
861 | bool (int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii, int *offsets); |
862 | bool (QBrush *fg, QBrush *sfg, QBrush *sbg, QBrush *abg); |
863 | int (); |
864 | bool (QIcon *icon, Qt::Alignment *a, QSize *size); |
865 | bool (QIcon *icon, QSize *size); |
866 | |
867 | void (const Declaration &decl, int *m); |
868 | |
869 | private: |
870 | void (); |
871 | void (const Declaration &decl, int *width, QCss::BorderStyle *style, QBrush *color); |
872 | LengthData (const Value& v); |
873 | int (const Declaration &decl); |
874 | QSize (const Declaration &decl); |
875 | void (const Declaration &decl, QSize *radii); |
876 | |
877 | QList<Declaration> ; |
878 | QFont ; |
879 | int ; |
880 | int ; |
881 | QPalette ; |
882 | }; |
883 | |
884 | } // namespace QCss |
885 | |
886 | QT_END_NAMESPACE |
887 | |
888 | Q_DECLARE_METATYPE( QCss::BackgroundData ) |
889 | Q_DECLARE_METATYPE( QCss::LengthData ) |
890 | Q_DECLARE_METATYPE( QCss::BorderData ) |
891 | |
892 | #undef QT_CSS_DECLARE_TYPEINFO |
893 | |
894 | #endif // QT_NO_CSSPARSER |
895 | |
896 | #endif |
897 | |