Keep the already-parsed list of terms in custom property values so that we don't...
[WebKit-https.git] / Source / WebCore / css / CSSParserValues.cpp
1 /*
2  * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2008, 2014 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
21 #include "config.h"
22 #include "CSSParserValues.h"
23
24 #include "CSSPrimitiveValue.h"
25 #include "CSSFunctionValue.h"
26 #include "CSSSelector.h"
27 #include "CSSSelectorList.h"
28 #include "SelectorPseudoTypeMap.h"
29 #include <wtf/text/StringBuilder.h>
30
31 namespace WebCore {
32
33 using namespace WTF;
34
35 void destroy(const CSSParserValue& value)
36 {
37     if (value.unit == CSSParserValue::Function)
38         delete value.function;
39     else if (value.unit == CSSParserValue::ValueList)
40         delete value.valueList;
41 }
42
43 CSSParserValueList::~CSSParserValueList()
44 {
45     for (size_t i = 0, size = m_values.size(); i < size; i++)
46         destroy(m_values[i]);
47 }
48
49 void CSSParserValueList::addValue(const CSSParserValue& v)
50 {
51     m_values.append(v);
52 }
53
54 void CSSParserValueList::insertValueAt(unsigned i, const CSSParserValue& v)
55 {
56     m_values.insert(i, v);
57 }
58
59 void CSSParserValueList::deleteValueAt(unsigned i)
60 {
61     m_values.remove(i);
62 }
63
64 void CSSParserValueList::extend(CSSParserValueList& valueList)
65 {
66     for (unsigned int i = 0; i < valueList.size(); ++i)
67         m_values.append(*(valueList.valueAt(i)));
68 }
69
70 String CSSParserValueList::toString()
71 {
72     // Build up a set of CSSValues and serialize them using cssText, separating multiple values
73     // with spaces.
74     // FIXME: Teach CSSParserValues how to serialize so that we don't have to create CSSValues
75     // just to perform this serialization.
76     StringBuilder builder;
77     for (unsigned i = 0; i < size(); i++) {
78         if (i)
79             builder.append(' ');
80         RefPtr<CSSValue> cssValue = valueAt(i)->createCSSValue();
81         if (!cssValue)
82             return "";
83         builder.append(cssValue->cssText());
84     }
85     return builder.toString().lower();
86 }
87
88 PassRefPtr<CSSValue> CSSParserValue::createCSSValue()
89 {
90     RefPtr<CSSValue> parsedValue;
91     if (id)
92         return CSSPrimitiveValue::createIdentifier(id);
93     
94     if (unit == CSSParserValue::Operator) {
95         RefPtr<CSSPrimitiveValue> primitiveValue = CSSPrimitiveValue::createParserOperator(iValue);
96         primitiveValue->setPrimitiveType(CSSPrimitiveValue::CSS_PARSER_OPERATOR);
97         return primitiveValue.release();
98     }
99     if (unit == CSSParserValue::Function)
100         return CSSFunctionValue::create(function);
101     if (unit == CSSParserValue::ValueList)
102         return CSSValueList::createFromParserValueList(*valueList);
103     if (unit >= CSSParserValue::Q_EMS)
104         return CSSPrimitiveValue::createAllowingMarginQuirk(fValue, CSSPrimitiveValue::CSS_EMS);
105
106     CSSPrimitiveValue::UnitTypes primitiveUnit = static_cast<CSSPrimitiveValue::UnitTypes>(unit);
107     switch (primitiveUnit) {
108     case CSSPrimitiveValue::CSS_IDENT:
109     case CSSPrimitiveValue::CSS_PROPERTY_ID:
110     case CSSPrimitiveValue::CSS_VALUE_ID:
111         return CSSPrimitiveValue::create(string, CSSPrimitiveValue::CSS_PARSER_IDENTIFIER);
112     case CSSPrimitiveValue::CSS_NUMBER:
113         return CSSPrimitiveValue::create(fValue, isInt ? CSSPrimitiveValue::CSS_PARSER_INTEGER : CSSPrimitiveValue::CSS_NUMBER);
114     case CSSPrimitiveValue::CSS_STRING:
115     case CSSPrimitiveValue::CSS_URI:
116     case CSSPrimitiveValue::CSS_PARSER_HEXCOLOR:
117         return CSSPrimitiveValue::create(string, primitiveUnit);
118     case CSSPrimitiveValue::CSS_PERCENTAGE:
119     case CSSPrimitiveValue::CSS_EMS:
120     case CSSPrimitiveValue::CSS_EXS:
121     case CSSPrimitiveValue::CSS_PX:
122     case CSSPrimitiveValue::CSS_CM:
123     case CSSPrimitiveValue::CSS_MM:
124     case CSSPrimitiveValue::CSS_IN:
125     case CSSPrimitiveValue::CSS_PT:
126     case CSSPrimitiveValue::CSS_PC:
127     case CSSPrimitiveValue::CSS_DEG:
128     case CSSPrimitiveValue::CSS_RAD:
129     case CSSPrimitiveValue::CSS_GRAD:
130     case CSSPrimitiveValue::CSS_MS:
131     case CSSPrimitiveValue::CSS_S:
132     case CSSPrimitiveValue::CSS_HZ:
133     case CSSPrimitiveValue::CSS_KHZ:
134     case CSSPrimitiveValue::CSS_VW:
135     case CSSPrimitiveValue::CSS_VH:
136     case CSSPrimitiveValue::CSS_VMIN:
137     case CSSPrimitiveValue::CSS_VMAX:
138     case CSSPrimitiveValue::CSS_TURN:
139     case CSSPrimitiveValue::CSS_REMS:
140     case CSSPrimitiveValue::CSS_CHS:
141     case CSSPrimitiveValue::CSS_FR:
142         return CSSPrimitiveValue::create(fValue, primitiveUnit);
143     case CSSPrimitiveValue::CSS_UNKNOWN:
144     case CSSPrimitiveValue::CSS_DIMENSION:
145     case CSSPrimitiveValue::CSS_ATTR:
146     case CSSPrimitiveValue::CSS_COUNTER:
147     case CSSPrimitiveValue::CSS_RECT:
148     case CSSPrimitiveValue::CSS_RGBCOLOR:
149     case CSSPrimitiveValue::CSS_DPPX:
150     case CSSPrimitiveValue::CSS_DPI:
151     case CSSPrimitiveValue::CSS_DPCM:
152     case CSSPrimitiveValue::CSS_PAIR:
153 #if ENABLE(DASHBOARD_SUPPORT)
154     case CSSPrimitiveValue::CSS_DASHBOARD_REGION:
155 #endif
156     case CSSPrimitiveValue::CSS_UNICODE_RANGE:
157     case CSSPrimitiveValue::CSS_PARSER_OPERATOR:
158     case CSSPrimitiveValue::CSS_PARSER_INTEGER:
159     case CSSPrimitiveValue::CSS_PARSER_IDENTIFIER:
160     case CSSPrimitiveValue::CSS_COUNTER_NAME:
161     case CSSPrimitiveValue::CSS_SHAPE:
162     case CSSPrimitiveValue::CSS_FONT_FAMILY:
163     case CSSPrimitiveValue::CSS_QUAD:
164 #if ENABLE(CSS_SCROLL_SNAP)
165     case CSSPrimitiveValue::CSS_LENGTH_REPEAT:
166 #endif
167     case CSSPrimitiveValue::CSS_CALC:
168     case CSSPrimitiveValue::CSS_CALC_PERCENTAGE_WITH_NUMBER:
169     case CSSPrimitiveValue::CSS_CALC_PERCENTAGE_WITH_LENGTH:
170         return 0;
171     }
172
173     ASSERT_NOT_REACHED();
174     return 0;
175 }
176
177 CSSParserSelector* CSSParserSelector::parsePagePseudoSelector(const CSSParserString& pseudoTypeString)
178 {
179     CSSSelector::PagePseudoClassType pseudoType;
180     if (pseudoTypeString.equalIgnoringCase("first"))
181         pseudoType = CSSSelector::PagePseudoClassFirst;
182     else if (pseudoTypeString.equalIgnoringCase("left"))
183         pseudoType = CSSSelector::PagePseudoClassLeft;
184     else if (pseudoTypeString.equalIgnoringCase("right"))
185         pseudoType = CSSSelector::PagePseudoClassRight;
186     else
187         return nullptr;
188
189     auto selector = std::make_unique<CSSParserSelector>();
190     selector->m_selector->setMatch(CSSSelector::PagePseudoClass);
191     selector->m_selector->setPagePseudoType(pseudoType);
192     return selector.release();
193 }
194
195 CSSParserSelector* CSSParserSelector::parsePseudoElementSelector(CSSParserString& pseudoTypeString)
196 {
197     pseudoTypeString.lower();
198     AtomicString name = pseudoTypeString;
199
200     CSSSelector::PseudoElementType pseudoType = CSSSelector::parsePseudoElementType(name);
201     if (pseudoType == CSSSelector::PseudoElementUnknown)
202         return nullptr;
203
204     auto selector = std::make_unique<CSSParserSelector>();
205     selector->m_selector->setMatch(CSSSelector::PseudoElement);
206     selector->m_selector->setPseudoElementType(pseudoType);
207     selector->m_selector->setValue(name);
208     return selector.release();
209 }
210
211 #if ENABLE(VIDEO_TRACK)
212 CSSParserSelector* CSSParserSelector::parsePseudoElementCueFunctionSelector(const CSSParserString& functionIdentifier, Vector<std::unique_ptr<CSSParserSelector>>* parsedSelectorVector)
213 {
214     ASSERT_UNUSED(functionIdentifier, String(functionIdentifier) == "cue(");
215
216     std::unique_ptr<Vector<std::unique_ptr<CSSParserSelector>>> selectorVector(parsedSelectorVector);
217
218     if (!selectorVector)
219         return nullptr;
220
221     auto selector = std::make_unique<CSSParserSelector>();
222     selector->m_selector->setMatch(CSSSelector::PseudoElement);
223     selector->m_selector->setPseudoElementType(CSSSelector::PseudoElementCue);
224     selector->adoptSelectorVector(*selectorVector);
225     return selector.release();
226 }
227 #endif
228
229 CSSParserSelector* CSSParserSelector::parsePseudoClassAndCompatibilityElementSelector(CSSParserString& pseudoTypeString)
230 {
231     if (pseudoTypeString.length() && pseudoTypeString[pseudoTypeString.length() - 1] == '(')
232         return nullptr;
233
234     PseudoClassOrCompatibilityPseudoElement pseudoType = parsePseudoClassAndCompatibilityElementString(pseudoTypeString);
235     if (pseudoType.pseudoClass != CSSSelector::PseudoClassUnknown) {
236         auto selector = std::make_unique<CSSParserSelector>();
237         selector->m_selector->setMatch(CSSSelector::PseudoClass);
238         selector->m_selector->setPseudoClassType(pseudoType.pseudoClass);
239         return selector.release();
240     }
241     if (pseudoType.compatibilityPseudoElement != CSSSelector::PseudoElementUnknown) {
242         auto selector = std::make_unique<CSSParserSelector>();
243         selector->m_selector->setMatch(CSSSelector::PseudoElement);
244         selector->m_selector->setPseudoElementType(pseudoType.compatibilityPseudoElement);
245         AtomicString name = pseudoTypeString;
246         selector->m_selector->setValue(name);
247         return selector.release();
248     }
249     return nullptr;
250 }
251
252 CSSParserSelector::CSSParserSelector()
253     : m_selector(std::make_unique<CSSSelector>())
254 {
255 }
256
257 CSSParserSelector::CSSParserSelector(const QualifiedName& tagQName)
258     : m_selector(std::make_unique<CSSSelector>(tagQName))
259 {
260 }
261
262 CSSParserSelector::~CSSParserSelector()
263 {
264     if (!m_tagHistory)
265         return;
266     Vector<std::unique_ptr<CSSParserSelector>, 16> toDelete;
267     std::unique_ptr<CSSParserSelector> selector = WTF::move(m_tagHistory);
268     while (true) {
269         std::unique_ptr<CSSParserSelector> next = WTF::move(selector->m_tagHistory);
270         toDelete.append(WTF::move(selector));
271         if (!next)
272             break;
273         selector = WTF::move(next);
274     }
275 }
276
277 void CSSParserSelector::adoptSelectorVector(Vector<std::unique_ptr<CSSParserSelector>>& selectorVector)
278 {
279     auto selectorList = std::make_unique<CSSSelectorList>();
280     selectorList->adoptSelectorVector(selectorVector);
281     m_selector->setSelectorList(WTF::move(selectorList));
282 }
283
284 void CSSParserSelector::setLangArgumentList(const Vector<CSSParserString>& stringVector)
285 {
286     ASSERT_WITH_MESSAGE(!stringVector.isEmpty(), "No CSS Selector takes an empty argument list.");
287     auto argumentList = std::make_unique<Vector<AtomicString>>();
288     argumentList->reserveInitialCapacity(stringVector.size());
289     for (const AtomicString& languageArgument : stringVector)
290         argumentList->append(languageArgument);
291     m_selector->setLangArgumentList(WTF::move(argumentList));
292 }
293
294 void CSSParserSelector::setPseudoClassValue(const CSSParserString& pseudoClassString)
295 {
296     ASSERT(m_selector->match() == CSSSelector::PseudoClass);
297
298     PseudoClassOrCompatibilityPseudoElement pseudoType = parsePseudoClassAndCompatibilityElementString(pseudoClassString);
299     m_selector->setPseudoClassType(pseudoType.pseudoClass);
300 }
301
302 static bool selectorListMatchesPseudoElement(const CSSSelectorList* selectorList)
303 {
304     if (!selectorList)
305         return false;
306
307     for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
308         for (const CSSSelector* selector = subSelector; selector; selector = selector->tagHistory()) {
309             if (selector->matchesPseudoElement())
310                 return true;
311             if (const CSSSelectorList* subselectorList = selector->selectorList()) {
312                 if (selectorListMatchesPseudoElement(subselectorList))
313                     return true;
314             }
315         }
316     }
317     return false;
318 }
319
320 bool CSSParserSelector::matchesPseudoElement() const
321 {
322     return m_selector->matchesPseudoElement() || selectorListMatchesPseudoElement(m_selector->selectorList());
323 }
324
325 void CSSParserSelector::insertTagHistory(CSSSelector::Relation before, std::unique_ptr<CSSParserSelector> selector, CSSSelector::Relation after)
326 {
327     if (m_tagHistory)
328         selector->setTagHistory(WTF::move(m_tagHistory));
329     setRelation(before);
330     selector->setRelation(after);
331     m_tagHistory = WTF::move(selector);
332 }
333
334 void CSSParserSelector::appendTagHistory(CSSSelector::Relation relation, std::unique_ptr<CSSParserSelector> selector)
335 {
336     CSSParserSelector* end = this;
337     while (end->tagHistory())
338         end = end->tagHistory();
339
340     end->setRelation(relation);
341     end->setTagHistory(WTF::move(selector));
342 }
343
344 void CSSParserSelector::appendTagHistory(CSSParserSelectorCombinator relation, std::unique_ptr<CSSParserSelector> selector)
345 {
346     CSSParserSelector* end = this;
347     while (end->tagHistory())
348         end = end->tagHistory();
349
350     CSSSelector::Relation selectorRelation;
351     switch (relation) {
352     case CSSParserSelectorCombinator::Child:
353         selectorRelation = CSSSelector::Child;
354         break;
355     case CSSParserSelectorCombinator::DescendantSpace:
356         selectorRelation = CSSSelector::Descendant;
357         break;
358 #if ENABLE(CSS_SELECTORS_LEVEL4)
359     case CSSParserSelectorCombinator::DescendantDoubleChild:
360         selectorRelation = CSSSelector::Descendant;
361         break;
362 #endif
363     case CSSParserSelectorCombinator::DirectAdjacent:
364         selectorRelation = CSSSelector::DirectAdjacent;
365         break;
366     case CSSParserSelectorCombinator::IndirectAdjacent:
367         selectorRelation = CSSSelector::IndirectAdjacent;
368         break;
369     }
370     end->setRelation(selectorRelation);
371
372 #if ENABLE(CSS_SELECTORS_LEVEL4)
373     if (relation == CSSParserSelectorCombinator::DescendantDoubleChild)
374         end->setDescendantUseDoubleChildSyntax();
375 #endif
376
377     end->setTagHistory(WTF::move(selector));
378 }
379
380 void CSSParserSelector::prependTagSelector(const QualifiedName& tagQName, bool tagIsForNamespaceRule)
381 {
382     auto second = std::make_unique<CSSParserSelector>();
383     second->m_selector = WTF::move(m_selector);
384     second->m_tagHistory = WTF::move(m_tagHistory);
385     m_tagHistory = WTF::move(second);
386
387     m_selector = std::make_unique<CSSSelector>(tagQName, tagIsForNamespaceRule);
388     m_selector->setRelation(CSSSelector::SubSelector);
389 }
390
391 }
392