- add third_party src.
[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 struct CSSParserFunction;
135
136 struct CSSParserValue {
137     CSSValueID id;
138     bool isInt;
139     union {
140         double fValue;
141         int iValue;
142         CSSParserString string;
143         CSSParserFunction* function;
144     };
145     enum {
146         Operator = 0x100000,
147         Function = 0x100001,
148         Q_EMS    = 0x100002
149     };
150     int unit;
151
152     inline void setFromNumber(double value, int unit = CSSPrimitiveValue::CSS_NUMBER);
153     inline void setFromFunction(CSSParserFunction*);
154
155     PassRefPtr<CSSValue> createCSSValue();
156 };
157
158 class CSSParserValueList {
159     WTF_MAKE_FAST_ALLOCATED;
160 public:
161     CSSParserValueList()
162         : m_current(0)
163     {
164     }
165     ~CSSParserValueList();
166
167     void addValue(const CSSParserValue&);
168     void insertValueAt(unsigned, const CSSParserValue&);
169     void deleteValueAt(unsigned);
170     void extend(CSSParserValueList&);
171
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()
177     {
178         if (!m_current)
179             return 0;
180         --m_current;
181         return current();
182     }
183
184     CSSParserValue* valueAt(unsigned i) { return i < m_values.size() ? &m_values[i] : 0; }
185
186     void clear() { m_values.clear(); }
187
188 private:
189     unsigned m_current;
190     Vector<CSSParserValue, 4> m_values;
191 };
192
193 struct CSSParserFunction {
194     WTF_MAKE_FAST_ALLOCATED;
195 public:
196     CSSParserString name;
197     OwnPtr<CSSParserValueList> args;
198 };
199
200 class CSSParserSelector {
201     WTF_MAKE_NONCOPYABLE(CSSParserSelector); WTF_MAKE_FAST_ALLOCATED;
202 public:
203     CSSParserSelector();
204     explicit CSSParserSelector(const QualifiedName&);
205     ~CSSParserSelector();
206
207     PassOwnPtr<CSSSelector> releaseSelector() { return m_selector.release(); }
208
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(); }
219
220     void adoptSelectorVector(Vector<OwnPtr<CSSParserSelector> >& selectorVector);
221
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(); }
227
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; }
231
232     bool isSimple() const;
233     bool hasShadowPseudo() const;
234
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);
241
242 private:
243     OwnPtr<CSSSelector> m_selector;
244     OwnPtr<CSSParserSelector> m_tagHistory;
245     CSSParserSelector* m_functionArgumentSelector;
246 };
247
248 inline bool CSSParserSelector::hasShadowPseudo() const
249 {
250     return m_selector->relation() == CSSSelector::ShadowPseudo;
251 }
252
253 inline void CSSParserValue::setFromNumber(double value, int unit)
254 {
255     id = CSSValueInvalid;
256     isInt = false;
257     fValue = value;
258     this->unit = unit;
259 }
260
261 inline void CSSParserValue::setFromFunction(CSSParserFunction* function)
262 {
263     id = CSSValueInvalid;
264     this->function = function;
265     unit = Function;
266 }
267
268 }
269
270 #endif