cssparser.h

00001 /*
00002  * This file is part of the DOM implementation for KDE.
00003  *
00004  * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 #ifndef _CSS_cssparser_h_
00022 #define _CSS_cssparser_h_
00023 
00024 #include <qstring.h>
00025 #include <qcolor.h>
00026 #include <dom/dom_string.h>
00027 
00028 namespace DOM {
00029     class StyleListImpl;
00030     class CSSStyleSheetImpl;
00031     class CSSRuleImpl;
00032     class CSSStyleRuleImpl;
00033     class DocumentImpl;
00034     class CSSValueImpl;
00035     class CSSValueListImpl;
00036     class CSSPrimitiveValueImpl;
00037     class CSSStyleDeclarationImpl;
00038     class CSSProperty;
00039     class CSSRuleListImpl;
00040 
00041 
00042     struct ParseString {
00043     unsigned short *string;
00044     int length;
00045     };
00046 
00047     struct Value;
00048     class ValueList;
00049 
00050     struct Function {
00051     ParseString name;
00052     ValueList *args;
00053     };
00054 
00055     struct Value {
00056     int id;
00057     union {
00058         double fValue;
00059         int iValue;
00060         ParseString string;
00061         struct Function *function;
00062     };
00063     enum {
00064         Operator = 0x100000,
00065         Function = 0x100001,
00066         Q_EMS     = 0x100002
00067     };
00068 
00069     int unit;
00070     };
00071 
00072     static inline QString qString( const ParseString &ps ) {
00073     return QString( (QChar *)ps.string, ps.length );
00074     }
00075     static inline DOMString domString( const ParseString &ps ) {
00076     return DOMString( (QChar *)ps.string, ps.length );
00077     }
00078 
00079     class ValueList {
00080     public:
00081     ValueList();
00082     ~ValueList();
00083     void addValue( const Value &val );
00084     Value *current() { return currentValue < numValues ? values + currentValue : 0; }
00085     Value *next() { ++currentValue; return current(); }
00086         bool isLast() const { return currentValue+1 >= numValues; }
00087     Value *values;
00088     int numValues;
00089     int maxValues;
00090     int currentValue;
00091     };
00092 
00093     class CSSParser
00094     {
00095     public:
00096     CSSParser( bool strictParsing = true );
00097     ~CSSParser();
00098 
00099     void parseSheet( DOM::CSSStyleSheetImpl *sheet, const DOM::DOMString &string );
00100     DOM::CSSRuleImpl *parseRule( DOM::CSSStyleSheetImpl *sheet, const DOM::DOMString &string );
00101     bool parseValue( DOM::CSSStyleDeclarationImpl *decls, int id, const DOM::DOMString &string,
00102              bool _important, bool _nonCSSHint );
00103     bool parseDeclaration( DOM::CSSStyleDeclarationImpl *decls, const DOM::DOMString &string,
00104                    bool _nonCSSHint );
00105 
00106     static CSSParser *current() { return currentParser; }
00107 
00108 
00109     DOM::DocumentImpl *document() const;
00110 
00111     unsigned int defaultNamespace();
00112 
00113     void addProperty( int propId, CSSValueImpl *value, bool important );
00114     bool hasProperties() const { return numParsedProperties > 0; }
00115     CSSStyleDeclarationImpl *createStyleDeclaration( CSSStyleRuleImpl *rule );
00116     void clearProperties();
00117 
00118     bool parseValue( int propId, bool important, int expected=1 );
00119     bool parseShortHand( const int *properties, int numProperties, bool important );
00120     bool parse4Values( const int *properties, bool important );
00121     bool parseContent( int propId, bool important );
00122 
00123         CSSValueImpl* parseBackgroundColor();
00124         CSSValueImpl* parseBackgroundImage();
00125         CSSValueImpl* parseBackgroundPositionXY(bool& xFound, bool& yFound);
00126         void parseBackgroundPosition(CSSValueImpl*& value1, CSSValueImpl*& value2);
00127 
00128         bool parseBackgroundProperty(int propId, int& propId1, int& propId2, CSSValueImpl*& retValue1, CSSValueImpl*& retValue2);
00129         bool parseBackgroundShorthand(bool important);
00130 
00131         void addBackgroundValue(CSSValueImpl*& lval, CSSValueImpl* rval);
00132 
00133     bool parseShape( int propId, bool important );
00134     bool parseFont(bool important);
00135         bool parseCounter(int propId, bool increment, bool important);
00136 
00137         // returns the found property
00138         // 0 if nothing found (or ok == false)
00139         // @param forward if true, it parses the next in the list
00140     CSSValueListImpl *parseFontFamily();
00141         CSSPrimitiveValueImpl *parseColor();
00142         CSSPrimitiveValueImpl *parseColorFromValue(Value* val);
00143         CSSValueImpl* parseCounterContent(ValueList *args, bool counters);
00144 
00145         static bool parseColor(const QString &name, QRgb& rgb);
00146 
00147         // CSS3 Parsing Routines (for properties specific to CSS3)
00148         bool parseShadow(int propId, bool important);
00149 
00150 
00151     public:
00152     bool strict;
00153     bool important;
00154     bool nonCSSHint;
00155     unsigned int id;
00156     DOM::StyleListImpl* styleElement;
00157     DOM::CSSRuleImpl *rule;
00158     ValueList *valueList;
00159     CSSProperty **parsedProperties;
00160     int numParsedProperties;
00161     int maxParsedProperties;
00162     bool inParseShortHand;
00163     static CSSParser *currentParser;
00164 
00165     // tokenizer methods and data
00166     public:
00167     int lex( void *yylval );
00168     int token() { return yyTok; }
00169     unsigned short *text( int *length);
00170     int lex();
00171     private:
00172     int yyparse();
00173         void runParser(int length);
00174 
00175     unsigned short *data;
00176     unsigned short *yytext;
00177     unsigned short *yy_c_buf_p;
00178     unsigned short yy_hold_char;
00179     int yy_last_accepting_state;
00180     unsigned short *yy_last_accepting_cpos;
00181         int block_nesting;
00182     int yyleng;
00183     int yyTok;
00184     int yy_start;
00185     };
00186 
00187 } // namespace
00188 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys