|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -const nwsapi = require("nwsapi"); |
4 | | - |
| 3 | +const domSelector = require("@asamuzakjp/dom-selector"); |
| 4 | +const DOMException = require("../generated/DOMException"); |
5 | 5 | const idlUtils = require("../generated/utils"); |
6 | 6 |
|
7 | | -function initNwsapi(node) { |
8 | | - const { _globalObject, _ownerDocument } = node; |
9 | | - |
10 | | - return nwsapi({ |
11 | | - document: idlUtils.wrapperForImpl(_ownerDocument), |
12 | | - DOMException: _globalObject.DOMException |
13 | | - }); |
14 | | -} |
15 | | - |
16 | | -exports.matchesDontThrow = (elImpl, selector) => { |
17 | | - const document = elImpl._ownerDocument; |
18 | | - |
19 | | - if (!document._nwsapiDontThrow) { |
20 | | - document._nwsapiDontThrow = initNwsapi(elImpl); |
21 | | - document._nwsapiDontThrow.configure({ |
22 | | - LOGERRORS: false, |
23 | | - VERBOSITY: false, |
24 | | - IDS_DUPES: true, |
25 | | - MIXEDCASE: true |
26 | | - }); |
| 7 | +exports.matchesDontThrow = (selectors, elementImpl) => { |
| 8 | + let matched; |
| 9 | + try { |
| 10 | + const element = idlUtils.wrapperForImpl(elementImpl); |
| 11 | + matched = domSelector.matches(selectors, element); |
| 12 | + } catch { |
| 13 | + matched = false; |
27 | 14 | } |
| 15 | + return matched; |
| 16 | +}; |
28 | 17 |
|
29 | | - return document._nwsapiDontThrow.match(selector, idlUtils.wrapperForImpl(elImpl)); |
| 18 | +exports.matches = (selectors, elementImpl, globalObject) => { |
| 19 | + let matched; |
| 20 | + try { |
| 21 | + const element = idlUtils.wrapperForImpl(elementImpl); |
| 22 | + matched = domSelector.matches(selectors, element); |
| 23 | + } catch (e) { |
| 24 | + if (e instanceof globalThis.DOMException && |
| 25 | + !(e instanceof globalObject.DOMException)) { |
| 26 | + throw DOMException.create(globalObject, [e.message, e.name]); |
| 27 | + } else if (e instanceof globalThis.TypeError && |
| 28 | + !(e instanceof globalObject.TypeError)) { |
| 29 | + throw new globalObject.TypeError(e.message); |
| 30 | + } else { |
| 31 | + throw e; |
| 32 | + } |
| 33 | + } |
| 34 | + return matched; |
30 | 35 | }; |
31 | 36 |
|
32 | | -// nwsapi gets `document.documentElement` at creation-time, so we have to initialize lazily, since in the initial |
33 | | -// stages of Document initialization, there is no documentElement present yet. |
34 | | -exports.addNwsapi = parentNode => { |
35 | | - const document = parentNode._ownerDocument; |
| 37 | +exports.closest = (selectors, elementImpl, globalObject) => { |
| 38 | + let matched; |
| 39 | + try { |
| 40 | + const element = idlUtils.wrapperForImpl(elementImpl); |
| 41 | + matched = domSelector.closest(selectors, element); |
| 42 | + } catch (e) { |
| 43 | + if (e instanceof globalThis.DOMException && |
| 44 | + !(e instanceof globalObject.DOMException)) { |
| 45 | + throw DOMException.create(globalObject, [e.message, e.name]); |
| 46 | + } else if (e instanceof globalThis.TypeError && |
| 47 | + !(e instanceof globalObject.TypeError)) { |
| 48 | + throw new globalObject.TypeError(e.message); |
| 49 | + } else { |
| 50 | + throw e; |
| 51 | + } |
| 52 | + } |
| 53 | + return matched; |
| 54 | +}; |
36 | 55 |
|
37 | | - if (!document._nwsapi) { |
38 | | - document._nwsapi = initNwsapi(parentNode); |
39 | | - document._nwsapi.configure({ |
40 | | - LOGERRORS: false, |
41 | | - IDS_DUPES: true, |
42 | | - MIXEDCASE: true |
43 | | - }); |
| 56 | +exports.querySelector = (selectors, parentNodeImpl, globalObject) => { |
| 57 | + let matched; |
| 58 | + try { |
| 59 | + const node = idlUtils.wrapperForImpl(parentNodeImpl); |
| 60 | + matched = domSelector.querySelector(selectors, node); |
| 61 | + } catch (e) { |
| 62 | + if (e instanceof globalThis.DOMException && |
| 63 | + !(e instanceof globalObject.DOMException)) { |
| 64 | + throw DOMException.create(globalObject, [e.message, e.name]); |
| 65 | + } else if (e instanceof globalThis.TypeError && |
| 66 | + !(e instanceof globalObject.TypeError)) { |
| 67 | + throw new globalObject.TypeError(e.message); |
| 68 | + } else { |
| 69 | + throw e; |
| 70 | + } |
44 | 71 | } |
| 72 | + return matched; |
| 73 | +}; |
45 | 74 |
|
46 | | - return document._nwsapi; |
| 75 | +exports.querySelectorAll = (selectors, parentNodeImpl, globalObject) => { |
| 76 | + let matched; |
| 77 | + try { |
| 78 | + const node = idlUtils.wrapperForImpl(parentNodeImpl); |
| 79 | + matched = domSelector.querySelectorAll(selectors, node); |
| 80 | + } catch (e) { |
| 81 | + if (e instanceof globalThis.DOMException && |
| 82 | + !(e instanceof globalObject.DOMException)) { |
| 83 | + throw DOMException.create(globalObject, [e.message, e.name]); |
| 84 | + } else if (e instanceof globalThis.TypeError && |
| 85 | + !(e instanceof globalObject.TypeError)) { |
| 86 | + throw new globalObject.TypeError(e.message); |
| 87 | + } else { |
| 88 | + throw e; |
| 89 | + } |
| 90 | + } |
| 91 | + return matched; |
47 | 92 | }; |
0 commit comments