OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | 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 | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 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 | 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 | 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 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 | 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 | 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 | 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. | 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
23 */ | 23 */ |
24 | 24 |
25 #include "config.h" | 25 #include "config.h" |
26 #include "core/dom/DOMTokenList.h" | 26 #include "core/dom/DOMTokenList.h" |
27 | 27 |
28 #include "bindings/v8/ExceptionMessages.h" | |
29 #include "bindings/v8/ExceptionState.h" | 28 #include "bindings/v8/ExceptionState.h" |
30 #include "core/dom/ExceptionCode.h" | 29 #include "core/dom/ExceptionCode.h" |
31 #include "core/html/parser/HTMLParserIdioms.h" | 30 #include "core/html/parser/HTMLParserIdioms.h" |
32 #include "wtf/text/StringBuilder.h" | 31 #include "wtf/text/StringBuilder.h" |
33 | 32 |
34 namespace WebCore { | 33 namespace WebCore { |
35 | 34 |
36 bool DOMTokenList::validateToken(const AtomicString& token, const char* method,
ExceptionState& exceptionState) | 35 bool DOMTokenList::validateToken(const AtomicString& token, ExceptionState& exce
ptionState) |
37 { | 36 { |
38 if (token.isEmpty()) { | 37 if (token.isEmpty()) { |
39 exceptionState.throwDOMException(SyntaxError, ExceptionMessages::failedT
oExecute(method, "DOMTokenList", "The token provided must not be empty.")); | 38 exceptionState.throwDOMException(SyntaxError, "The token provided must n
ot be empty."); |
40 return false; | 39 return false; |
41 } | 40 } |
42 | 41 |
43 unsigned length = token.length(); | 42 unsigned length = token.length(); |
44 for (unsigned i = 0; i < length; ++i) { | 43 for (unsigned i = 0; i < length; ++i) { |
45 if (isHTMLSpace<UChar>(token[i])) { | 44 if (isHTMLSpace<UChar>(token[i])) { |
46 exceptionState.throwDOMException(InvalidCharacterError, ExceptionMes
sages::failedToExecute(method, "DOMTokenList", "The token provided ('" + token +
"') contains HTML space characters, which are not valid in tokens.")); | 45 exceptionState.throwDOMException(InvalidCharacterError, "The token p
rovided ('" + token + "') contains HTML space characters, which are not valid in
tokens."); |
47 return false; | 46 return false; |
48 } | 47 } |
49 } | 48 } |
50 | 49 |
51 return true; | 50 return true; |
52 } | 51 } |
53 | 52 |
54 bool DOMTokenList::validateTokens(const Vector<String>& tokens, const char* meth
od, ExceptionState& exceptionState) | 53 bool DOMTokenList::validateTokens(const Vector<String>& tokens, ExceptionState&
exceptionState) |
55 { | 54 { |
56 for (size_t i = 0; i < tokens.size(); ++i) { | 55 for (size_t i = 0; i < tokens.size(); ++i) { |
57 if (!validateToken(tokens[i], method, exceptionState)) | 56 if (!validateToken(tokens[i], exceptionState)) |
58 return false; | 57 return false; |
59 } | 58 } |
60 | 59 |
61 return true; | 60 return true; |
62 } | 61 } |
63 | 62 |
64 bool DOMTokenList::contains(const AtomicString& token, ExceptionState& exception
State) const | 63 bool DOMTokenList::contains(const AtomicString& token, ExceptionState& exception
State) const |
65 { | 64 { |
66 if (!validateToken(token, "contains", exceptionState)) | 65 if (!validateToken(token, exceptionState)) |
67 return false; | 66 return false; |
68 return containsInternal(token); | 67 return containsInternal(token); |
69 } | 68 } |
70 | 69 |
71 void DOMTokenList::add(const AtomicString& token, ExceptionState& exceptionState
) | 70 void DOMTokenList::add(const AtomicString& token, ExceptionState& exceptionState
) |
72 { | 71 { |
73 Vector<String> tokens; | 72 Vector<String> tokens; |
74 tokens.append(token.string()); | 73 tokens.append(token.string()); |
75 add(tokens, exceptionState); | 74 add(tokens, exceptionState); |
76 } | 75 } |
77 | 76 |
78 void DOMTokenList::add(const Vector<String>& tokens, ExceptionState& exceptionSt
ate) | 77 void DOMTokenList::add(const Vector<String>& tokens, ExceptionState& exceptionSt
ate) |
79 { | 78 { |
80 Vector<String> filteredTokens; | 79 Vector<String> filteredTokens; |
81 filteredTokens.reserveCapacity(tokens.size()); | 80 filteredTokens.reserveCapacity(tokens.size()); |
82 for (size_t i = 0; i < tokens.size(); ++i) { | 81 for (size_t i = 0; i < tokens.size(); ++i) { |
83 if (!validateToken(tokens[i], "add", exceptionState)) | 82 if (!validateToken(tokens[i], exceptionState)) |
84 return; | 83 return; |
85 if (containsInternal(tokens[i])) | 84 if (containsInternal(tokens[i])) |
86 continue; | 85 continue; |
87 if (filteredTokens.contains(tokens[i])) | 86 if (filteredTokens.contains(tokens[i])) |
88 continue; | 87 continue; |
89 filteredTokens.append(tokens[i]); | 88 filteredTokens.append(tokens[i]); |
90 } | 89 } |
91 | 90 |
92 if (filteredTokens.isEmpty()) | 91 if (filteredTokens.isEmpty()) |
93 return; | 92 return; |
94 | 93 |
95 setValue(addTokens(value(), filteredTokens)); | 94 setValue(addTokens(value(), filteredTokens)); |
96 } | 95 } |
97 | 96 |
98 void DOMTokenList::remove(const AtomicString& token, ExceptionState& exceptionSt
ate) | 97 void DOMTokenList::remove(const AtomicString& token, ExceptionState& exceptionSt
ate) |
99 { | 98 { |
100 Vector<String> tokens; | 99 Vector<String> tokens; |
101 tokens.append(token.string()); | 100 tokens.append(token.string()); |
102 remove(tokens, exceptionState); | 101 remove(tokens, exceptionState); |
103 } | 102 } |
104 | 103 |
105 void DOMTokenList::remove(const Vector<String>& tokens, ExceptionState& exceptio
nState) | 104 void DOMTokenList::remove(const Vector<String>& tokens, ExceptionState& exceptio
nState) |
106 { | 105 { |
107 if (!validateTokens(tokens, "remove", exceptionState)) | 106 if (!validateTokens(tokens, exceptionState)) |
108 return; | 107 return; |
109 | 108 |
110 // Check using containsInternal first since it is a lot faster than going | 109 // Check using containsInternal first since it is a lot faster than going |
111 // through the string character by character. | 110 // through the string character by character. |
112 bool found = false; | 111 bool found = false; |
113 for (size_t i = 0; i < tokens.size(); ++i) { | 112 for (size_t i = 0; i < tokens.size(); ++i) { |
114 if (containsInternal(tokens[i])) { | 113 if (containsInternal(tokens[i])) { |
115 found = true; | 114 found = true; |
116 break; | 115 break; |
117 } | 116 } |
118 } | 117 } |
119 | 118 |
120 if (found) | 119 if (found) |
121 setValue(removeTokens(value(), tokens)); | 120 setValue(removeTokens(value(), tokens)); |
122 } | 121 } |
123 | 122 |
124 bool DOMTokenList::toggle(const AtomicString& token, ExceptionState& exceptionSt
ate) | 123 bool DOMTokenList::toggle(const AtomicString& token, ExceptionState& exceptionSt
ate) |
125 { | 124 { |
126 if (!validateToken(token, "toggle", exceptionState)) | 125 if (!validateToken(token, exceptionState)) |
127 return false; | 126 return false; |
128 | 127 |
129 if (containsInternal(token)) { | 128 if (containsInternal(token)) { |
130 removeInternal(token); | 129 removeInternal(token); |
131 return false; | 130 return false; |
132 } | 131 } |
133 addInternal(token); | 132 addInternal(token); |
134 return true; | 133 return true; |
135 } | 134 } |
136 | 135 |
137 bool DOMTokenList::toggle(const AtomicString& token, bool force, ExceptionState&
exceptionState) | 136 bool DOMTokenList::toggle(const AtomicString& token, bool force, ExceptionState&
exceptionState) |
138 { | 137 { |
139 if (!validateToken(token, "toggle", exceptionState)) | 138 if (!validateToken(token, exceptionState)) |
140 return false; | 139 return false; |
141 | 140 |
142 if (force) | 141 if (force) |
143 addInternal(token); | 142 addInternal(token); |
144 else | 143 else |
145 removeInternal(token); | 144 removeInternal(token); |
146 | 145 |
147 return force; | 146 return force; |
148 } | 147 } |
149 | 148 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 output.append(' '); | 235 output.append(' '); |
237 } else { | 236 } else { |
238 output.append(token); // Step 9 | 237 output.append(token); // Step 9 |
239 } | 238 } |
240 } | 239 } |
241 | 240 |
242 return output.toString(); | 241 return output.toString(); |
243 } | 242 } |
244 | 243 |
245 } // namespace WebCore | 244 } // namespace WebCore |
OLD | NEW |