@markuplint/selector 3.5.0 → 3.6.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.
@@ -42,7 +42,7 @@ function contentModelPseudoClass(specs) {
42
42
  })
43
43
  .flat()
44
44
  .filter((m) => m.matched);
45
- if (matched.length) {
45
+ if (matched.length > 0) {
46
46
  return {
47
47
  specificity: [0, 1, 0],
48
48
  matched: true,
@@ -1,3 +1,3 @@
1
1
  export declare class InvalidSelectorError extends Error {
2
- name: string;
2
+ name: string;
3
3
  }
@@ -1,13 +1,13 @@
1
1
  import type { Specificity, RegexSelector } from './types';
2
2
  export type SelectorMatches = SelectorMatched | SelectorUnmatched;
3
3
  type SelectorMatched = {
4
- readonly matched: true;
5
- readonly selector: string;
6
- readonly specificity: Specificity;
7
- readonly data?: Readonly<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
- readonly matched: false;
10
+ readonly matched: false;
11
11
  };
12
12
  export declare function matchSelector(el: Node, selector: string | RegexSelector | undefined): SelectorMatches;
13
13
  export {};
@@ -9,7 +9,7 @@ const selector_1 = require("./selector");
9
9
  function matchSelector(
10
10
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
11
  el, selector) {
12
- if (!selector) {
12
+ if (selector == null || selector === '') {
13
13
  return {
14
14
  matched: false,
15
15
  };
@@ -17,7 +17,7 @@ el, selector) {
17
17
  if (typeof selector === 'string') {
18
18
  const sel = new selector_1.Selector(selector);
19
19
  const specificity = sel.match(el);
20
- if (specificity) {
20
+ if (specificity !== false) {
21
21
  return {
22
22
  matched: true,
23
23
  selector,
@@ -1,3 +1,7 @@
1
- export declare function regexSelectorMatches(reg: string, raw: string, ignoreCase: boolean): {
2
- [x: string]: string;
1
+ export declare function regexSelectorMatches(
2
+ reg: string,
3
+ raw: string,
4
+ ignoreCase: boolean,
5
+ ): {
6
+ [x: string]: string;
3
7
  } | null;
package/lib/selector.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { SelectorResult, Specificity } from './types';
2
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
@@ -58,14 +58,14 @@ class Ruleset {
58
58
  return new Ruleset(selectors, extended, 0);
59
59
  }
60
60
  constructor(selectors, extended, depth) {
61
- var _a;
61
+ var _a, _b;
62
62
  _Ruleset_selectorGroup.set(this, []);
63
63
  tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f").push(...selectors.map(selector => new StructuredSelector(selector, depth, extended)));
64
64
  const head = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0];
65
- this.headCombinator = (head === null || head === void 0 ? void 0 : head.headCombinator) || null;
65
+ this.headCombinator = (_a = head === null || head === void 0 ? void 0 : head.headCombinator) !== null && _a !== void 0 ? _a : null;
66
66
  if (this.headCombinator) {
67
67
  if (depth <= 0) {
68
- throw new invalid_selector_error_1.InvalidSelectorError(`'${(_a = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0]) === null || _a === void 0 ? void 0 : _a.selector}' is not a valid selector`);
68
+ throw new invalid_selector_error_1.InvalidSelectorError(`'${(_b = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0]) === null || _b === void 0 ? void 0 : _b.selector}' is not a valid selector`);
69
69
  }
70
70
  }
71
71
  }
@@ -86,13 +86,13 @@ class Ruleset {
86
86
  _Ruleset_selectorGroup = new WeakMap();
87
87
  class StructuredSelector {
88
88
  constructor(selector, depth, extended) {
89
- var _a;
89
+ var _a, _b;
90
90
  _StructuredSelector_edge.set(this, void 0);
91
91
  _StructuredSelector_selector.set(this, void 0);
92
92
  tslib_1.__classPrivateFieldSet(this, _StructuredSelector_selector, selector, "f");
93
93
  tslib_1.__classPrivateFieldSet(this, _StructuredSelector_edge, new SelectorTarget(extended, depth), "f");
94
94
  this.headCombinator =
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;
95
+ ((_a = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0]) === null || _a === void 0 ? void 0 : _a.type) === 'combinator' ? (_b = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes[0].value) !== null && _b !== void 0 ? _b : null : null;
96
96
  const nodes = tslib_1.__classPrivateFieldGet(this, _StructuredSelector_selector, "f").nodes.slice();
97
97
  if (0 < depth && this.headCombinator) {
98
98
  nodes.unshift((0, postcss_selector_parser_1.pseudo)({ value: ':scope' }));
@@ -180,15 +180,15 @@ class SelectorTarget {
180
180
  el,
181
181
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
182
182
  scope, count) {
183
- var _a;
183
+ var _a, _b, _c;
184
184
  const result = this._match(el, scope, count);
185
185
  if (selLog.enabled) {
186
186
  const nodeName = el.nodeName;
187
- const selector = ((_a = tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) === null || _a === void 0 ? void 0 : _a.target.toString()) || this.toString();
187
+ const selector = (_b = (_a = tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) === null || _a === void 0 ? void 0 : _a.target.toString()) !== null && _b !== void 0 ? _b : this.toString();
188
188
  const combinator = result.combinator ? ` ${result.combinator}` : '';
189
189
  selLog('The %s element by "%s" => %s (%d)', nodeName, `${selector}${combinator}`, result.matched, count);
190
190
  if (selector === ':scope') {
191
- selLog(`† Scope is the ${(scope === null || scope === void 0 ? void 0 : scope.nodeName) || null}`);
191
+ selLog(`† Scope is the ${(_c = scope === null || scope === void 0 ? void 0 : scope.nodeName) !== null && _c !== void 0 ? _c : null}`);
192
192
  }
193
193
  }
194
194
  delete result.combinator;
@@ -256,7 +256,7 @@ class SelectorTarget {
256
256
  unitCheck.specificity[2] + res.specificity[2],
257
257
  ];
258
258
  }
259
- if (matchedNodes.length) {
259
+ if (matchedNodes.length > 0) {
260
260
  return {
261
261
  combinator: '␣',
262
262
  specificity,
@@ -300,7 +300,7 @@ class SelectorTarget {
300
300
  specificity[1] += res.specificity[1];
301
301
  specificity[2] += res.specificity[2];
302
302
  }
303
- if (matchedNodes.length) {
303
+ if (matchedNodes.length > 0) {
304
304
  return {
305
305
  combinator: '>',
306
306
  specificity,
@@ -343,7 +343,7 @@ class SelectorTarget {
343
343
  specificity[1] += res.specificity[1];
344
344
  specificity[2] += res.specificity[2];
345
345
  }
346
- if (matchedNodes.length) {
346
+ if (matchedNodes.length > 0) {
347
347
  return {
348
348
  combinator: '+',
349
349
  specificity,
@@ -394,7 +394,7 @@ class SelectorTarget {
394
394
  unitCheck.specificity[2] + res.specificity[2],
395
395
  ];
396
396
  }
397
- if (matchedNodes.length) {
397
+ if (matchedNodes.length > 0) {
398
398
  return {
399
399
  combinator: '~',
400
400
  specificity,
@@ -554,7 +554,7 @@ el) {
554
554
  break;
555
555
  }
556
556
  case '*=': {
557
- if (valueOfEl.indexOf(value) === -1) {
557
+ if (!valueOfEl.includes(value)) {
558
558
  return false;
559
559
  }
560
560
  break;
@@ -592,7 +592,7 @@ scope, extended, depth) {
592
592
  let parent = el.parentElement;
593
593
  while (parent) {
594
594
  const matched = ruleset.match(parent, scope).filter((r) => r.matched);
595
- if (matched.length) {
595
+ if (matched.length > 0) {
596
596
  return {
597
597
  specificity,
598
598
  matched: true,
@@ -636,7 +636,7 @@ scope, extended, depth) {
636
636
  const matched = resList.filter((r) => r.matched);
637
637
  return {
638
638
  specificity,
639
- matched: !!matched.length,
639
+ matched: matched.length > 0,
640
640
  nodes: matched.map(m => m.nodes).flat(),
641
641
  has: matched.map(m => m.has).flat(),
642
642
  };
@@ -650,7 +650,7 @@ scope, extended, depth) {
650
650
  const has = getSiblings(el)
651
651
  .map(sib => ruleset.match(sib, el).filter((m) => m.matched))
652
652
  .flat();
653
- if (has.length) {
653
+ if (has.length > 0) {
654
654
  return {
655
655
  specificity,
656
656
  matched: true,
@@ -667,7 +667,7 @@ scope, extended, depth) {
667
667
  const has = getDescendants(el)
668
668
  .map(sib => ruleset.match(sib, el).filter((m) => m.matched))
669
669
  .flat();
670
- if (has.length) {
670
+ if (has.length > 0) {
671
671
  return {
672
672
  specificity,
673
673
  matched: true,
@@ -688,7 +688,7 @@ scope, extended, depth) {
688
688
  const matched = resList.filter((r) => r.matched);
689
689
  return {
690
690
  specificity: [0, 0, 0],
691
- matched: !!matched.length,
691
+ matched: matched.length > 0,
692
692
  nodes: matched.map(m => m.nodes).flat(),
693
693
  has: matched.map(m => m.has).flat(),
694
694
  };
@@ -792,7 +792,8 @@ function isScope(
792
792
  el,
793
793
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
794
794
  scope) {
795
- return el === scope || el.parentNode === null;
795
+ var _a;
796
+ return (_a = el === scope) !== null && _a !== void 0 ? _a : el.parentNode === null;
796
797
  }
797
798
  function getDescendants(
798
799
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
@@ -807,8 +808,8 @@ el, includeSelf = false) {
807
808
  function getSiblings(
808
809
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
809
810
  el) {
810
- var _a;
811
- return Array.from(((_a = el.parentElement) === null || _a === void 0 ? void 0 : _a.children) || []);
811
+ var _a, _b;
812
+ return Array.from((_b = (_a = el.parentElement) === null || _a === void 0 ? void 0 : _a.children) !== null && _b !== void 0 ? _b : []);
812
813
  }
813
814
  function getSpecificity(
814
815
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
package/lib/types.d.ts CHANGED
@@ -1,24 +1,24 @@
1
1
  export type Specificity = readonly [number, number, number];
2
2
  export type SelectorResult = SelectorMatchedResult | SelectorUnmatchedResult;
3
3
  export type SelectorMatchedResult = {
4
- readonly specificity: Specificity;
5
- readonly matched: true;
6
- readonly nodes: readonly (Element | Text)[];
7
- readonly has: readonly 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
- readonly specificity: Specificity;
11
- readonly matched: false;
12
- readonly not?: readonly SelectorMatchedResult[];
10
+ readonly specificity: Specificity;
11
+ readonly matched: false;
12
+ readonly not?: readonly SelectorMatchedResult[];
13
13
  };
14
14
  export type RegexSelector = RegexSelectorWithoutCombination & {
15
- readonly combination?: {
16
- readonly 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
- readonly nodeName?: string;
22
- readonly attrName?: string;
23
- readonly 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.5.0",
3
+ "version": "3.6.1",
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,11 +25,11 @@
25
25
  "tslib": "^2.4.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@markuplint/ml-spec": "3.5.0",
28
+ "@markuplint/ml-spec": "3.6.1",
29
29
  "@types/debug": "^4.1.7",
30
30
  "@types/jsdom": "16",
31
31
  "jsdom": "19",
32
32
  "type-fest": "^3.6.1"
33
33
  },
34
- "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
34
+ "gitHead": "3cdf5a088b2da03773d5d4461d0e65ec32290a00"
35
35
  }