@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/src/js/parser.js CHANGED
@@ -7,9 +7,10 @@ import { findAll, parse, toPlainObject, walk } from 'css-tree';
7
7
 
8
8
  /* constants */
9
9
  import {
10
- DUO, HEX, MAX_BIT_16, BIT_HYPHEN, REG_LOGICAL_PSEUDO, REG_SHADOW_PSEUDO,
11
- SELECTOR, SELECTOR_PSEUDO_CLASS, SELECTOR_PSEUDO_ELEMENT, SYNTAX_ERR,
12
- TYPE_FROM, TYPE_TO, U_FFFD
10
+ BIT_01, BIT_02, BIT_04, BIT_08, BIT_16, BIT_32, BIT_FFFF, BIT_HYPHEN,
11
+ DUO, HEX, REG_LOGICAL_PSEUDO, REG_SHADOW_PSEUDO, SELECTOR, SELECTOR_ATTR,
12
+ SELECTOR_CLASS, SELECTOR_ID, SELECTOR_PSEUDO_CLASS, SELECTOR_PSEUDO_ELEMENT,
13
+ SELECTOR_TYPE, SYNTAX_ERR, TYPE_FROM, TYPE_TO, U_FFFD
13
14
  } from './constant.js';
14
15
 
15
16
  /**
@@ -87,7 +88,7 @@ export const preprocess = (...args) => {
87
88
  throw new DOMException(`Invalid selector ${selector}`, SYNTAX_ERR);
88
89
  }
89
90
  // escape char above 0xFFFF
90
- } else if (codePoint > MAX_BIT_16) {
91
+ } else if (codePoint > BIT_FFFF) {
91
92
  const str = `\\${codePoint.toString(HEX)} `;
92
93
  if (postHash.length === DUO) {
93
94
  postHash = str;
@@ -218,5 +219,65 @@ export const walkAST = (ast = {}) => {
218
219
  return [...branches];
219
220
  };
220
221
 
222
+ /**
223
+ * sort AST
224
+ * @param {Array.<object>} asts - collection of AST
225
+ * @returns {Array.<object>} - collection of sorted AST
226
+ */
227
+ export const sortAST = asts => {
228
+ const arr = [...asts];
229
+ if (arr.length > 1) {
230
+ const order = new Map([
231
+ [SELECTOR_PSEUDO_ELEMENT, BIT_01],
232
+ [SELECTOR_ID, BIT_02],
233
+ [SELECTOR_CLASS, BIT_04],
234
+ [SELECTOR_TYPE, BIT_08],
235
+ [SELECTOR_ATTR, BIT_16],
236
+ [SELECTOR_PSEUDO_CLASS, BIT_32]
237
+ ]);
238
+ arr.sort((a, b) => {
239
+ const { type: typeA } = a;
240
+ const { type: typeB } = b;
241
+ const bitA = order.get(typeA);
242
+ const bitB = order.get(typeB);
243
+ let res;
244
+ if (bitA === bitB) {
245
+ res = 0;
246
+ } else if (bitA > bitB) {
247
+ res = 1;
248
+ } else {
249
+ res = -1;
250
+ }
251
+ return res;
252
+ });
253
+ }
254
+ return arr;
255
+ };
256
+
257
+ /**
258
+ * parse AST name - e.g. ns|E -> { prefix: ns, localName: E }
259
+ * @private
260
+ * @param {string} selector - type selector
261
+ * @returns {object} - node properties
262
+ */
263
+ export const parseAstName = selector => {
264
+ let prefix;
265
+ let localName;
266
+ if (selector && typeof selector === 'string') {
267
+ if (selector.indexOf('|') > -1) {
268
+ [prefix, localName] = selector.split('|');
269
+ } else {
270
+ prefix = '*';
271
+ localName = selector;
272
+ }
273
+ } else {
274
+ throw new DOMException(`Invalid selector ${selector}`, SYNTAX_ERR);
275
+ }
276
+ return {
277
+ prefix,
278
+ localName
279
+ };
280
+ };
281
+
221
282
  /* export */
222
283
  export { generate as generateCSS } from 'css-tree';
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { Matcher };
2
- export let matcher: Matcher;
1
+ export { Finder };
2
+ export let finder: Finder;
3
3
  export function matches(selector: string, node: object, opt?: {
4
4
  warn?: boolean;
5
5
  }): boolean;
@@ -12,4 +12,4 @@ export function querySelector(selector: string, node: object, opt?: {
12
12
  export function querySelectorAll(selector: string, node: object, opt?: {
13
13
  warn?: boolean;
14
14
  }): Array<object | undefined>;
15
- import { Matcher } from './js/matcher.js';
15
+ import { Finder } from './js/finder.js';
@@ -22,10 +22,10 @@ export const BIT_04: 4;
22
22
  export const BIT_08: 8;
23
23
  export const BIT_16: 16;
24
24
  export const BIT_32: 32;
25
+ export const BIT_FFFF: 65535;
25
26
  export const BIT_HYPHEN: 45;
26
27
  export const DUO: 2;
27
28
  export const HEX: 16;
28
- export const MAX_BIT_16: 65535;
29
29
  export const TYPE_FROM: 8;
30
30
  export const TYPE_TO: -1;
31
31
  export const ELEMENT_NODE: 1;
@@ -1,3 +1,4 @@
1
+ export function prepareDOMObjects(node: object): Array<object>;
1
2
  export function isInShadowTree(node?: object): boolean;
2
3
  export function getSlottedTextContent(node?: object): string | null;
3
4
  export function getDirectionality(node?: object): string | null;
@@ -5,4 +6,4 @@ export function isContentEditable(node?: object): boolean;
5
6
  export function isNamespaceDeclared(ns?: string, node?: object): boolean;
6
7
  export function isInclusive(nodeA?: object, nodeB?: object): boolean;
7
8
  export function isPreceding(nodeA?: object, nodeB?: object): boolean;
8
- export function selectorToNodeProps(selector: string, node?: object): object;
9
+ export function sortNodes(nodes?: Array<object> | Set<object>): Array<object | undefined>;
@@ -0,0 +1,30 @@
1
+ export class Finder {
2
+ private _onError;
3
+ private _setup;
4
+ private _correspond;
5
+ private _traverse;
6
+ private _collectNthChild;
7
+ private _collectNthOfType;
8
+ private _matchAnPlusB;
9
+ private _matchDirectionPseudoClass;
10
+ private _matchLanguagePseudoClass;
11
+ private _matchHasPseudoFunc;
12
+ private _matchLogicalPseudoFunc;
13
+ private _matchPseudoClassSelector;
14
+ private _matchShadowHostPseudoClass;
15
+ private _matchSelector;
16
+ private _matchLeaves;
17
+ private _findDescendantNodes;
18
+ private _matchCombinator;
19
+ private _findNode;
20
+ private _findEntryNodes;
21
+ private _getEntryTwig;
22
+ private _collectNodes;
23
+ private _matchNodes;
24
+ private _find;
25
+ matches(node: object, selector: string, opt: object): boolean;
26
+ closest(node: object, selector: string, opt: object): object | null;
27
+ querySelector(node: object, selector: string, opt: object): object | null;
28
+ querySelectorAll(node: object, selector: string, opt: object): Array<object | undefined>;
29
+ #private;
30
+ }
@@ -1,57 +1,11 @@
1
- export class Matcher {
2
- _onError(e: Error): void;
3
- _setup(node: object): Array<object>;
4
- _sortLeaves(leaves: Array<object>): Array<object>;
5
- _correspond(selector: string): Array<Array<object | undefined>>;
6
- _traverse(node?: object, walker?: object): object | null;
7
- _collectNthChild(anb: {
8
- a: number;
9
- b: number;
10
- reverse?: boolean;
11
- selector?: object;
12
- }, node: object): Set<object>;
13
- _collectNthOfType(anb: {
14
- a: number;
15
- b: number;
16
- reverse?: boolean;
17
- }, node: object): Set<object>;
18
- _matchAnPlusB(ast: object, node: object, nthName: string): Set<object>;
19
- _matchPseudoElementSelector(astName: string, opt?: {
20
- forgive?: boolean;
21
- }): void;
22
- _matchDirectionPseudoClass(ast: object, node: object): object | null;
23
- _matchLanguagePseudoClass(ast: object, node: object): object | null;
24
- _matchHasPseudoFunc(leaves: Array<object>, node: object): boolean;
25
- _matchLogicalPseudoFunc(astData: object, node: object): object | null;
26
- _matchPseudoClassSelector(ast: object, node: object, opt?: {
27
- forgive?: boolean;
28
- }): Set<object>;
29
- _matchAttributeSelector(ast: object, node: object): object | null;
30
- _matchClassSelector(ast: object, node: object): object | null;
31
- _matchIDSelector(ast: object, node: object): object | null;
32
- _matchTypeSelector(ast: object, node: object, opt?: {
33
- forgive?: boolean;
34
- }): object | null;
35
- _matchShadowHostPseudoClass(ast: object, node: object): object | null;
36
- _matchSelector(ast: object, node: object, opt?: object): Set<object>;
37
- _matchLeaves(leaves: Array<object>, node: object, opt?: object): boolean;
38
- _findDescendantNodes(leaves: Array<object>, baseNode: object): object;
39
- _matchCombinator(twig: object, node: object, opt?: {
40
- dir?: string;
41
- forgive?: boolean;
42
- }): Set<object>;
43
- _findNode(leaves: Array<object>, opt?: {
44
- node?: object;
45
- }): object | null;
46
- _findEntryNodes(twig: object, targetType: string): object;
47
- _getEntryTwig(branch: Array<object>, targetType: string): object;
48
- _collectNodes(targetType: string): Array<Array<object | undefined>>;
49
- _sortNodes(nodes: Array<object> | Set<object>): Array<object | undefined>;
50
- _matchNodes(targetType: string): Set<object>;
51
- _find(targetType: string, node: object, selector: string, opt?: object): Set<object>;
52
- matches(node: object, selector: string, opt: object): boolean;
53
- closest(node: object, selector: string, opt: object): object | null;
54
- querySelector(node: object, selector: string, opt: object): object | null;
55
- querySelectorAll(node: object, selector: string, opt: object): Array<object | undefined>;
56
- #private;
57
- }
1
+ export function _matchAttributeSelector(ast: object, node: object): object | null;
2
+ export function _matchClassSelector(ast: object, node: object): object | null;
3
+ export function _matchIDSelector(ast: object, node: object): object | null;
4
+ export function _matchTypeSelector(ast: object, node: object, opt?: {
5
+ forgive?: boolean;
6
+ }): object | null;
7
+ export function matchSelector(ast: object, node: object, opt: object): object | null;
8
+ export function matchPseudoElementSelector(astName: string, opt?: {
9
+ forgive?: boolean;
10
+ warn?: boolean;
11
+ }): void;
@@ -2,4 +2,6 @@ export function unescapeSelector(selector?: string): string | null;
2
2
  export function preprocess(...args: any[]): string;
3
3
  export function parseSelector(selector: string): object;
4
4
  export function walkAST(ast?: object): Array<object | undefined>;
5
+ export function sortAST(asts: Array<object>): Array<object>;
6
+ export function parseAstName(selector: string): object;
5
7
  export { generate as generateCSS } from "css-tree";