2 * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
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.
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.
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.
21 #ifndef CSSParserValues_h
22 #define CSSParserValues_h
24 #include "CSSSelector.h"
25 #include "CSSValueKeywords.h"
26 #include "CSSValueList.h"
27 #include <wtf/text/AtomicString.h>
28 #include <wtf/text/WTFString.h>
35 struct CSSParserString {
36 void init(LChar* characters, unsigned length)
38 m_data.characters8 = characters;
43 void init(UChar* characters, unsigned length)
45 m_data.characters16 = characters;
50 void init(const String& string)
52 m_length = string.length();
53 if (!m_length || string.is8Bit()) {
54 m_data.characters8 = const_cast<LChar*>(string.characters8());
57 m_data.characters16 = const_cast<UChar*>(string.characters16());
64 m_data.characters8 = 0;
69 bool is8Bit() const { return m_is8Bit; }
70 LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
71 UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
72 template <typename CharacterType>
73 CharacterType* characters() const;
75 unsigned length() const { return m_length; }
76 void setLength(unsigned length) { m_length = length; }
80 UChar operator[](unsigned i) const
82 ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
84 return m_data.characters8[i];
85 return m_data.characters16[i];
88 bool equalIgnoringCase(const char* str) const
91 return WTF::equalIgnoringCase(str, characters8(), length());
92 return WTF::equalIgnoringCase(str, characters16(), length());
95 operator String() const { return is8Bit() ? String(m_data.characters8, m_length) : String(m_data.characters16, m_length); }
96 operator AtomicString() const { return is8Bit() ? AtomicString(m_data.characters8, m_length) : AtomicString(m_data.characters16, m_length); }
106 struct CSSParserFunction;
108 struct CSSParserValue {
114 CSSParserString string;
115 CSSParserFunction* function;
116 CSSParserValueList* valueList;
121 ValueList = 0x100002,
126 void setFromValueList(std::unique_ptr<CSSParserValueList>);
128 PassRefPtr<CSSValue> createCSSValue();
131 void destroy(const CSSParserValue&);
133 class CSSParserValueList {
134 WTF_MAKE_FAST_ALLOCATED;
140 ~CSSParserValueList();
142 void addValue(const CSSParserValue&);
143 void insertValueAt(unsigned, const CSSParserValue&);
144 void deleteValueAt(unsigned);
145 void extend(CSSParserValueList&);
147 unsigned size() const { return m_values.size(); }
148 unsigned currentIndex() { return m_current; }
149 CSSParserValue* current() { return m_current < m_values.size() ? &m_values[m_current] : 0; }
150 CSSParserValue* next() { ++m_current; return current(); }
151 CSSParserValue* previous()
158 void setCurrentIndex(unsigned index)
160 ASSERT(index < m_values.size());
161 if (index < m_values.size())
165 CSSParserValue* valueAt(unsigned i) { return i < m_values.size() ? &m_values[i] : 0; }
167 void clear() { m_values.clear(); }
171 Vector<CSSParserValue, 4> m_values;
174 struct CSSParserFunction {
175 WTF_MAKE_FAST_ALLOCATED;
177 CSSParserString name;
178 std::unique_ptr<CSSParserValueList> args;
181 class CSSParserSelector {
182 WTF_MAKE_FAST_ALLOCATED;
184 static CSSParserSelector* parsePagePseudoSelector(const CSSParserString& pseudoTypeString);
185 static CSSParserSelector* parsePseudoElementSelector(CSSParserString& pseudoTypeString);
186 static CSSParserSelector* parsePseudoElementCueFunctionSelector(const CSSParserString& functionIdentifier, Vector<std::unique_ptr<CSSParserSelector>>* selectorVector);
187 static CSSParserSelector* parsePseudoClassAndCompatibilityElementSelector(CSSParserString& pseudoTypeString);
190 explicit CSSParserSelector(const QualifiedName&);
191 ~CSSParserSelector();
193 std::unique_ptr<CSSSelector> releaseSelector() { return std::move(m_selector); }
195 void setValue(const AtomicString& value) { m_selector->setValue(value); }
196 void setAttribute(const QualifiedName& value, bool isCaseInsensitive) { m_selector->setAttribute(value, isCaseInsensitive); }
197 void setArgument(const AtomicString& value) { m_selector->setArgument(value); }
198 void setMatch(CSSSelector::Match value) { m_selector->m_match = value; }
199 void setRelation(CSSSelector::Relation value) { m_selector->m_relation = value; }
200 void setForPage() { m_selector->setForPage(); }
203 void adoptSelectorVector(Vector<std::unique_ptr<CSSParserSelector>>& selectorVector);
205 void setPseudoClassValue(const CSSParserString& pseudoClassString);
206 CSSSelector::PseudoClassType pseudoClassType() const { return m_selector->pseudoClassType(); }
207 bool isCustomPseudoElement() const { return m_selector->isCustomPseudoElement(); }
209 bool isPseudoElementCueFunction() const
211 #if ENABLE(VIDEO_TRACK)
212 return m_selector->m_match == CSSSelector::PseudoElement && m_selector->pseudoElementType() == CSSSelector::PseudoElementCue;
218 bool isSimple() const;
219 bool hasShadowDescendant() const;
221 CSSParserSelector* tagHistory() const { return m_tagHistory.get(); }
222 void setTagHistory(std::unique_ptr<CSSParserSelector> selector) { m_tagHistory = std::move(selector); }
223 void clearTagHistory() { m_tagHistory.reset(); }
224 void insertTagHistory(CSSSelector::Relation before, std::unique_ptr<CSSParserSelector>, CSSSelector::Relation after);
225 void appendTagHistory(CSSSelector::Relation, std::unique_ptr<CSSParserSelector>);
226 void prependTagSelector(const QualifiedName&, bool tagIsForNamespaceRule = false);
229 std::unique_ptr<CSSSelector> m_selector;
230 std::unique_ptr<CSSParserSelector> m_tagHistory;
233 inline bool CSSParserSelector::hasShadowDescendant() const
235 return m_selector->relation() == CSSSelector::ShadowDescendant;
238 inline void CSSParserValue::setFromValueList(std::unique_ptr<CSSParserValueList> valueList)
240 id = CSSValueInvalid;
241 this->valueList = valueList.release();