OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * 1. Redistributions of source code must retain the above copyright | |
8 * notice, this list of conditions and the following disclaimer. | |
9 * 2. Redistributions in binary form must reproduce the above copyright | |
10 * notice, this list of conditions and the following disclaimer in the | |
11 * documentation and/or other materials provided with the distribution. | |
12 * | |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | |
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 */ | |
24 | |
25 #include "config.h" | |
26 #include "core/html/DOMTokenList.h" | |
27 | |
28 #include "bindings/v8/ExceptionState.h" | |
29 #include "core/dom/ExceptionCode.h" | |
30 #include "core/html/parser/HTMLParserIdioms.h" | |
31 #include "wtf/text/StringBuilder.h" | |
32 | |
33 namespace WebCore { | |
34 | |
35 bool DOMTokenList::validateToken(const AtomicString& token, ExceptionState& es) | |
36 { | |
37 if (token.isEmpty()) { | |
38 es.throwDOMException(SyntaxError); | |
39 return false; | |
40 } | |
41 | |
42 unsigned length = token.length(); | |
43 for (unsigned i = 0; i < length; ++i) { | |
44 if (isHTMLSpace(token[i])) { | |
45 es.throwDOMException(InvalidCharacterError); | |
46 return false; | |
47 } | |
48 } | |
49 | |
50 return true; | |
51 } | |
52 | |
53 bool DOMTokenList::validateTokens(const Vector<String>& tokens, ExceptionState&
es) | |
54 { | |
55 for (size_t i = 0; i < tokens.size(); ++i) { | |
56 if (!validateToken(tokens[i], es)) | |
57 return false; | |
58 } | |
59 | |
60 return true; | |
61 } | |
62 | |
63 bool DOMTokenList::contains(const AtomicString& token, ExceptionState& es) const | |
64 { | |
65 if (!validateToken(token, es)) | |
66 return false; | |
67 return containsInternal(token); | |
68 } | |
69 | |
70 void DOMTokenList::add(const AtomicString& token, ExceptionState& es) | |
71 { | |
72 Vector<String> tokens; | |
73 tokens.append(token.string()); | |
74 add(tokens, es); | |
75 } | |
76 | |
77 void DOMTokenList::add(const Vector<String>& tokens, ExceptionState& es) | |
78 { | |
79 Vector<String> filteredTokens; | |
80 for (size_t i = 0; i < tokens.size(); ++i) { | |
81 if (!validateToken(tokens[i], es)) | |
82 return; | |
83 if (!containsInternal(tokens[i])) | |
84 filteredTokens.append(tokens[i]); | |
85 } | |
86 | |
87 if (filteredTokens.isEmpty()) | |
88 return; | |
89 | |
90 setValue(addTokens(value(), filteredTokens)); | |
91 } | |
92 | |
93 void DOMTokenList::remove(const AtomicString& token, ExceptionState& es) | |
94 { | |
95 Vector<String> tokens; | |
96 tokens.append(token.string()); | |
97 remove(tokens, es); | |
98 } | |
99 | |
100 void DOMTokenList::remove(const Vector<String>& tokens, ExceptionState& es) | |
101 { | |
102 if (!validateTokens(tokens, es)) | |
103 return; | |
104 | |
105 // Check using containsInternal first since it is a lot faster than going | |
106 // through the string character by character. | |
107 bool found = false; | |
108 for (size_t i = 0; i < tokens.size(); ++i) { | |
109 if (containsInternal(tokens[i])) { | |
110 found = true; | |
111 break; | |
112 } | |
113 } | |
114 | |
115 if (found) | |
116 setValue(removeTokens(value(), tokens)); | |
117 } | |
118 | |
119 bool DOMTokenList::toggle(const AtomicString& token, ExceptionState& es) | |
120 { | |
121 if (!validateToken(token, es)) | |
122 return false; | |
123 | |
124 if (containsInternal(token)) { | |
125 removeInternal(token); | |
126 return false; | |
127 } | |
128 addInternal(token); | |
129 return true; | |
130 } | |
131 | |
132 bool DOMTokenList::toggle(const AtomicString& token, bool force, ExceptionState&
es) | |
133 { | |
134 if (!validateToken(token, es)) | |
135 return false; | |
136 | |
137 if (force) | |
138 addInternal(token); | |
139 else | |
140 removeInternal(token); | |
141 | |
142 return force; | |
143 } | |
144 | |
145 void DOMTokenList::addInternal(const AtomicString& token) | |
146 { | |
147 if (!containsInternal(token)) | |
148 setValue(addToken(value(), token)); | |
149 } | |
150 | |
151 void DOMTokenList::removeInternal(const AtomicString& token) | |
152 { | |
153 // Check using contains first since it uses AtomicString comparisons instead | |
154 // of character by character testing. | |
155 if (!containsInternal(token)) | |
156 return; | |
157 setValue(removeToken(value(), token)); | |
158 } | |
159 | |
160 String DOMTokenList::addToken(const AtomicString& input, const AtomicString& tok
en) | |
161 { | |
162 Vector<String> tokens; | |
163 tokens.append(token.string()); | |
164 return addTokens(input, tokens); | |
165 } | |
166 | |
167 String DOMTokenList::addTokens(const AtomicString& input, const Vector<String>&
tokens) | |
168 { | |
169 bool needsSpace = false; | |
170 | |
171 StringBuilder builder; | |
172 if (!input.isEmpty()) { | |
173 builder.append(input); | |
174 needsSpace = !isHTMLSpace(input[input.length() - 1]); | |
175 } | |
176 | |
177 for (size_t i = 0; i < tokens.size(); ++i) { | |
178 if (needsSpace) | |
179 builder.append(' '); | |
180 builder.append(tokens[i]); | |
181 needsSpace = true; | |
182 } | |
183 | |
184 return builder.toString(); | |
185 } | |
186 | |
187 String DOMTokenList::removeToken(const AtomicString& input, const AtomicString&
token) | |
188 { | |
189 Vector<String> tokens; | |
190 tokens.append(token.string()); | |
191 return removeTokens(input, tokens); | |
192 } | |
193 | |
194 String DOMTokenList::removeTokens(const AtomicString& input, const Vector<String
>& tokens) | |
195 { | |
196 // Algorithm defined at http://www.whatwg.org/specs/web-apps/current-work/mu
ltipage/common-microsyntaxes.html#remove-a-token-from-a-string | |
197 // New spec is at http://dom.spec.whatwg.org/#remove-a-token-from-a-string | |
198 | |
199 unsigned inputLength = input.length(); | |
200 StringBuilder output; // 3 | |
201 output.reserveCapacity(inputLength); | |
202 unsigned position = 0; // 4 | |
203 | |
204 // Step 5 | |
205 while (position < inputLength) { | |
206 if (isHTMLSpace(input[position])) { // 6 | |
207 output.append(input[position++]); // 6.1, 6.2 | |
208 continue; // 6.3 | |
209 } | |
210 | |
211 // Step 7 | |
212 StringBuilder tokenBuilder; | |
213 while (position < inputLength && isNotHTMLSpace(input[position])) | |
214 tokenBuilder.append(input[position++]); | |
215 | |
216 // Step 8 | |
217 String token = tokenBuilder.toString(); | |
218 if (tokens.contains(token)) { | |
219 // Step 8.1 | |
220 while (position < inputLength && isHTMLSpace(input[position])) | |
221 ++position; | |
222 | |
223 // Step 8.2 | |
224 size_t j = output.length(); | |
225 while (j > 0 && isHTMLSpace(output[j - 1])) | |
226 --j; | |
227 output.resize(j); | |
228 | |
229 // Step 8.3 | |
230 if (position < inputLength && !output.isEmpty()) | |
231 output.append(' '); | |
232 } else { | |
233 output.append(token); // Step 9 | |
234 } | |
235 } | |
236 | |
237 return output.toString(); | |
238 } | |
239 | |
240 } // namespace WebCore | |
OLD | NEW |