From 9bd20e6b891d61b585d5102e0912d7f560d05f89 Mon Sep 17 00:00:00 2001 From: "asamuzaK (Kazz)" Date: Sun, 18 Feb 2024 07:22:39 +0900 Subject: [PATCH 1/2] Fix nwsapi to v2.2.2 --- package.json | 2 +- test/jsdom-issues.test.js | 71 ++++++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index f0c61a75..7024e2ea 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "bidi-js": "^1.0.3", "css-tree": "^2.3.1", "is-potential-custom-element-name": "^1.0.1", - "nwsapi": "^2.2.7" + "nwsapi": "2.2.2" }, "devDependencies": { "@types/css-tree": "^2.3.6", diff --git a/test/jsdom-issues.test.js b/test/jsdom-issues.test.js index d872a700..3c579ccb 100644 --- a/test/jsdom-issues.test.js +++ b/test/jsdom-issues.test.js @@ -21,29 +21,35 @@ const jsdom = (str = '') => { url: 'http://localhost/', beforeParse: window => { const domSelector = new DOMSelector(window); + + const matches = domSelector.matches.bind(domSelector); window.Element.prototype.matches = function (...args) { if (!args.length) { const msg = '1 argument required, but only 0 present.'; throw new window.TypeError(msg); } const [selector] = args; - return domSelector.matches(selector, this); + return matches(selector, this); }; + + const closest = domSelector.closest.bind(domSelector); window.Element.prototype.closest = function (...args) { if (!args.length) { const msg = '1 argument required, but only 0 present.'; throw new window.TypeError(msg); } const [selector] = args; - return domSelector.closest(selector, this); + return closest(selector, this); }; + + const querySelector = domSelector.querySelector.bind(domSelector); window.Document.prototype.querySelector = function (...args) { if (!args.length) { const msg = '1 argument required, but only 0 present.'; throw new window.TypeError(msg); } const [selector] = args; - return domSelector.querySelector(selector, this); + return querySelector(selector, this); }; window.DocumentFragment.prototype.querySelector = function (...args) { if (!args.length) { @@ -51,7 +57,7 @@ const jsdom = (str = '') => { throw new window.TypeError(msg); } const [selector] = args; - return domSelector.querySelector(selector, this); + return querySelector(selector, this); }; window.Element.prototype.querySelector = function (...args) { if (!args.length) { @@ -59,15 +65,17 @@ const jsdom = (str = '') => { throw new window.TypeError(msg); } const [selector] = args; - return domSelector.querySelector(selector, this); + return querySelector(selector, this); }; + + const querySelectorAll = domSelector.querySelectorAll.bind(domSelector); window.Document.prototype.querySelectorAll = function (...args) { if (!args.length) { const msg = '1 argument required, but only 0 present.'; throw new window.TypeError(msg); } const [selector] = args; - return domSelector.querySelectorAll(selector, this); + return querySelectorAll(selector, this); }; window.DocumentFragment.prototype.querySelectorAll = function (...args) { if (!args.length) { @@ -75,7 +83,7 @@ const jsdom = (str = '') => { throw new window.TypeError(msg); } const [selector] = args; - return domSelector.querySelectorAll(selector, this); + return querySelectorAll(selector, this); }; window.Element.prototype.querySelectorAll = function (...args) { if (!args.length) { @@ -83,7 +91,7 @@ const jsdom = (str = '') => { throw new window.TypeError(msg); } const [selector] = args; - return domSelector.querySelectorAll(selector, this); + return querySelectorAll(selector, this); }; } }); @@ -492,6 +500,53 @@ describe('jsdom issues tagged with `selectors` label', () => { }); }); + describe('#3544 - https://github.com/jsdom/jsdom/issues/3544', () => { + let document; + beforeEach(() => { + const dom = jsdom(); + document = dom.window.document; + }); + afterEach(() => { + document = null; + }); + + it('should get matched node(s)', () => { + const body = document.body; + const div = document.createElement('div'); + const ul = document.createElement('ul'); + const li = document.createElement('li'); + ul.appendChild(li); + div.appendChild(ul); + body.appendChild(div); + const res1 = div.querySelectorAll('UL'); + const res2 = div.querySelectorAll('ul'); + const res3 = div.querySelectorAll('ul > li'); + const res4 = div.querySelectorAll('UL > LI'); + assert.deepEqual(res1, [ul], 'result1'); + assert.deepEqual(res2, [ul], 'result2'); + assert.deepEqual(res3, [li], 'result3'); + assert.deepEqual(res4, [li], 'result4'); + }); + + it('should get matched node(s)', () => { + const body = document.body; + const div = document.createElement('div'); + const ul = document.createElement('ul'); + const li = document.createElement('li'); + ul.appendChild(li); + div.appendChild(ul); + body.appendChild(div); + const res1 = ul.matches('UL'); + const res2 = ul.matches('ul'); + const res3 = li.matches('ul > li'); + const res4 = li.matches('UL > LI'); + assert.isTrue(res1, 'result1'); + assert.isTrue(res2, 'result2'); + assert.isTrue(res3, 'result3'); + assert.isTrue(res4, 'result4'); + }); + }); + describe('#3666 - https://github.com/jsdom/jsdom/issues/3666', () => { const html = ` From 2608bdd91301be3653c14273f47739a312815fdb Mon Sep 17 00:00:00 2001 From: "asamuzaK (Kazz)" Date: Sun, 18 Feb 2024 07:36:18 +0900 Subject: [PATCH 2/2] Update package.json --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 7024e2ea..956f5078 100644 --- a/package.json +++ b/package.json @@ -42,13 +42,16 @@ "eslint-plugin-regexp": "^2.2.0", "eslint-plugin-unicorn": "^51.0.1", "happy-dom": "^13.3.8", - "jsdom": "24.0.0", + "jsdom": "^24.0.0", "linkedom": "^0.16.8", "mocha": "^10.3.0", "sinon": "^17.0.1", "typescript": "^5.3.3", "wpt-runner": "^5.0.0" }, + "overrides": { + "nwsapi": "2.2.2" + }, "scripts": { "bench": "node benchmark/bench.js", "bench-sizzle": "node benchmark/bench-sizzle.js",