@asamuzakjp/dom-selector 0.3.0 → 0.4.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/README.md +1 -1
- package/package.json +3 -4
- package/src/js/matcher.js +34 -21
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ npm i @asamuzakjp/dom-selector
|
|
|
23
23
|
```javascript
|
|
24
24
|
const {
|
|
25
25
|
matches, closest, querySelector, querySelectorAll
|
|
26
|
-
} = require('dom-selector');
|
|
26
|
+
} = require('@asamuzakjp/dom-selector');
|
|
27
27
|
```
|
|
28
28
|
|
|
29
29
|
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
package/package.json
CHANGED
|
@@ -19,7 +19,6 @@
|
|
|
19
19
|
"types": "types/index.d.ts",
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@types/css-tree": "^2.3.1",
|
|
22
|
-
"@types/node": "^20.1.0",
|
|
23
22
|
"css-tree": "^2.3.1"
|
|
24
23
|
},
|
|
25
24
|
"devDependencies": {
|
|
@@ -27,8 +26,8 @@
|
|
|
27
26
|
"chai": "^4.3.7",
|
|
28
27
|
"eslint": "^8.40.0",
|
|
29
28
|
"eslint-config-standard": "^17.0.0",
|
|
30
|
-
"eslint-plugin-jsdoc": "^
|
|
31
|
-
"eslint-plugin-regexp": "^1.
|
|
29
|
+
"eslint-plugin-jsdoc": "^44.2.0",
|
|
30
|
+
"eslint-plugin-regexp": "^1.15.0",
|
|
32
31
|
"eslint-plugin-unicorn": "^47.0.0",
|
|
33
32
|
"jsdom": "^22.0.0",
|
|
34
33
|
"mocha": "^10.2.0",
|
|
@@ -41,5 +40,5 @@
|
|
|
41
40
|
"test": "c8 --reporter=text mocha --exit test/**/*.test.js",
|
|
42
41
|
"tsc": "npx tsc"
|
|
43
42
|
},
|
|
44
|
-
"version": "0.
|
|
43
|
+
"version": "0.4.2"
|
|
45
44
|
}
|
package/src/js/matcher.js
CHANGED
|
@@ -49,14 +49,16 @@ const collectNthChild = (node = {}, opt = {}) => {
|
|
|
49
49
|
nth += (++n * a);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
if (nth >= 0) {
|
|
53
|
+
let i = 0;
|
|
54
|
+
while (i < l && nth < l) {
|
|
55
|
+
if (i === nth) {
|
|
56
|
+
const item = arr[i];
|
|
57
|
+
res.add(item);
|
|
58
|
+
nth += a;
|
|
59
|
+
}
|
|
60
|
+
i++;
|
|
58
61
|
}
|
|
59
|
-
i++;
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
}
|
|
@@ -109,19 +111,21 @@ const collectNthOfType = (node = {}, opt = {}) => {
|
|
|
109
111
|
nth += a;
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
if (nth >= 0) {
|
|
115
|
+
let i = 0;
|
|
116
|
+
let j = 0;
|
|
117
|
+
while (i < l && nth < l) {
|
|
118
|
+
const item = arr[i];
|
|
119
|
+
const { localName: itemLocalName, prefix: itemPrefix } = item;
|
|
120
|
+
if (itemLocalName === localName && itemPrefix === prefix) {
|
|
121
|
+
if (j === nth) {
|
|
122
|
+
res.add(item);
|
|
123
|
+
nth += a;
|
|
124
|
+
}
|
|
125
|
+
j++;
|
|
121
126
|
}
|
|
122
|
-
|
|
127
|
+
i++;
|
|
123
128
|
}
|
|
124
|
-
i++;
|
|
125
129
|
}
|
|
126
130
|
}
|
|
127
131
|
}
|
|
@@ -161,9 +165,7 @@ const matchTypeSelector = (ast = {}, node = {}) => {
|
|
|
161
165
|
if (astName === '*' || astName === '*|*' || astName === nodeName ||
|
|
162
166
|
(astName === '|*' && !nodePrefix) ||
|
|
163
167
|
(astPrefix === '*' && astNodeName === nodeName) ||
|
|
164
|
-
(astPrefix === '' && !nodePrefix &&
|
|
165
|
-
(astNodeName === '*' || astNodeName === nodeName)) ||
|
|
166
|
-
(astPrefix === nodePrefix &&
|
|
168
|
+
((astPrefix === nodePrefix || (astPrefix === '' && !nodePrefix)) &&
|
|
167
169
|
(astNodeName === '*' || astNodeName === nodeName))) {
|
|
168
170
|
res = node;
|
|
169
171
|
}
|
|
@@ -999,6 +1001,11 @@ class Matcher {
|
|
|
999
1001
|
firstItem = items.shift();
|
|
1000
1002
|
itemLeaves.push(item, firstItem);
|
|
1001
1003
|
}
|
|
1004
|
+
if (firstItem.type === PSEUDO_CLASS_SELECTOR &&
|
|
1005
|
+
firstItem.name === 'has') {
|
|
1006
|
+
matched = false;
|
|
1007
|
+
break;
|
|
1008
|
+
}
|
|
1002
1009
|
const arr = this._matchCombinator(itemLeaves, node);
|
|
1003
1010
|
if (arr.length) {
|
|
1004
1011
|
matched = true;
|
|
@@ -1019,6 +1026,12 @@ class Matcher {
|
|
|
1019
1026
|
let matched;
|
|
1020
1027
|
for (const items of ast) {
|
|
1021
1028
|
const item = items.shift();
|
|
1029
|
+
// NOTE: according to MDN, :not() can not contain :not()
|
|
1030
|
+
// but spec says nothing about that?
|
|
1031
|
+
if (item.type === PSEUDO_CLASS_SELECTOR && item.name === 'not') {
|
|
1032
|
+
matched = true;
|
|
1033
|
+
break;
|
|
1034
|
+
}
|
|
1022
1035
|
const arr = this._matchArgumentLeaf(item, node);
|
|
1023
1036
|
if (arr.length) {
|
|
1024
1037
|
matched = true;
|