SVGCSSParser.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  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.
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public
  8. License as published by the Free Software Foundation; either
  9. version 2 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public License
  15. along with this library; see the file COPYING.LIB. If not, write to
  16. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. Boston, MA 02110-1301, USA.
  18. */
  19. #include "config.h"
  20. #if ENABLE(SVG)
  21. #include "CSSInheritedValue.h"
  22. #include "CSSInitialValue.h"
  23. #include "CSSParser.h"
  24. #include "CSSPropertyNames.h"
  25. #include "CSSValueKeywords.h"
  26. #include "CSSValueList.h"
  27. #include "RenderTheme.h"
  28. #include "SVGPaint.h"
  29. using namespace std;
  30. namespace WebCore {
  31. bool CSSParser::parseSVGValue(CSSPropertyID propId, bool important)
  32. {
  33. CSSParserValue* value = m_valueList->current();
  34. if (!value)
  35. return false;
  36. int id = value->id;
  37. bool valid_primitive = false;
  38. RefPtr<CSSValue> parsedValue;
  39. switch (propId) {
  40. /* The comment to the right defines all valid value of these
  41. * properties as defined in SVG 1.1, Appendix N. Property index */
  42. case CSSPropertyAlignmentBaseline:
  43. // auto | baseline | before-edge | text-before-edge | middle |
  44. // central | after-edge | text-after-edge | ideographic | alphabetic |
  45. // hanging | mathematical | inherit
  46. if (id == CSSValueAuto || id == CSSValueBaseline || id == CSSValueMiddle ||
  47. (id >= CSSValueBeforeEdge && id <= CSSValueMathematical))
  48. valid_primitive = true;
  49. break;
  50. case CSSPropertyBaselineShift:
  51. // baseline | super | sub | <percentage> | <length> | inherit
  52. if (id == CSSValueBaseline || id == CSSValueSub ||
  53. id >= CSSValueSuper)
  54. valid_primitive = true;
  55. else
  56. valid_primitive = validUnit(value, FLength | FPercent, SVGAttributeMode);
  57. break;
  58. case CSSPropertyDominantBaseline:
  59. // auto | use-script | no-change | reset-size | ideographic |
  60. // alphabetic | hanging | mathematical | central | middle |
  61. // text-after-edge | text-before-edge | inherit
  62. if (id == CSSValueAuto || id == CSSValueMiddle ||
  63. (id >= CSSValueUseScript && id <= CSSValueResetSize) ||
  64. (id >= CSSValueCentral && id <= CSSValueMathematical))
  65. valid_primitive = true;
  66. break;
  67. case CSSPropertyEnableBackground:
  68. // accumulate | new [x] [y] [width] [height] | inherit
  69. if (id == CSSValueAccumulate) // TODO : new
  70. valid_primitive = true;
  71. break;
  72. case CSSPropertyMarkerStart:
  73. case CSSPropertyMarkerMid:
  74. case CSSPropertyMarkerEnd:
  75. case CSSPropertyMask:
  76. if (id == CSSValueNone)
  77. valid_primitive = true;
  78. else if (value->unit == CSSPrimitiveValue::CSS_URI) {
  79. parsedValue = CSSPrimitiveValue::create(value->string, CSSPrimitiveValue::CSS_URI);
  80. if (parsedValue)
  81. m_valueList->next();
  82. }
  83. break;
  84. case CSSPropertyClipRule: // nonzero | evenodd | inherit
  85. case CSSPropertyFillRule:
  86. if (id == CSSValueNonzero || id == CSSValueEvenodd)
  87. valid_primitive = true;
  88. break;
  89. case CSSPropertyStrokeMiterlimit: // <miterlimit> | inherit
  90. valid_primitive = validUnit(value, FNumber | FNonNeg, SVGAttributeMode);
  91. break;
  92. case CSSPropertyStrokeLinejoin: // miter | round | bevel | inherit
  93. if (id == CSSValueMiter || id == CSSValueRound || id == CSSValueBevel)
  94. valid_primitive = true;
  95. break;
  96. case CSSPropertyStrokeLinecap: // butt | round | square | inherit
  97. if (id == CSSValueButt || id == CSSValueRound || id == CSSValueSquare)
  98. valid_primitive = true;
  99. break;
  100. case CSSPropertyStrokeOpacity: // <opacity-value> | inherit
  101. case CSSPropertyFillOpacity:
  102. case CSSPropertyStopOpacity:
  103. case CSSPropertyFloodOpacity:
  104. valid_primitive = (!id && validUnit(value, FNumber | FPercent, SVGAttributeMode));
  105. break;
  106. case CSSPropertyShapeRendering:
  107. // auto | optimizeSpeed | crispEdges | geometricPrecision | inherit
  108. if (id == CSSValueAuto || id == CSSValueOptimizespeed ||
  109. id == CSSValueCrispedges || id == CSSValueGeometricprecision)
  110. valid_primitive = true;
  111. break;
  112. case CSSPropertyColorRendering: // auto | optimizeSpeed | optimizeQuality | inherit
  113. if (id == CSSValueAuto || id == CSSValueOptimizespeed ||
  114. id == CSSValueOptimizequality)
  115. valid_primitive = true;
  116. break;
  117. case CSSPropertyBufferedRendering: // auto | dynamic | static
  118. if (id == CSSValueAuto || id == CSSValueDynamic || id == CSSValueStatic)
  119. valid_primitive = true;
  120. break;
  121. case CSSPropertyColorProfile: // auto | sRGB | <name> | <uri> inherit
  122. if (id == CSSValueAuto || id == CSSValueSrgb)
  123. valid_primitive = true;
  124. break;
  125. case CSSPropertyColorInterpolation: // auto | sRGB | linearRGB | inherit
  126. case CSSPropertyColorInterpolationFilters:
  127. if (id == CSSValueAuto || id == CSSValueSrgb || id == CSSValueLinearrgb)
  128. valid_primitive = true;
  129. break;
  130. /* Start of supported CSS properties with validation. This is needed for parseShortHand to work
  131. * correctly and allows optimization in applyRule(..)
  132. */
  133. case CSSPropertyTextAnchor: // start | middle | end | inherit
  134. if (id == CSSValueStart || id == CSSValueMiddle || id == CSSValueEnd)
  135. valid_primitive = true;
  136. break;
  137. case CSSPropertyGlyphOrientationVertical: // auto | <angle> | inherit
  138. if (id == CSSValueAuto) {
  139. valid_primitive = true;
  140. break;
  141. }
  142. /* fallthrough intentional */
  143. case CSSPropertyGlyphOrientationHorizontal: // <angle> (restricted to _deg_ per SVG 1.1 spec) | inherit
  144. if (value->unit == CSSPrimitiveValue::CSS_DEG || value->unit == CSSPrimitiveValue::CSS_NUMBER) {
  145. parsedValue = CSSPrimitiveValue::create(value->fValue, CSSPrimitiveValue::CSS_DEG);
  146. if (parsedValue)
  147. m_valueList->next();
  148. }
  149. break;
  150. case CSSPropertyFill: // <paint> | inherit
  151. case CSSPropertyStroke: // <paint> | inherit
  152. {
  153. if (id == CSSValueNone)
  154. parsedValue = SVGPaint::createNone();
  155. else if (id == CSSValueCurrentcolor)
  156. parsedValue = SVGPaint::createCurrentColor();
  157. else if ((id >= CSSValueActiveborder && id <= CSSValueWindowtext) || id == CSSValueMenu)
  158. parsedValue = SVGPaint::createColor(RenderTheme::defaultTheme()->systemColor(id));
  159. else if (value->unit == CSSPrimitiveValue::CSS_URI) {
  160. RGBA32 c = Color::transparent;
  161. if (m_valueList->next()) {
  162. if (parseColorFromValue(m_valueList->current(), c))
  163. parsedValue = SVGPaint::createURIAndColor(value->string, c);
  164. else if (m_valueList->current()->id == CSSValueNone)
  165. parsedValue = SVGPaint::createURIAndNone(value->string);
  166. }
  167. if (!parsedValue)
  168. parsedValue = SVGPaint::createURI(value->string);
  169. } else
  170. parsedValue = parseSVGPaint();
  171. if (parsedValue)
  172. m_valueList->next();
  173. }
  174. break;
  175. case CSSPropertyStopColor: // TODO : icccolor
  176. case CSSPropertyFloodColor:
  177. case CSSPropertyLightingColor:
  178. if ((id >= CSSValueAqua && id <= CSSValueWindowtext) ||
  179. (id >= CSSValueAliceblue && id <= CSSValueYellowgreen))
  180. parsedValue = SVGColor::createFromString(value->string);
  181. else if (id == CSSValueCurrentcolor)
  182. parsedValue = SVGColor::createCurrentColor();
  183. else // TODO : svgcolor (iccColor)
  184. parsedValue = parseSVGColor();
  185. if (parsedValue)
  186. m_valueList->next();
  187. break;
  188. case CSSPropertyVectorEffect: // none | non-scaling-stroke | inherit
  189. if (id == CSSValueNone || id == CSSValueNonScalingStroke)
  190. valid_primitive = true;
  191. break;
  192. case CSSPropertyWritingMode:
  193. // lr-tb | rl_tb | tb-rl | lr | rl | tb | inherit
  194. if (id == CSSValueLrTb || id == CSSValueRlTb || id == CSSValueTbRl || id == CSSValueLr || id == CSSValueRl || id == CSSValueTb)
  195. valid_primitive = true;
  196. break;
  197. case CSSPropertyStrokeWidth: // <length> | inherit
  198. case CSSPropertyStrokeDashoffset:
  199. valid_primitive = validUnit(value, FLength | FPercent, SVGAttributeMode);
  200. break;
  201. case CSSPropertyStrokeDasharray: // none | <dasharray> | inherit
  202. if (id == CSSValueNone)
  203. valid_primitive = true;
  204. else
  205. parsedValue = parseSVGStrokeDasharray();
  206. break;
  207. case CSSPropertyKerning: // auto | normal | <length> | inherit
  208. if (id == CSSValueAuto || id == CSSValueNormal)
  209. valid_primitive = true;
  210. else
  211. valid_primitive = validUnit(value, FLength, SVGAttributeMode);
  212. break;
  213. case CSSPropertyClipPath: // <uri> | none | inherit
  214. case CSSPropertyFilter:
  215. if (id == CSSValueNone)
  216. valid_primitive = true;
  217. else if (value->unit == CSSPrimitiveValue::CSS_URI) {
  218. parsedValue = CSSPrimitiveValue::create(value->string, (CSSPrimitiveValue::UnitTypes) value->unit);
  219. if (parsedValue)
  220. m_valueList->next();
  221. }
  222. break;
  223. case CSSPropertyWebkitSvgShadow:
  224. if (id == CSSValueNone)
  225. valid_primitive = true;
  226. else {
  227. RefPtr<CSSValueList> shadowValueList = parseShadow(m_valueList.get(), propId);
  228. if (shadowValueList) {
  229. addProperty(propId, shadowValueList.release(), important);
  230. m_valueList->next();
  231. return true;
  232. }
  233. return false;
  234. }
  235. case CSSPropertyMaskType: // luminance | alpha | inherit
  236. if (id == CSSValueLuminance || id == CSSValueAlpha)
  237. valid_primitive = true;
  238. break;
  239. /* shorthand properties */
  240. case CSSPropertyMarker:
  241. {
  242. ShorthandScope scope(this, propId);
  243. m_implicitShorthand = true;
  244. if (!parseValue(CSSPropertyMarkerStart, important))
  245. return false;
  246. if (m_valueList->current()) {
  247. rollbackLastProperties(1);
  248. return false;
  249. }
  250. CSSValue* value = m_parsedProperties.last().value();
  251. addProperty(CSSPropertyMarkerMid, value, important);
  252. addProperty(CSSPropertyMarkerEnd, value, important);
  253. m_implicitShorthand = false;
  254. return true;
  255. }
  256. default:
  257. // If you crash here, it's because you added a css property and are not handling it
  258. // in either this switch statement or the one in CSSParser::parseValue
  259. ASSERT_WITH_MESSAGE(0, "unimplemented propertyID: %d", propId);
  260. return false;
  261. }
  262. if (valid_primitive) {
  263. if (id != 0)
  264. parsedValue = CSSPrimitiveValue::createIdentifier(id);
  265. else if (value->unit == CSSPrimitiveValue::CSS_STRING)
  266. parsedValue = CSSPrimitiveValue::create(value->string, (CSSPrimitiveValue::UnitTypes) value->unit);
  267. else if (value->unit >= CSSPrimitiveValue::CSS_NUMBER && value->unit <= CSSPrimitiveValue::CSS_KHZ)
  268. parsedValue = CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit);
  269. else if (value->unit >= CSSParserValue::Q_EMS)
  270. parsedValue = CSSPrimitiveValue::createAllowingMarginQuirk(value->fValue, CSSPrimitiveValue::CSS_EMS);
  271. if (isCalculation(value)) {
  272. // FIXME calc() http://webkit.org/b/16662 : actually create a CSSPrimitiveValue here, ie
  273. // parsedValue = CSSPrimitiveValue::create(m_parsedCalculation.release());
  274. m_parsedCalculation.release();
  275. parsedValue = 0;
  276. }
  277. m_valueList->next();
  278. }
  279. if (!parsedValue || (m_valueList->current() && !inShorthand()))
  280. return false;
  281. addProperty(propId, parsedValue.release(), important);
  282. return true;
  283. }
  284. PassRefPtr<CSSValue> CSSParser::parseSVGStrokeDasharray()
  285. {
  286. RefPtr<CSSValueList> ret = CSSValueList::createCommaSeparated();
  287. CSSParserValue* value = m_valueList->current();
  288. bool valid_primitive = true;
  289. while (value) {
  290. valid_primitive = validUnit(value, FLength | FPercent | FNonNeg, SVGAttributeMode);
  291. if (!valid_primitive)
  292. break;
  293. if (value->id != 0)
  294. ret->append(CSSPrimitiveValue::createIdentifier(value->id));
  295. else if (value->unit >= CSSPrimitiveValue::CSS_NUMBER && value->unit <= CSSPrimitiveValue::CSS_KHZ)
  296. ret->append(CSSPrimitiveValue::create(value->fValue, (CSSPrimitiveValue::UnitTypes) value->unit));
  297. value = m_valueList->next();
  298. if (value && value->unit == CSSParserValue::Operator && value->iValue == ',')
  299. value = m_valueList->next();
  300. }
  301. if (!valid_primitive)
  302. return 0;
  303. return ret.release();
  304. }
  305. PassRefPtr<CSSValue> CSSParser::parseSVGPaint()
  306. {
  307. RGBA32 c = Color::transparent;
  308. if (!parseColorFromValue(m_valueList->current(), c))
  309. return SVGPaint::createUnknown();
  310. return SVGPaint::createColor(Color(c));
  311. }
  312. PassRefPtr<CSSValue> CSSParser::parseSVGColor()
  313. {
  314. RGBA32 c = Color::transparent;
  315. if (!parseColorFromValue(m_valueList->current(), c))
  316. return 0;
  317. return SVGColor::createFromColor(Color(c));
  318. }
  319. }
  320. #endif // ENABLE(SVG)