This source file includes following definitions.
- validateToken
- validateTokens
- contains
- add
- add
- remove
- remove
- toggle
- toggle
- addInternal
- removeInternal
- addToken
- addTokens
- removeToken
- removeTokens
#include "config.h"
#include "core/dom/DOMTokenList.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/html/parser/HTMLParserIdioms.h"
#include "wtf/text/StringBuilder.h"
namespace WebCore {
bool DOMTokenList::validateToken(const String& token, ExceptionState& exceptionState)
{
if (token.isEmpty()) {
exceptionState.throwDOMException(SyntaxError, "The token provided must not be empty.");
return false;
}
unsigned length = token.length();
for (unsigned i = 0; i < length; ++i) {
if (isHTMLSpace<UChar>(token[i])) {
exceptionState.throwDOMException(InvalidCharacterError, "The token provided ('" + token + "') contains HTML space characters, which are not valid in tokens.");
return false;
}
}
return true;
}
bool DOMTokenList::validateTokens(const Vector<String>& tokens, ExceptionState& exceptionState)
{
for (size_t i = 0; i < tokens.size(); ++i) {
if (!validateToken(tokens[i], exceptionState))
return false;
}
return true;
}
bool DOMTokenList::contains(const AtomicString& token, ExceptionState& exceptionState) const
{
if (!validateToken(token, exceptionState))
return false;
return containsInternal(token);
}
void DOMTokenList::add(const AtomicString& token, ExceptionState& exceptionState)
{
Vector<String> tokens;
tokens.append(token.string());
add(tokens, exceptionState);
}
void DOMTokenList::add(const Vector<String>& tokens, ExceptionState& exceptionState)
{
Vector<String> filteredTokens;
filteredTokens.reserveCapacity(tokens.size());
for (size_t i = 0; i < tokens.size(); ++i) {
if (!validateToken(tokens[i], exceptionState))
return;
if (containsInternal(AtomicString(tokens[i])))
continue;
if (filteredTokens.contains(tokens[i]))
continue;
filteredTokens.append(tokens[i]);
}
if (filteredTokens.isEmpty())
return;
setValue(addTokens(value(), filteredTokens));
}
void DOMTokenList::remove(const AtomicString& token, ExceptionState& exceptionState)
{
Vector<String> tokens;
tokens.append(token.string());
remove(tokens, exceptionState);
}
void DOMTokenList::remove(const Vector<String>& tokens, ExceptionState& exceptionState)
{
if (!validateTokens(tokens, exceptionState))
return;
bool found = false;
for (size_t i = 0; i < tokens.size(); ++i) {
if (containsInternal(AtomicString(tokens[i]))) {
found = true;
break;
}
}
if (found)
setValue(removeTokens(value(), tokens));
}
bool DOMTokenList::toggle(const AtomicString& token, ExceptionState& exceptionState)
{
if (!validateToken(token, exceptionState))
return false;
if (containsInternal(token)) {
removeInternal(token);
return false;
}
addInternal(token);
return true;
}
bool DOMTokenList::toggle(const AtomicString& token, bool force, ExceptionState& exceptionState)
{
if (!validateToken(token, exceptionState))
return false;
if (force)
addInternal(token);
else
removeInternal(token);
return force;
}
void DOMTokenList::addInternal(const AtomicString& token)
{
if (!containsInternal(token))
setValue(addToken(value(), token));
}
void DOMTokenList::removeInternal(const AtomicString& token)
{
if (!containsInternal(token))
return;
setValue(removeToken(value(), token));
}
AtomicString DOMTokenList::addToken(const AtomicString& input, const AtomicString& token)
{
Vector<String> tokens;
tokens.append(token.string());
return addTokens(input, tokens);
}
AtomicString DOMTokenList::addTokens(const AtomicString& input, const Vector<String>& tokens)
{
bool needsSpace = false;
StringBuilder builder;
if (!input.isEmpty()) {
builder.append(input);
needsSpace = !isHTMLSpace<UChar>(input[input.length() - 1]);
}
for (size_t i = 0; i < tokens.size(); ++i) {
if (needsSpace)
builder.append(' ');
builder.append(tokens[i]);
needsSpace = true;
}
return builder.toAtomicString();
}
AtomicString DOMTokenList::removeToken(const AtomicString& input, const AtomicString& token)
{
Vector<String> tokens;
tokens.append(token.string());
return removeTokens(input, tokens);
}
AtomicString DOMTokenList::removeTokens(const AtomicString& input, const Vector<String>& tokens)
{
unsigned inputLength = input.length();
StringBuilder output;
output.reserveCapacity(inputLength);
unsigned position = 0;
while (position < inputLength) {
if (isHTMLSpace<UChar>(input[position])) {
output.append(input[position++]);
continue;
}
StringBuilder tokenBuilder;
while (position < inputLength && isNotHTMLSpace<UChar>(input[position]))
tokenBuilder.append(input[position++]);
String token = tokenBuilder.toString();
if (tokens.contains(token)) {
while (position < inputLength && isHTMLSpace<UChar>(input[position]))
++position;
size_t j = output.length();
while (j > 0 && isHTMLSpace<UChar>(output[j - 1]))
--j;
output.resize(j);
if (position < inputLength && !output.isEmpty())
output.append(' ');
} else {
output.append(token);
}
}
return output.toAtomicString();
}
}