Skip to content

Commit

Permalink
Update DOMTokenList's contains and add replace
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashenry authored and domenic committed May 6, 2016
1 parent c2933e9 commit 9384cd0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
43 changes: 31 additions & 12 deletions lib/jsdom/living/dom-token-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@ class DOMTokenList {

contains(token) {
token = String(token);

validateToken(token);
return indexOf(this, token) !== -1;
}

replace(token, newToken) {
token = String(token);
newToken = String(newToken);
validateTokens(token, newToken);
const tokenIndex = indexOf(this, token);
if (tokenIndex === -1) {
return;
}
const newTokenIndex = indexOf(this, newToken);
if (newTokenIndex !== -1) {
spliceLite(this, newTokenIndex, 1);
}
this[INTERNAL].tokens[tokenIndex] = newToken;
update(this);
}

add(/* tokens... */) {
for (let i = 0; i < arguments.length; i++) {
const token = String(arguments[i]);
validateToken(token);
validateTokens(token);

if (indexOf(this, token) === -1) {
push(this, token);
Expand All @@ -39,7 +53,7 @@ class DOMTokenList {
remove(/* tokens... */) {
for (let i = 0; i < arguments.length; i++) {
const token = String(arguments[i]);
validateToken(token);
validateTokens(token);

const index = indexOf(this, token);
if (index !== -1) {
Expand All @@ -57,7 +71,7 @@ class DOMTokenList {
token = String(token);
force = force === undefined ? undefined : Boolean(force);

validateToken(token);
validateTokens(token);

const index = indexOf(this, token);

Expand Down Expand Up @@ -102,14 +116,19 @@ function serialize(list) {
return value === null ? "" : value;
}

function validateToken(token) {
if (token === "") {
throw new DOMException(DOMException.SYNTAX_ERR, "The token provided must not be empty.");
function validateTokens(/* tokens... */) {
for (let i = 0; i < arguments.length; i++) {
const token = String(arguments[i]);
if (token === "") {
throw new DOMException(DOMException.SYNTAX_ERR, "The token provided must not be empty.");
}
}

if (/\s/.test(token)) {
const whitespaceMsg = "The token provided contains HTML space characters, which are not valid in tokens.";
throw new DOMException(DOMException.INVALID_CHARACTER_ERR, whitespaceMsg);
for (let i = 0; i < arguments.length; i++) {
const token = String(arguments[i]);
if (/\s/.test(token)) {
const whitespaceMsg = "The token provided contains HTML space characters, which are not valid in tokens.";
throw new DOMException(DOMException.INVALID_CHARACTER_ERR, whitespaceMsg);
}
}
}

Expand Down
22 changes: 0 additions & 22 deletions test/living-dom/class-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ exports[".contains(token) returns whether token exists"] = t => {
t.done();
};

exports[".contains() throws if token empty"] = t => {
function block() {
el.classList.contains("");
}

// TODO: should be SyntaxError
t.throws(block, window.DOMException);

t.done();
};

exports[".contains() throws if token contains whitespace"] = t => {
function block() {
el.classList.contains(" ");
}

// TODO: should be InvalidCharacterError
t.throws(block, window.DOMException);

t.done();
};

exports[".add(tokens...) adds provided tokens"] = t => {
// add zero tokens
el.classList.add();
Expand Down

0 comments on commit 9384cd0

Please sign in to comment.