@markuplint/selector 3.8.0 → 3.10.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.
@@ -1,3 +1,3 @@
1
1
  import type { MLMLSpec } from '@markuplint/ml-spec';
2
2
  import { Selector } from './selector';
3
- export declare function createSelector(selector: string, specs: MLMLSpec): Selector;
3
+ export declare function createSelector(selector: string, specs?: MLMLSpec): Selector;
@@ -11,11 +11,13 @@ function createSelector(selector, specs) {
11
11
  if (instance) {
12
12
  return instance;
13
13
  }
14
- instance = new selector_1.Selector(selector, {
15
- model: (0, content_model_pseudo_class_1.contentModelPseudoClass)(specs),
16
- aria: (0, aria_pseudo_class_1.ariaPseudoClass)(),
17
- role: (0, aria_role_pseudo_class_1.ariaRolePseudoClass)(specs),
18
- });
14
+ instance = new selector_1.Selector(selector, specs
15
+ ? {
16
+ model: (0, content_model_pseudo_class_1.contentModelPseudoClass)(specs),
17
+ aria: (0, aria_pseudo_class_1.ariaPseudoClass)(),
18
+ role: (0, aria_role_pseudo_class_1.ariaRolePseudoClass)(specs),
19
+ }
20
+ : undefined);
19
21
  caches.set(selector, instance);
20
22
  return instance;
21
23
  }
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { compareSpecificity } from './compare-specificity';
2
2
  export { matchSelector } from './match-selector';
3
3
  export { createSelector } from './create-selector';
4
+ export { InvalidSelectorError } from './invalid-selector-error';
4
5
  export * from './types';
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSelector = exports.matchSelector = exports.compareSpecificity = void 0;
3
+ exports.InvalidSelectorError = exports.createSelector = exports.matchSelector = exports.compareSpecificity = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  var compare_specificity_1 = require("./compare-specificity");
6
6
  Object.defineProperty(exports, "compareSpecificity", { enumerable: true, get: function () { return compare_specificity_1.compareSpecificity; } });
@@ -8,4 +8,6 @@ var match_selector_1 = require("./match-selector");
8
8
  Object.defineProperty(exports, "matchSelector", { enumerable: true, get: function () { return match_selector_1.matchSelector; } });
9
9
  var create_selector_1 = require("./create-selector");
10
10
  Object.defineProperty(exports, "createSelector", { enumerable: true, get: function () { return create_selector_1.createSelector; } });
11
+ var invalid_selector_error_1 = require("./invalid-selector-error");
12
+ Object.defineProperty(exports, "InvalidSelectorError", { enumerable: true, get: function () { return invalid_selector_error_1.InvalidSelectorError; } });
11
13
  tslib_1.__exportStar(require("./types"), exports);
@@ -1,3 +1,5 @@
1
1
  export declare class InvalidSelectorError extends Error {
2
- name: string;
2
+ name: string;
3
+ selector: string;
4
+ constructor(selector: string, message?: string);
3
5
  }
@@ -2,9 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InvalidSelectorError = void 0;
4
4
  class InvalidSelectorError extends Error {
5
- constructor() {
6
- super(...arguments);
5
+ constructor(selector, message) {
6
+ super(message !== null && message !== void 0 ? message : `Invalid selector: "${selector}"`);
7
7
  this.name = 'InvalidSelectorError';
8
+ this.selector = selector;
8
9
  }
9
10
  }
10
11
  exports.InvalidSelectorError = InvalidSelectorError;
@@ -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 {};
@@ -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
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
@@ -51,21 +51,24 @@ class Ruleset {
51
51
  }
52
52
  catch (e) {
53
53
  if (e instanceof Error) {
54
- throw new Error(`${e.message} At the selector: "${selector}"`);
54
+ throw new invalid_selector_error_1.InvalidSelectorError(selector);
55
55
  }
56
56
  throw e;
57
57
  }
58
58
  return new Ruleset(selectors, extended, 0);
59
59
  }
60
60
  constructor(selectors, extended, depth) {
61
- var _a, _b;
61
+ var _a, _b, _c;
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
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(`'${(_b = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0]) === null || _b === void 0 ? void 0 : _b.selector}' is not a valid selector`);
68
+ if ((_b = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0]) === null || _b === void 0 ? void 0 : _b.selector) {
69
+ throw new invalid_selector_error_1.InvalidSelectorError((_c = tslib_1.__classPrivateFieldGet(this, _Ruleset_selectorGroup, "f")[0]) === null || _c === void 0 ? void 0 : _c.selector);
70
+ }
71
+ throw new Error('Combinated selector depth is not expected');
69
72
  }
70
73
  }
71
74
  }
@@ -227,7 +230,7 @@ class SelectorTarget {
227
230
  const has = [];
228
231
  const not = [];
229
232
  let ancestor = el.parentElement;
230
- let specificity;
233
+ let specificity = undefined;
231
234
  while (ancestor) {
232
235
  const res = target.match(ancestor, scope, count + 1);
233
236
  if (!specificity) {
@@ -365,7 +368,7 @@ class SelectorTarget {
365
368
  const has = [];
366
369
  const not = [];
367
370
  let prev = el.previousElementSibling;
368
- let specificity;
371
+ let specificity = undefined;
369
372
  while (prev) {
370
373
  const res = target.match(prev, scope, count + 1);
371
374
  if (!specificity) {
@@ -814,7 +817,7 @@ el) {
814
817
  function getSpecificity(
815
818
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
816
819
  results) {
817
- let specificity;
820
+ let specificity = undefined;
818
821
  for (const result of results) {
819
822
  if (specificity) {
820
823
  const order = (0, compare_specificity_1.compareSpecificity)(specificity, result.specificity);
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.8.0",
3
+ "version": "3.10.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>",
@@ -20,16 +20,16 @@
20
20
  "clean": "tsc --build --clean"
21
21
  },
22
22
  "dependencies": {
23
- "@markuplint/ml-spec": "3.8.0",
24
- "@types/debug": "^4.1.7",
23
+ "@markuplint/ml-spec": "3.10.0",
24
+ "@types/debug": "^4.1.8",
25
25
  "debug": "^4.3.4",
26
- "postcss-selector-parser": "^6.0.11",
27
- "tslib": "^2.4.1",
28
- "type-fest": "^3.8.0"
26
+ "postcss-selector-parser": "^6.0.13",
27
+ "tslib": "^2.5.3",
28
+ "type-fest": "^3.11.1"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/jsdom": "21.1.1",
32
- "jsdom": "21.1.1"
32
+ "jsdom": "22.0.0"
33
33
  },
34
- "gitHead": "adc6e432cccba7cfad0dc8bf9f92e5aaf1107359"
34
+ "gitHead": "9547b8dca20736a93e4d01af2d61bee314ba5718"
35
35
  }