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