@markuplint/selector 3.4.0 → 3.5.0

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.
@@ -6,7 +6,9 @@ const ml_spec_1 = require("@markuplint/ml-spec");
6
6
  * Version Syntax is not support yet.
7
7
  */
8
8
  function ariaPseudoClass() {
9
- return (content) => (el) => {
9
+ return (content) => (
10
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
+ el) => {
10
12
  const aria = ariaPseudoClassParser(content);
11
13
  const name = (0, ml_spec_1.getAccname)(el);
12
14
  switch (aria.type) {
@@ -3,7 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ariaRolePseudoClass = void 0;
4
4
  const ml_spec_1 = require("@markuplint/ml-spec");
5
5
  function ariaRolePseudoClass(specs) {
6
- return (content) => (el) => {
6
+ return (content) => (
7
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
+ el) => {
7
9
  var _a, _b;
8
10
  const aria = ariaPseudoClassParser(content);
9
11
  const computed = (0, ml_spec_1.getComputedRole)(specs, el, (_a = aria.version) !== null && _a !== void 0 ? _a : '1.2');
@@ -4,7 +4,9 @@ exports.contentModelPseudoClass = void 0;
4
4
  const ml_spec_1 = require("@markuplint/ml-spec");
5
5
  const create_selector_1 = require("../create-selector");
6
6
  function contentModelPseudoClass(specs) {
7
- return (category) => (el) => {
7
+ return (category) => (
8
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
9
+ el) => {
8
10
  category = category.trim().toLowerCase();
9
11
  const selectors = (0, ml_spec_1.contentModelCategoryToTagNames)(`#${category}`, specs.def);
10
12
  const matched = selectors
@@ -1,3 +1,3 @@
1
1
  export declare class InvalidSelectorError extends Error {
2
- name: string;
2
+ name: string;
3
3
  }
package/lib/is.js CHANGED
@@ -1,15 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isPureHTMLElement = exports.isNonDocumentTypeChildNode = exports.isElement = void 0;
4
- function isElement(node) {
4
+ function isElement(
5
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
6
+ node) {
5
7
  return node.nodeType === node.ELEMENT_NODE;
6
8
  }
7
9
  exports.isElement = isElement;
8
- function isNonDocumentTypeChildNode(node) {
10
+ function isNonDocumentTypeChildNode(
11
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
12
+ node) {
9
13
  return 'previousElementSibling' in node && 'nextElementSibling' in node;
10
14
  }
11
15
  exports.isNonDocumentTypeChildNode = isNonDocumentTypeChildNode;
12
- function isPureHTMLElement(el) {
16
+ function isPureHTMLElement(
17
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
18
+ el) {
13
19
  return el.localName !== el.nodeName;
14
20
  }
15
21
  exports.isPureHTMLElement = isPureHTMLElement;
@@ -1,13 +1,13 @@
1
1
  import type { Specificity, RegexSelector } from './types';
2
2
  export type SelectorMatches = SelectorMatched | SelectorUnmatched;
3
3
  type SelectorMatched = {
4
- matched: true;
5
- selector: string;
6
- specificity: Specificity;
7
- data?: Record<string, string>;
4
+ readonly matched: true;
5
+ readonly selector: string;
6
+ readonly specificity: Specificity;
7
+ readonly data?: Readonly<Record<string, string>>;
8
8
  };
9
9
  type SelectorUnmatched = {
10
- matched: false;
10
+ readonly matched: false;
11
11
  };
12
12
  export declare function matchSelector(el: Node, selector: string | RegexSelector | undefined): SelectorMatches;
13
13
  export {};
@@ -6,7 +6,9 @@ const tslib_1 = require("tslib");
6
6
  const is_1 = require("./is");
7
7
  const regex_selector_matches_1 = require("./regex-selector-matches");
8
8
  const selector_1 = require("./selector");
9
- function matchSelector(el, selector) {
9
+ function matchSelector(
10
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
+ el, selector) {
10
12
  if (!selector) {
11
13
  return {
12
14
  matched: false,
@@ -29,7 +31,9 @@ function matchSelector(el, selector) {
29
31
  return regexSelect(el, selector);
30
32
  }
31
33
  exports.matchSelector = matchSelector;
32
- function regexSelect(el, selector) {
34
+ function regexSelect(
35
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
36
+ el, selector) {
33
37
  let edge = new SelectorTarget(selector);
34
38
  let edgeSelector = selector.combination;
35
39
  while (edgeSelector) {
@@ -49,7 +53,9 @@ class SelectorTarget {
49
53
  from(target, combinator) {
50
54
  tslib_1.__classPrivateFieldSet(this, _SelectorTarget_combinedFrom, { target, combinator }, "f");
51
55
  }
52
- match(el) {
56
+ match(
57
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
58
+ el) {
53
59
  const unitCheck = this._matchWithoutCombineChecking(el);
54
60
  if (!unitCheck.matched) {
55
61
  return unitCheck;
@@ -139,12 +145,16 @@ class SelectorTarget {
139
145
  }
140
146
  }
141
147
  }
142
- _matchWithoutCombineChecking(el) {
148
+ _matchWithoutCombineChecking(
149
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
150
+ el) {
143
151
  return uncombinedRegexSelect(el, tslib_1.__classPrivateFieldGet(this, _SelectorTarget_selector, "f"));
144
152
  }
145
153
  }
146
154
  _SelectorTarget_combinedFrom = new WeakMap(), _SelectorTarget_selector = new WeakMap();
147
- function uncombinedRegexSelect(el, selector) {
155
+ function uncombinedRegexSelect(
156
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
157
+ el, selector) {
148
158
  if (!(0, is_1.isElement)(el)) {
149
159
  return {
150
160
  matched: false,
@@ -1,7 +1,3 @@
1
- export declare function regexSelectorMatches(
2
- reg: string,
3
- raw: string,
4
- ignoreCase: boolean,
5
- ): {
6
- [x: string]: string;
1
+ export declare function regexSelectorMatches(reg: string, raw: string, ignoreCase: boolean): {
2
+ [x: string]: string;
7
3
  } | null;
package/lib/selector.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { SelectorResult, Specificity } from './types';
2
- type ExtendedPseudoClass = Record<string, (content: string) => (el: Element) => SelectorResult>;
2
+ type ExtendedPseudoClass = Readonly<Record<string, (content: string) => (el: Element) => SelectorResult>>;
3
3
  export declare class Selector {
4
- #private;
5
- constructor(selector: string, extended?: ExtendedPseudoClass);
6
- match(el: Node, scope?: ParentNode | null): Specificity | false;
7
- search(el: Node, scope?: ParentNode | null): SelectorResult[];
4
+ #private;
5
+ constructor(selector: string, extended?: ExtendedPseudoClass);
6
+ match(el: Node, scope?: ParentNode | null): Specificity | false;
7
+ search(el: Node, scope?: ParentNode | null): SelectorResult[];
8
8
  }
9
9
  export {};
package/lib/selector.js CHANGED
@@ -16,7 +16,12 @@ class Selector {
16
16
  _Selector_ruleset.set(this, void 0);
17
17
  tslib_1.__classPrivateFieldSet(this, _Selector_ruleset, Ruleset.parse(selector, extended), "f");
18
18
  }
19
- match(el, scope = (0, is_1.isElement)(el) ? el : null) {
19
+ match(
20
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
21
+ el,
22
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
23
+ scope) {
24
+ scope = (0, is_1.isElement)(el) ? el : null;
20
25
  const results = this.search(el, scope);
21
26
  for (const result of results) {
22
27
  if (result.matched) {
@@ -25,7 +30,12 @@ class Selector {
25
30
  }
26
31
  return false;
27
32
  }
28
- search(el, scope = (0, is_1.isElement)(el) ? el : null) {
33
+ search(
34
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
35
+ el,
36
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
37
+ scope) {
38
+ scope = (0, is_1.isElement)(el) ? el : null;
29
39
  return tslib_1.__classPrivateFieldGet(this, _Selector_ruleset, "f").match(el, scope);
30
40
  }
31
41
  }
@@ -59,7 +69,11 @@ class Ruleset {
59
69
  }
60
70
  }
61
71
  }
62
- match(el, scope) {
72
+ match(
73
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
74
+ el,
75
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
76
+ scope) {
63
77
  (0, debug_1.log)('<%s> (%s)', (0, is_1.isElement)(el) ? el.localName : el.nodeName, scope ? ((0, is_1.isElement)(scope) ? scope.localName : scope.nodeName) : null);
64
78
  return tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f").map(selector => {
65
79
  selLog('"%s"', selector.selector);
@@ -79,10 +93,11 @@ class StructuredSelector {
79
93
  tslib_1.__classPrivateFieldSet(this, _StructuredSelector_edge, new SelectorTarget(extended, depth), "f");
80
94
  this.headCombinator =
81
95
  ((_a = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0]) === null || _a === void 0 ? void 0 : _a.type) === 'combinator' ? tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0].value || null : null;
96
+ const nodes = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.slice();
82
97
  if (0 < depth && this.headCombinator) {
83
- tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.unshift((0, postcss_selector_parser_1.pseudo)({ value: ':scope' }));
98
+ nodes.unshift((0, postcss_selector_parser_1.pseudo)({ value: ':scope' }));
84
99
  }
85
- tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.forEach(node => {
100
+ nodes.forEach(node => {
86
101
  switch (node.type) {
87
102
  case 'combinator': {
88
103
  const combinedTarget = new SelectorTarget(extended, depth);
@@ -109,7 +124,11 @@ class StructuredSelector {
109
124
  get selector() {
110
125
  return tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.join('');
111
126
  }
112
- match(el, scope) {
127
+ match(
128
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
129
+ el,
130
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
131
+ scope) {
113
132
  return tslib_1.__classPrivateFieldGet(this, _StructuredSelector_edge, "f").match(el, scope, 0);
114
133
  }
115
134
  }
@@ -156,7 +175,11 @@ class SelectorTarget {
156
175
  from(target, combinator) {
157
176
  tslib_1.__classPrivateFieldSet(this, _SelectorTarget_combinedFrom, { target, combinator }, "f");
158
177
  }
159
- match(el, scope, count) {
178
+ match(
179
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
180
+ el,
181
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
182
+ scope, count) {
160
183
  var _a;
161
184
  const result = this._match(el, scope, count);
162
185
  if (selLog.enabled) {
@@ -181,7 +204,11 @@ class SelectorTarget {
181
204
  this.pseudo.map(pseudo => pseudo.value).join(''),
182
205
  ].join('');
183
206
  }
184
- _match(el, scope, count) {
207
+ _match(
208
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
209
+ el,
210
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
211
+ scope, count) {
185
212
  const unitCheck = this._matchWithoutCombineChecking(el, scope);
186
213
  if (!unitCheck.matched) {
187
214
  return unitCheck;
@@ -250,7 +277,7 @@ class SelectorTarget {
250
277
  const matchedNodes = [];
251
278
  const has = [];
252
279
  const not = [];
253
- const specificity = unitCheck.specificity;
280
+ const specificity = [...unitCheck.specificity];
254
281
  const parentNode = el.parentElement;
255
282
  if (parentNode) {
256
283
  const res = target.match(parentNode, scope, count + 1);
@@ -294,7 +321,7 @@ class SelectorTarget {
294
321
  const matchedNodes = [];
295
322
  const has = [];
296
323
  const not = [];
297
- const specificity = unitCheck.specificity;
324
+ const specificity = [...unitCheck.specificity];
298
325
  if (el.previousElementSibling) {
299
326
  const res = target.match(el.previousElementSibling, scope, count + 1);
300
327
  specificity[0] += res.specificity[0];
@@ -392,7 +419,11 @@ class SelectorTarget {
392
419
  }
393
420
  }
394
421
  }
395
- _matchWithoutCombineChecking(el, scope) {
422
+ _matchWithoutCombineChecking(
423
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
424
+ el,
425
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
426
+ scope) {
396
427
  var _a;
397
428
  const specificity = [0, 0, 0];
398
429
  if (!(0, is_1.isElement)(el)) {
@@ -483,7 +514,9 @@ class SelectorTarget {
483
514
  }
484
515
  }
485
516
  _SelectorTarget_combinedFrom = new WeakMap(), _SelectorTarget_extended = new WeakMap(), _SelectorTarget_isAdded = new WeakMap();
486
- function attrMatch(attr, el) {
517
+ function attrMatch(attr,
518
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
519
+ el) {
487
520
  return Array.from(el.attributes).some(attrOfEl => {
488
521
  if (attr.attribute !== attrOfEl.localName) {
489
522
  return false;
@@ -543,7 +576,11 @@ function attrMatch(attr, el) {
543
576
  return true;
544
577
  });
545
578
  }
546
- function pseudoMatch(pseudo, el, scope, extended, depth) {
579
+ function pseudoMatch(pseudo,
580
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
581
+ el,
582
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
583
+ scope, extended, depth) {
547
584
  switch (pseudo.value) {
548
585
  //
549
586
  /**
@@ -750,10 +787,16 @@ function pseudoMatch(pseudo, el, scope, extended, depth) {
750
787
  }
751
788
  }
752
789
  }
753
- function isScope(el, scope) {
790
+ function isScope(
791
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
792
+ el,
793
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
794
+ scope) {
754
795
  return el === scope || el.parentNode === null;
755
796
  }
756
- function getDescendants(el, includeSelf = false) {
797
+ function getDescendants(
798
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
799
+ el, includeSelf = false) {
757
800
  return [
758
801
  ...Array.from(el.children)
759
802
  .map(child => getDescendants(child, true))
@@ -761,11 +804,15 @@ function getDescendants(el, includeSelf = false) {
761
804
  ...(includeSelf ? [el] : []),
762
805
  ];
763
806
  }
764
- function getSiblings(el) {
807
+ function getSiblings(
808
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
809
+ el) {
765
810
  var _a;
766
811
  return Array.from(((_a = el.parentElement) === null || _a === void 0 ? void 0 : _a.children) || []);
767
812
  }
768
- function getSpecificity(results) {
813
+ function getSpecificity(
814
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
815
+ results) {
769
816
  let specificity;
770
817
  for (const result of results) {
771
818
  if (specificity) {
package/lib/types.d.ts CHANGED
@@ -1,24 +1,24 @@
1
- export type Specificity = [number, number, number];
1
+ export type Specificity = readonly [number, number, number];
2
2
  export type SelectorResult = SelectorMatchedResult | SelectorUnmatchedResult;
3
3
  export type SelectorMatchedResult = {
4
- specificity: Specificity;
5
- matched: true;
6
- nodes: (Element | Text)[];
7
- has: SelectorMatchedResult[];
4
+ readonly specificity: Specificity;
5
+ readonly matched: true;
6
+ readonly nodes: readonly (Element | Text)[];
7
+ readonly has: readonly SelectorMatchedResult[];
8
8
  };
9
9
  export type SelectorUnmatchedResult = {
10
- specificity: Specificity;
11
- matched: false;
12
- not?: SelectorMatchedResult[];
10
+ readonly specificity: Specificity;
11
+ readonly matched: false;
12
+ readonly not?: readonly SelectorMatchedResult[];
13
13
  };
14
14
  export type RegexSelector = RegexSelectorWithoutCombination & {
15
- combination?: {
16
- combinator: RegexSelectorCombinator;
17
- } & RegexSelector;
15
+ readonly combination?: {
16
+ readonly combinator: RegexSelectorCombinator;
17
+ } & RegexSelector;
18
18
  };
19
19
  export type RegexSelectorCombinator = ' ' | '>' | '+' | '~' | ':has(+)' | ':has(~)';
20
20
  export type RegexSelectorWithoutCombination = {
21
- nodeName?: string;
22
- attrName?: string;
23
- attrValue?: string;
21
+ readonly nodeName?: string;
22
+ readonly attrName?: string;
23
+ readonly attrValue?: string;
24
24
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/selector",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "Extended W3C Selectors matcher",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -25,10 +25,11 @@
25
25
  "tslib": "^2.4.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@markuplint/ml-spec": "3.4.0",
28
+ "@markuplint/ml-spec": "3.5.0",
29
29
  "@types/debug": "^4.1.7",
30
30
  "@types/jsdom": "16",
31
- "jsdom": "19"
31
+ "jsdom": "19",
32
+ "type-fest": "^3.6.1"
32
33
  },
33
- "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
34
+ "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
34
35
  }