@markuplint/selector 3.11.0 → 4.0.0-alpha.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.
@@ -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';
2
+ import { Selector } from './selector.js';
3
3
  export declare function createSelector(selector: string, specs?: MLMLSpec): Selector;
@@ -1,24 +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, specs
11
+ instance = new Selector(selector, specs
15
12
  ? {
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),
13
+ model: contentModelPseudoClass(specs),
14
+ aria: ariaPseudoClass(),
15
+ role: ariaRolePseudoClass(specs),
19
16
  }
20
17
  : undefined);
21
18
  caches.set(selector, instance);
22
19
  return instance;
23
20
  }
24
- 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,16 +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() {
5
+ export function ariaPseudoClass() {
9
6
  return (content) => (
10
7
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
8
  el) => {
12
9
  const aria = ariaPseudoClassParser(content);
13
- const name = (0, ml_spec_1.getAccname)(el);
10
+ const name = getAccname(el);
14
11
  switch (aria.type) {
15
12
  case 'hasName': {
16
13
  if (name) {
@@ -43,12 +40,11 @@ function ariaPseudoClass() {
43
40
  }
44
41
  };
45
42
  }
46
- exports.ariaPseudoClass = ariaPseudoClass;
47
43
  function ariaPseudoClassParser(syntax) {
48
44
  const [_query, _version] = syntax.split('|');
49
- const query = _query === null || _query === void 0 ? void 0 : _query.replace(/\s+/g, '').toLowerCase();
50
- const version = _version !== null && _version !== void 0 ? _version : ml_spec_1.ARIA_RECOMMENDED_VERSION;
51
- if (!(0, ml_spec_1.validateAriaVersion)(version)) {
45
+ const query = _query?.replace(/\s+/g, '').toLowerCase();
46
+ const version = _version ?? ARIA_RECOMMENDED_VERSION;
47
+ if (!validateAriaVersion(version)) {
52
48
  throw new SyntaxError(`Unsupported ARIA version: ${version}`);
53
49
  }
54
50
  switch (query) {
@@ -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,15 +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) {
1
+ import { validateAriaVersion, ARIA_RECOMMENDED_VERSION, getComputedRole } from '@markuplint/ml-spec';
2
+ export function ariaRolePseudoClass(specs) {
6
3
  return (content) => (
7
4
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
5
  el) => {
9
- var _a, _b;
10
6
  const aria = ariaPseudoClassParser(content);
11
- const computed = (0, ml_spec_1.getComputedRole)(specs, el, (_a = aria.version) !== null && _a !== void 0 ? _a : ml_spec_1.ARIA_RECOMMENDED_VERSION);
12
- 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) {
13
9
  return {
14
10
  specificity: [0, 1, 0],
15
11
  matched: true,
@@ -23,16 +19,14 @@ function ariaRolePseudoClass(specs) {
23
19
  };
24
20
  };
25
21
  }
26
- exports.ariaRolePseudoClass = ariaRolePseudoClass;
27
22
  function ariaPseudoClassParser(syntax) {
28
- var _a;
29
23
  const [roleName, _version] = syntax.split('|');
30
- const version = _version !== null && _version !== void 0 ? _version : ml_spec_1.ARIA_RECOMMENDED_VERSION;
31
- if (!(0, ml_spec_1.validateAriaVersion)(version)) {
24
+ const version = _version ?? ARIA_RECOMMENDED_VERSION;
25
+ if (!validateAriaVersion(version)) {
32
26
  throw new SyntaxError(`Unsupported ARIA version: ${version}`);
33
27
  }
34
28
  return {
35
- role: (_a = roleName === null || roleName === void 0 ? void 0 : roleName.trim().toLowerCase()) !== null && _a !== void 0 ? _a : syntax.trim().toLowerCase(),
29
+ role: roleName?.trim().toLowerCase() ?? syntax.trim().toLowerCase(),
36
30
  version,
37
31
  };
38
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,14 +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) {
1
+ import { contentModelCategoryToTagNames } from '@markuplint/ml-spec';
2
+ import { createSelector } from '../create-selector.js';
3
+ export function contentModelPseudoClass(specs) {
7
4
  return (category) => (
8
5
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
9
6
  el) => {
10
7
  category = category.trim().toLowerCase();
11
- const selectors = (0, ml_spec_1.contentModelCategoryToTagNames)(`#${category}`, specs.def);
8
+ const selectors = contentModelCategoryToTagNames(`#${category}`, specs.def);
12
9
  const matched = selectors
13
10
  .map(selector => {
14
11
  if (selector === '#custom') {
@@ -38,7 +35,7 @@ function contentModelPseudoClass(specs) {
38
35
  },
39
36
  ];
40
37
  }
41
- return (0, create_selector_1.createSelector)(selector, specs).search(el);
38
+ return createSelector(selector, specs).search(el);
42
39
  })
43
40
  .flat()
44
41
  .filter((m) => m.matched);
@@ -56,4 +53,3 @@ function contentModelPseudoClass(specs) {
56
53
  };
57
54
  };
58
55
  }
59
- exports.contentModelPseudoClass = contentModelPseudoClass;
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { compareSpecificity } from './compare-specificity';
2
- export { matchSelector } from './match-selector';
3
- export { createSelector } from './create-selector';
4
- export { InvalidSelectorError } from './invalid-selector-error';
5
- 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,13 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidSelectorError = 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
- var invalid_selector_error_1 = require("./invalid-selector-error");
12
- Object.defineProperty(exports, "InvalidSelectorError", { enumerable: true, get: function () { return invalid_selector_error_1.InvalidSelectorError; } });
13
- 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,11 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InvalidSelectorError = void 0;
4
- class InvalidSelectorError extends Error {
1
+ export class InvalidSelectorError extends Error {
5
2
  constructor(selector, message) {
6
- super(message !== null && message !== void 0 ? message : `Invalid selector: "${selector}"`);
3
+ super(message ?? `Invalid selector: "${selector}"`);
7
4
  this.name = 'InvalidSelectorError';
8
5
  this.selector = selector;
9
6
  }
10
7
  }
11
- exports.InvalidSelectorError = InvalidSelectorError;
package/lib/is.js CHANGED
@@ -1,21 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isPureHTMLElement = exports.isNonDocumentTypeChildNode = exports.isElement = void 0;
4
- function isElement(
1
+ export function isElement(
5
2
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
6
3
  node) {
7
4
  return node.nodeType === node.ELEMENT_NODE;
8
5
  }
9
- exports.isElement = isElement;
10
- function isNonDocumentTypeChildNode(
6
+ export function isNonDocumentTypeChildNode(
11
7
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
12
8
  node) {
13
9
  return 'previousElementSibling' in node && 'nextElementSibling' in node;
14
10
  }
15
- exports.isNonDocumentTypeChildNode = isNonDocumentTypeChildNode;
16
- function isPureHTMLElement(
11
+ export function isPureHTMLElement(
17
12
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
18
13
  el) {
19
14
  return el.localName !== el.nodeName;
20
15
  }
21
- exports.isPureHTMLElement = isPureHTMLElement;
@@ -1,4 +1,4 @@
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
4
  readonly matched: true;
@@ -1,12 +1,9 @@
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(
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(
10
7
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
8
  el, selector) {
12
9
  if (selector == null || selector === '') {
@@ -15,7 +12,7 @@ el, selector) {
15
12
  };
16
13
  }
17
14
  if (typeof selector === 'string') {
18
- const sel = new selector_1.Selector(selector);
15
+ const sel = new Selector(selector);
19
16
  const specificity = sel.match(el);
20
17
  if (specificity !== false) {
21
18
  return {
@@ -30,7 +27,6 @@ el, selector) {
30
27
  }
31
28
  return regexSelect(el, selector);
32
29
  }
33
- exports.matchSelector = matchSelector;
34
30
  function regexSelect(
35
31
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
36
32
  el, selector) {
@@ -48,10 +44,10 @@ class SelectorTarget {
48
44
  constructor(selector) {
49
45
  _SelectorTarget_combinedFrom.set(this, null);
50
46
  _SelectorTarget_selector.set(this, void 0);
51
- tslib_1.__classPrivateFieldSet(this, _SelectorTarget_selector, selector, "f");
47
+ __classPrivateFieldSet(this, _SelectorTarget_selector, selector, "f");
52
48
  }
53
49
  from(target, combinator) {
54
- tslib_1.__classPrivateFieldSet(this, _SelectorTarget_combinedFrom, { target, combinator }, "f");
50
+ __classPrivateFieldSet(this, _SelectorTarget_combinedFrom, { target, combinator }, "f");
55
51
  }
56
52
  match(
57
53
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
@@ -60,13 +56,13 @@ class SelectorTarget {
60
56
  if (!unitCheck.matched) {
61
57
  return unitCheck;
62
58
  }
63
- if (!tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) {
59
+ if (!__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f")) {
64
60
  return unitCheck;
65
61
  }
66
- if (!(0, is_1.isNonDocumentTypeChildNode)(el)) {
62
+ if (!isNonDocumentTypeChildNode(el)) {
67
63
  return unitCheck;
68
64
  }
69
- const { target, combinator } = tslib_1.__classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f");
65
+ const { target, combinator } = __classPrivateFieldGet(this, _SelectorTarget_combinedFrom, "f");
70
66
  switch (combinator) {
71
67
  // Descendant combinator
72
68
  case ' ': {
@@ -141,21 +137,21 @@ class SelectorTarget {
141
137
  return { matched: false };
142
138
  }
143
139
  default: {
144
- 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`);
145
141
  }
146
142
  }
147
143
  }
148
144
  _matchWithoutCombineChecking(
149
145
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
150
146
  el) {
151
- return uncombinedRegexSelect(el, tslib_1.__classPrivateFieldGet(this, _SelectorTarget_selector, "f"));
147
+ return uncombinedRegexSelect(el, __classPrivateFieldGet(this, _SelectorTarget_selector, "f"));
152
148
  }
153
149
  }
154
150
  _SelectorTarget_combinedFrom = new WeakMap(), _SelectorTarget_selector = new WeakMap();
155
151
  function uncombinedRegexSelect(
156
152
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
157
153
  el, selector) {
158
- if (!(0, is_1.isElement)(el)) {
154
+ if (!isElement(el)) {
159
155
  return {
160
156
  matched: false,
161
157
  };
@@ -166,7 +162,7 @@ el, selector) {
166
162
  const specificity = [0, 0, 0];
167
163
  const specifiedAttr = new Map();
168
164
  if (selector.nodeName) {
169
- 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));
170
166
  if (matchedNodeName) {
171
167
  delete matchedNodeName.$0;
172
168
  }
@@ -184,7 +180,7 @@ el, selector) {
184
180
  const selectorAttrName = selector.attrName;
185
181
  const matchedAttrNameList = Array.from(el.attributes).map(attr => {
186
182
  const attrName = attr.name;
187
- const matchedAttrName = (0, regex_selector_matches_1.regexSelectorMatches)(selectorAttrName, attrName, (0, is_1.isPureHTMLElement)(el));
183
+ const matchedAttrName = regexSelectorMatches(selectorAttrName, attrName, isPureHTMLElement(el));
188
184
  if (matchedAttrName) {
189
185
  delete matchedAttrName.$0;
190
186
  data = {
@@ -204,7 +200,7 @@ el, selector) {
204
200
  const matchedAttrValueList = Array.from(el.attributes).map(attr => {
205
201
  const attrName = attr.name;
206
202
  const attrValue = attr.value;
207
- const matchedAttrValue = (0, regex_selector_matches_1.regexSelectorMatches)(selectorAttrValue, attrValue, (0, is_1.isPureHTMLElement)(el));
203
+ const matchedAttrValue = regexSelectorMatches(selectorAttrValue, attrValue, isPureHTMLElement(el));
208
204
  if (matchedAttrValue) {
209
205
  delete matchedAttrValue.$0;
210
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,7 +12,6 @@ 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
17
  if (matched && matched[1]) {
package/lib/selector.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SelectorResult, Specificity } from './types';
1
+ import type { SelectorResult, Specificity } from './types.js';
2
2
  type ExtendedPseudoClass = Readonly<Record<string, (content: string) => (el: Element) => SelectorResult>>;
3
3
  export declare class Selector {
4
4
  #private;