Skip to content

Commit

Permalink
Convert DOMTokenList to IDL
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu authored and domenic committed Sep 30, 2017
1 parent 30ead5a commit e653356
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 258 deletions.
239 changes: 0 additions & 239 deletions lib/jsdom/living/dom-token-list.js

This file was deleted.

5 changes: 0 additions & 5 deletions lib/jsdom/living/helpers/ordered-set-parser.js

This file was deleted.

104 changes: 104 additions & 0 deletions lib/jsdom/living/helpers/ordered-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"use strict";

// https://infra.spec.whatwg.org/#sets
//
// Only use this class if a Set cannot be used, e.g. when "replace" operation is needed, since there's no way to replace
// an element while keep the relative order using a Set, only remove and then add something at the end.

module.exports = class OrderedSet {
constructor() {
this._items = [];
}

append(item) {
if (!this.contains(item)) {
this._items.push(item);
}
}

prepend(item) {
if (!this.contains(item)) {
this._items.unshift(item);
}
}

replace(item, replacement) {
let seen = false;
for (let i = 0; i < this._items.length;) {
const isInstance = this._items[i] === item || this._items[i] === replacement;
if (seen && isInstance) {
this._items.splice(i, 1);
} else {
if (isInstance) {
this._items[i] = replacement;
seen = true;
}
i++;
}
}
}

remove(...items) {
this.removePredicate(item => items.includes(item));
}

removePredicate(predicate) {
for (let i = 0; i < this._items.length;) {
if (predicate(this._items[i])) {
this._items.splice(i, 1);
} else {
i++;
}
}
}

empty() {
this._items.length = 0;
}

contains(item) {
return this._items.includes(item);
}

get size() {
return this._items.length;
}

isEmpty() {
return this._items.length === 0;
}

// Useful for other parts of jsdom

[Symbol.iterator]() {
return this._items[Symbol.iterator]();
}

keys() {
return this._items.keys();
}

get(index) {
return this._items[index];
}

some(func) {
return this._items.some(func);
}

// https://dom.spec.whatwg.org/#concept-ordered-set-parser
static parse(input) {
const tokens = new OrderedSet();
for (const token of input.split(/[\t\n\f\r ]+/)) {
if (token) {
tokens.append(token);
}
}
return tokens;
}

// https://dom.spec.whatwg.org/#concept-ordered-set-serializer
serialize() {
return this._items.join(" ");
}
};
4 changes: 2 additions & 2 deletions lib/jsdom/living/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

exports.DOMException = require("domexception");
exports.NamedNodeMap = require("./attributes").NamedNodeMap;
exports.Attr = require("./generated/Attr").interface;
exports.Node = require("./generated/Node").interface;
exports.Element = require("./generated/Element").interface;
Expand All @@ -19,6 +18,7 @@ exports.NodeList = require("./generated/NodeList").interface;
exports.HTMLCollection = require("./generated/HTMLCollection").interface;
exports.HTMLOptionsCollection = require("./generated/HTMLOptionsCollection").interface;
exports.DOMStringMap = require("./generated/DOMStringMap").interface;
exports.DOMTokenList = require("./generated/DOMTokenList").interface;

exports.Event = require("./generated/Event").interface;
exports.CustomEvent = require("./generated/CustomEvent").interface;
Expand Down Expand Up @@ -57,8 +57,8 @@ require("../level2/style")(exports);
require("../level3/xpath")(exports);

// These are OK but need migration to webidl2js eventually.
exports.NamedNodeMap = require("./attributes").NamedNodeMap;
require("./node-filter")(exports);
exports.DOMTokenList = require("./dom-token-list").DOMTokenList;

exports.URL = require("whatwg-url").URL;
exports.URLSearchParams = require("whatwg-url").URLSearchParams;
Loading

0 comments on commit e653356

Please sign in to comment.