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;
134 struct CSSParserFunction;
136 struct CSSParserValue {
142 CSSParserString string;
143 CSSParserFunction* function;
152 inline void setFromNumber(double value, int unit = CSSPrimitiveValue::CSS_NUMBER);
153 inline void setFromFunction(CSSParserFunction*);
155 PassRefPtr<CSSValue> createCSSValue();
158 class CSSParserValueList {
159 WTF_MAKE_FAST_ALLOCATED;
165 ~CSSParserValueList();
167 void addValue(const CSSParserValue&);
168 void insertValueAt(unsigned, const CSSParserValue&);
169 void deleteValueAt(unsigned);
170 void extend(CSSParserValueList&);
172 unsigned size() const { return m_values.size(); }
173 unsigned currentIndex() { return m_current; }
174 CSSParserValue* current() { return m_current < m_values.size() ? &m_values[m_current] : 0; }
175 CSSParserValue* next() { ++m_current; return current(); }
176 CSSParserValue* previous()
184 CSSParserValue* valueAt(unsigned i) { return i < m_values.size() ? &m_values[i] : 0; }
186 void clear() { m_values.clear(); }
190 Vector<CSSParserValue, 4> m_values;
193 struct CSSParserFunction {
194 WTF_MAKE_FAST_ALLOCATED;
196 CSSParserString name;
197 OwnPtr<CSSParserValueList> args;
200 class CSSParserSelector {
201 WTF_MAKE_NONCOPYABLE(CSSParserSelector); WTF_MAKE_FAST_ALLOCATED;
204 explicit CSSParserSelector(const QualifiedName&);
205 ~CSSParserSelector();
207 PassOwnPtr<CSSSelector> releaseSelector() { return m_selector.release(); }
209 CSSSelector::Relation relation() const { return m_selector->relation(); }
210 void setValue(const AtomicString& value) { m_selector->setValue(value); }
211 void setAttribute(const QualifiedName& value) { m_selector->setAttribute(value); }
212 void setArgument(const AtomicString& value) { m_selector->setArgument(value); }
213 void setMatch(CSSSelector::Match value) { m_selector->m_match = value; }
214 void setRelation(CSSSelector::Relation value) { m_selector->m_relation = value; }
215 void setForPage() { m_selector->setForPage(); }
216 void setRelationIsAffectedByPseudoContent() { m_selector->setRelationIsAffectedByPseudoContent(); }
217 bool relationIsAffectedByPseudoContent() const { return m_selector->relationIsAffectedByPseudoContent(); }
218 void setMatchUserAgentOnly() { m_selector->setMatchUserAgentOnly(); }
220 void adoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectorVector);
222 CSSParserSelector* functionArgumentSelector() const { return m_functionArgumentSelector; }
223 void setFunctionArgumentSelector(CSSParserSelector* selector) { m_functionArgumentSelector = selector; }
224 bool isDistributedPseudoElement() const { return m_selector->isDistributedPseudoElement(); }
225 CSSParserSelector* findDistributedPseudoElementSelector() const;
226 bool isContentPseudoElement() const { return m_selector->isContentPseudoElement(); }
228 CSSSelector::PseudoType pseudoType() const { return m_selector->pseudoType(); }
229 bool isCustomPseudoElement() const { return m_selector->isCustomPseudoElement(); }
230 bool needsCrossingTreeScopeBoundary() const { return isCustomPseudoElement() || pseudoType() == CSSSelector::PseudoCue; }
232 bool isSimple() const;
233 bool hasShadowPseudo() const;
235 CSSParserSelector* tagHistory() const { return m_tagHistory.get(); }
236 void setTagHistory(PassOwnPtr<CSSParserSelector> selector) { m_tagHistory = selector; }
237 void clearTagHistory() { m_tagHistory.clear(); }
238 void insertTagHistory(CSSSelector::Relation before, PassOwnPtr<CSSParserSelector>, CSSSelector::Relation after);
239 void appendTagHistory(CSSSelector::Relation, PassOwnPtr<CSSParserSelector>);
240 void prependTagSelector(const QualifiedName&, bool tagIsForNamespaceRule = false);
243 OwnPtr<CSSSelector> m_selector;
244 OwnPtr<CSSParserSelector> m_tagHistory;
245 CSSParserSelector* m_functionArgumentSelector;
248 inline bool CSSParserSelector::hasShadowPseudo() const
250 return m_selector->relation() == CSSSelector::ShadowPseudo;
253 inline void CSSParserValue::setFromNumber(double value, int unit)
255 id = CSSValueInvalid;
261 inline void CSSParserValue::setFromFunction(CSSParserFunction* function)
263 id = CSSValueInvalid;
264 this->function = function;