|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -const domSelector = require("@asamuzakjp/dom-selector"); |
4 | | -const { wrapperForImpl } = require("../generated/utils"); |
5 | | - |
6 | | -exports.matchesDontThrow = (selectors, elementImpl) => { |
7 | | - const element = wrapperForImpl(elementImpl); |
8 | | - try { |
9 | | - return domSelector.matches(selectors, element); |
10 | | - } catch { |
11 | | - return false; |
| 3 | +const nwsapi = require("nwsapi"); |
| 4 | + |
| 5 | +const idlUtils = require("../generated/utils"); |
| 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 | + }); |
12 | 27 | } |
13 | | -}; |
14 | 28 |
|
15 | | -exports.matches = (selectors, elementImpl) => { |
16 | | - const element = wrapperForImpl(elementImpl); |
17 | | - return domSelector.matches(selectors, element); |
| 29 | + return document._nwsapiDontThrow.match(selector, idlUtils.wrapperForImpl(elImpl)); |
18 | 30 | }; |
19 | 31 |
|
20 | | -exports.closest = (selectors, elementImpl) => { |
21 | | - const element = wrapperForImpl(elementImpl); |
22 | | - return domSelector.closest(selectors, element); |
23 | | -}; |
| 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; |
24 | 36 |
|
25 | | -exports.querySelector = (selectors, parentNodeImpl) => { |
26 | | - const node = wrapperForImpl(parentNodeImpl); |
27 | | - return domSelector.querySelector(selectors, node); |
28 | | -}; |
| 37 | + if (!document._nwsapi) { |
| 38 | + document._nwsapi = initNwsapi(parentNode); |
| 39 | + document._nwsapi.configure({ |
| 40 | + LOGERRORS: false, |
| 41 | + IDS_DUPES: true, |
| 42 | + MIXEDCASE: true |
| 43 | + }); |
| 44 | + } |
29 | 45 |
|
30 | | -exports.querySelectorAll = (selectors, parentNodeImpl) => { |
31 | | - const node = wrapperForImpl(parentNodeImpl); |
32 | | - return domSelector.querySelectorAll(selectors, node); |
| 46 | + return document._nwsapi; |
33 | 47 | }; |
0 commit comments