@markuplint/selector 3.0.0-dev.25 → 3.0.0-dev.300

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,2 +1,2 @@
1
- import type { Specificity } from './types';
1
+ import type { Specificity } from './types.js';
2
2
  export declare function compareSpecificity(a: Specificity, b: Specificity): 0 | 1 | -1;
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.compareSpecificity = void 0;
4
- function compareSpecificity(a, b) {
1
+ export function compareSpecificity(a, b) {
5
2
  if (a[0] < b[0]) {
6
3
  return -1;
7
4
  }
@@ -22,4 +19,3 @@ function compareSpecificity(a, b) {
22
19
  }
23
20
  return 0;
24
21
  }
25
- exports.compareSpecificity = compareSpecificity;
@@ -1,3 +1,3 @@
1
1
  import type { MLMLSpec } from '@markuplint/ml-spec';
2
- import { Selector } from './selector';
3
- export declare function createSelector(selector: string, specs: MLMLSpec): Selector;
2
+ import { Selector } from './selector.js';
3
+ export declare function createSelector(selector: string, specs?: MLMLSpec): Selector;
@@ -1,22 +1,20 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSelector = void 0;
4
- const aria_pseudo_class_1 = require("./extended-selector/aria-pseudo-class");
5
- const aria_role_pseudo_class_1 = require("./extended-selector/aria-role-pseudo-class");
6
- const content_model_pseudo_class_1 = require("./extended-selector/content-model-pseudo-class");
7
- const selector_1 = require("./selector");
1
+ import { ariaPseudoClass } from './extended-selector/aria-pseudo-class.js';
2
+ import { ariaRolePseudoClass } from './extended-selector/aria-role-pseudo-class.js';
3
+ import { contentModelPseudoClass } from './extended-selector/content-model-pseudo-class.js';
4
+ import { Selector } from './selector.js';
8
5
  const caches = new Map();
9
- function createSelector(selector, specs) {
6
+ export function createSelector(selector, specs) {
10
7
  let instance = caches.get(selector);
11
8
  if (instance) {
12
9
  return instance;
13
10
  }
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
- });
11
+ instance = new Selector(selector, specs
12
+ ? {
13
+ model: contentModelPseudoClass(specs),
14
+ aria: ariaPseudoClass(),
15
+ role: ariaRolePseudoClass(specs),
16
+ }
17
+ : undefined);
19
18
  caches.set(selector, instance);
20
19
  return instance;
21
20
  }
22
- exports.createSelector = createSelector;
package/lib/debug.js CHANGED
@@ -1,18 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.enableDebug = exports.log = void 0;
4
- const tslib_1 = require("tslib");
5
- const debug_1 = tslib_1.__importDefault(require("debug"));
1
+ import debug from 'debug';
6
2
  const CLI_NS = 'markuplint-cli';
7
- exports.log = (0, debug_1.default)('selector');
8
- function enableDebug() {
9
- if (!exports.log.enabled) {
10
- debug_1.default.enable(`${exports.log.namespace}*`);
11
- (0, exports.log)(`Debug enable: ${exports.log.namespace}`);
12
- if (!debug_1.default.enabled(CLI_NS)) {
13
- debug_1.default.enable(`${exports.log.namespace}*,${CLI_NS}*`);
14
- (0, exports.log)(`Debug enable: ${exports.log.namespace}, ${CLI_NS}`);
3
+ export const log = debug('selector');
4
+ export function enableDebug() {
5
+ if (!log.enabled) {
6
+ debug.enable(`${log.namespace}*`);
7
+ log(`Debug enable: ${log.namespace}`);
8
+ if (!debug.enabled(CLI_NS)) {
9
+ debug.enable(`${log.namespace}*,${CLI_NS}*`);
10
+ log(`Debug enable: ${log.namespace}, ${CLI_NS}`);
15
11
  }
16
12
  }
17
13
  }
18
- exports.enableDebug = enableDebug;
@@ -1,4 +1,4 @@
1
- import type { SelectorResult } from '../types';
1
+ import type { SelectorResult } from '../types.js';
2
2
  /**
3
3
  * Version Syntax is not support yet.
4
4
  */
@@ -1,14 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ariaPseudoClass = void 0;
4
- const ml_spec_1 = require("@markuplint/ml-spec");
1
+ import { validateAriaVersion, ARIA_RECOMMENDED_VERSION, getAccname } from '@markuplint/ml-spec';
5
2
  /**
6
3
  * Version Syntax is not support yet.
7
4
  */
8
- function ariaPseudoClass() {
9
- return (content) => (el) => {
5
+ export function ariaPseudoClass() {
6
+ return (content) => (
7
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
+ el) => {
10
9
  const aria = ariaPseudoClassParser(content);
11
- const name = (0, ml_spec_1.getAccname)(el);
10
+ const name = getAccname(el);
12
11
  switch (aria.type) {
13
12
  case 'hasName': {
14
13
  if (name) {
@@ -41,11 +40,13 @@ function ariaPseudoClass() {
41
40
  }
42
41
  };
43
42
  }
44
- exports.ariaPseudoClass = ariaPseudoClass;
45
43
  function ariaPseudoClassParser(syntax) {
46
44
  const [_query, _version] = syntax.split('|');
47
- const query = _query.replace(/\s+/g, '').toLowerCase();
48
- const version = _version === '1.1' ? '1.1' : '1.2';
45
+ const query = _query?.replace(/\s+/g, '').toLowerCase();
46
+ const version = _version ?? ARIA_RECOMMENDED_VERSION;
47
+ if (!validateAriaVersion(version)) {
48
+ throw new SyntaxError(`Unsupported ARIA version: ${version}`);
49
+ }
49
50
  switch (query) {
50
51
  case 'hasname': {
51
52
  return {
@@ -1,3 +1,3 @@
1
- import type { SelectorResult } from '../types';
1
+ import type { SelectorResult } from '../types.js';
2
2
  import type { MLMLSpec } from '@markuplint/ml-spec';
3
3
  export declare function ariaRolePseudoClass(specs: MLMLSpec): (content: string) => (el: Element) => SelectorResult;
@@ -1,13 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ariaRolePseudoClass = void 0;
4
- const ml_spec_1 = require("@markuplint/ml-spec");
5
- function ariaRolePseudoClass(specs) {
6
- return (content) => (el) => {
7
- var _a, _b;
1
+ import { validateAriaVersion, ARIA_RECOMMENDED_VERSION, getComputedRole } from '@markuplint/ml-spec';
2
+ export function ariaRolePseudoClass(specs) {
3
+ return (content) => (
4
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
5
+ el) => {
8
6
  const aria = ariaPseudoClassParser(content);
9
- const computed = (0, ml_spec_1.getComputedRole)(specs, el, (_a = aria.version) !== null && _a !== void 0 ? _a : '1.2');
10
- if (((_b = computed.role) === null || _b === void 0 ? void 0 : _b.name) === aria.role) {
7
+ const computed = getComputedRole(specs, el, aria.version ?? ARIA_RECOMMENDED_VERSION);
8
+ if (computed.role?.name === aria.role) {
11
9
  return {
12
10
  specificity: [0, 1, 0],
13
11
  matched: true,
@@ -21,12 +19,14 @@ function ariaRolePseudoClass(specs) {
21
19
  };
22
20
  };
23
21
  }
24
- exports.ariaRolePseudoClass = ariaRolePseudoClass;
25
22
  function ariaPseudoClassParser(syntax) {
26
23
  const [roleName, _version] = syntax.split('|');
27
- const version = _version === '1.1' ? '1.1' : '1.2';
24
+ const version = _version ?? ARIA_RECOMMENDED_VERSION;
25
+ if (!validateAriaVersion(version)) {
26
+ throw new SyntaxError(`Unsupported ARIA version: ${version}`);
27
+ }
28
28
  return {
29
- role: roleName.trim().toLowerCase(),
29
+ role: roleName?.trim().toLowerCase() ?? syntax.trim().toLowerCase(),
30
30
  version,
31
31
  };
32
32
  }
@@ -1,3 +1,3 @@
1
- import type { SelectorResult } from '../types';
1
+ import type { SelectorResult } from '../types.js';
2
2
  import type { MLMLSpec } from '@markuplint/ml-spec';
3
3
  export declare function contentModelPseudoClass(specs: MLMLSpec): (category: string) => (el: Element) => SelectorResult;
@@ -1,12 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.contentModelPseudoClass = void 0;
4
- const ml_spec_1 = require("@markuplint/ml-spec");
5
- const create_selector_1 = require("../create-selector");
6
- function contentModelPseudoClass(specs) {
7
- return (category) => (el) => {
1
+ import { contentModelCategoryToTagNames } from '@markuplint/ml-spec';
2
+ import { createSelector } from '../create-selector.js';
3
+ export function contentModelPseudoClass(specs) {
4
+ return (category) => (
5
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
6
+ el) => {
8
7
  category = category.trim().toLowerCase();
9
- const selectors = (0, ml_spec_1.contentModelCategoryToTagNames)(`#${category}`, specs.def);
8
+ const selectors = contentModelCategoryToTagNames(`#${category}`, specs.def);
10
9
  const matched = selectors
11
10
  .map(selector => {
12
11
  if (selector === '#custom') {
@@ -36,11 +35,11 @@ function contentModelPseudoClass(specs) {
36
35
  },
37
36
  ];
38
37
  }
39
- return (0, create_selector_1.createSelector)(selector, specs).search(el);
38
+ return createSelector(selector, specs).search(el);
40
39
  })
41
40
  .flat()
42
41
  .filter((m) => m.matched);
43
- if (matched.length) {
42
+ if (matched.length > 0) {
44
43
  return {
45
44
  specificity: [0, 1, 0],
46
45
  matched: true,
@@ -54,4 +53,3 @@ function contentModelPseudoClass(specs) {
54
53
  };
55
54
  };
56
55
  }
57
- exports.contentModelPseudoClass = contentModelPseudoClass;
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { compareSpecificity } from './compare-specificity';
2
- export { matchSelector } from './match-selector';
3
- export { createSelector } from './create-selector';
4
- export * from './types';
1
+ export { compareSpecificity } from './compare-specificity.js';
2
+ export { matchSelector } from './match-selector.js';
3
+ export { createSelector } from './create-selector.js';
4
+ export { InvalidSelectorError } from './invalid-selector-error.js';
5
+ export * from './types.js';
package/lib/index.js CHANGED
@@ -1,11 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createSelector = exports.matchSelector = exports.compareSpecificity = void 0;
4
- const tslib_1 = require("tslib");
5
- var compare_specificity_1 = require("./compare-specificity");
6
- Object.defineProperty(exports, "compareSpecificity", { enumerable: true, get: function () { return compare_specificity_1.compareSpecificity; } });
7
- var match_selector_1 = require("./match-selector");
8
- Object.defineProperty(exports, "matchSelector", { enumerable: true, get: function () { return match_selector_1.matchSelector; } });
9
- var create_selector_1 = require("./create-selector");
10
- Object.defineProperty(exports, "createSelector", { enumerable: true, get: function () { return create_selector_1.createSelector; } });
11
- tslib_1.__exportStar(require("./types"), exports);
1
+ export { compareSpecificity } from './compare-specificity.js';
2
+ export { matchSelector } from './match-selector.js';
3
+ export { createSelector } from './create-selector.js';
4
+ export { InvalidSelectorError } from './invalid-selector-error.js';
5
+ export * from './types.js';
@@ -1,3 +1,5 @@
1
1
  export declare class InvalidSelectorError extends Error {
2
2
  name: string;
3
+ selector: string;
4
+ constructor(selector: string, message?: string);
3
5
  }
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidSelectorError = void 0;
4
- class InvalidSelectorError extends Error {
5
- constructor() {
6
- super(...arguments);
1
+ export class InvalidSelectorError extends Error {
2
+ constructor(selector, message) {
3
+ super(message ?? `Invalid selector: "${selector}"`);
7
4
  this.name = 'InvalidSelectorError';
5
+ this.selector = selector;
8
6
  }
9
7
  }
10
- exports.InvalidSelectorError = InvalidSelectorError;
package/lib/is.js CHANGED
@@ -1,15 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPureHTMLElement = exports.isNonDocumentTypeChildNode = exports.isElement = void 0;
4
- function isElement(node) {
1
+ export function isElement(
2
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
3
+ node) {
5
4
  return node.nodeType === node.ELEMENT_NODE;
6
5
  }
7
- exports.isElement = isElement;
8
- function isNonDocumentTypeChildNode(node) {
6
+ export function isNonDocumentTypeChildNode(
7
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
+ node) {
9
9
  return 'previousElementSibling' in node && 'nextElementSibling' in node;
10
10
  }
11
- exports.isNonDocumentTypeChildNode = isNonDocumentTypeChildNode;
12
- function isPureHTMLElement(el) {
11
+ export function isPureHTMLElement(
12
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
13
+ el) {
13
14
  return el.localName !== el.nodeName;
14
15
  }
15
- exports.isPureHTMLElement = isPureHTMLElement;
@@ -1,13 +1,13 @@
1
- import type { Specificity, RegexSelector } from './types';
1
+ import type { Specificity, RegexSelector } from './types.js';
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 {};
@@ -1,21 +1,20 @@
1
- "use strict";
2
1
  var _SelectorTarget_combinedFrom, _SelectorTarget_selector;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.matchSelector = void 0;
5
- const tslib_1 = require("tslib");
6
- const is_1 = require("./is");
7
- const regex_selector_matches_1 = require("./regex-selector-matches");
8
- const selector_1 = require("./selector");
9
- function matchSelector(el, selector) {
10
- if (!selector) {
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
3
+ import { isElement, isNonDocumentTypeChildNode, isPureHTMLElement } from './is.js';
4
+ import { regexSelectorMatches } from './regex-selector-matches.js';
5
+ import { Selector } from './selector.js';
6
+ export function matchSelector(
7
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
+ el, selector) {
9
+ if (selector == null || selector === '') {
11
10
  return {
12
11
  matched: false,
13
12
  };
14
13
  }
15
14
  if (typeof selector === 'string') {
16
- const sel = new selector_1.Selector(selector);
15
+ const sel = new Selector(selector);
17
16
  const specificity = sel.match(el);
18
- if (specificity) {
17
+ if (specificity !== false) {
19
18
  return {
20
19
  matched: true,
21
20
  selector,
@@ -28,8 +27,9 @@ function matchSelector(el, selector) {
28
27
  }
29
28
  return regexSelect(el, selector);
30
29
  }
31
- exports.matchSelector = matchSelector;
32
- function regexSelect(el, selector) {
30
+ function regexSelect(
31
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
32
+ el, selector) {
33
33
  let edge = new SelectorTarget(selector);
34
34
  let edgeSelector = selector.combination;
35
35
  while (edgeSelector) {
@@ -44,23 +44,25 @@ class SelectorTarget {
44
44
  constructor(selector) {
45
45
  _SelectorTarget_combinedFrom.set(this, null);
46
46
  _SelectorTarget_selector.set(this, void 0);
47
- tslib_1.__classPrivateFieldSet(this, _SelectorTarget_selector, selector, "f");
47
+ __classPrivateFieldSet(this, _SelectorTarget_selector, selector, "f");
48
48
  }
49
49
  from(target, combinator) {
50
- tslib_1.__classPrivateFieldSet(this, _SelectorTarget_combinedFrom, { target, combinator }, "f");
50
+ __classPrivateFieldSet(this, _SelectorTarget_combinedFrom, { target, combinator }, "f");
51
51
  }
52
- match(el) {
52
+ match(
53
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
54
+ el) {
53
55
  const unitCheck = this._matchWithoutCombineChecking(el);
54
56
  if (!unitCheck.matched) {
55
57
  return unitCheck;
56
58
  }
57
- if (!tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) {
59
+ if (!__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) {
58
60
  return unitCheck;
59
61
  }
60
- if (!(0, is_1.isNonDocumentTypeChildNode)(el)) {
62
+ if (!isNonDocumentTypeChildNode(el)) {
61
63
  return unitCheck;
62
64
  }
63
- const { target, combinator } = tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f");
65
+ const { target, combinator } = __classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f");
64
66
  switch (combinator) {
65
67
  // Descendant combinator
66
68
  case ' ': {
@@ -135,17 +137,21 @@ class SelectorTarget {
135
137
  return { matched: false };
136
138
  }
137
139
  default: {
138
- throw new Error(`Unsupported ${tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f").combinator} combinator in selector`);
140
+ throw new Error(`Unsupported ${__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f").combinator} combinator in selector`);
139
141
  }
140
142
  }
141
143
  }
142
- _matchWithoutCombineChecking(el) {
143
- return uncombinedRegexSelect(el, tslib_1.__classPrivateFieldGet(this, _SelectorTarget_selector, "f"));
144
+ _matchWithoutCombineChecking(
145
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
146
+ el) {
147
+ return uncombinedRegexSelect(el, __classPrivateFieldGet(this, _SelectorTarget_selector, "f"));
144
148
  }
145
149
  }
146
150
  _SelectorTarget_combinedFrom = new WeakMap(), _SelectorTarget_selector = new WeakMap();
147
- function uncombinedRegexSelect(el, selector) {
148
- if (!(0, is_1.isElement)(el)) {
151
+ function uncombinedRegexSelect(
152
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
153
+ el, selector) {
154
+ if (!isElement(el)) {
149
155
  return {
150
156
  matched: false,
151
157
  };
@@ -156,7 +162,7 @@ function uncombinedRegexSelect(el, selector) {
156
162
  const specificity = [0, 0, 0];
157
163
  const specifiedAttr = new Map();
158
164
  if (selector.nodeName) {
159
- const matchedNodeName = (0, regex_selector_matches_1.regexSelectorMatches)(selector.nodeName, el.localName, (0, is_1.isPureHTMLElement)(el));
165
+ const matchedNodeName = regexSelectorMatches(selector.nodeName, el.localName, isPureHTMLElement(el));
160
166
  if (matchedNodeName) {
161
167
  delete matchedNodeName.$0;
162
168
  }
@@ -174,7 +180,7 @@ function uncombinedRegexSelect(el, selector) {
174
180
  const selectorAttrName = selector.attrName;
175
181
  const matchedAttrNameList = Array.from(el.attributes).map(attr => {
176
182
  const attrName = attr.name;
177
- const matchedAttrName = (0, regex_selector_matches_1.regexSelectorMatches)(selectorAttrName, attrName, (0, is_1.isPureHTMLElement)(el));
183
+ const matchedAttrName = regexSelectorMatches(selectorAttrName, attrName, isPureHTMLElement(el));
178
184
  if (matchedAttrName) {
179
185
  delete matchedAttrName.$0;
180
186
  data = {
@@ -194,7 +200,7 @@ function uncombinedRegexSelect(el, selector) {
194
200
  const matchedAttrValueList = Array.from(el.attributes).map(attr => {
195
201
  const attrName = attr.name;
196
202
  const attrValue = attr.value;
197
- const matchedAttrValue = (0, regex_selector_matches_1.regexSelectorMatches)(selectorAttrValue, attrValue, (0, is_1.isPureHTMLElement)(el));
203
+ const matchedAttrValue = regexSelectorMatches(selectorAttrValue, attrValue, isPureHTMLElement(el));
198
204
  if (matchedAttrValue) {
199
205
  delete matchedAttrValue.$0;
200
206
  data = {
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.regexSelectorMatches = void 0;
4
- function regexSelectorMatches(reg, raw, ignoreCase) {
1
+ export function regexSelectorMatches(reg, raw, ignoreCase) {
5
2
  const res = {};
6
3
  const pattern = toRegexp(reg);
7
4
  const regex = new RegExp(pattern instanceof RegExp ? pattern : `^${pattern.trim()}$`, ignoreCase ? 'i' : undefined);
@@ -15,10 +12,9 @@ function regexSelectorMatches(reg, raw, ignoreCase) {
15
12
  ...matched.groups,
16
13
  };
17
14
  }
18
- exports.regexSelectorMatches = regexSelectorMatches;
19
15
  function toRegexp(pattern) {
20
16
  const matched = pattern.match(/^\/(.+)\/([ig]*)$/i);
21
- if (matched) {
17
+ if (matched && matched[1]) {
22
18
  return new RegExp(matched[1], matched[2]);
23
19
  }
24
20
  return pattern;
package/lib/selector.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { SelectorResult, Specificity } from './types';
2
- type ExtendedPseudoClass = Record<string, (content: string) => (el: Element) => SelectorResult>;
1
+ import type { SelectorResult, Specificity } from './types.js';
2
+ type ExtendedPseudoClass = Readonly<Record<string, (content: string) => (el: Element) => SelectorResult>>;
3
3
  export declare class Selector {
4
4
  #private;
5
5
  constructor(selector: string, extended?: ExtendedPseudoClass);