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