1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QCSSPARSER_P_H
43 #define QCSSPARSER_P_H
49 // This file is not part of the Qt API. It exists for the convenience
50 // of the QLibrary class. This header file may change from
51 // version to version without notice, or even be removed.
56 #include <QtCore/QStringList>
57 #include <QtCore/QVector>
58 #include <QtCore/QVariant>
59 #include <QtCore/QPair>
60 #include <QtCore/QSize>
61 #include <QtCore/QMultiHash>
62 #include <QtGui/QFont>
63 #include <QtGui/QPalette>
64 #include <QtCore/QSharedData>
70 #ifndef QT_NO_CSSPARSER
72 // VxWorks defines NONE as (-1) "for times when NULL won't do"
73 #if defined(Q_OS_VXWORKS) && defined(NONE)
76 #if defined(Q_OS_INTEGRITY)
110 QtSelectionForeground,
111 QtSelectionBackground,
124 QtAlternateBackground,
141 BorderTopRightRadius,
142 BorderBottomLeftRadius,
143 BorderBottomRightRadius,
150 BackgroundAttachment,
180 OutlineTopLeftRadius,
181 OutlineTopRightRadius,
182 OutlineBottomLeftRadius,
183 OutlineBottomRightRadius,
245 /* keep these in same order as QPalette::ColorRole */
246 Value_FirstColorRole,
247 Value_WindowText = Value_FirstColorRole,
260 Value_HighlightedText,
264 Value_LastColorRole = Value_AlternateBase,
283 BorderStyle_DotDotDash,
334 PositionMode_Unknown,
336 PositionMode_Relative,
337 PositionMode_Absolute,
339 NumKnownPositionModes
350 StyleFeature_None = 0,
351 StyleFeature_BackgroundColor = 1,
352 StyleFeature_BackgroundGradient = 2,
353 NumKnownStyleFeatures = 4
356 struct Q_GUI_EXPORT Value
372 inline Value() : type(Unknown) { }
375 QString toString() const;
379 ColorData() : role(QPalette::NoRole), type(Invalid) {}
380 ColorData(const QColor &col) : color(col), role(QPalette::NoRole), type(Color) {}
381 ColorData(QPalette::ColorRole r) : role(r), type(Role) {}
383 QPalette::ColorRole role;
384 enum { Invalid, Color, Role} type;
388 BrushData() : role(QPalette::NoRole), type(Invalid) {}
389 BrushData(const QBrush &br) : brush(br), role(QPalette::NoRole), type(Brush) {}
390 BrushData(QPalette::ColorRole r) : role(r), type(Role) {}
392 QPalette::ColorRole role;
393 enum { Invalid, Brush, Role, DependsOnThePalette } type;
396 struct BackgroundData {
400 Qt::Alignment alignment;
405 enum { None, Px, Ex, Em } unit;
415 // 1. StyleRule - x:hover, y:clicked > z:checked { prop1: value1; prop2: value2; }
416 // 2. QVector<Selector> - x:hover, y:clicked z:checked
417 // 3. QVector<BasicSelector> - y:clicked z:checked
418 // 4. QVector<Declaration> - { prop1: value1; prop2: value2; }
419 // 5. Declaration - prop1: value1;
421 struct Q_GUI_EXPORT Declaration
423 struct DeclarationData : public QSharedData
425 inline DeclarationData() : propertyId(UnknownProperty), important(false) {}
428 QVector<Value> values;
432 QExplicitlySharedDataPointer<DeclarationData> d;
433 inline Declaration() : d(new DeclarationData()) {}
434 inline bool isEmpty() const { return d->property.isEmpty() && d->propertyId == UnknownProperty; }
437 QColor colorValue(const QPalette & = QPalette()) const;
438 void colorValues(QColor *c, const QPalette & = QPalette()) const;
439 QBrush brushValue(const QPalette & = QPalette()) const;
440 void brushValues(QBrush *c, const QPalette & = QPalette()) const;
442 BorderStyle styleValue() const;
443 void styleValues(BorderStyle *s) const;
445 Origin originValue() const;
446 Repeat repeatValue() const;
447 Qt::Alignment alignmentValue() const;
448 PositionMode positionValue() const;
449 Attachment attachmentValue() const;
450 int styleFeaturesValue() const;
452 bool intValue(int *i, const char *unit = 0) const;
453 bool realValue(qreal *r, const char *unit = 0) const;
455 QSize sizeValue() const;
456 QRect rectValue() const;
457 QString uriValue() const;
458 QIcon iconValue() const;
460 void borderImageValue(QString *image, int *cuts, TileMode *h, TileMode *v) const;
463 const quint64 PseudoClass_Unknown = Q_UINT64_C(0x0000000000000000);
464 const quint64 PseudoClass_Enabled = Q_UINT64_C(0x0000000000000001);
465 const quint64 PseudoClass_Disabled = Q_UINT64_C(0x0000000000000002);
466 const quint64 PseudoClass_Pressed = Q_UINT64_C(0x0000000000000004);
467 const quint64 PseudoClass_Focus = Q_UINT64_C(0x0000000000000008);
468 const quint64 PseudoClass_Hover = Q_UINT64_C(0x0000000000000010);
469 const quint64 PseudoClass_Checked = Q_UINT64_C(0x0000000000000020);
470 const quint64 PseudoClass_Unchecked = Q_UINT64_C(0x0000000000000040);
471 const quint64 PseudoClass_Indeterminate = Q_UINT64_C(0x0000000000000080);
472 const quint64 PseudoClass_Unspecified = Q_UINT64_C(0x0000000000000100);
473 const quint64 PseudoClass_Selected = Q_UINT64_C(0x0000000000000200);
474 const quint64 PseudoClass_Horizontal = Q_UINT64_C(0x0000000000000400);
475 const quint64 PseudoClass_Vertical = Q_UINT64_C(0x0000000000000800);
476 const quint64 PseudoClass_Window = Q_UINT64_C(0x0000000000001000);
477 const quint64 PseudoClass_Children = Q_UINT64_C(0x0000000000002000);
478 const quint64 PseudoClass_Sibling = Q_UINT64_C(0x0000000000004000);
479 const quint64 PseudoClass_Default = Q_UINT64_C(0x0000000000008000);
480 const quint64 PseudoClass_First = Q_UINT64_C(0x0000000000010000);
481 const quint64 PseudoClass_Last = Q_UINT64_C(0x0000000000020000);
482 const quint64 PseudoClass_Middle = Q_UINT64_C(0x0000000000040000);
483 const quint64 PseudoClass_OnlyOne = Q_UINT64_C(0x0000000000080000);
484 const quint64 PseudoClass_PreviousSelected = Q_UINT64_C(0x0000000000100000);
485 const quint64 PseudoClass_NextSelected = Q_UINT64_C(0x0000000000200000);
486 const quint64 PseudoClass_Flat = Q_UINT64_C(0x0000000000400000);
487 const quint64 PseudoClass_Left = Q_UINT64_C(0x0000000000800000);
488 const quint64 PseudoClass_Right = Q_UINT64_C(0x0000000001000000);
489 const quint64 PseudoClass_Top = Q_UINT64_C(0x0000000002000000);
490 const quint64 PseudoClass_Bottom = Q_UINT64_C(0x0000000004000000);
491 const quint64 PseudoClass_Exclusive = Q_UINT64_C(0x0000000008000000);
492 const quint64 PseudoClass_NonExclusive = Q_UINT64_C(0x0000000010000000);
493 const quint64 PseudoClass_Frameless = Q_UINT64_C(0x0000000020000000);
494 const quint64 PseudoClass_ReadOnly = Q_UINT64_C(0x0000000040000000);
495 const quint64 PseudoClass_Active = Q_UINT64_C(0x0000000080000000);
496 const quint64 PseudoClass_Closable = Q_UINT64_C(0x0000000100000000);
497 const quint64 PseudoClass_Movable = Q_UINT64_C(0x0000000200000000);
498 const quint64 PseudoClass_Floatable = Q_UINT64_C(0x0000000400000000);
499 const quint64 PseudoClass_Minimized = Q_UINT64_C(0x0000000800000000);
500 const quint64 PseudoClass_Maximized = Q_UINT64_C(0x0000001000000000);
501 const quint64 PseudoClass_On = Q_UINT64_C(0x0000002000000000);
502 const quint64 PseudoClass_Off = Q_UINT64_C(0x0000004000000000);
503 const quint64 PseudoClass_Editable = Q_UINT64_C(0x0000008000000000);
504 const quint64 PseudoClass_Item = Q_UINT64_C(0x0000010000000000);
505 const quint64 PseudoClass_Closed = Q_UINT64_C(0x0000020000000000);
506 const quint64 PseudoClass_Open = Q_UINT64_C(0x0000040000000000);
507 const quint64 PseudoClass_EditFocus = Q_UINT64_C(0x0000080000000000);
508 const quint64 PseudoClass_Alternate = Q_UINT64_C(0x0000100000000000);
509 // The Any specifier is never generated, but can be used as a wildcard in searches.
510 const quint64 PseudoClass_Any = Q_UINT64_C(0x0000ffffffffffff);
511 const int NumPseudos = 46;
515 Pseudo() : type(0), negated(false) { }
522 struct AttributeSelector
524 enum ValueMatchType {
530 inline AttributeSelector() : valueMatchCriterium(NoMatch) {}
534 ValueMatchType valueMatchCriterium;
539 inline BasicSelector() : relationToNext(NoRelation) {}
543 MatchNextSelectorIfAncestor,
544 MatchNextSelectorIfParent,
545 MatchNextSelectorIfPreceeds
551 QVector<Pseudo> pseudos;
552 QVector<AttributeSelector> attributeSelectors;
554 Relation relationToNext;
557 struct Q_GUI_EXPORT Selector
559 QVector<BasicSelector> basicSelectors;
560 int specificity() const;
561 quint64 pseudoClass(quint64 *negated = 0) const;
562 QString pseudoElement() const;
570 struct Q_GUI_EXPORT ValueExtractor
572 ValueExtractor(const QVector<Declaration> &declarations, const QPalette & = QPalette());
574 bool extractFont(QFont *font, int *fontSizeAdjustment);
575 bool extractBackground(QBrush *, QString *, Repeat *, Qt::Alignment *, QCss::Origin *, QCss::Attachment *,
577 bool extractGeometry(int *w, int *h, int *minw, int *minh, int *maxw, int *maxh);
578 bool extractPosition(int *l, int *t, int *r, int *b, QCss::Origin *, Qt::Alignment *,
579 QCss::PositionMode *, Qt::Alignment *);
580 bool extractBox(int *margins, int *paddings, int *spacing = 0);
581 bool extractBorder(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii);
582 bool extractOutline(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii, int *offsets);
583 bool extractPalette(QBrush *fg, QBrush *sfg, QBrush *sbg, QBrush *abg);
584 int extractStyleFeatures();
585 bool extractImage(QIcon *icon, Qt::Alignment *a, QSize *size);
587 int lengthValue(const Declaration &decl);
591 void borderValue(const Declaration &decl, int *width, QCss::BorderStyle *style, QBrush *color);
592 LengthData lengthValue(const Value& v);
593 void lengthValues(const Declaration &decl, int *m);
594 QSize sizeValue(const Declaration &decl);
595 void sizeValues(const Declaration &decl, QSize *radii);
597 QVector<Declaration> declarations;
606 StyleRule() : order(0) { }
607 QVector<Selector> selectors;
608 QVector<Declaration> declarations;
615 QVector<StyleRule> styleRules;
621 QVector<Declaration> declarations;
630 enum StyleSheetOrigin {
631 StyleSheetOrigin_Unspecified,
632 StyleSheetOrigin_UserAgent,
633 StyleSheetOrigin_User,
634 StyleSheetOrigin_Author,
635 StyleSheetOrigin_Inline
638 struct Q_GUI_EXPORT StyleSheet
640 StyleSheet() : origin(StyleSheetOrigin_Unspecified), depth(0) { }
641 QVector<StyleRule> styleRules; //only contains rules that are not indexed
642 QVector<MediaRule> mediaRules;
643 QVector<PageRule> pageRules;
644 QVector<ImportRule> importRules;
645 StyleSheetOrigin origin;
646 int depth; // applicable only for inline style sheets
647 QMultiHash<QString, StyleRule> nameIndex;
648 QMultiHash<QString, StyleRule> idIndex;
649 void buildIndexes(Qt::CaseSensitivity nameCaseSensitivity = Qt::CaseSensitive);
652 class Q_GUI_EXPORT StyleSelector
655 StyleSelector() : nameCaseSensitivity(Qt::CaseSensitive) {}
656 virtual ~StyleSelector();
663 QVector<StyleRule> styleRulesForNode(NodePtr node);
664 QVector<Declaration> declarationsForNode(NodePtr node, const char *extraPseudo = 0);
666 virtual bool nodeNameEquals(NodePtr node, const QString& nodeName) const;
667 virtual QString attribute(NodePtr node, const QString &name) const = 0;
668 virtual bool hasAttributes(NodePtr node) const = 0;
669 virtual QStringList nodeIds(NodePtr node) const;
670 virtual QStringList nodeNames(NodePtr node) const = 0;
671 virtual bool isNullNode(NodePtr node) const = 0;
672 virtual NodePtr parentNode(NodePtr node) const = 0;
673 virtual NodePtr previousSiblingNode(NodePtr node) const = 0;
674 virtual NodePtr duplicateNode(NodePtr node) const = 0;
675 virtual void freeNode(NodePtr node) const = 0;
677 QVector<StyleSheet> styleSheets;
679 Qt::CaseSensitivity nameCaseSensitivity;
681 void matchRule(NodePtr node, const StyleRule &rules, StyleSheetOrigin origin,
682 int depth, QMap<uint, StyleRule> *weightedRules);
683 bool selectorMatches(const Selector &rule, NodePtr node);
684 bool basicSelectorMatches(const BasicSelector &rule, NodePtr node);
735 struct Q_GUI_EXPORT Symbol
737 inline Symbol() : token(NONE), start(0), len(-1) {}
741 QString lexem() const;
744 class Q_GUI_EXPORT Scanner
747 static QString preprocess(const QString &input, bool *hasEscapeSequences = 0);
748 static void scan(const QString &preprocessedInput, QVector<Symbol> *symbols);
751 class Q_GUI_EXPORT Parser
755 Parser(const QString &css, bool file = false);
757 void init(const QString &css, bool file = false);
758 bool parse(StyleSheet *styleSheet, Qt::CaseSensitivity nameCaseSensitivity = Qt::CaseSensitive);
759 Symbol errorSymbol();
761 bool parseImport(ImportRule *importRule);
762 bool parseMedia(MediaRule *mediaRule);
763 bool parseMedium(QStringList *media);
764 bool parsePage(PageRule *pageRule);
765 bool parsePseudoPage(QString *selector);
766 bool parseNextOperator(Value *value);
767 bool parseCombinator(BasicSelector::Relation *relation);
768 bool parseProperty(Declaration *decl);
769 bool parseRuleset(StyleRule *styleRule);
770 bool parseSelector(Selector *sel);
771 bool parseSimpleSelector(BasicSelector *basicSel);
772 bool parseClass(QString *name);
773 bool parseElementName(QString *name);
774 bool parseAttrib(AttributeSelector *attr);
775 bool parsePseudo(Pseudo *pseudo);
776 bool parseNextDeclaration(Declaration *declaration);
777 bool parsePrio(Declaration *declaration);
778 bool parseExpr(QVector<Value> *values);
779 bool parseTerm(Value *value);
780 bool parseFunction(QString *name, QString *args);
781 bool parseHexColor(QColor *col);
782 bool testAndParseUri(QString *uri);
784 inline bool testRuleset() { return testSelector(); }
785 inline bool testSelector() { return testSimpleSelector(); }
786 inline bool parseNextSelector(Selector *sel) { if (!testSelector()) return recordError(); return parseSelector(sel); }
787 bool testSimpleSelector();
788 inline bool parseNextSimpleSelector(BasicSelector *basicSel) { if (!testSimpleSelector()) return recordError(); return parseSimpleSelector(basicSel); }
789 inline bool testElementName() { return test(IDENT) || test(STAR); }
790 inline bool testClass() { return test(DOT); }
791 inline bool testAttrib() { return test(LBRACKET); }
792 inline bool testPseudo() { return test(COLON); }
793 inline bool testMedium() { return test(IDENT); }
794 inline bool parseNextMedium(QStringList *media) { if (!testMedium()) return recordError(); return parseMedium(media); }
795 inline bool testPseudoPage() { return test(COLON); }
796 inline bool testImport() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("import")); }
797 inline bool testMedia() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("media")); }
798 inline bool testPage() { return testTokenAndEndsWith(ATKEYWORD_SYM, QLatin1String("page")); }
799 inline bool testCombinator() { return test(PLUS) || test(GREATER) || test(S); }
800 inline bool testProperty() { return test(IDENT); }
802 inline bool testExpr() { return testTerm(); }
803 inline bool parseNextExpr(QVector<Value> *values) { if (!testExpr()) return recordError(); return parseExpr(values); }
805 inline bool testHexColor() { return test(HASH); }
806 inline bool testFunction() { return test(FUNCTION); }
807 inline bool parseNextFunction(QString *name, QString *args) { if (!testFunction()) return recordError(); return parseFunction(name, args); }
809 inline bool lookupElementName() const { return lookup() == IDENT || lookup() == STAR; }
811 inline void skipSpace() { while (test(S)) {}; }
813 inline bool hasNext() const { return index < symbols.count(); }
814 inline TokenType next() { return symbols.at(index++).token; }
815 bool next(TokenType t);
816 bool test(TokenType t);
817 inline void prev() { index--; }
818 inline const Symbol &symbol() const { return symbols.at(index - 1); }
819 inline QString lexem() const { return symbol().lexem(); }
820 QString unquotedLexem() const;
821 QString lexemUntil(TokenType t);
822 bool until(TokenType target, TokenType target2 = NONE);
823 inline TokenType lookup() const {
824 return (index - 1) < symbols.count() ? symbols.at(index - 1).token : NONE;
827 bool testTokenAndEndsWith(TokenType t, QLatin1String str);
829 inline bool recordError() { errorIndex = index; return false; }
831 QVector<Symbol> symbols;
834 bool hasEscapeSequences;
842 Q_DECLARE_METATYPE( QCss::BackgroundData )
843 Q_DECLARE_METATYPE( QCss::LengthData )
844 Q_DECLARE_METATYPE( QCss::BorderData )
847 #endif // QT_NO_CSSPARSER