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