1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/*
* Copyright (C) 2003 Lars Knoll (knoll@kde.org)
* Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef CSSParser_h
#define CSSParser_h
#include "AtomicString.h"
#include "Color.h"
#include "MediaQuery.h"
#include <wtf/HashSet.h>
#include <wtf/Vector.h>
namespace WebCore {
class CSSMutableStyleDeclaration;
class CSSPrimitiveValue;
class CSSProperty;
class CSSRule;
class CSSRuleList;
class CSSSelector;
class CSSStyleSheet;
class CSSValue;
class CSSValueList;
class Document;
class MediaList;
class MediaQueryExp;
class StyleBase;
class StyleList;
struct Function;
struct ParseString {
UChar* characters;
int length;
void lower();
operator String() const { return String(characters, length); }
operator AtomicString() const { return AtomicString(characters, length); }
};
struct Value {
int id;
bool isInt;
union {
double fValue;
int iValue;
ParseString string;
Function* function;
};
enum {
Operator = 0x100000,
Function = 0x100001,
Q_EMS = 0x100002
};
int unit;
};
class ValueList {
public:
ValueList() : m_current(0) { }
~ValueList();
void addValue(const Value& v) { m_values.append(v); }
unsigned size() const { return m_values.size(); }
Value* current() { return m_current < m_values.size() ? &m_values[m_current] : 0; }
Value* next() { ++m_current; return current(); }
Value* valueAt(unsigned i) { return i < m_values.size() ? &m_values[i] : 0; }
void deleteValueAt(unsigned i) { m_values.remove(i); }
private:
Vector<Value, 16> m_values;
unsigned m_current;
};
struct Function {
ParseString name;
ValueList* args;
~Function() { delete args; }
};
class CSSParser {
public:
CSSParser(bool strictParsing = true);
~CSSParser();
void parseSheet(CSSStyleSheet*, const String&);
PassRefPtr<CSSRule> parseRule(CSSStyleSheet*, const String&);
bool parseValue(CSSMutableStyleDeclaration*, int propId, const String&, bool important);
static bool parseColor(RGBA32& color, const String&, bool strict = false);
bool parseColor(CSSMutableStyleDeclaration*, const String&);
bool parseDeclaration(CSSMutableStyleDeclaration*, const String&);
bool parseMediaQuery(MediaList*, const String&);
Document* document() const;
void addProperty(int propId, PassRefPtr<CSSValue>, bool important);
void rollbackLastProperties(int num);
bool hasProperties() const { return m_numParsedProperties > 0; }
bool parseValue(int propId, bool important);
bool parseShorthand(int propId, const int* properties, int numProperties, bool important);
bool parse4Values(int propId, const int* properties, bool important);
bool parseContent(int propId, bool important);
PassRefPtr<CSSValue> parseBackgroundColor();
bool parseFillImage(RefPtr<CSSValue>&);
PassRefPtr<CSSValue> parseFillPositionXY(bool& xFound, bool& yFound);
void parseFillPosition(RefPtr<CSSValue>&, RefPtr<CSSValue>&);
PassRefPtr<CSSValue> parseFillSize();
bool parseFillProperty(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
bool parseFillShorthand(int propId, const int* properties, int numProperties, bool important);
void addFillValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
void addTransitionValue(RefPtr<CSSValue>& lval, PassRefPtr<CSSValue> rval);
PassRefPtr<CSSValue> parseTransitionDuration();
PassRefPtr<CSSValue> parseTransitionRepeatCount();
PassRefPtr<CSSValue> parseTransitionTimingFunction();
bool parseTimingFunctionValue(ValueList*& args, double& result);
PassRefPtr<CSSValue> parseTransitionProperty();
bool parseTransitionProperty(int propId, RefPtr<CSSValue>&);
bool parseTransitionShorthand(bool important);
bool parseDashboardRegions(int propId, bool important);
bool parseShape(int propId, bool important);
bool parseFont(bool important);
PassRefPtr<CSSValueList> parseFontFamily();
bool parseCounter(int propId, int defaultValue, bool important);
PassRefPtr<CSSValue> parseCounterContent(ValueList* args, bool counters);
bool parseColorParameters(Value*, int* colorValues, bool parseAlpha);
bool parseHSLParameters(Value*, double* colorValues, bool parseAlpha);
PassRefPtr<CSSPrimitiveValue> parseColor(Value* = 0);
bool parseColorFromValue(Value*, RGBA32&, bool = false);
static bool parseColor(const String&, RGBA32& rgb, bool strict);
bool parseFontFaceSrc();
bool parseFontFaceUnicodeRange();
#if ENABLE(SVG)
bool parseSVGValue(int propId, bool important);
PassRefPtr<CSSValue> parseSVGPaint();
PassRefPtr<CSSValue> parseSVGColor();
PassRefPtr<CSSValue> parseSVGStrokeDasharray();
#endif
// CSS3 Parsing Routines (for properties specific to CSS3)
bool parseShadow(int propId, bool important);
bool parseBorderImage(int propId, bool important, RefPtr<CSSValue>&);
bool parseReflect(int propId, bool important);
// Image generators
bool parseCanvas(RefPtr<CSSValue>&);
bool parseGradient(RefPtr<CSSValue>&);
PassRefPtr<CSSValueList> parseTransform();
bool parseTransformOrigin(int propId, int& propId1, int& propId2, RefPtr<CSSValue>&, RefPtr<CSSValue>&);
int yyparse();
CSSSelector* createFloatingSelector();
CSSSelector* sinkFloatingSelector(CSSSelector*);
ValueList* createFloatingValueList();
ValueList* sinkFloatingValueList(ValueList*);
Function* createFloatingFunction();
Function* sinkFloatingFunction(Function*);
Value& sinkFloatingValue(Value&);
MediaList* createMediaList();
CSSRule* createCharsetRule(const ParseString&);
CSSRule* createImportRule(const ParseString&, MediaList*);
CSSRule* createMediaRule(MediaList*, CSSRuleList*);
CSSRuleList* createRuleList();
CSSRule* createStyleRule(CSSSelector*);
CSSRule* createFontFaceRule();
MediaQueryExp* createFloatingMediaQueryExp(const AtomicString&, ValueList*);
MediaQueryExp* sinkFloatingMediaQueryExp(MediaQueryExp*);
Vector<MediaQueryExp*>* createFloatingMediaQueryExpList();
Vector<MediaQueryExp*>* sinkFloatingMediaQueryExpList(Vector<MediaQueryExp*>*);
MediaQuery* createFloatingMediaQuery(MediaQuery::Restrictor, const String&, Vector<MediaQueryExp*>*);
MediaQuery* createFloatingMediaQuery(Vector<MediaQueryExp*>*);
MediaQuery* sinkFloatingMediaQuery(MediaQuery*);
public:
bool m_strict;
bool m_important;
int m_id;
CSSStyleSheet* m_styleSheet;
RefPtr<CSSRule> m_rule;
MediaQuery* m_mediaQuery;
ValueList* m_valueList;
CSSProperty** m_parsedProperties;
int m_numParsedProperties;
int m_maxParsedProperties;
int m_inParseShorthand;
int m_currentShorthand;
bool m_implicitShorthand;
AtomicString m_defaultNamespace;
// tokenizer methods and data
public:
int lex(void* yylval);
int token() { return yyTok; }
UChar* text(int* length);
int lex();
private:
void clearProperties();
void setupParser(const char* prefix, const String&, const char* suffix);
bool inShorthand() const { return m_inParseShorthand; }
void checkForOrphanedUnits();
UChar* m_data;
UChar* yytext;
UChar* yy_c_buf_p;
UChar yy_hold_char;
int yy_last_accepting_state;
UChar* yy_last_accepting_cpos;
int yyleng;
int yyTok;
int yy_start;
Vector<RefPtr<StyleBase> > m_parsedStyleObjects;
Vector<RefPtr<CSSRuleList> > m_parsedRuleLists;
HashSet<CSSSelector*> m_floatingSelectors;
HashSet<ValueList*> m_floatingValueLists;
HashSet<Function*> m_floatingFunctions;
MediaQuery* m_floatingMediaQuery;
MediaQueryExp* m_floatingMediaQueryExp;
Vector<MediaQueryExp*>* m_floatingMediaQueryExpList;
// defines units allowed for a certain property, used in parseUnit
enum Units {
FUnknown = 0x0000,
FInteger = 0x0001,
FNumber = 0x0002, // Real Numbers
FPercent = 0x0004,
FLength = 0x0008,
FAngle = 0x0010,
FTime = 0x0020,
FFrequency = 0x0040,
FRelative = 0x0100,
FNonNeg = 0x0200
};
friend inline Units operator|(Units a, Units b)
{
return static_cast<Units>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
}
static bool validUnit(Value*, Units, bool strict);
friend class TransformOperationInfo;
};
int cssPropertyID(const ParseString&);
int cssPropertyID(const String&);
int cssValueKeywordID(const ParseString&);
class ShorthandScope {
public:
ShorthandScope(CSSParser* parser, int propId) : m_parser(parser)
{
if (!(m_parser->m_inParseShorthand++))
m_parser->m_currentShorthand = propId;
}
~ShorthandScope()
{
if (!(--m_parser->m_inParseShorthand))
m_parser->m_currentShorthand = 0;
}
private:
CSSParser* m_parser;
};
} // namespace WebCore
#endif // CSSParser_h
|