From 01c91c24ebf05a264c78a2da5f588757753582f1 Mon Sep 17 00:00:00 2001 From: "asamuzaK (Kazz)" Date: Tue, 20 Feb 2024 01:22:15 +0900 Subject: [PATCH] Fix default export --- src/js/finder.js | 2 +- src/js/matcher.js | 3 +- test/matcher.test.js | 7 +- test/wpt/wpt-runner-cjs.cjs | 153 ++++++++++-------------------------- 4 files changed, 49 insertions(+), 116 deletions(-) diff --git a/src/js/finder.js b/src/js/finder.js index 98a70ea2..3f605682 100644 --- a/src/js/finder.js +++ b/src/js/finder.js @@ -8,7 +8,7 @@ import nwsapi from 'nwsapi'; import { isContentEditable, isInShadowTree, resolveContent, sortNodes } from './dom-util.js'; -import matcher from './matcher.js'; +import { matcher } from './matcher.js'; import { filterSelector, generateCSS, parseSelector, sortAST, unescapeSelector, walkAST } from './parser.js'; diff --git a/src/js/matcher.js b/src/js/matcher.js index 73beaa41..082aefcb 100644 --- a/src/js/matcher.js +++ b/src/js/matcher.js @@ -492,4 +492,5 @@ export class Matcher { } } -export default new Matcher(); +export const matcher = new Matcher(); +export default matcher; diff --git a/test/matcher.test.js b/test/matcher.test.js index ab168efd..163fce67 100644 --- a/test/matcher.test.js +++ b/test/matcher.test.js @@ -8,7 +8,7 @@ import { JSDOM } from 'jsdom'; import { afterEach, beforeEach, describe, it } from 'mocha'; /* test */ -import matcher, { Matcher } from '../src/js/matcher.js'; +import defaultMatcher, { matcher, Matcher } from '../src/js/matcher.js'; import { EMPTY, IDENTIFIER, SELECTOR_ATTR, SELECTOR_PSEUDO_CLASS, SELECTOR_TYPE, STRING } from '../src/js/constant.js'; @@ -84,6 +84,11 @@ describe('matcher', () => { it('should be instance of Matcher', () => { assert.instanceOf(matcher, Matcher, 'instance'); }); + + it('should be instance of Matcher', () => { + assert.instanceOf(defaultMatcher, Matcher, 'instance'); + assert.deepEqual(defaultMatcher, matcher, 'result'); + }); }); describe('match pseudo-element selector', () => { diff --git a/test/wpt/wpt-runner-cjs.cjs b/test/wpt/wpt-runner-cjs.cjs index f7004f58..c05acb87 100644 --- a/test/wpt/wpt-runner-cjs.cjs +++ b/test/wpt/wpt-runner-cjs.cjs @@ -7,165 +7,76 @@ const { DOMSelector } = require('../../dist/cjs/index.js'); const setup = window => { const domSelector = new DOMSelector(window); + + const matches = domSelector.matches.bind(domSelector); window.Element.prototype.matches = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - let res; - try { - const [selector] = args; - res = domSelector.matches(selector, this); - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = matches(selector, this); return !!res; }; + + const closest = domSelector.closest.bind(domSelector); window.Element.prototype.closest = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - let res; - try { - const [selector] = args; - res = domSelector.closest(selector, this); - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = closest(selector, this); return res ?? null; }; + + const querySelector = domSelector.querySelector.bind(domSelector); window.Document.prototype.querySelector = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - let res; - try { - const [selector] = args; - res = domSelector.querySelector(selector, this); - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = querySelector(selector, this); return res ?? null; }; window.DocumentFragment.prototype.querySelector = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - let res; - try { - const [selector] = args; - res = domSelector.querySelector(selector, this); - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = querySelector(selector, this); return res ?? null; }; window.Element.prototype.querySelector = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - let res; - try { - const [selector] = args; - res = domSelector.querySelector(selector, this); - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = querySelector(selector, this); return res ?? null; }; + + const querySelectorAll = domSelector.querySelectorAll.bind(domSelector); window.Document.prototype.querySelectorAll = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - const res = []; - try { - const [selector] = args; - const arr = domSelector.querySelectorAll(selector, this); - if (arr.length) { - res.push(...arr); - } - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = querySelectorAll(selector, this); return res; }; window.DocumentFragment.prototype.querySelectorAll = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - const res = []; - try { - const [selector] = args; - const arr = domSelector.querySelectorAll(selector, this); - if (arr.length) { - res.push(...arr); - } - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = querySelectorAll(selector, this); return res; }; window.Element.prototype.querySelectorAll = function (...args) { if (!args.length) { throw new window.TypeError('1 argument required, but only 0 present.'); } - const res = []; - try { - const [selector] = args; - const arr = domSelector.querySelectorAll(selector, this); - if (arr.length) { - res.push(...arr); - } - } catch (e) { - if (e instanceof globalThis.DOMException && - !(e instanceof window.DOMException)) { - const { message, name } = e; - throw new window.DOMException(message, name); - } else { - throw e; - } - } + const [selector] = args; + const res = querySelectorAll(selector, this); return res; }; window.requestAnimationFrame = function (callback) { @@ -194,6 +105,22 @@ const filter = testPath => { 'has-style-sharing-006.html', 'has-style-sharing-007-ref.html', 'has-style-sharing-007.html', + 'has-style-sharing-pseudo-001-ref.html', + 'has-style-sharing-pseudo-001.html', + 'has-style-sharing-pseudo-002-ref.html', + 'has-style-sharing-pseudo-002.html', + 'has-style-sharing-pseudo-003-ref.html', + 'has-style-sharing-pseudo-003.html', + 'has-style-sharing-pseudo-004-ref.html', + 'has-style-sharing-pseudo-004.html', + 'has-style-sharing-pseudo-005-ref.html', + 'has-style-sharing-pseudo-005.html', + 'has-style-sharing-pseudo-006-ref.html', + 'has-style-sharing-pseudo-006.html', + 'has-style-sharing-pseudo-007-ref.html', + 'has-style-sharing-pseudo-007.html', + 'has-style-sharing-pseudo-008-ref.html', + 'has-style-sharing-pseudo-008.html', 'has-visited-ref.html', 'has-visited.html', 'is-where-error-recovery.html',