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 "CSSParserSelector.h"
23
24#include "CSSSelector.h"
25#include "CSSSelectorList.h"
26#include "SelectorPseudoTypeMap.h"
27
28#if COMPILER(MSVC)
29// See https://msdn.microsoft.com/en-us/library/1wea5zwe.aspx
30#pragma warning(disable: 4701)
31#endif
32
33namespace WebCore {
34
35CSSParserSelector* CSSParserSelector::parsePagePseudoSelector(const AtomicString& pseudoTypeString)
36{
37 CSSSelector::PagePseudoClassType pseudoType;
38 if (equalLettersIgnoringASCIICase(pseudoTypeString, "first"))
39 pseudoType = CSSSelector::PagePseudoClassFirst;
40 else if (equalLettersIgnoringASCIICase(pseudoTypeString, "left"))
41 pseudoType = CSSSelector::PagePseudoClassLeft;
42 else if (equalLettersIgnoringASCIICase(pseudoTypeString, "right"))
43 pseudoType = CSSSelector::PagePseudoClassRight;
44 else
45 return nullptr;
46
47 auto selector = std::make_unique<CSSParserSelector>();
48 selector->m_selector->setMatch(CSSSelector::PagePseudoClass);
49 selector->m_selector->setPagePseudoType(pseudoType);
50 return selector.release();
51}
52
53CSSParserSelector* CSSParserSelector::parsePseudoElementSelectorFromStringView(StringView& pseudoTypeString)
54{
55 AtomicString name = pseudoTypeString.toAtomicString();
56
57 CSSSelector::PseudoElementType pseudoType = CSSSelector::parsePseudoElementType(name);
58 if (pseudoType == CSSSelector::PseudoElementUnknown) {
59 // FIXME-NEWPARSER: We can't add "slotted" to the map without breaking the old
60 // parser, so this hack ensures the new parser still recognizes it. When the new
61 // parser turns on, we can add "slotted" to the map and remove this code.
62 if (pseudoTypeString.startsWithIgnoringASCIICase("slotted"))
63 pseudoType = CSSSelector::PseudoElementSlotted;
64 else
65 return nullptr;
66 }
67
68 auto selector = std::make_unique<CSSParserSelector>();
69 selector->m_selector->setMatch(CSSSelector::PseudoElement);
70 selector->m_selector->setPseudoElementType(pseudoType);
71 if (pseudoType == CSSSelector::PseudoElementWebKitCustomLegacyPrefixed) {
72 ASSERT_WITH_MESSAGE(name == "-webkit-input-placeholder", "-webkit-input-placeholder is the only LegacyPrefix pseudo type.");
73 if (name == "-webkit-input-placeholder")
74 name = AtomicString("placeholder", AtomicString::ConstructFromLiteral);
75 }
76 selector->m_selector->setValue(name);
77 return selector.release();
78}
79
80CSSParserSelector* CSSParserSelector::parsePseudoClassSelectorFromStringView(StringView& pseudoTypeString)
81{
82 PseudoClassOrCompatibilityPseudoElement pseudoType = parsePseudoClassAndCompatibilityElementString(pseudoTypeString);
83 if (pseudoType.pseudoClass != CSSSelector::PseudoClassUnknown) {
84 auto selector = std::make_unique<CSSParserSelector>();
85 selector->m_selector->setMatch(CSSSelector::PseudoClass);
86 selector->m_selector->setPseudoClassType(pseudoType.pseudoClass);
87 return selector.release();
88 }
89 if (pseudoType.compatibilityPseudoElement != CSSSelector::PseudoElementUnknown) {
90 auto selector = std::make_unique<CSSParserSelector>();
91 selector->m_selector->setMatch(CSSSelector::PseudoElement);
92 selector->m_selector->setPseudoElementType(pseudoType.compatibilityPseudoElement);
93 AtomicString name = pseudoTypeString.toAtomicString();
94 selector->m_selector->setValue(name);
95 return selector.release();
96 }
97 return nullptr;
98}
99
100CSSParserSelector::CSSParserSelector()
101 : m_selector(std::make_unique<CSSSelector>())
102{
103}
104
105CSSParserSelector::CSSParserSelector(const QualifiedName& tagQName)
106 : m_selector(std::make_unique<CSSSelector>(tagQName))
107{
108}
109
110CSSParserSelector::~CSSParserSelector()
111{
112 if (!m_tagHistory)
113 return;
114 Vector<std::unique_ptr<CSSParserSelector>, 16> toDelete;
115 std::unique_ptr<CSSParserSelector> selector = WTFMove(m_tagHistory);
116 while (true) {
117 std::unique_ptr<CSSParserSelector> next = WTFMove(selector->m_tagHistory);
118 toDelete.append(WTFMove(selector));
119 if (!next)
120 break;
121 selector = WTFMove(next);
122 }
123}
124
125void CSSParserSelector::adoptSelectorVector(Vector<std::unique_ptr<CSSParserSelector>>&& selectorVector)
126{
127 m_selector->setSelectorList(std::make_unique<CSSSelectorList>(WTFMove(selectorVector)));
128}
129
130void CSSParserSelector::setLangArgumentList(std::unique_ptr<Vector<AtomicString>> argumentList)
131{
132 ASSERT_WITH_MESSAGE(!argumentList->isEmpty(), "No CSS Selector takes an empty argument list.");
133 m_selector->setLangArgumentList(WTFMove(argumentList));
134}
135
136void CSSParserSelector::setSelectorList(std::unique_ptr<CSSSelectorList> selectorList)
137{
138 m_selector->setSelectorList(WTFMove(selectorList));
139}
140
141static bool selectorListMatchesPseudoElement(const CSSSelectorList* selectorList)
142{
143 if (!selectorList)
144 return false;
145
146 for (const CSSSelector* subSelector = selectorList->first(); subSelector; subSelector = CSSSelectorList::next(subSelector)) {
147 for (const CSSSelector* selector = subSelector; selector; selector = selector->tagHistory()) {
148 if (selector->matchesPseudoElement())
149 return true;
150 if (const CSSSelectorList* subselectorList = selector->selectorList()) {
151 if (selectorListMatchesPseudoElement(subselectorList))
152 return true;
153 }
154 }
155 }
156 return false;
157}
158
159bool CSSParserSelector::matchesPseudoElement() const
160{
161 return m_selector->matchesPseudoElement() || selectorListMatchesPseudoElement(m_selector->selectorList());
162}
163
164void CSSParserSelector::insertTagHistory(CSSSelector::RelationType before, std::unique_ptr<CSSParserSelector> selector, CSSSelector::RelationType after)
165{
166 if (m_tagHistory)
167 selector->setTagHistory(WTFMove(m_tagHistory));
168 setRelation(before);
169 selector->setRelation(after);
170 m_tagHistory = WTFMove(selector);
171}
172
173void CSSParserSelector::appendTagHistory(CSSSelector::RelationType relation, std::unique_ptr<CSSParserSelector> selector)
174{
175 CSSParserSelector* end = this;
176 while (end->tagHistory())
177 end = end->tagHistory();
178
179 end->setRelation(relation);
180 end->setTagHistory(WTFMove(selector));
181}
182
183void CSSParserSelector::appendTagHistory(CSSParserSelectorCombinator relation, std::unique_ptr<CSSParserSelector> selector)
184{
185 CSSParserSelector* end = this;
186 while (end->tagHistory())
187 end = end->tagHistory();
188
189 CSSSelector::RelationType selectorRelation;
190 switch (relation) {
191 case CSSParserSelectorCombinator::Child:
192 selectorRelation = CSSSelector::Child;
193 break;
194 case CSSParserSelectorCombinator::DescendantSpace:
195 selectorRelation = CSSSelector::DescendantSpace;
196 break;
197 case CSSParserSelectorCombinator::DirectAdjacent:
198 selectorRelation = CSSSelector::DirectAdjacent;
199 break;
200 case CSSParserSelectorCombinator::IndirectAdjacent:
201 selectorRelation = CSSSelector::IndirectAdjacent;
202 break;
203 }
204 end->setRelation(selectorRelation);
205 end->setTagHistory(WTFMove(selector));
206}
207
208void CSSParserSelector::prependTagSelector(const QualifiedName& tagQName, bool tagIsForNamespaceRule)
209{
210 auto second = std::make_unique<CSSParserSelector>();
211 second->m_selector = WTFMove(m_selector);
212 second->m_tagHistory = WTFMove(m_tagHistory);
213 m_tagHistory = WTFMove(second);
214
215 m_selector = std::make_unique<CSSSelector>(tagQName, tagIsForNamespaceRule);
216 m_selector->setRelation(CSSSelector::Subselector);
217}
218
219std::unique_ptr<CSSParserSelector> CSSParserSelector::releaseTagHistory()
220{
221 setRelation(CSSSelector::Subselector);
222 return WTFMove(m_tagHistory);
223}
224
225// FIXME-NEWPARSER: Add support for :host-context
226bool CSSParserSelector::isHostPseudoSelector() const
227{
228 return match() == CSSSelector::PseudoClass && pseudoClassType() == CSSSelector::PseudoClassHost;
229}
230
231}
232
233