2 Copyright (C) 2008 Eric Seidel <eric@webkit.org>
3 Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
4 2004, 2005, 2007, 2010 Rob Buis <buis@kde.org>
5 Copyright (C) 2005, 2006 Apple Computer, Inc.
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
25 #include "CSSPropertyNames.h"
26 #include "CSSValueKeywords.h"
27 #include "RuntimeEnabledFeatures.h"
28 #include "core/css/CSSParser.h"
29 #include "core/css/CSSValueList.h"
30 #include "core/rendering/RenderTheme.h"
31 #include "core/svg/SVGPaint.h"
37 static bool isSystemColor(int id)
39 return (id >= CSSValueActiveborder && id <= CSSValueWindowtext) || id == CSSValueMenu;
42 bool CSSParser::parseSVGValue(CSSPropertyID propId, bool important)
44 CSSParserValue* value = m_valueList->current();
48 CSSValueID id = value->id;
50 bool valid_primitive = false;
51 RefPtr<CSSValue> parsedValue;
54 /* The comment to the right defines all valid value of these
55 * properties as defined in SVG 1.1, Appendix N. Property index */
56 case CSSPropertyAlignmentBaseline:
57 // auto | baseline | before-edge | text-before-edge | middle |
58 // central | after-edge | text-after-edge | ideographic | alphabetic |
59 // hanging | mathematical | inherit
60 if (id == CSSValueAuto || id == CSSValueBaseline || id == CSSValueMiddle ||
61 (id >= CSSValueBeforeEdge && id <= CSSValueMathematical))
62 valid_primitive = true;
65 case CSSPropertyBaselineShift:
66 // baseline | super | sub | <percentage> | <length> | inherit
67 if (id == CSSValueBaseline || id == CSSValueSub ||
69 valid_primitive = true;
71 valid_primitive = validUnit(value, FLength | FPercent, SVGAttributeMode);
74 case CSSPropertyDominantBaseline:
75 // auto | use-script | no-change | reset-size | ideographic |
76 // alphabetic | hanging | mathematical | central | middle |
77 // text-after-edge | text-before-edge | inherit
78 if (id == CSSValueAuto || id == CSSValueMiddle ||
79 (id >= CSSValueUseScript && id <= CSSValueResetSize) ||
80 (id >= CSSValueCentral && id <= CSSValueMathematical))
81 valid_primitive = true;
84 case CSSPropertyEnableBackground:
85 // accumulate | new [x] [y] [width] [height] | inherit
86 if (id == CSSValueAccumulate) // TODO : new
87 valid_primitive = true;
90 case CSSPropertyMarkerStart:
91 case CSSPropertyMarkerMid:
92 case CSSPropertyMarkerEnd:
94 if (id == CSSValueNone)
95 valid_primitive = true;
96 else if (value->unit == CSSPrimitiveValue::CSS_URI) {
97 parsedValue = CSSPrimitiveValue::create(value->string, CSSPrimitiveValue::CSS_URI);
103 case CSSPropertyClipRule: // nonzero | evenodd | inherit
104 case CSSPropertyFillRule:
105 if (id == CSSValueNonzero || id == CSSValueEvenodd)
106 valid_primitive = true;
109 case CSSPropertyStrokeMiterlimit: // <miterlimit> | inherit
110 valid_primitive = validUnit(value, FNumber | FNonNeg, SVGAttributeMode);
113 case CSSPropertyStrokeLinejoin: // miter | round | bevel | inherit
114 if (id == CSSValueMiter || id == CSSValueRound || id == CSSValueBevel)
115 valid_primitive = true;
118 case CSSPropertyStrokeLinecap: // butt | round | square | inherit
119 if (id == CSSValueButt || id == CSSValueRound || id == CSSValueSquare)
120 valid_primitive = true;
123 case CSSPropertyStrokeOpacity: // <opacity-value> | inherit
124 case CSSPropertyFillOpacity:
125 case CSSPropertyStopOpacity:
126 case CSSPropertyFloodOpacity:
127 valid_primitive = (!id && validUnit(value, FNumber | FPercent, SVGAttributeMode));
130 case CSSPropertyShapeRendering:
131 // auto | optimizeSpeed | crispEdges | geometricPrecision | inherit
132 if (id == CSSValueAuto || id == CSSValueOptimizespeed ||
133 id == CSSValueCrispedges || id == CSSValueGeometricprecision)
134 valid_primitive = true;
137 case CSSPropertyImageRendering: // auto | optimizeSpeed |
138 case CSSPropertyColorRendering: // optimizeQuality | inherit
139 if (id == CSSValueAuto || id == CSSValueOptimizespeed ||
140 id == CSSValueOptimizequality)
141 valid_primitive = true;
144 case CSSPropertyBufferedRendering: // auto | dynamic | static
145 if (id == CSSValueAuto || id == CSSValueDynamic || id == CSSValueStatic)
146 valid_primitive = true;
149 case CSSPropertyColorProfile: // auto | sRGB | <name> | <uri> inherit
150 if (id == CSSValueAuto || id == CSSValueSrgb)
151 valid_primitive = true;
154 case CSSPropertyColorInterpolation: // auto | sRGB | linearRGB | inherit
155 case CSSPropertyColorInterpolationFilters:
156 if (id == CSSValueAuto || id == CSSValueSrgb || id == CSSValueLinearrgb)
157 valid_primitive = true;
160 /* Start of supported CSS properties with validation. This is needed for parseShortHand to work
161 * correctly and allows optimization in applyRule(..)
164 case CSSPropertyTextAnchor: // start | middle | end | inherit
165 if (id == CSSValueStart || id == CSSValueMiddle || id == CSSValueEnd)
166 valid_primitive = true;
169 case CSSPropertyGlyphOrientationVertical: // auto | <angle> | inherit
170 if (id == CSSValueAuto) {
171 valid_primitive = true;
174 /* fallthrough intentional */
175 case CSSPropertyGlyphOrientationHorizontal: // <angle> (restricted to _deg_ per SVG 1.1 spec) | inherit
176 if (value->unit == CSSPrimitiveValue::CSS_DEG || value->unit == CSSPrimitiveValue::CSS_NUMBER) {
177 parsedValue = CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::CSS_DEG);
184 case CSSPropertyFill: // <paint> | inherit
185 case CSSPropertyStroke: // <paint> | inherit
187 if (id == CSSValueNone)
188 parsedValue = SVGPaint::createNone();
189 else if (id == CSSValueCurrentcolor)
190 parsedValue = SVGPaint::createCurrentColor();
191 else if (isSystemColor(id))
192 parsedValue = SVGPaint::createColor(RenderTheme::theme().systemColor(id));
193 else if (value->unit == CSSPrimitiveValue::CSS_URI) {
194 RGBA32 c = Color::transparent;
195 if (m_valueList->next()) {
196 if (parseColorFromValue(m_valueList->current(), c))
197 parsedValue = SVGPaint::createURIAndColor(value->string, c);
198 else if (m_valueList->current()->id == CSSValueNone)
199 parsedValue = SVGPaint::createURIAndNone(value->string);
202 parsedValue = SVGPaint::createURI(value->string);
204 parsedValue = parseSVGPaint();
211 case CSSPropertyStopColor: // TODO : icccolor
212 case CSSPropertyFloodColor:
213 case CSSPropertyLightingColor:
214 if (isSystemColor(id))
215 parsedValue = SVGColor::createFromColor(RenderTheme::theme().systemColor(id));
216 else if ((id >= CSSValueAqua && id <= CSSValueTransparent) ||
217 (id >= CSSValueAliceblue && id <= CSSValueYellowgreen) || id == CSSValueGrey)
218 parsedValue = SVGColor::createFromString(value->string);
219 else if (id == CSSValueCurrentcolor)
220 parsedValue = SVGColor::createCurrentColor();
221 else // TODO : svgcolor (iccColor)
222 parsedValue = parseSVGColor();
229 case CSSPropertyPaintOrder:
230 if (!RuntimeEnabledFeatures::svgPaintOrderEnabled())
233 if (m_valueList->size() == 1 && id == CSSValueNormal)
234 valid_primitive = true;
235 else if ((parsedValue = parsePaintOrder()))
239 case CSSPropertyVectorEffect: // none | non-scaling-stroke | inherit
240 if (id == CSSValueNone || id == CSSValueNonScalingStroke)
241 valid_primitive = true;
244 case CSSPropertyWritingMode:
245 // lr-tb | rl_tb | tb-rl | lr | rl | tb | inherit
246 if (id == CSSValueLrTb || id == CSSValueRlTb || id == CSSValueTbRl || id == CSSValueLr || id == CSSValueRl || id == CSSValueTb)
247 valid_primitive = true;
250 case CSSPropertyStrokeWidth: // <length> | inherit
251 case CSSPropertyStrokeDashoffset:
252 valid_primitive = validUnit(value, FLength | FPercent, SVGAttributeMode);
254 case CSSPropertyStrokeDasharray: // none | <dasharray> | inherit
255 if (id == CSSValueNone)
256 valid_primitive = true;
258 parsedValue = parseSVGStrokeDasharray();
262 case CSSPropertyKerning: // auto | normal | <length> | inherit
263 if (id == CSSValueAuto || id == CSSValueNormal)
264 valid_primitive = true;
266 valid_primitive = validUnit(value, FLength, SVGAttributeMode);
269 case CSSPropertyClipPath: // <uri> | none | inherit
270 case CSSPropertyFilter:
271 if (id == CSSValueNone)
272 valid_primitive = true;
273 else if (value->unit == CSSPrimitiveValue::CSS_URI) {
274 parsedValue = CSSPrimitiveValue::create(value->string, (CSSPrimitiveValue::UnitTypes) value->unit);
279 case CSSPropertyMaskType: // luminance | alpha | inherit
280 if (id == CSSValueLuminance || id == CSSValueAlpha)
281 valid_primitive = true;
284 /* shorthand properties */
285 case CSSPropertyMarker:
287 ShorthandScope scope(this, propId);
288 CSSParser::ImplicitScope implicitScope(this, PropertyImplicit);
289 if (!parseValue(CSSPropertyMarkerStart, important))
291 if (m_valueList->current()) {
292 rollbackLastProperties(1);
295 CSSValue* value = m_parsedProperties.last().value();
296 addProperty(CSSPropertyMarkerMid, value, important);
297 addProperty(CSSPropertyMarkerEnd, value, important);
301 // If you crash here, it's because you added a css property and are not handling it
302 // in either this switch statement or the one in CSSParser::parseValue
303 ASSERT_WITH_MESSAGE(0, "unimplemented propertyID: %d", propId);
307 if (valid_primitive) {
309 parsedValue = CSSPrimitiveValue::createIdentifier(id);
310 else if (value->unit == CSSPrimitiveValue::CSS_STRING)
311 parsedValue = CSSPrimitiveValue::create(value->string, (CSSPrimitiveValue::UnitTypes) value->unit);
312 else if (value->unit >= CSSPrimitiveValue::CSS_NUMBER && value->unit <= CSSPrimitiveValue::CSS_KHZ)
313 parsedValue = CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit);
314 else if (value->unit >= CSSParserValue::Q_EMS)
315 parsedValue = CSSPrimitiveValue::createAllowingMarginQuirk(value->fValue, CSSPrimitiveValue::CSS_EMS);
316 if (isCalculation(value)) {
317 // FIXME calc() http://webkit.org/b/16662 : actually create a CSSPrimitiveValue here, ie
318 // parsedValue = CSSPrimitiveValue::create(m_parsedCalculation.release());
319 m_parsedCalculation.release();
324 if (!parsedValue || (m_valueList->current() && !inShorthand()))
327 addProperty(propId, parsedValue.release(), important);
331 PassRefPtr<CSSValue> CSSParser::parseSVGStrokeDasharray()
333 RefPtr<CSSValueList> ret = CSSValueList::createCommaSeparated();
334 CSSParserValue* value = m_valueList->current();
335 bool valid_primitive = true;
337 valid_primitive = validUnit(value, FLength | FPercent | FNonNeg, SVGAttributeMode);
338 if (!valid_primitive)
341 ret->append(CSSPrimitiveValue::createIdentifier(value->id));
342 else if (value->unit >= CSSPrimitiveValue::CSS_NUMBER && value->unit <= CSSPrimitiveValue::CSS_KHZ)
343 ret->append(CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit));
344 value = m_valueList->next();
345 if (value && value->unit == CSSParserValue::Operator && value->iValue == ',')
346 value = m_valueList->next();
348 if (!valid_primitive)
350 return ret.release();
353 PassRefPtr<CSSValue> CSSParser::parseSVGPaint()
355 RGBA32 c = Color::transparent;
356 if (!parseColorFromValue(m_valueList->current(), c))
357 return SVGPaint::createUnknown();
358 return SVGPaint::createColor(Color(c));
361 PassRefPtr<CSSValue> CSSParser::parseSVGColor()
363 RGBA32 c = Color::transparent;
364 if (!parseColorFromValue(m_valueList->current(), c))
366 return SVGColor::createFromColor(Color(c));
369 // normal | [ fill || stroke || markers ]
370 PassRefPtr<CSSValue> CSSParser::parsePaintOrder() const
372 if (m_valueList->size() > 3)
375 CSSParserValue* value = m_valueList->current();
379 RefPtr<CSSValueList> parsedValues = CSSValueList::createSpaceSeparated();
381 // The default paint-order is: Fill, Stroke, Markers.
382 bool seenFill = false,
389 // normal inside [fill || stroke || markers] not valid
403 case CSSValueMarkers:
413 parsedValues->append(CSSPrimitiveValue::createIdentifier(value->id));
414 } while ((value = m_valueList->next()));
416 // fill out the rest of the paint order
418 parsedValues->append(CSSPrimitiveValue::createIdentifier(CSSValueFill));
420 parsedValues->append(CSSPrimitiveValue::createIdentifier(CSSValueStroke));
422 parsedValues->append(CSSPrimitiveValue::createIdentifier(CSSValueMarkers));
424 return parsedValues.release();