Skip to content

Commit

Permalink
Fix default export
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Feb 19, 2024
1 parent 6ce0903 commit 01c91c2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 116 deletions.
2 changes: 1 addition & 1 deletion src/js/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion src/js/matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,5 @@ export class Matcher {
}
}

export default new Matcher();
export const matcher = new Matcher();
export default matcher;
7 changes: 6 additions & 1 deletion test/matcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down
153 changes: 40 additions & 113 deletions test/wpt/wpt-runner-cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 01c91c2

Please sign in to comment.