CSSParserValues.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
  3. * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  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. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include "config.h"
  21. #include "CSSParserValues.h"
  22. #include "CSSPrimitiveValue.h"
  23. #include "CSSFunctionValue.h"
  24. #include "CSSSelector.h"
  25. #include "CSSSelectorList.h"
  26. #if ENABLE(CSS_VARIABLES)
  27. #include "CSSVariableValue.h"
  28. #endif
  29. namespace WebCore {
  30. using namespace WTF;
  31. CSSParserValueList::~CSSParserValueList()
  32. {
  33. size_t numValues = m_values.size();
  34. for (size_t i = 0; i < numValues; i++) {
  35. if (m_values[i].unit == CSSParserValue::Function)
  36. delete m_values[i].function;
  37. }
  38. }
  39. void CSSParserValueList::addValue(const CSSParserValue& v)
  40. {
  41. m_values.append(v);
  42. }
  43. void CSSParserValueList::insertValueAt(unsigned i, const CSSParserValue& v)
  44. {
  45. m_values.insert(i, v);
  46. }
  47. void CSSParserValueList::deleteValueAt(unsigned i)
  48. {
  49. m_values.remove(i);
  50. }
  51. void CSSParserValueList::extend(CSSParserValueList& valueList)
  52. {
  53. for (unsigned int i = 0; i < valueList.size(); ++i)
  54. m_values.append(*(valueList.valueAt(i)));
  55. }
  56. PassRefPtr<CSSValue> CSSParserValue::createCSSValue()
  57. {
  58. RefPtr<CSSValue> parsedValue;
  59. if (id)
  60. return CSSPrimitiveValue::createIdentifier(id);
  61. if (unit == CSSParserValue::Operator) {
  62. RefPtr<CSSPrimitiveValue> primitiveValue = CSSPrimitiveValue::createIdentifier(iValue);
  63. primitiveValue->setPrimitiveType(CSSPrimitiveValue::CSS_PARSER_OPERATOR);
  64. return primitiveValue;
  65. }
  66. if (unit == CSSParserValue::Function)
  67. return CSSFunctionValue::create(function);
  68. if (unit >= CSSParserValue::Q_EMS)
  69. return CSSPrimitiveValue::createAllowingMarginQuirk(fValue, CSSPrimitiveValue::CSS_EMS);
  70. CSSPrimitiveValue::UnitTypes primitiveUnit = static_cast<CSSPrimitiveValue::UnitTypes>(unit);
  71. switch (primitiveUnit) {
  72. case CSSPrimitiveValue::CSS_IDENT:
  73. return CSSPrimitiveValue::create(string, CSSPrimitiveValue::CSS_PARSER_IDENTIFIER);
  74. case CSSPrimitiveValue::CSS_NUMBER:
  75. return CSSPrimitiveValue::create(fValue, isInt ? CSSPrimitiveValue::CSS_PARSER_INTEGER : CSSPrimitiveValue::CSS_NUMBER);
  76. case CSSPrimitiveValue::CSS_STRING:
  77. case CSSPrimitiveValue::CSS_URI:
  78. #if ENABLE(CSS_VARIABLES)
  79. case CSSPrimitiveValue::CSS_VARIABLE_NAME:
  80. #endif
  81. case CSSPrimitiveValue::CSS_PARSER_HEXCOLOR:
  82. return CSSPrimitiveValue::create(string, primitiveUnit);
  83. case CSSPrimitiveValue::CSS_PERCENTAGE:
  84. case CSSPrimitiveValue::CSS_EMS:
  85. case CSSPrimitiveValue::CSS_EXS:
  86. case CSSPrimitiveValue::CSS_PX:
  87. case CSSPrimitiveValue::CSS_CM:
  88. case CSSPrimitiveValue::CSS_MM:
  89. case CSSPrimitiveValue::CSS_IN:
  90. case CSSPrimitiveValue::CSS_PT:
  91. case CSSPrimitiveValue::CSS_PC:
  92. case CSSPrimitiveValue::CSS_DEG:
  93. case CSSPrimitiveValue::CSS_RAD:
  94. case CSSPrimitiveValue::CSS_GRAD:
  95. case CSSPrimitiveValue::CSS_MS:
  96. case CSSPrimitiveValue::CSS_S:
  97. case CSSPrimitiveValue::CSS_HZ:
  98. case CSSPrimitiveValue::CSS_KHZ:
  99. case CSSPrimitiveValue::CSS_VW:
  100. case CSSPrimitiveValue::CSS_VH:
  101. case CSSPrimitiveValue::CSS_VMIN:
  102. case CSSPrimitiveValue::CSS_VMAX:
  103. case CSSPrimitiveValue::CSS_TURN:
  104. case CSSPrimitiveValue::CSS_REMS:
  105. case CSSPrimitiveValue::CSS_CHS:
  106. return CSSPrimitiveValue::create(fValue, primitiveUnit);
  107. case CSSPrimitiveValue::CSS_UNKNOWN:
  108. case CSSPrimitiveValue::CSS_DIMENSION:
  109. case CSSPrimitiveValue::CSS_ATTR:
  110. case CSSPrimitiveValue::CSS_COUNTER:
  111. case CSSPrimitiveValue::CSS_RECT:
  112. case CSSPrimitiveValue::CSS_RGBCOLOR:
  113. case CSSPrimitiveValue::CSS_DPPX:
  114. case CSSPrimitiveValue::CSS_DPI:
  115. case CSSPrimitiveValue::CSS_DPCM:
  116. case CSSPrimitiveValue::CSS_PAIR:
  117. #if ENABLE(DASHBOARD_SUPPORT)
  118. case CSSPrimitiveValue::CSS_DASHBOARD_REGION:
  119. #endif
  120. case CSSPrimitiveValue::CSS_UNICODE_RANGE:
  121. case CSSPrimitiveValue::CSS_PARSER_OPERATOR:
  122. case CSSPrimitiveValue::CSS_PARSER_INTEGER:
  123. case CSSPrimitiveValue::CSS_PARSER_IDENTIFIER:
  124. case CSSPrimitiveValue::CSS_COUNTER_NAME:
  125. case CSSPrimitiveValue::CSS_SHAPE:
  126. case CSSPrimitiveValue::CSS_QUAD:
  127. case CSSPrimitiveValue::CSS_CALC:
  128. case CSSPrimitiveValue::CSS_CALC_PERCENTAGE_WITH_NUMBER:
  129. case CSSPrimitiveValue::CSS_CALC_PERCENTAGE_WITH_LENGTH:
  130. return 0;
  131. }
  132. ASSERT_NOT_REACHED();
  133. return 0;
  134. }
  135. CSSParserSelector::CSSParserSelector()
  136. : m_selector(adoptPtr(fastNew<CSSSelector>()))
  137. {
  138. }
  139. CSSParserSelector::CSSParserSelector(const QualifiedName& tagQName)
  140. : m_selector(adoptPtr(new CSSSelector(tagQName)))
  141. {
  142. }
  143. CSSParserSelector::~CSSParserSelector()
  144. {
  145. if (!m_tagHistory)
  146. return;
  147. Vector<OwnPtr<CSSParserSelector>, 16> toDelete;
  148. OwnPtr<CSSParserSelector> selector = m_tagHistory.release();
  149. while (true) {
  150. OwnPtr<CSSParserSelector> next = selector->m_tagHistory.release();
  151. toDelete.append(selector.release());
  152. if (!next)
  153. break;
  154. selector = next.release();
  155. }
  156. }
  157. void CSSParserSelector::adoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectorVector)
  158. {
  159. CSSSelectorList* selectorList = fastNew<CSSSelectorList>();
  160. selectorList->adoptSelectorVector(selectorVector);
  161. m_selector->setSelectorList(adoptPtr(selectorList));
  162. }
  163. bool CSSParserSelector::isSimple() const
  164. {
  165. if (m_selector->selectorList() || m_selector->matchesPseudoElement())
  166. return false;
  167. if (!m_tagHistory)
  168. return true;
  169. if (m_selector->m_match == CSSSelector::Tag) {
  170. // We can't check against anyQName() here because namespace may not be nullAtom.
  171. // Example:
  172. // @namespace "http://www.w3.org/2000/svg";
  173. // svg:not(:root) { ...
  174. if (m_selector->tagQName().localName() == starAtom)
  175. return m_tagHistory->isSimple();
  176. }
  177. return false;
  178. }
  179. void CSSParserSelector::insertTagHistory(CSSSelector::Relation before, PassOwnPtr<CSSParserSelector> selector, CSSSelector::Relation after)
  180. {
  181. if (m_tagHistory)
  182. selector->setTagHistory(m_tagHistory.release());
  183. setRelation(before);
  184. selector->setRelation(after);
  185. m_tagHistory = selector;
  186. }
  187. void CSSParserSelector::appendTagHistory(CSSSelector::Relation relation, PassOwnPtr<CSSParserSelector> selector)
  188. {
  189. CSSParserSelector* end = this;
  190. while (end->tagHistory())
  191. end = end->tagHistory();
  192. end->setRelation(relation);
  193. end->setTagHistory(selector);
  194. }
  195. void CSSParserSelector::prependTagSelector(const QualifiedName& tagQName, bool tagIsForNamespaceRule)
  196. {
  197. OwnPtr<CSSParserSelector> second = adoptPtr(new CSSParserSelector);
  198. second->m_selector = m_selector.release();
  199. second->m_tagHistory = m_tagHistory.release();
  200. m_tagHistory = second.release();
  201. m_selector = adoptPtr(new CSSSelector(tagQName, tagIsForNamespaceRule));
  202. m_selector->m_relation = CSSSelector::SubSelector;
  203. }
  204. }