| 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 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "core/dom/DOMTokenList.h" | 26 #include "core/dom/DOMTokenList.h" |
| 27 | 27 |
| 28 #include "bindings/v8/ExceptionMessages.h" | 28 #include "bindings/v8/ExceptionMessages.h" |
| 29 #include "bindings/v8/ExceptionState.h" | 29 #include "bindings/v8/ExceptionState.h" |
| 30 #include "core/dom/ExceptionCode.h" | 30 #include "core/dom/ExceptionCode.h" |
| 31 #include "core/html/parser/HTMLParserIdioms.h" | 31 #include "core/html/parser/HTMLParserIdioms.h" |
| 32 #include "wtf/text/StringBuilder.h" | 32 #include "wtf/text/StringBuilder.h" |
| 33 | 33 |
| 34 namespace WebCore { | 34 namespace WebCore { |
| 35 | 35 |
| 36 bool DOMTokenList::validateToken(const AtomicString& token, const char* method,
ExceptionState& es) | 36 bool DOMTokenList::validateToken(const AtomicString& token, const char* method,
ExceptionState& exceptionState) |
| 37 { | 37 { |
| 38 if (token.isEmpty()) { | 38 if (token.isEmpty()) { |
| 39 es.throwDOMException(SyntaxError, ExceptionMessages::failedToExecute(met
hod, "DOMTokenList", "The token provided must not be empty.")); | 39 exceptionState.throwDOMException(SyntaxError, ExceptionMessages::failedT
oExecute(method, "DOMTokenList", "The token provided must not be empty.")); |
| 40 return false; | 40 return false; |
| 41 } | 41 } |
| 42 | 42 |
| 43 unsigned length = token.length(); | 43 unsigned length = token.length(); |
| 44 for (unsigned i = 0; i < length; ++i) { | 44 for (unsigned i = 0; i < length; ++i) { |
| 45 if (isHTMLSpace<UChar>(token[i])) { | 45 if (isHTMLSpace<UChar>(token[i])) { |
| 46 es.throwDOMException(InvalidCharacterError, ExceptionMessages::faile
dToExecute(method, "DOMTokenList", "The token provided ('" + token + "') contain
s HTML space characters, which are not valid in tokens.")); | 46 exceptionState.throwDOMException(InvalidCharacterError, ExceptionMes
sages::failedToExecute(method, "DOMTokenList", "The token provided ('" + token +
"') contains HTML space characters, which are not valid in tokens.")); |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 return true; | 51 return true; |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool DOMTokenList::validateTokens(const Vector<String>& tokens, const char* meth
od, ExceptionState& es) | 54 bool DOMTokenList::validateTokens(const Vector<String>& tokens, const char* meth
od, ExceptionState& exceptionState) |
| 55 { | 55 { |
| 56 for (size_t i = 0; i < tokens.size(); ++i) { | 56 for (size_t i = 0; i < tokens.size(); ++i) { |
| 57 if (!validateToken(tokens[i], method, es)) | 57 if (!validateToken(tokens[i], method, exceptionState)) |
| 58 return false; | 58 return false; |
| 59 } | 59 } |
| 60 | 60 |
| 61 return true; | 61 return true; |
| 62 } | 62 } |
| 63 | 63 |
| 64 bool DOMTokenList::contains(const AtomicString& token, ExceptionState& es) const | 64 bool DOMTokenList::contains(const AtomicString& token, ExceptionState& exception
State) const |
| 65 { | 65 { |
| 66 if (!validateToken(token, "contains", es)) | 66 if (!validateToken(token, "contains", exceptionState)) |
| 67 return false; | 67 return false; |
| 68 return containsInternal(token); | 68 return containsInternal(token); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void DOMTokenList::add(const AtomicString& token, ExceptionState& es) | 71 void DOMTokenList::add(const AtomicString& token, ExceptionState& exceptionState
) |
| 72 { | 72 { |
| 73 Vector<String> tokens; | 73 Vector<String> tokens; |
| 74 tokens.append(token.string()); | 74 tokens.append(token.string()); |
| 75 add(tokens, es); | 75 add(tokens, exceptionState); |
| 76 } | 76 } |
| 77 | 77 |
| 78 void DOMTokenList::add(const Vector<String>& tokens, ExceptionState& es) | 78 void DOMTokenList::add(const Vector<String>& tokens, ExceptionState& exceptionSt
ate) |
| 79 { | 79 { |
| 80 Vector<String> filteredTokens; | 80 Vector<String> filteredTokens; |
| 81 filteredTokens.reserveCapacity(tokens.size()); | 81 filteredTokens.reserveCapacity(tokens.size()); |
| 82 for (size_t i = 0; i < tokens.size(); ++i) { | 82 for (size_t i = 0; i < tokens.size(); ++i) { |
| 83 if (!validateToken(tokens[i], "add", es)) | 83 if (!validateToken(tokens[i], "add", exceptionState)) |
| 84 return; | 84 return; |
| 85 if (containsInternal(tokens[i])) | 85 if (containsInternal(tokens[i])) |
| 86 continue; | 86 continue; |
| 87 if (filteredTokens.contains(tokens[i])) | 87 if (filteredTokens.contains(tokens[i])) |
| 88 continue; | 88 continue; |
| 89 filteredTokens.append(tokens[i]); | 89 filteredTokens.append(tokens[i]); |
| 90 } | 90 } |
| 91 | 91 |
| 92 if (filteredTokens.isEmpty()) | 92 if (filteredTokens.isEmpty()) |
| 93 return; | 93 return; |
| 94 | 94 |
| 95 setValue(addTokens(value(), filteredTokens)); | 95 setValue(addTokens(value(), filteredTokens)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void DOMTokenList::remove(const AtomicString& token, ExceptionState& es) | 98 void DOMTokenList::remove(const AtomicString& token, ExceptionState& exceptionSt
ate) |
| 99 { | 99 { |
| 100 Vector<String> tokens; | 100 Vector<String> tokens; |
| 101 tokens.append(token.string()); | 101 tokens.append(token.string()); |
| 102 remove(tokens, es); | 102 remove(tokens, exceptionState); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void DOMTokenList::remove(const Vector<String>& tokens, ExceptionState& es) | 105 void DOMTokenList::remove(const Vector<String>& tokens, ExceptionState& exceptio
nState) |
| 106 { | 106 { |
| 107 if (!validateTokens(tokens, "remove", es)) | 107 if (!validateTokens(tokens, "remove", exceptionState)) |
| 108 return; | 108 return; |
| 109 | 109 |
| 110 // Check using containsInternal first since it is a lot faster than going | 110 // Check using containsInternal first since it is a lot faster than going |
| 111 // through the string character by character. | 111 // through the string character by character. |
| 112 bool found = false; | 112 bool found = false; |
| 113 for (size_t i = 0; i < tokens.size(); ++i) { | 113 for (size_t i = 0; i < tokens.size(); ++i) { |
| 114 if (containsInternal(tokens[i])) { | 114 if (containsInternal(tokens[i])) { |
| 115 found = true; | 115 found = true; |
| 116 break; | 116 break; |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 if (found) | 120 if (found) |
| 121 setValue(removeTokens(value(), tokens)); | 121 setValue(removeTokens(value(), tokens)); |
| 122 } | 122 } |
| 123 | 123 |
| 124 bool DOMTokenList::toggle(const AtomicString& token, ExceptionState& es) | 124 bool DOMTokenList::toggle(const AtomicString& token, ExceptionState& exceptionSt
ate) |
| 125 { | 125 { |
| 126 if (!validateToken(token, "toggle", es)) | 126 if (!validateToken(token, "toggle", exceptionState)) |
| 127 return false; | 127 return false; |
| 128 | 128 |
| 129 if (containsInternal(token)) { | 129 if (containsInternal(token)) { |
| 130 removeInternal(token); | 130 removeInternal(token); |
| 131 return false; | 131 return false; |
| 132 } | 132 } |
| 133 addInternal(token); | 133 addInternal(token); |
| 134 return true; | 134 return true; |
| 135 } | 135 } |
| 136 | 136 |
| 137 bool DOMTokenList::toggle(const AtomicString& token, bool force, ExceptionState&
es) | 137 bool DOMTokenList::toggle(const AtomicString& token, bool force, ExceptionState&
exceptionState) |
| 138 { | 138 { |
| 139 if (!validateToken(token, "toggle", es)) | 139 if (!validateToken(token, "toggle", exceptionState)) |
| 140 return false; | 140 return false; |
| 141 | 141 |
| 142 if (force) | 142 if (force) |
| 143 addInternal(token); | 143 addInternal(token); |
| 144 else | 144 else |
| 145 removeInternal(token); | 145 removeInternal(token); |
| 146 | 146 |
| 147 return force; | 147 return force; |
| 148 } | 148 } |
| 149 | 149 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 output.append(' '); | 236 output.append(' '); |
| 237 } else { | 237 } else { |
| 238 output.append(token); // Step 9 | 238 output.append(token); // Step 9 |
| 239 } | 239 } |
| 240 } | 240 } |
| 241 | 241 |
| 242 return output.toString(); | 242 return output.toString(); |
| 243 } | 243 } |
| 244 | 244 |
| 245 } // namespace WebCore | 245 } // namespace WebCore |
| OLD | NEW |