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 "CSSValueKeywords.h"
25 #include "core/css/CSSPrimitiveValue.h"
26 #include "core/css/CSSSelector.h"
27 #include "core/css/CSSValueList.h"
28 #include "wtf/text/AtomicString.h"
29 #include "wtf/text/WTFString.h"
36 struct CSSParserString {
37 void init(const LChar* characters, unsigned length)
39 m_data.characters8 = characters;
44 void init(const UChar* characters, unsigned length)
46 m_data.characters16 = characters;
51 void init(const String& string)
53 init(string, 0, string.length());
56 void init(const String& string, unsigned startOffset, unsigned length)
60 m_data.characters8 = 0;
64 if (string.is8Bit()) {
65 m_data.characters8 = const_cast<LChar*>(string.characters8()) + startOffset;
68 m_data.characters16 = const_cast<UChar*>(string.characters16()) + startOffset;
75 m_data.characters8 = 0;
80 void trimTrailingWhitespace();
82 bool is8Bit() const { return m_is8Bit; }
83 const LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
84 const UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
85 template <typename CharacterType>
86 const CharacterType* characters() const;
88 unsigned length() const { return m_length; }
89 void setLength(unsigned length) { m_length = length; }
91 UChar operator[](unsigned i) const
93 ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
95 return m_data.characters8[i];
96 return m_data.characters16[i];
99 bool equalIgnoringCase(const char* str) const
102 return WTF::equalIgnoringCase(str, characters8(), length());
103 return WTF::equalIgnoringCase(str, characters16(), length());
106 template <size_t strLength>
107 bool startsWithIgnoringCase(const char (&str)[strLength]) const
109 return startsWithIgnoringCase(str, strLength - 1);
112 bool startsWithIgnoringCase(const char* str, size_t strLength) const
114 if (length() < strLength)
116 return is8Bit() ? WTF::equalIgnoringCase(str, characters8(), strLength) : WTF::equalIgnoringCase(str, characters16(), strLength);
119 operator String() const { return is8Bit() ? String(m_data.characters8, m_length) : StringImpl::create8BitIfPossible(m_data.characters16, m_length); }
120 operator AtomicString() const { return is8Bit() ? AtomicString(m_data.characters8, m_length) : AtomicString(m_data.characters16, m_length); }
122 AtomicString atomicSubstring(unsigned position, unsigned length) const;
124 bool isFunction() const { return length() > 0 && (*this)[length() - 1] == '('; }
127 const LChar* characters8;
128 const UChar* characters16;
135 inline const LChar* CSSParserString::characters<LChar>() const { return characters8(); }
138 inline const UChar* CSSParserString::characters<UChar>() const { return characters16(); }
140 struct CSSParserFunction;
142 struct CSSParserValue {
148 CSSParserString string;
149 CSSParserFunction* function;
150 CSSParserValueList* valueList;
155 ValueList = 0x100002,
160 inline void setFromNumber(double value, int unit = CSSPrimitiveValue::CSS_NUMBER);
161 inline void setFromFunction(CSSParserFunction*);
162 inline void setFromValueList(PassOwnPtr<CSSParserValueList>);
164 PassRefPtr<CSSValue> createCSSValue();
167 class CSSParserValueList {
168 WTF_MAKE_FAST_ALLOCATED;
174 ~CSSParserValueList();
176 void addValue(const CSSParserValue&);
177 void insertValueAt(unsigned, const CSSParserValue&);
178 void deleteValueAt(unsigned);
179 void extend(CSSParserValueList&);
181 unsigned size() const { return m_values.size(); }
182 unsigned currentIndex() { return m_current; }
183 CSSParserValue* current() { return m_current < m_values.size() ? &m_values[m_current] : 0; }
184 CSSParserValue* next() { ++m_current; return current(); }
185 CSSParserValue* previous()
193 CSSParserValue* valueAt(unsigned i) { return i < m_values.size() ? &m_values[i] : 0; }
195 void clear() { m_values.clear(); }
199 Vector<CSSParserValue, 4> m_values;
202 struct CSSParserFunction {
203 WTF_MAKE_FAST_ALLOCATED;
205 CSSParserString name;
206 OwnPtr<CSSParserValueList> args;
209 class CSSParserSelector {
210 WTF_MAKE_NONCOPYABLE(CSSParserSelector); WTF_MAKE_FAST_ALLOCATED;
213 explicit CSSParserSelector(const QualifiedName&);
214 ~CSSParserSelector();
216 PassOwnPtr<CSSSelector> releaseSelector() { return m_selector.release(); }
218 CSSSelector::Relation relation() const { return m_selector->relation(); }
219 void setValue(const AtomicString& value) { m_selector->setValue(value); }
220 void setAttribute(const QualifiedName& value) { m_selector->setAttribute(value); }
221 void setArgument(const AtomicString& value) { m_selector->setArgument(value); }
222 void setMatch(CSSSelector::Match value) { m_selector->m_match = value; }
223 void setRelation(CSSSelector::Relation value) { m_selector->m_relation = value; }
224 void setForPage() { m_selector->setForPage(); }
225 void setRelationIsAffectedByPseudoContent() { m_selector->setRelationIsAffectedByPseudoContent(); }
226 bool relationIsAffectedByPseudoContent() const { return m_selector->relationIsAffectedByPseudoContent(); }
228 void adoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectorVector);
230 CSSParserSelector* functionArgumentSelector() const { return m_functionArgumentSelector; }
231 void setFunctionArgumentSelector(CSSParserSelector* selector) { m_functionArgumentSelector = selector; }
232 bool isDistributedPseudoElement() const { return m_selector->isDistributedPseudoElement(); }
233 CSSParserSelector* findDistributedPseudoElementSelector() const;
234 bool isContentPseudoElement() const { return m_selector->isContentPseudoElement(); }
236 CSSSelector::PseudoType pseudoType() const { return m_selector->pseudoType(); }
237 bool isCustomPseudoElement() const { return m_selector->isCustomPseudoElement(); }
238 bool needsCrossingTreeScopeBoundary() const { return isCustomPseudoElement() || pseudoType() == CSSSelector::PseudoCue; }
240 bool isSimple() const;
241 bool hasShadowPseudo() const;
243 CSSParserSelector* tagHistory() const { return m_tagHistory.get(); }
244 void setTagHistory(PassOwnPtr<CSSParserSelector> selector) { m_tagHistory = selector; }
245 void clearTagHistory() { m_tagHistory.clear(); }
246 void insertTagHistory(CSSSelector::Relation before, PassOwnPtr<CSSParserSelector>, CSSSelector::Relation after);
247 void appendTagHistory(CSSSelector::Relation, PassOwnPtr<CSSParserSelector>);
248 void prependTagSelector(const QualifiedName&, bool tagIsForNamespaceRule = false);
251 OwnPtr<CSSSelector> m_selector;
252 OwnPtr<CSSParserSelector> m_tagHistory;
253 CSSParserSelector* m_functionArgumentSelector;
256 inline bool CSSParserSelector::hasShadowPseudo() const
258 return m_selector->relation() == CSSSelector::ShadowPseudo;
261 inline void CSSParserValue::setFromNumber(double value, int unit)
263 id = CSSValueInvalid;
269 inline void CSSParserValue::setFromFunction(CSSParserFunction* function)
271 id = CSSValueInvalid;
272 this->function = function;
276 inline void CSSParserValue::setFromValueList(PassOwnPtr<CSSParserValueList> valueList)
278 id = CSSValueInvalid;
279 this->valueList = valueList.leakPtr();