@asamuzakjp/dom-selector 0.2.1
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/LICENSE +21 -0
- package/README.md +119 -0
- package/package.json +45 -0
- package/src/index.js +61 -0
- package/src/js/constant.js +47 -0
- package/src/js/matcher.js +1257 -0
- package/src/js/parser.js +51 -0
- package/types/index.d.ts +4 -0
- package/types/js/constant.d.ts +41 -0
- package/types/js/matcher.d.ts +33 -0
- package/types/js/parser.d.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 asamuzaK (Kazz)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# DOM Selector
|
|
2
|
+
|
|
3
|
+
[](https://github.com/asamuzaK/domSelector/actions/workflows/node.js.yml)
|
|
4
|
+
[](https://github.com/asamuzaK/domSelector/actions/workflows/codeql.yml)
|
|
5
|
+
<!--
|
|
6
|
+
[](https://www.npmjs.com/package/url-sanitizer)
|
|
7
|
+
[](https://github.com/asamuzaK/domSelector/releases)
|
|
8
|
+
-->
|
|
9
|
+
|
|
10
|
+
Retrieve DOM node from the given CSS selector.
|
|
11
|
+
**Experimental**
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```console
|
|
17
|
+
npm i dom-selector
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
const {
|
|
25
|
+
matches, closest, querySelector, querySelectorAll
|
|
26
|
+
} = require('dom-selector');
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
30
|
+
|
|
31
|
+
#### Table of Contents
|
|
32
|
+
|
|
33
|
+
- [matches][1]
|
|
34
|
+
- [Parameters][2]
|
|
35
|
+
- [closest][3]
|
|
36
|
+
- [Parameters][4]
|
|
37
|
+
- [querySelector][5]
|
|
38
|
+
- [Parameters][6]
|
|
39
|
+
- [querySelectorAll][7]
|
|
40
|
+
- [Parameters][8]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
### matches(selector, node)
|
|
44
|
+
|
|
45
|
+
Implementation of [Element.matches()][62].
|
|
46
|
+
|
|
47
|
+
#### Parameters
|
|
48
|
+
|
|
49
|
+
- `selector` **[string][56]** CSS selector
|
|
50
|
+
- `node` **[object][57]** Referenced Element node
|
|
51
|
+
|
|
52
|
+
Returns **[boolean][58]** Result
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
### closest(selector, node)
|
|
56
|
+
|
|
57
|
+
Implementation of [Element.closest()][63].
|
|
58
|
+
|
|
59
|
+
#### Parameters
|
|
60
|
+
|
|
61
|
+
- `selector` **[string][56]** CSS selector
|
|
62
|
+
- `node` **[object][57]** Referenced Element node
|
|
63
|
+
|
|
64
|
+
Returns **[object][57]?** Matched node
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
### querySelector(selector, refPoint)
|
|
68
|
+
|
|
69
|
+
Implementation of [Document.querySelector()][64], [Element.querySelector()][65].
|
|
70
|
+
|
|
71
|
+
#### Parameters
|
|
72
|
+
|
|
73
|
+
- `selector` **[string][56]** CSS selector
|
|
74
|
+
- `refPoint` **[object][57]** Reference point. Document or Element node
|
|
75
|
+
|
|
76
|
+
Returns **[object][57]?** Matched node
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
### querySelectorAll(selector, refPoint)
|
|
80
|
+
|
|
81
|
+
Implementation of [Document.querySelectorAll()][66], [Element.querySelectorAll()][67].
|
|
82
|
+
**NOTE**: returns [Array][59], not [NodeList][61].
|
|
83
|
+
|
|
84
|
+
#### Parameters
|
|
85
|
+
|
|
86
|
+
- `selector` **[string][56]** CSS selector
|
|
87
|
+
- `refPoint` **[object][57]** Reference point. Document or Element node
|
|
88
|
+
|
|
89
|
+
Returns **[Array][59]<([object][57] \| [undefined][60])>** Array of matched nodes
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
## Acknowledgments
|
|
93
|
+
|
|
94
|
+
The following resources have been of great help in the development of the DOM Selector.
|
|
95
|
+
|
|
96
|
+
- [CSSTree](https://github.com/csstree/csstree)
|
|
97
|
+
- [selery](https://github.com/danburzo/selery)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
[1]: #matches
|
|
101
|
+
[2]: #parameters
|
|
102
|
+
[3]: #closest
|
|
103
|
+
[4]: #parameters-1
|
|
104
|
+
[5]: #queryselector
|
|
105
|
+
[6]: #parameters-2
|
|
106
|
+
[7]: #queryselectorall
|
|
107
|
+
[8]: #parameters-3
|
|
108
|
+
[56]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
109
|
+
[57]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
110
|
+
[58]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
111
|
+
[59]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
|
|
112
|
+
[60]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined
|
|
113
|
+
[61]: https://developer.mozilla.org/docs/Web/API/NodeList
|
|
114
|
+
[62]: https://developer.mozilla.org/docs/Web/API/Element/matches
|
|
115
|
+
[63]: https://developer.mozilla.org/docs/Web/API/Element/closest
|
|
116
|
+
[64]: https://developer.mozilla.org/docs/Web/API/Document/querySelector
|
|
117
|
+
[65]: https://developer.mozilla.org/docs/Web/API/Element/querySelector
|
|
118
|
+
[66]: https://developer.mozilla.org/docs/Web/API/Document/querySelectorAll
|
|
119
|
+
[67]: https://developer.mozilla.org/docs/Web/API/Element/querySelectorAll
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@asamuzakjp/dom-selector",
|
|
3
|
+
"description": "Retrieve DOM node from the given CSS selector.",
|
|
4
|
+
"author": "asamuzaK",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://github.com/asamuzaK/domSelector#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/asamuzaK/domSelector/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/asamuzaK/domSelector.git"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"src",
|
|
16
|
+
"types"
|
|
17
|
+
],
|
|
18
|
+
"main": "src/index.js",
|
|
19
|
+
"types": "types/index.d.ts",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@types/css-tree": "^2.3.1",
|
|
22
|
+
"@types/node": "^20.1.0",
|
|
23
|
+
"css-tree": "^2.3.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"c8": "^7.13.0",
|
|
27
|
+
"chai": "^4.3.7",
|
|
28
|
+
"eslint": "^8.40.0",
|
|
29
|
+
"eslint-config-standard": "^17.0.0",
|
|
30
|
+
"eslint-plugin-jsdoc": "^43.2.0",
|
|
31
|
+
"eslint-plugin-regexp": "^1.14.0",
|
|
32
|
+
"eslint-plugin-unicorn": "^47.0.0",
|
|
33
|
+
"jsdom": "^22.0.0",
|
|
34
|
+
"mocha": "^10.2.0",
|
|
35
|
+
"sinon": "^15.0.4",
|
|
36
|
+
"typescript": "^5.0.4"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "npm run tsc && npm run lint && npm test",
|
|
40
|
+
"lint": "eslint --fix .",
|
|
41
|
+
"test": "c8 --reporter=text mocha --exit test/**/*.test.js",
|
|
42
|
+
"tsc": "npx tsc"
|
|
43
|
+
},
|
|
44
|
+
"version": "0.2.1"
|
|
45
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* DOM Selector - retrieve DOM node from the given CSS selector
|
|
3
|
+
* @license MIT
|
|
4
|
+
* @copyright asamuzaK (Kazz)
|
|
5
|
+
* @see {@link https://github.com/asamuzaK/domSelector/blob/main/LICENSE}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/* import */
|
|
9
|
+
const { Matcher } = require('./js/matcher.js');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* matches - Element.matches()
|
|
13
|
+
* @param {string} selector - CSS selector
|
|
14
|
+
* @param {object} node - Element node
|
|
15
|
+
* @returns {boolean} - result
|
|
16
|
+
*/
|
|
17
|
+
const matches = (selector, node) => {
|
|
18
|
+
const matcher = new Matcher(selector, node);
|
|
19
|
+
return matcher.matches();
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* closest - Element.closest()
|
|
24
|
+
* @param {string} selector - CSS selector
|
|
25
|
+
* @param {object} node - Element node
|
|
26
|
+
* @returns {?object} - matched node
|
|
27
|
+
*/
|
|
28
|
+
const closest = (selector, node) => {
|
|
29
|
+
const matcher = new Matcher(selector, node);
|
|
30
|
+
return matcher.closest();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* querySelector - Document.querySelector(), Element.querySelector()
|
|
35
|
+
* @param {string} selector - CSS selector
|
|
36
|
+
* @param {object} refPoint - Document or Element node
|
|
37
|
+
* @returns {?object} - matched node
|
|
38
|
+
*/
|
|
39
|
+
const querySelector = (selector, refPoint) => {
|
|
40
|
+
const matcher = new Matcher(selector, refPoint);
|
|
41
|
+
return matcher.querySelector();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* querySelectorAll - Document.querySelectorAll(), Element.querySelectorAll()
|
|
46
|
+
* NOTE: returns Array, not NodeList
|
|
47
|
+
* @param {string} selector - CSS selector
|
|
48
|
+
* @param {object} refPoint - Document or Element node
|
|
49
|
+
* @returns {Array.<object|undefined>} - array of matched nodes
|
|
50
|
+
*/
|
|
51
|
+
const querySelectorAll = (selector, refPoint) => {
|
|
52
|
+
const matcher = new Matcher(selector, refPoint);
|
|
53
|
+
return matcher.querySelectorAll();
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
module.exports = {
|
|
57
|
+
closest,
|
|
58
|
+
matches,
|
|
59
|
+
querySelector,
|
|
60
|
+
querySelectorAll
|
|
61
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* constant.js
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
AN_PLUS_B: 'AnPlusB',
|
|
7
|
+
AT_RULE: 'Atrule',
|
|
8
|
+
AT_RULE_PRELUDE: 'AtrulePrelude',
|
|
9
|
+
ATTRIBUTE_SELECTOR: 'AttributeSelector',
|
|
10
|
+
BLOCK: 'Block',
|
|
11
|
+
BRACKETS: 'Brackets',
|
|
12
|
+
CDC: 'CDC',
|
|
13
|
+
CDO: 'CDO',
|
|
14
|
+
CLASS_SELECTOR: 'ClassSelector',
|
|
15
|
+
COMBINATOR: 'Combinator',
|
|
16
|
+
COMMENT: 'Comment',
|
|
17
|
+
DECLARATION: 'Declaration',
|
|
18
|
+
DECLARATION_LIST: 'DeclarationList',
|
|
19
|
+
DIMENSION: 'Dimension',
|
|
20
|
+
FUNCTION: 'Function',
|
|
21
|
+
HASH: 'Hash',
|
|
22
|
+
ID_SELECTOR: 'IdSelector',
|
|
23
|
+
IDENTIFIER: 'Identifier',
|
|
24
|
+
MEDIA_FEATURE: 'MediaFeature',
|
|
25
|
+
MEDIA_QUERY: 'MediaQuery',
|
|
26
|
+
MEDIA_QUERY_LIST: 'MediaQueryList',
|
|
27
|
+
NESTING_SELECTOR: 'NestingSelector',
|
|
28
|
+
NTH: 'Nth',
|
|
29
|
+
NUMBER: 'Number',
|
|
30
|
+
OPERATOR: 'Operator',
|
|
31
|
+
PARENTHESES: 'Parentheses',
|
|
32
|
+
PERCENTAGE: 'Percentage',
|
|
33
|
+
PSEUDO_CLASS_SELECTOR: 'PseudoClassSelector',
|
|
34
|
+
PSEUDO_ELEMENT_SELECTOR: 'PseudoElementSelector',
|
|
35
|
+
RATIO: 'Ratio',
|
|
36
|
+
RAW: 'Raw',
|
|
37
|
+
RULE: 'Rule',
|
|
38
|
+
SELECTOR: 'Selector',
|
|
39
|
+
SELECTOR_LIST: 'SelectorList',
|
|
40
|
+
STRING: 'String',
|
|
41
|
+
STYLE_SHEET: 'StyleSheet',
|
|
42
|
+
TYPE_SELECTOR: 'TypeSelector',
|
|
43
|
+
UNICODE_RANGE: 'UnicodeRange',
|
|
44
|
+
URL: 'Url',
|
|
45
|
+
VALUE: 'Value',
|
|
46
|
+
WHITESPACE: 'WhiteSpace'
|
|
47
|
+
};
|