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

source code of qtbase/src/gui/text/qcssparser_p.h