Skip to content

Commit

Permalink
Fix jsdom to-port-to-wpts error (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK authored Dec 29, 2023
1 parent 1f3f0ea commit 1d02b53
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/js/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2518,7 +2518,8 @@ export class Matcher {
}
} else if (!filterLeaves.length) {
if (targetType === TARGET_ALL) {
nodes = collectedNodes;
const n = [...nodes];
nodes = new Set([...n, ...collectedNodes]);
} else {
const [node] = [...collectedNodes];
nodes.add(node);
Expand Down
4 changes: 4 additions & 0 deletions src/js/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const preprocess = (...args) => {
} else if (selector === undefined || selector === null) {
selector = Object.prototype.toString.call(selector)
.slice(TYPE_FROM, TYPE_TO).toLowerCase();
} else if (Array.isArray(selector)) {
selector = selector.join(',');
} else if (Object.prototype.hasOwnProperty.call(selector, 'toString')) {
selector = selector.toString();
} else {
throw new DOMException(`Invalid selector ${selector}`, SYNTAX_ERR);
}
Expand Down
31 changes: 31 additions & 0 deletions test/wpt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2070,4 +2070,35 @@ describe('local wpt test cases', () => {
assert.isFalse(res, 'result');
});
});

describe('jsdom: to-port-to-wpts/query-selector-all.js', () => {
it('should get matched node(s)', () => {
const html = `
<span>Hello!<strong>Goodbye!</strong><strong>Hello Again</strong></span>
<p>This is an <em>Important</em> paragraph</p>
`;
document.body.innerHTML = html;
const res = document.querySelectorAll(['strong', 'em']);
assert.strictEqual(res.length, 3, 'length');
assert.strictEqual(res[0].localName, 'strong', 'node');
assert.strictEqual(res[1].localName, 'strong', 'node');
assert.strictEqual(res[2].localName, 'em', 'node');
});

it('should get matched node(s)', () => {
const html = `
<span>Hello!<strong>Goodbye!</strong><strong>Hello Again</strong></span>
<p>This is an <em>Important</em> paragraph</p>
`;
document.body.innerHTML = html;
const stringifiableObj = {
toString() {
return 'p';
}
};
const res = document.querySelectorAll(stringifiableObj);
assert.strictEqual(res.length, 1, 'length');
assert.strictEqual(res[0].localName, 'p', 'node');
});
});
});

0 comments on commit 1d02b53

Please sign in to comment.