@asamuzakjp/dom-selector 2.0.3-a.1 → 2.0.3-a.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/index.js.map +3 -3
- package/dist/cjs/js/constant.js +1 -1
- package/dist/cjs/js/constant.js.map +3 -3
- package/dist/cjs/js/dom-util.js +1 -1
- package/dist/cjs/js/dom-util.js.map +3 -3
- package/dist/cjs/js/finder.js +2 -0
- package/dist/cjs/js/finder.js.map +7 -0
- package/dist/cjs/js/matcher.js +1 -1
- package/dist/cjs/js/matcher.js.map +3 -3
- package/dist/cjs/js/parser.js +2 -2
- package/dist/cjs/js/parser.js.map +3 -3
- package/package.json +1 -1
- package/src/index.js +19 -19
- package/src/js/constant.js +1 -1
- package/src/js/dom-util.js +71 -21
- package/src/js/finder.js +2722 -0
- package/src/js/matcher.js +339 -3055
- package/src/js/parser.js +65 -4
- package/types/index.d.ts +3 -3
- package/types/js/constant.d.ts +1 -1
- package/types/js/dom-util.d.ts +2 -1
- package/types/js/finder.d.ts +30 -0
- package/types/js/matcher.d.ts +11 -57
- package/types/js/parser.d.ts +2 -0
package/src/index.js
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
/* import */
|
|
9
|
-
import {
|
|
9
|
+
import { Finder } from './js/finder.js';
|
|
10
10
|
|
|
11
11
|
/* export for test */
|
|
12
|
-
export {
|
|
12
|
+
export { Finder };
|
|
13
13
|
|
|
14
14
|
/* instance, export for test */
|
|
15
|
-
export let
|
|
15
|
+
export let finder = new Finder();
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* matches
|
|
@@ -25,13 +25,13 @@ export let matcher = new Matcher();
|
|
|
25
25
|
export const matches = (selector, node, opt) => {
|
|
26
26
|
let res;
|
|
27
27
|
try {
|
|
28
|
-
if (!
|
|
29
|
-
|
|
28
|
+
if (!finder) {
|
|
29
|
+
finder = new Finder();
|
|
30
30
|
}
|
|
31
|
-
res =
|
|
31
|
+
res = finder.matches(node, selector, opt);
|
|
32
32
|
} catch (e) {
|
|
33
33
|
if (e instanceof Error) {
|
|
34
|
-
|
|
34
|
+
finder = null;
|
|
35
35
|
}
|
|
36
36
|
throw e;
|
|
37
37
|
}
|
|
@@ -49,13 +49,13 @@ export const matches = (selector, node, opt) => {
|
|
|
49
49
|
export const closest = (selector, node, opt) => {
|
|
50
50
|
let res;
|
|
51
51
|
try {
|
|
52
|
-
if (!
|
|
53
|
-
|
|
52
|
+
if (!finder) {
|
|
53
|
+
finder = new Finder();
|
|
54
54
|
}
|
|
55
|
-
res =
|
|
55
|
+
res = finder.closest(node, selector, opt);
|
|
56
56
|
} catch (e) {
|
|
57
57
|
if (e instanceof globalThis[e.name]) {
|
|
58
|
-
|
|
58
|
+
finder = null;
|
|
59
59
|
}
|
|
60
60
|
throw e;
|
|
61
61
|
}
|
|
@@ -73,13 +73,13 @@ export const closest = (selector, node, opt) => {
|
|
|
73
73
|
export const querySelector = (selector, node, opt) => {
|
|
74
74
|
let res;
|
|
75
75
|
try {
|
|
76
|
-
if (!
|
|
77
|
-
|
|
76
|
+
if (!finder) {
|
|
77
|
+
finder = new Finder();
|
|
78
78
|
}
|
|
79
|
-
res =
|
|
79
|
+
res = finder.querySelector(node, selector, opt);
|
|
80
80
|
} catch (e) {
|
|
81
81
|
if (e instanceof globalThis[e.name]) {
|
|
82
|
-
|
|
82
|
+
finder = null;
|
|
83
83
|
}
|
|
84
84
|
throw e;
|
|
85
85
|
}
|
|
@@ -98,13 +98,13 @@ export const querySelector = (selector, node, opt) => {
|
|
|
98
98
|
export const querySelectorAll = (selector, node, opt) => {
|
|
99
99
|
let res;
|
|
100
100
|
try {
|
|
101
|
-
if (!
|
|
102
|
-
|
|
101
|
+
if (!finder) {
|
|
102
|
+
finder = new Finder();
|
|
103
103
|
}
|
|
104
|
-
res =
|
|
104
|
+
res = finder.querySelectorAll(node, selector, opt);
|
|
105
105
|
} catch (e) {
|
|
106
106
|
if (e instanceof globalThis[e.name]) {
|
|
107
|
-
|
|
107
|
+
finder = null;
|
|
108
108
|
}
|
|
109
109
|
throw e;
|
|
110
110
|
}
|
package/src/js/constant.js
CHANGED
|
@@ -29,10 +29,10 @@ export const BIT_04 = 4;
|
|
|
29
29
|
export const BIT_08 = 8;
|
|
30
30
|
export const BIT_16 = 0x10;
|
|
31
31
|
export const BIT_32 = 0x20;
|
|
32
|
+
export const BIT_FFFF = 0xFFFF;
|
|
32
33
|
export const BIT_HYPHEN = 0x2D;
|
|
33
34
|
export const DUO = 2;
|
|
34
35
|
export const HEX = 16;
|
|
35
|
-
export const MAX_BIT_16 = 0xFFFF;
|
|
36
36
|
export const TYPE_FROM = 8;
|
|
37
37
|
export const TYPE_TO = -1;
|
|
38
38
|
|
package/src/js/dom-util.js
CHANGED
|
@@ -9,9 +9,63 @@ import bidiFactory from 'bidi-js';
|
|
|
9
9
|
import {
|
|
10
10
|
DOCUMENT_FRAGMENT_NODE, DOCUMENT_NODE, DOCUMENT_POSITION_CONTAINS,
|
|
11
11
|
DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_PRECEDING, ELEMENT_NODE,
|
|
12
|
-
REG_SHADOW_MODE,
|
|
12
|
+
REG_SHADOW_MODE, TEXT_NODE, TYPE_FROM, TYPE_TO
|
|
13
13
|
} from './constant.js';
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* prepare window, document, root node
|
|
17
|
+
* @param {object} node - Document, DocumentFragment, Element node
|
|
18
|
+
* @returns {Array.<object>} - array of window, document, root node
|
|
19
|
+
*/
|
|
20
|
+
export const prepareDOMObjects = node => {
|
|
21
|
+
if (!node || !node.nodeType || !node.nodeName) {
|
|
22
|
+
const type =
|
|
23
|
+
Object.prototype.toString.call(node).slice(TYPE_FROM, TYPE_TO);
|
|
24
|
+
const msg = `Unexpected type ${type}`;
|
|
25
|
+
throw new TypeError(msg);
|
|
26
|
+
} else if (node.nodeType !== DOCUMENT_NODE &&
|
|
27
|
+
node.nodeType !== DOCUMENT_FRAGMENT_NODE &&
|
|
28
|
+
node.nodeType !== ELEMENT_NODE) {
|
|
29
|
+
const msg = `Unexpected node ${node.nodeName}`;
|
|
30
|
+
throw new TypeError(msg);
|
|
31
|
+
}
|
|
32
|
+
let document;
|
|
33
|
+
let root;
|
|
34
|
+
switch (node.nodeType) {
|
|
35
|
+
case DOCUMENT_NODE: {
|
|
36
|
+
document = node;
|
|
37
|
+
root = node;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case DOCUMENT_FRAGMENT_NODE: {
|
|
41
|
+
document = node.ownerDocument;
|
|
42
|
+
root = node;
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case ELEMENT_NODE:
|
|
46
|
+
default: {
|
|
47
|
+
document = node.ownerDocument;
|
|
48
|
+
let parent = node;
|
|
49
|
+
while (parent) {
|
|
50
|
+
if (parent.parentNode) {
|
|
51
|
+
parent = parent.parentNode;
|
|
52
|
+
} else {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
root = parent;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// NOTE: nullable
|
|
61
|
+
const window = document.defaultView;
|
|
62
|
+
return [
|
|
63
|
+
window,
|
|
64
|
+
document,
|
|
65
|
+
root
|
|
66
|
+
];
|
|
67
|
+
};
|
|
68
|
+
|
|
15
69
|
/**
|
|
16
70
|
* is in shadow tree
|
|
17
71
|
* @param {object} node - node
|
|
@@ -269,26 +323,22 @@ export const isPreceding = (nodeA = {}, nodeB = {}) => {
|
|
|
269
323
|
};
|
|
270
324
|
|
|
271
325
|
/**
|
|
272
|
-
*
|
|
273
|
-
* @param {
|
|
274
|
-
* @
|
|
275
|
-
* @returns {object} - node properties
|
|
326
|
+
* sort nodes
|
|
327
|
+
* @param {Array.<object>|Set.<object>} nodes - collection of nodes
|
|
328
|
+
* @returns {Array.<object|undefined>} - collection of sorted nodes
|
|
276
329
|
*/
|
|
277
|
-
export const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
330
|
+
export const sortNodes = (nodes = []) => {
|
|
331
|
+
const arr = [...nodes];
|
|
332
|
+
if (arr.length > 1) {
|
|
333
|
+
arr.sort((a, b) => {
|
|
334
|
+
let res;
|
|
335
|
+
if (isPreceding(b, a)) {
|
|
336
|
+
res = 1;
|
|
337
|
+
} else {
|
|
338
|
+
res = -1;
|
|
339
|
+
}
|
|
340
|
+
return res;
|
|
341
|
+
});
|
|
289
342
|
}
|
|
290
|
-
return
|
|
291
|
-
prefix,
|
|
292
|
-
tagName
|
|
293
|
-
};
|
|
343
|
+
return arr;
|
|
294
344
|
};
|