@asamuzakjp/dom-selector 0.19.3 → 0.19.4
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/package.json +9 -6
- package/src/js/matcher.js +62 -58
- package/types/js/matcher.d.ts +1 -1
package/package.json
CHANGED
|
@@ -20,24 +20,27 @@
|
|
|
20
20
|
"types": "types/index.d.ts",
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"css-tree": "^2.3.1",
|
|
23
|
-
"is-potential-custom-element-name": "^1.0.1"
|
|
23
|
+
"is-potential-custom-element-name": "^1.0.1",
|
|
24
|
+
"xpath": "^0.0.32"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
27
|
"@types/css-tree": "^2.3.1",
|
|
27
28
|
"benchmark": "^2.1.4",
|
|
28
29
|
"c8": "^8.0.0",
|
|
29
30
|
"chai": "^4.3.7",
|
|
30
|
-
"
|
|
31
|
+
"css2xpath": "^0.0.3",
|
|
32
|
+
"eslint": "^8.44.0",
|
|
31
33
|
"eslint-config-standard": "^17.1.0",
|
|
32
|
-
"eslint-plugin-
|
|
34
|
+
"eslint-plugin-import": "^2.27.5",
|
|
35
|
+
"eslint-plugin-jsdoc": "^46.4.3",
|
|
33
36
|
"eslint-plugin-regexp": "^1.15.0",
|
|
34
37
|
"eslint-plugin-unicorn": "^47.0.0",
|
|
35
38
|
"jsdom": "^22.1.0",
|
|
36
39
|
"linkedom": "^0.14.26",
|
|
37
40
|
"mocha": "^10.2.0",
|
|
38
|
-
"nwsapi": "^2.2.
|
|
41
|
+
"nwsapi": "^2.2.7",
|
|
39
42
|
"sinon": "^15.2.0",
|
|
40
|
-
"typescript": "^5.1.
|
|
43
|
+
"typescript": "^5.1.6"
|
|
41
44
|
},
|
|
42
45
|
"scripts": {
|
|
43
46
|
"bench": "node benchmark/benchmark.js",
|
|
@@ -46,5 +49,5 @@
|
|
|
46
49
|
"test": "c8 --reporter=text mocha --exit test/**/*.test.js",
|
|
47
50
|
"tsc": "npx tsc"
|
|
48
51
|
},
|
|
49
|
-
"version": "0.19.
|
|
52
|
+
"version": "0.19.4"
|
|
50
53
|
}
|
package/src/js/matcher.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
/* import */
|
|
6
6
|
import isCustomElementName from 'is-potential-custom-element-name';
|
|
7
|
+
import xpath from 'xpath';
|
|
7
8
|
import { generateCSS, parseSelector, walkAST } from './parser.js';
|
|
8
9
|
|
|
9
10
|
/* constants */
|
|
@@ -1875,12 +1876,12 @@ export class Matcher {
|
|
|
1875
1876
|
while (refNode) {
|
|
1876
1877
|
const bool = this._matchLeaves(leaves, refNode);
|
|
1877
1878
|
if (bool) {
|
|
1878
|
-
arr.
|
|
1879
|
+
arr.push(refNode);
|
|
1879
1880
|
}
|
|
1880
1881
|
refNode = refNode.previousElementSibling;
|
|
1881
1882
|
}
|
|
1882
1883
|
if (arr.length) {
|
|
1883
|
-
matched = new Set(arr);
|
|
1884
|
+
matched = new Set(arr.reverse());
|
|
1884
1885
|
}
|
|
1885
1886
|
break;
|
|
1886
1887
|
}
|
|
@@ -1901,12 +1902,66 @@ export class Matcher {
|
|
|
1901
1902
|
while (refNode) {
|
|
1902
1903
|
const bool = this._matchLeaves(leaves, refNode);
|
|
1903
1904
|
if (bool) {
|
|
1904
|
-
arr.
|
|
1905
|
+
arr.push(refNode);
|
|
1905
1906
|
}
|
|
1906
1907
|
refNode = refNode.parentNode;
|
|
1907
1908
|
}
|
|
1908
1909
|
if (arr.length) {
|
|
1909
|
-
matched = new Set(arr);
|
|
1910
|
+
matched = new Set(arr.reverse());
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
return matched;
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
/**
|
|
1919
|
+
* match combinator
|
|
1920
|
+
* @param {object} combo - combinator
|
|
1921
|
+
* @param {object} prevNodes - collection of Element nodes
|
|
1922
|
+
* @param {object} nextNodes - collection of Element nodes
|
|
1923
|
+
* @returns {object} - collection of matched nodes
|
|
1924
|
+
*/
|
|
1925
|
+
_matchCombo(combo, prevNodes, nextNodes) {
|
|
1926
|
+
const { name: comboName } = combo;
|
|
1927
|
+
const matched = new Set();
|
|
1928
|
+
for (const node of nextNodes) {
|
|
1929
|
+
const { parentNode, previousElementSibling } = node;
|
|
1930
|
+
switch (comboName) {
|
|
1931
|
+
case '+': {
|
|
1932
|
+
const refNode = previousElementSibling;
|
|
1933
|
+
if (refNode && prevNodes.has(refNode)) {
|
|
1934
|
+
matched.add(node);
|
|
1935
|
+
}
|
|
1936
|
+
break;
|
|
1937
|
+
}
|
|
1938
|
+
case '~': {
|
|
1939
|
+
let refNode = previousElementSibling;
|
|
1940
|
+
while (refNode) {
|
|
1941
|
+
if (refNode && prevNodes.has(refNode)) {
|
|
1942
|
+
matched.add(node);
|
|
1943
|
+
break;
|
|
1944
|
+
}
|
|
1945
|
+
refNode = refNode.previousElementSibling;
|
|
1946
|
+
}
|
|
1947
|
+
break;
|
|
1948
|
+
}
|
|
1949
|
+
case '>': {
|
|
1950
|
+
const refNode = parentNode;
|
|
1951
|
+
if (refNode && prevNodes.has(refNode)) {
|
|
1952
|
+
matched.add(node);
|
|
1953
|
+
}
|
|
1954
|
+
break;
|
|
1955
|
+
}
|
|
1956
|
+
case ' ':
|
|
1957
|
+
default: {
|
|
1958
|
+
let refNode = parentNode;
|
|
1959
|
+
while (refNode) {
|
|
1960
|
+
if (refNode && prevNodes.has(refNode)) {
|
|
1961
|
+
matched.add(node);
|
|
1962
|
+
break;
|
|
1963
|
+
}
|
|
1964
|
+
refNode = refNode.parentNode;
|
|
1910
1965
|
}
|
|
1911
1966
|
}
|
|
1912
1967
|
}
|
|
@@ -2041,6 +2096,9 @@ export class Matcher {
|
|
|
2041
2096
|
break;
|
|
2042
2097
|
}
|
|
2043
2098
|
}
|
|
2099
|
+
} else if (root.nodeType === DOCUMENT_NODE) {
|
|
2100
|
+
const a = xpath.select(`//*[local-name()='${tagName}']`, root);
|
|
2101
|
+
arr.push(...a);
|
|
2044
2102
|
} else if (root.nodeType === DOCUMENT_FRAGMENT_NODE) {
|
|
2045
2103
|
const walker = document.createTreeWalker(root, FILTER_SHOW_ELEMENT);
|
|
2046
2104
|
let nextNode = walker.firstChild();
|
|
@@ -2210,60 +2268,6 @@ export class Matcher {
|
|
|
2210
2268
|
];
|
|
2211
2269
|
}
|
|
2212
2270
|
|
|
2213
|
-
/**
|
|
2214
|
-
* match combinator
|
|
2215
|
-
* @param {object} combo - combinator
|
|
2216
|
-
* @param {object} prevNodes - collection of Element nodes
|
|
2217
|
-
* @param {object} nextNodes - collection of Element nodes
|
|
2218
|
-
* @returns {object} - collection of matched nodes
|
|
2219
|
-
*/
|
|
2220
|
-
_matchCombo(combo, prevNodes, nextNodes) {
|
|
2221
|
-
const { name: comboName } = combo;
|
|
2222
|
-
const matched = new Set();
|
|
2223
|
-
for (const node of nextNodes) {
|
|
2224
|
-
const { parentNode, previousElementSibling } = node;
|
|
2225
|
-
switch (comboName) {
|
|
2226
|
-
case '+': {
|
|
2227
|
-
const refNode = previousElementSibling;
|
|
2228
|
-
if (refNode && prevNodes.has(refNode)) {
|
|
2229
|
-
matched.add(node);
|
|
2230
|
-
}
|
|
2231
|
-
break;
|
|
2232
|
-
}
|
|
2233
|
-
case '~': {
|
|
2234
|
-
let refNode = previousElementSibling;
|
|
2235
|
-
while (refNode) {
|
|
2236
|
-
if (refNode && prevNodes.has(refNode)) {
|
|
2237
|
-
matched.add(node);
|
|
2238
|
-
break;
|
|
2239
|
-
}
|
|
2240
|
-
refNode = refNode.previousElementSibling;
|
|
2241
|
-
}
|
|
2242
|
-
break;
|
|
2243
|
-
}
|
|
2244
|
-
case '>': {
|
|
2245
|
-
const refNode = parentNode;
|
|
2246
|
-
if (refNode && prevNodes.has(refNode)) {
|
|
2247
|
-
matched.add(node);
|
|
2248
|
-
}
|
|
2249
|
-
break;
|
|
2250
|
-
}
|
|
2251
|
-
case ' ':
|
|
2252
|
-
default: {
|
|
2253
|
-
let refNode = parentNode;
|
|
2254
|
-
while (refNode) {
|
|
2255
|
-
if (refNode && prevNodes.has(refNode)) {
|
|
2256
|
-
matched.add(node);
|
|
2257
|
-
break;
|
|
2258
|
-
}
|
|
2259
|
-
refNode = refNode.parentNode;
|
|
2260
|
-
}
|
|
2261
|
-
}
|
|
2262
|
-
}
|
|
2263
|
-
}
|
|
2264
|
-
return matched;
|
|
2265
|
-
}
|
|
2266
|
-
|
|
2267
2271
|
/**
|
|
2268
2272
|
* match nodes
|
|
2269
2273
|
* @param {string} range - target range
|
package/types/js/matcher.d.ts
CHANGED
|
@@ -38,9 +38,9 @@ export class Matcher {
|
|
|
38
38
|
_matchTwig(twig: object, node: object, opt?: {
|
|
39
39
|
find?: string;
|
|
40
40
|
}): object;
|
|
41
|
+
_matchCombo(combo: object, prevNodes: object, nextNodes: object): object;
|
|
41
42
|
_findNodes(twig: object, range: string): object;
|
|
42
43
|
_collectNodes(range: string): any[];
|
|
43
|
-
_matchCombo(combo: object, prevNodes: object, nextNodes: object): object;
|
|
44
44
|
_matchNodes(range: string): object;
|
|
45
45
|
_find(range: string): object;
|
|
46
46
|
_sortNodes(nodes: object): any[];
|