Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / CSSParserValues.h
1 /*
2  * Copyright (C) 2003 Lars Knoll (knoll@kde.org)
3  * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 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 #ifndef CSSParserValues_h
22 #define CSSParserValues_h
23
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"
30
31 namespace WebCore {
32
33 class CSSValue;
34 class QualifiedName;
35
36 struct CSSParserString {
37     void init(const LChar* characters, unsigned length)
38     {
39         m_data.characters8 = characters;
40         m_length = length;
41         m_is8Bit = true;
42     }
43
44     void init(const UChar* characters, unsigned length)
45     {
46         m_data.characters16 = characters;
47         m_length = length;
48         m_is8Bit = false;
49     }
50
51     void init(const String& string)
52     {
53         init(string, 0, string.length());
54     }
55
56     void init(const String& string, unsigned startOffset, unsigned length)
57     {
58         m_length = length;
59         if (!m_length) {
60             m_data.characters8 = 0;
61             m_is8Bit = true;
62             return;
63         }
64         if (string.is8Bit()) {
65             m_data.characters8 = const_cast<LChar*>(string.characters8()) + startOffset;
66             m_is8Bit = true;
67         } else {
68             m_data.characters16 = const_cast<UChar*>(string.characters16()) + startOffset;
69             m_is8Bit = false;
70         }
71     }
72
73     void clear()
74     {
75         m_data.characters8 = 0;
76         m_length = 0;
77         m_is8Bit = true;
78     }
79
80     void trimTrailingWhitespace();
81
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;
87
88     unsigned length() const { return m_length; }
89     void setLength(unsigned length) { m_length = length; }
90
91     UChar operator[](unsigned i) const
92     {
93         ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
94         if (is8Bit())
95             return m_data.characters8[i];
96         return m_data.characters16[i];
97     }
98
99     bool equalIgnoringCase(const char* str) const
100     {
101         if (is8Bit())
102             return WTF::equalIgnoringCase(str, characters8(), length());
103         return WTF::equalIgnoringCase(str, characters16(), length());
104     }
105
106     template <size_t strLength>
107     bool startsWithIgnoringCase(const char (&str)[strLength]) const
108     {
109         return startsWithIgnoringCase(str, strLength - 1);
110     }
111
112     bool startsWithIgnoringCase(const char* str, size_t strLength) const
113     {
114         if (length() < strLength)
115             return false;
116         return is8Bit() ? WTF::equalIgnoringCase(str, characters8(), strLength) : WTF::equalIgnoringCase(str, characters16(), strLength);
117     }
118
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); }
121
122     AtomicString atomicSubstring(unsigned position, unsigned length) const;
123
124     bool isFunction() const { return length() > 0 && (*this)[length() - 1] == '('; }
125
126     union {
127         const LChar* characters8;
128         const UChar* characters16;
129     } m_data;
130     unsigned m_length;
131     bool m_is8Bit;
132 };
133
134 template <>
135 inline const LChar* CSSParserString::characters<LChar>() const { return characters8(); }
136
137 template <>
138 inline const UChar* CSSParserString::characters<UChar>() const { return characters16(); }
139
140 struct CSSParserFunction;
141
142 struct CSSParserValue {
143     CSSValueID id;
144     bool isInt;
145     union {
146         double fValue;
147         int iValue;
148         CSSParserString string;
149         CSSParserFunction* function;
150         CSSParserValueList* valueList;
151     };
152     enum {
153         Operator  = 0x100000,
154         Function  = 0x100001,
155         ValueList = 0x100002,
156         Q_EMS     = 0x100003,
157     };
158     int unit;
159
160     inline void setFromNumber(double value, int unit = CSSPrimitiveValue::CSS_NUMBER);
161     inline void setFromFunction(CSSParserFunction*);
162     inline void setFromValueList(PassOwnPtr<CSSParserValueList>);
163
164     PassRefPtr<CSSValue> createCSSValue();
165 };
166
167 class CSSParserValueList {
168     WTF_MAKE_FAST_ALLOCATED;
169 public:
170     CSSParserValueList()
171         : m_current(0)
172     {
173     }
174     ~CSSParserValueList();
175
176     void addValue(const CSSParserValue&);
177     void insertValueAt(unsigned, const CSSParserValue&);
178     void deleteValueAt(unsigned);
179     void extend(CSSParserValueList&);
180
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()
186     {
187         if (!m_current)
188             return 0;
189         --m_current;
190         return current();
191     }
192
193     CSSParserValue* valueAt(unsigned i) { return i < m_values.size() ? &m_values[i] : 0; }
194
195     void clear() { m_values.clear(); }
196
197 private:
198     unsigned m_current;
199     Vector<CSSParserValue, 4> m_values;
200 };
201
202 struct CSSParserFunction {
203     WTF_MAKE_FAST_ALLOCATED;
204 public:
205     CSSParserString name;
206     OwnPtr<CSSParserValueList> args;
207 };
208
209 class CSSParserSelector {
210     WTF_MAKE_NONCOPYABLE(CSSParserSelector); WTF_MAKE_FAST_ALLOCATED;
211 public:
212     CSSParserSelector();
213     explicit CSSParserSelector(const QualifiedName&);
214     ~CSSParserSelector();
215
216     PassOwnPtr<CSSSelector> releaseSelector() { return m_selector.release(); }
217
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(); }
227
228     void adoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectorVector);
229
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(); }
235
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; }
239
240     bool isSimple() const;
241     bool hasShadowPseudo() const;
242
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);
249
250 private:
251     OwnPtr<CSSSelector> m_selector;
252     OwnPtr<CSSParserSelector> m_tagHistory;
253     CSSParserSelector* m_functionArgumentSelector;
254 };
255
256 inline bool CSSParserSelector::hasShadowPseudo() const
257 {
258     return m_selector->relation() == CSSSelector::ShadowPseudo;
259 }
260
261 inline void CSSParserValue::setFromNumber(double value, int unit)
262 {
263     id = CSSValueInvalid;
264     isInt = false;
265     fValue = value;
266     this->unit = unit;
267 }
268
269 inline void CSSParserValue::setFromFunction(CSSParserFunction* function)
270 {
271     id = CSSValueInvalid;
272     this->function = function;
273     unit = Function;
274 }
275
276 inline void CSSParserValue::setFromValueList(PassOwnPtr<CSSParserValueList> valueList)
277 {
278     id = CSSValueInvalid;
279     this->valueList = valueList.leakPtr();
280     unit = ValueList;
281 }
282
283 }
284
285 #endif