@markuplint/rules 3.0.0-alpha.66 → 3.0.0-dev.24

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.
Files changed (129) hide show
  1. package/README.md +10 -44
  2. package/lib/attr-check.d.ts +2 -2
  3. package/lib/attr-check.js +1 -1
  4. package/lib/attr-duplication/index.js +1 -1
  5. package/lib/attr-value-quotes/index.d.ts +3 -3
  6. package/lib/attr-value-quotes/index.js +1 -1
  7. package/lib/case-sensitive-attr-name/index.d.ts +1 -1
  8. package/lib/case-sensitive-attr-name/index.js +3 -3
  9. package/lib/case-sensitive-tag-name/index.d.ts +1 -1
  10. package/lib/case-sensitive-tag-name/index.js +3 -3
  11. package/lib/character-reference/index.js +1 -1
  12. package/lib/class-naming/index.d.ts +1 -1
  13. package/lib/class-naming/index.js +2 -2
  14. package/lib/create-message.d.ts +1 -1
  15. package/lib/create-message.js +5 -5
  16. package/lib/debug.d.ts +1 -1
  17. package/lib/deprecated-element/index.js +2 -2
  18. package/lib/doctype/index.d.ts +2 -2
  19. package/lib/doctype/index.js +2 -2
  20. package/lib/end-tag/index.js +3 -18
  21. package/lib/helpers.d.ts +8 -32
  22. package/lib/helpers.js +17 -81
  23. package/lib/index.d.ts +2 -62
  24. package/lib/index.js +4 -0
  25. package/lib/ineffective-attr/index.js +1 -1
  26. package/lib/invalid-attr/index.d.ts +3 -2
  27. package/lib/invalid-attr/index.js +19 -14
  28. package/lib/label-has-control/index.d.ts +2 -0
  29. package/lib/label-has-control/index.js +28 -0
  30. package/lib/landmark-roles/index.d.ts +3 -3
  31. package/lib/landmark-roles/index.js +3 -3
  32. package/lib/no-boolean-attr-value/index.js +1 -1
  33. package/lib/no-default-value/index.js +1 -1
  34. package/lib/no-empty-palpable-content/index.d.ts +6 -0
  35. package/lib/no-empty-palpable-content/index.js +34 -0
  36. package/lib/no-hard-code-id/index.js +1 -1
  37. package/lib/no-refer-to-non-existent-id/index.js +1 -1
  38. package/lib/no-use-event-handler-attr/index.d.ts +1 -1
  39. package/lib/no-use-event-handler-attr/index.js +6 -6
  40. package/lib/permitted-contents/choice.d.ts +3 -0
  41. package/lib/permitted-contents/choice.js +37 -0
  42. package/lib/permitted-contents/complex-branch.d.ts +13 -0
  43. package/lib/permitted-contents/complex-branch.js +27 -0
  44. package/lib/permitted-contents/content-model.d.ts +2 -0
  45. package/lib/permitted-contents/content-model.js +49 -0
  46. package/lib/permitted-contents/count-pattern.d.ts +13 -0
  47. package/lib/permitted-contents/count-pattern.js +155 -0
  48. package/lib/permitted-contents/debug.browser.d.ts +30 -0
  49. package/lib/permitted-contents/debug.browser.js +14 -0
  50. package/lib/permitted-contents/debug.d.ts +3 -0
  51. package/lib/permitted-contents/debug.js +13 -0
  52. package/lib/permitted-contents/index.d.ts +1 -7
  53. package/lib/permitted-contents/index.js +76 -246
  54. package/lib/permitted-contents/matches-selector.d.ts +3 -0
  55. package/lib/permitted-contents/matches-selector.js +185 -0
  56. package/lib/permitted-contents/order.d.ts +13 -0
  57. package/lib/permitted-contents/order.js +89 -0
  58. package/lib/permitted-contents/recursive-branch.d.ts +4 -0
  59. package/lib/permitted-contents/recursive-branch.js +43 -0
  60. package/lib/permitted-contents/represent-transparent-nodes.d.ts +6 -0
  61. package/lib/permitted-contents/represent-transparent-nodes.js +76 -0
  62. package/lib/permitted-contents/start.d.ts +12 -0
  63. package/lib/permitted-contents/start.js +71 -0
  64. package/lib/permitted-contents/transparent.d.ts +2 -0
  65. package/lib/permitted-contents/transparent.js +29 -0
  66. package/lib/permitted-contents/types.d.ts +45 -0
  67. package/lib/permitted-contents/types.js +2 -0
  68. package/lib/permitted-contents/utils.d.ts +32 -0
  69. package/lib/permitted-contents/utils.js +184 -0
  70. package/lib/require-accessible-name/index.d.ts +1 -1
  71. package/lib/require-accessible-name/index.js +11 -3
  72. package/lib/required-attr/index.d.ts +2 -2
  73. package/lib/required-attr/index.js +1 -1
  74. package/lib/required-element/index.d.ts +1 -1
  75. package/lib/required-element/index.js +1 -1
  76. package/lib/required-h1/index.js +2 -2
  77. package/lib/use-list/index.d.ts +2 -2
  78. package/lib/use-list/index.js +3 -3
  79. package/lib/wai-aria/checkings/abstract-role.js +1 -1
  80. package/lib/wai-aria/checkings/implicit-role.js +2 -2
  81. package/lib/wai-aria/checkings/interaction-in-hidden.d.ts +11 -0
  82. package/lib/wai-aria/checkings/interaction-in-hidden.js +47 -0
  83. package/lib/wai-aria/checkings/{non-existant-role.d.ts → non-existent-role.d.ts} +1 -1
  84. package/lib/wai-aria/checkings/{non-existant-role.js → non-existent-role.js} +4 -4
  85. package/lib/wai-aria/checkings/permitted-roles.js +1 -1
  86. package/lib/wai-aria/checkings/presentational-children.d.ts +29 -0
  87. package/lib/wai-aria/checkings/presentational-children.js +60 -0
  88. package/lib/wai-aria/checkings/{checking-required-owned-elements.d.ts → required-owned-elements.d.ts} +0 -0
  89. package/lib/wai-aria/checkings/required-owned-elements.js +101 -0
  90. package/lib/wai-aria/checkings/value.d.ts +6 -0
  91. package/lib/wai-aria/checkings/value.js +74 -4
  92. package/lib/wai-aria/index.js +25 -15
  93. package/lib/wai-aria/types.d.ts +3 -1
  94. package/package.json +12 -8
  95. package/schema.json +6 -0
  96. package/tsconfig.tsbuildinfo +1 -1
  97. package/lib/__foo/index.d.ts +0 -2
  98. package/lib/__foo/index.js +0 -46
  99. package/lib/attr-equal-space-after/index.d.ts +0 -3
  100. package/lib/attr-equal-space-after/index.js +0 -90
  101. package/lib/attr-equal-space-before/index.d.ts +0 -3
  102. package/lib/attr-equal-space-before/index.js +0 -90
  103. package/lib/attr-spacing/index.d.ts +0 -6
  104. package/lib/attr-spacing/index.js +0 -100
  105. package/lib/helper/aria/get-aria.d.ts +0 -7
  106. package/lib/helper/aria/get-aria.js +0 -53
  107. package/lib/helper/aria/get-computed-role.d.ts +0 -10
  108. package/lib/helper/aria/get-computed-role.js +0 -24
  109. package/lib/helper/aria/get-implicit-role.d.ts +0 -7
  110. package/lib/helper/aria/get-implicit-role.js +0 -12
  111. package/lib/helper/aria/get-permitted-roles.d.ts +0 -14
  112. package/lib/helper/aria/get-permitted-roles.js +0 -26
  113. package/lib/helper/aria/resolve-version.d.ts +0 -2
  114. package/lib/helper/aria/resolve-version.js +0 -21
  115. package/lib/helper/spec/aria-spec.d.ts +0 -5
  116. package/lib/helper/spec/aria-spec.js +0 -12
  117. package/lib/helper/spec/get-role-spec.d.ts +0 -11
  118. package/lib/helper/spec/get-role-spec.js +0 -39
  119. package/lib/helper/spec/html-spec.d.ts +0 -2
  120. package/lib/helper/spec/html-spec.js +0 -14
  121. package/lib/indentation/index.d.ts +0 -7
  122. package/lib/indentation/index.js +0 -208
  123. package/lib/permitted-contents/array.combination.d.ts +0 -1
  124. package/lib/permitted-contents/array.combination.js +0 -33
  125. package/lib/permitted-contents/permitted-content.spec-to-regexp.d.ts +0 -11
  126. package/lib/permitted-contents/permitted-content.spec-to-regexp.js +0 -230
  127. package/lib/permitted-contents/unfold-content-models-to-tags.d.ts +0 -2
  128. package/lib/permitted-contents/unfold-content-models-to-tags.js +0 -8
  129. package/lib/wai-aria/checkings/checking-required-owned-elements.js +0 -63
@@ -1,9 +1,3 @@
1
- import type { ContentModel } from '@markuplint/ml-spec';
2
- declare type TagRule = ContentModel & {
3
- tag: string;
4
- };
5
- declare type Options = {
6
- ignoreHasMutableChildren: boolean;
7
- };
1
+ import type { Options, TagRule } from './types';
8
2
  declare const _default: import("@markuplint/ml-core").RuleSeed<TagRule[], Options>;
9
3
  export default _default;
@@ -1,281 +1,111 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
3
  const ml_core_1 = require("@markuplint/ml-core");
5
- const ml_spec_1 = require("@markuplint/ml-spec");
6
- const permitted_content_spec_to_regexp_1 = tslib_1.__importDefault(require("./permitted-content.spec-to-regexp"));
7
- const expMapOnNodeId = new Map();
4
+ const content_model_1 = require("./content-model");
5
+ const represent_transparent_nodes_1 = require("./represent-transparent-nodes");
8
6
  exports.default = (0, ml_core_1.createRule)({
9
7
  defaultValue: [],
10
8
  defaultOptions: {
11
9
  ignoreHasMutableChildren: true,
12
10
  },
13
11
  async verify({ document, report, t }) {
14
- let idCounter = 0;
15
12
  await document.walkOn('Element', el => {
16
- var _a;
13
+ var _a, _b, _c;
17
14
  if (!el.rule.value) {
18
15
  return;
19
16
  }
20
- if (el.rule.option.ignoreHasMutableChildren && el.hasMutableChildren()) {
17
+ if (el.rule.options.ignoreHasMutableChildren && el.hasMutableChildren()) {
21
18
  return;
22
19
  }
23
- const specType = el.namespaceURI === 'http://www.w3.org/1999/xhtml'
24
- ? 'HTML'
25
- : el.namespaceURI === 'http://www.w3.org/2000/svg'
26
- ? 'SVG'
27
- : 'Any Language';
28
- const childNodes = el.getChildElementsAndTextNodeWithoutWhitespaces();
29
- const spec = !el.isCustomElement && ((_a = (0, ml_core_1.getSpec)(el, document.specs)) === null || _a === void 0 ? void 0 : _a.contentModel);
30
- const expGen = new permitted_content_spec_to_regexp_1.default(document.specs, idCounter++);
31
- if (spec) {
32
- if (spec.descendantOf && !el.closest(spec.descendantOf)) {
33
- report({
34
- scope: el,
35
- message: t('{0} must be {1}', t('the "{0*}" {1}', el.localName, 'element'), t('{0} of {1}', 'descendant', t('the "{0*}" {1}', spec.descendantOf, 'element'))),
36
- });
37
- return;
38
- }
39
- let matched = false;
40
- if (spec.conditional) {
41
- for (const conditional of spec.conditional) {
42
- matched = el.matches(conditional.condition);
43
- // console.log({ ...conditional, matched });
44
- if (matched) {
45
- try {
46
- const parentExp = getRegExpFromParentNode(document.specs, el, expGen);
47
- const exp = expGen.specToRegExp(conditional.contents, parentExp, el.namespaceURI);
48
- const conditionalResult = match(exp, childNodes, false, document.specs);
49
- if (!conditionalResult) {
50
- const message = t('{0} is {1:c}', t('{0} of {1}', t('the {0}', 'content'), t('the "{0*}" {1}', el.localName, 'element')), 'invalid');
51
- report({
52
- scope: el,
53
- message: specType !== 'Any Language'
54
- ? t('{0} according to {1}', message, `the ${specType} specification`)
55
- : message,
56
- line: el.startLine,
57
- col: el.startCol,
58
- raw: el.raw,
59
- });
60
- break;
61
- }
62
- }
63
- catch (e) {
64
- if (e instanceof Error) {
65
- // eslint-disable-next-line no-console
66
- console.warn(el.raw, 'conditional', conditional, e.message);
67
- }
68
- else {
69
- throw e;
70
- }
71
- }
72
- }
20
+ const results = (0, content_model_1.contentModel)(el, el.rule.value, el.rule.options);
21
+ for (const { type, scope, query, hint } of results) {
22
+ let message = '';
23
+ if (hint) {
24
+ if (hint.max != null) {
25
+ message =
26
+ t('there is more content than it needs') +
27
+ t('. ') +
28
+ t('the max number of elements required is {0}', `${hint.max}`);
73
29
  }
74
30
  }
75
- if (!matched) {
76
- try {
77
- const exp = getRegExpFromNode(document.specs, el, expGen);
78
- const specResult = match(exp, childNodes, false, document.specs);
79
- if (!specResult) {
80
- const message = t('{0} is {1:c}', t('{0} of {1}', t('the {0}', 'content'), t('the "{0*}" {1}', el.localName, 'element')), 'invalid');
81
- report({
82
- scope: el,
83
- message: specType !== 'Any Language'
84
- ? t('{0} according to {1}', message, `the ${specType} specification`)
85
- : message,
86
- });
87
- }
31
+ switch (type) {
32
+ case 'MATCHED':
33
+ case 'MATCHED_ZERO': {
34
+ break;
88
35
  }
89
- catch (e) {
90
- if (e instanceof Error) {
91
- // eslint-disable-next-line no-console
92
- console.warn(el.raw, 'HTML Spec', e.message);
93
- }
94
- else {
95
- throw e;
96
- }
36
+ case 'MISSING_NODE_ONE_OR_MORE': {
37
+ message =
38
+ message ||
39
+ t('Require {0}', t('one or more elements')) + t('. ') + '(' + t('Need "{0*}"', query) + ')';
40
+ report({
41
+ scope,
42
+ message,
43
+ });
44
+ break;
97
45
  }
98
- }
99
- }
100
- for (const rule of el.rule.value) {
101
- if (rule.tag.toLowerCase() !== el.localName.toLowerCase()) {
102
- continue;
103
- }
104
- const parentExp = getRegExpFromParentNode(document.specs, el, expGen);
105
- try {
106
- const exp = expGen.specToRegExp(rule.contents, parentExp, el.namespaceURI);
107
- // Evaluate the custom element if the optional schema.
108
- const r = match(exp, childNodes, el.isCustomElement, document.specs);
109
- if (!r) {
46
+ case 'MISSING_NODE_REQUIRED': {
47
+ message =
48
+ message ||
49
+ t('Require {0}', t('an {0}', 'element')) + t('. ') + '(' + t('Need "{0*}"', query) + ')';
110
50
  report({
111
- scope: el,
112
- message: t('{0} is {1:c}', t('{0} of {1}', t('the {0}', 'content'), t('the "{0*}" {1}', el.localName, 'element')), 'invalid'),
113
- line: el.startLine,
114
- col: el.startCol,
115
- raw: el.raw,
51
+ scope,
52
+ message,
116
53
  });
117
- return;
54
+ break;
118
55
  }
119
- }
120
- catch (e) {
121
- if (e instanceof Error) {
122
- // eslint-disable-next-line no-console
123
- console.warn(el.raw, 'rule', rule, e.message);
56
+ case 'UNEXPECTED_EXTRA_NODE': {
57
+ const not = (_a = hint.not) !== null && _a !== void 0 ? _a : scope;
58
+ message =
59
+ message ||
60
+ (represent_transparent_nodes_1.transparentMode.has(scope)
61
+ ? t('{0} is not allowed in {1} through the transparent model in this context', name(not, t), name(el, t))
62
+ : t('{0} is not allowed in {1} in this context', name(not, t), name(el, t)));
63
+ report({
64
+ scope: not,
65
+ message,
66
+ });
67
+ break;
68
+ }
69
+ case 'TRANSPARENT_MODEL_DISALLOWS': {
70
+ const not = (_b = hint.not) !== null && _b !== void 0 ? _b : scope;
71
+ const tp = (_c = hint.transparent) !== null && _c !== void 0 ? _c : el;
72
+ report({
73
+ scope: not,
74
+ message: t('{0} is {1} but {2}', name(tp, t), t('a {0}', 'transparent model'), t('also disallows {0} in this context', name(not, t))),
75
+ });
76
+ break;
77
+ }
78
+ case 'NOTHING': {
79
+ report({
80
+ scope: el,
81
+ message: t('{0} disallows {1}', t('the {0}', 'element'), 'contents'),
82
+ });
83
+ break;
124
84
  }
125
- else {
126
- throw e;
85
+ default: {
86
+ throw new Error('Unreachable code');
127
87
  }
128
88
  }
129
89
  }
90
+ represent_transparent_nodes_1.transparentMode.clear();
130
91
  });
131
- expMapOnNodeId.clear();
132
92
  },
133
93
  });
134
- function normalization(el, evalCustomElement) {
135
- return el
136
- .map(node => {
137
- // FIXME: https://github.com/markuplint/markuplint/issues/388
138
- if (!evalCustomElement && node.is(node.ELEMENT_NODE) && node.isCustomElement) {
139
- return '';
140
- }
141
- if (!node.is(node.ELEMENT_NODE)) {
142
- return '<#text>';
143
- }
144
- const { localNameWithNS } = (0, ml_core_1.resolveNamespace)(node.localName, node.namespaceURI);
145
- return `<${localNameWithNS}>`;
146
- })
147
- .join('');
148
- }
149
- function getRegExpFromNode(specs, el, expGen) {
150
- var _a;
151
- // console.log({ n: node.nodeName });
152
- if (expMapOnNodeId.has(el.uuid)) {
153
- return expMapOnNodeId.get(el.uuid);
94
+ function name(scope, t) {
95
+ if (scope.is(scope.ELEMENT_NODE)) {
96
+ return t('the "{0}" {1}', scope.localName, 'element');
154
97
  }
155
- const parentExp = el.parentNode ? getRegExpFromNode(specs, el.parentNode, expGen) : null;
156
- const spec = el.nodeType === el.ELEMENT_NODE && !el.isCustomElement && ((_a = (0, ml_core_1.getSpec)(el, specs)) === null || _a === void 0 ? void 0 : _a.contentModel);
157
- const contentRule = spec ? spec.contents : true;
158
- const exp = expGen.specToRegExp(contentRule, parentExp, el.nodeType === el.ELEMENT_NODE ? el.namespaceURI : undefined);
159
- expMapOnNodeId.set(el.uuid, exp);
160
- return exp;
161
- }
162
- function getRegExpFromParentNode(specs, el, expGen) {
163
- // console.log({ p: node.nodeName });
164
- const parentExp = el.parentNode ? getRegExpFromNode(specs, el.parentNode, expGen) : null;
165
- return parentExp;
166
- }
167
- const matchingCacheMap = new Map();
168
- function match(exp, childNodes, evalCustomElement, specs) {
169
- const target = normalization(childNodes, evalCustomElement);
170
- const cacheKey = target +
171
- exp.source +
172
- childNodes.map(n => (n.is(n.ELEMENT_NODE) ? n.toNormalizeString() : n.originRaw)).join('');
173
- const res = matchingCacheMap.get(cacheKey);
174
- if (res != null) {
175
- return res;
98
+ if (scope.is(scope.TEXT_NODE)) {
99
+ return t('the {0}', 'text node');
176
100
  }
177
- const matched = _match(target, exp, childNodes, specs);
178
- matchingCacheMap.set(cacheKey, matched);
179
- return matched;
180
- }
181
- function _match(target, exp, childNodes, specs) {
182
- const result = exp.exec(target);
183
- if (!result) {
184
- return false;
101
+ if (scope.is(scope.CDATA_SECTION_NODE)) {
102
+ return t('the {0}', 'comment');
185
103
  }
186
- const capGroups = result.groups;
187
- // console.log({ exp, target, capGroups });
188
- if (!capGroups) {
189
- return true;
104
+ if (scope.is(scope.DOCUMENT_TYPE_NODE)) {
105
+ return t('the {0}', 'doctype');
190
106
  }
191
- const groupNames = Object.keys(capGroups);
192
- for (const groupName of groupNames) {
193
- const matched = capGroups[groupName];
194
- if (!matched) {
195
- continue;
196
- }
197
- const [type, , ..._selector] = groupName.split(/(?<=[a-z0-9])_/gi);
198
- // console.log({ type, _selector });
199
- switch (type) {
200
- case 'ACM': {
201
- const [model, tag] = _selector;
202
- const selectors = (0, ml_spec_1.contentModelCategoryToTagNames)(`#${model}`, specs).filter(selector => {
203
- const [, tagName] = /^([^[\]]+)(?:\[[^\]]+\])?$/.exec(selector) || [];
204
- return tagName.toLowerCase() === tag.toLowerCase();
205
- });
206
- for (const node of childNodes) {
207
- if (node.is(node.TEXT_NODE)) {
208
- continue;
209
- }
210
- if (node.nodeName.toLowerCase() !== tag.toLowerCase()) {
211
- continue;
212
- }
213
- for (const selector of selectors) {
214
- const matched = node.matches(selector);
215
- // console.log({ raw: node.raw, selector, matched });
216
- if (matched) {
217
- return true;
218
- }
219
- }
220
- return false;
221
- }
222
- break;
223
- }
224
- case 'NAD': {
225
- let targetsMaybeIncludesNotAllowedDescendants = Array.from(new Set(matched.split(/><|<|>/g).filter(_ => _)));
226
- const contents = new Set();
227
- const transparentGroupName = groupNames.find(name => /^TRANSPARENT_[0-9]+$/.test(name));
228
- const inTransparent = _selector.includes('__InTRANSPARENT')
229
- ? transparentGroupName && capGroups[transparentGroupName]
230
- ? capGroups[transparentGroupName].split(/><|<|>/g).filter(_ => _)
231
- : null
232
- : null;
233
- _selector.forEach(content => {
234
- if (content[0] === '_') {
235
- (0, ml_spec_1.contentModelCategoryToTagNames)(content.replace('_', '#'), specs).forEach(tag => contents.add(tag));
236
- return;
237
- }
238
- contents.add(content);
239
- });
240
- const selectors = Array.from(contents);
241
- targetsMaybeIncludesNotAllowedDescendants = targetsMaybeIncludesNotAllowedDescendants.filter(content => inTransparent ? inTransparent.includes(content) : true);
242
- // console.log({
243
- // groupName,
244
- // matched,
245
- // _selector,
246
- // type,
247
- // selectors,
248
- // inTransparent,
249
- // targetsMaybeIncludesNotAllowedDescendants,
250
- // });
251
- for (const node of childNodes) {
252
- for (const target of targetsMaybeIncludesNotAllowedDescendants) {
253
- if (node.is(node.TEXT_NODE)) {
254
- if (selectors.includes('#text')) {
255
- return false;
256
- }
257
- continue;
258
- }
259
- if (node.nodeName.toLowerCase() !== target.toLowerCase()) {
260
- continue;
261
- }
262
- for (const selector of selectors) {
263
- // console.log({ selector, target });
264
- // Self
265
- if (node.matches(selector)) {
266
- return false;
267
- }
268
- // Descendants
269
- const nodeList = node.querySelectorAll(selector);
270
- if (nodeList.length) {
271
- return false;
272
- }
273
- }
274
- }
275
- }
276
- break;
277
- }
278
- }
107
+ if (scope.is(scope.MARKUPLINT_PREPROCESSOR_BLOCK)) {
108
+ return t('the {0}', 'code block');
279
109
  }
280
- return true;
110
+ return t('the {0}', 'node');
281
111
  }
@@ -0,0 +1,3 @@
1
+ import type { ChildNode, Options, Result, Specs } from './types';
2
+ export type SelectorResult = Result<'UNMATCHED_SELECTOR_BUT_MAY_EMPTY' | 'MISSING_NODE' | 'UNMATCHED_SELECTORS'>;
3
+ export declare function matchesSelector(query: string, node: ChildNode | undefined, specs: Specs, options: Options, depth: number): SelectorResult;
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.matchesSelector = void 0;
4
+ const ml_spec_1 = require("@markuplint/ml-spec");
5
+ const debug_1 = require("./debug");
6
+ const utils_1 = require("./utils");
7
+ function matchesSelector(query, node, specs, options, depth) {
8
+ const nodeLog = debug_1.cmLog.extend(`node#${depth}`);
9
+ const { selector, hasText, hasCustom } = optCondition(query, specs);
10
+ if (node == null) {
11
+ if (hasText) {
12
+ nodeLog('<#text>.matches(%s) => ""', query);
13
+ return {
14
+ type: 'MATCHED_ZERO',
15
+ matched: node ? [node] : [],
16
+ unmatched: [],
17
+ zeroMatch: true,
18
+ query,
19
+ hint: {},
20
+ };
21
+ }
22
+ return {
23
+ type: 'MISSING_NODE',
24
+ matched: [],
25
+ unmatched: [],
26
+ zeroMatch: false,
27
+ query,
28
+ hint: {},
29
+ };
30
+ }
31
+ if (node.is(node.TEXT_NODE)) {
32
+ if (hasText) {
33
+ nodeLog('<#text>.matches(%s) => "%s"', query, node.raw.trim());
34
+ return {
35
+ type: 'MATCHED',
36
+ matched: [node],
37
+ unmatched: [],
38
+ zeroMatch: true,
39
+ query,
40
+ hint: {},
41
+ };
42
+ }
43
+ if (node.isWhitespace()) {
44
+ nodeLog('<#text>.matches(%s) => WHITESPACE', query);
45
+ return {
46
+ type: 'MATCHED',
47
+ matched: [node],
48
+ unmatched: [],
49
+ zeroMatch: true,
50
+ query,
51
+ hint: {},
52
+ };
53
+ }
54
+ // Disallows a text node
55
+ return {
56
+ type: 'UNEXPECTED_EXTRA_NODE',
57
+ matched: [],
58
+ unmatched: [node],
59
+ zeroMatch: false,
60
+ query,
61
+ hint: {},
62
+ };
63
+ }
64
+ if (node.is(node.MARKUPLINT_PREPROCESSOR_BLOCK)) {
65
+ nodeLog('%s.matches(%s) => PBlock', node.raw, query);
66
+ return {
67
+ type: 'MATCHED',
68
+ matched: [node],
69
+ unmatched: [],
70
+ zeroMatch: !!hasText,
71
+ query,
72
+ hint: {},
73
+ };
74
+ }
75
+ if (node.is(node.ELEMENT_NODE)) {
76
+ if (node.elementType !== 'html' && hasCustom) {
77
+ nodeLog('%s.matches(%s) => CustomElement', node.raw, query);
78
+ return {
79
+ type: 'MATCHED',
80
+ matched: [node],
81
+ unmatched: [],
82
+ zeroMatch: !!hasText,
83
+ query,
84
+ hint: {},
85
+ };
86
+ }
87
+ const result = (0, utils_1.matches)(selector, node, specs);
88
+ nodeLog('%s.matches(%s) => %s', node.raw, query, result.matched);
89
+ if (result.matched) {
90
+ return {
91
+ type: 'MATCHED',
92
+ matched: [node],
93
+ unmatched: [],
94
+ zeroMatch: !!hasText,
95
+ query,
96
+ hint: {},
97
+ };
98
+ }
99
+ if (hasText) {
100
+ return {
101
+ type: 'UNMATCHED_SELECTOR_BUT_MAY_EMPTY',
102
+ matched: [],
103
+ unmatched: [node],
104
+ zeroMatch: true,
105
+ query,
106
+ hint: {
107
+ not: result.not,
108
+ },
109
+ };
110
+ }
111
+ return {
112
+ type: 'UNMATCHED_SELECTORS',
113
+ matched: [],
114
+ unmatched: [node],
115
+ zeroMatch: false,
116
+ query,
117
+ hint: {
118
+ not: result.not,
119
+ },
120
+ };
121
+ }
122
+ return {
123
+ type: 'MATCHED',
124
+ matched: [node],
125
+ unmatched: [],
126
+ zeroMatch: !!hasText,
127
+ query,
128
+ hint: {},
129
+ };
130
+ }
131
+ exports.matchesSelector = matchesSelector;
132
+ const conditionWithoutSpecs = {
133
+ '#custom': {
134
+ selector: '#custom',
135
+ hasCustom: true,
136
+ hasText: false,
137
+ },
138
+ '#text': {
139
+ selector: '#text',
140
+ hasCustom: false,
141
+ hasText: true,
142
+ },
143
+ };
144
+ const optConditionSpecsBaseCaches = new Map();
145
+ function optCondition(query, specs) {
146
+ var _a;
147
+ const condWithoutSpecs = conditionWithoutSpecs[query];
148
+ if (condWithoutSpecs) {
149
+ return condWithoutSpecs;
150
+ }
151
+ const queryCaches = (_a = optConditionSpecsBaseCaches.get(specs)) !== null && _a !== void 0 ? _a : new Map();
152
+ const cached = queryCaches.get(query);
153
+ if (cached) {
154
+ return cached;
155
+ }
156
+ let hasCustom = false;
157
+ let hasText = false;
158
+ const selector = query.replace(/^(?::model\(([^)]+)\)|#([a-z-]+))(.*)$/, (_, $model, _model, $last) => {
159
+ const _selectors = (0, ml_spec_1.contentModelCategoryToTagNames)(`#${$model !== null && $model !== void 0 ? $model : _model}`, specs.def);
160
+ if (_selectors.length === 0) {
161
+ throw new Error(`${$model !== null && $model !== void 0 ? $model : _model} is empty`);
162
+ }
163
+ const selectors = [];
164
+ for (const selector of _selectors) {
165
+ if (selector === '#custom') {
166
+ hasCustom = true;
167
+ continue;
168
+ }
169
+ if (selector === '#text') {
170
+ hasText = true;
171
+ continue;
172
+ }
173
+ selectors.push(selector);
174
+ }
175
+ return `:is(${selectors.join(',')})${$last !== null && $last !== void 0 ? $last : ''}`;
176
+ });
177
+ const result = {
178
+ selector,
179
+ hasCustom,
180
+ hasText,
181
+ };
182
+ queryCaches.set(query, result);
183
+ optConditionSpecsBaseCaches.set(specs, queryCaches);
184
+ return result;
185
+ }
@@ -0,0 +1,13 @@
1
+ import type { ChildNode, Options, Result, Specs } from './types';
2
+ import type { PermittedContentPattern } from '@markuplint/ml-spec';
3
+ /**
4
+ * Check ordered array
5
+ *
6
+ * @param contents
7
+ * @param nodes
8
+ * @param specs
9
+ * @param options
10
+ * @param depth
11
+ * @returns
12
+ */
13
+ export declare function order(contents: ReadonlyArray<PermittedContentPattern>, nodes: ReadonlyArray<ChildNode>, specs: Specs, options: Options, depth: number): Result;
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.order = void 0;
4
+ const helpers_1 = require("../helpers");
5
+ const complex_branch_1 = require("./complex-branch");
6
+ const debug_1 = require("./debug");
7
+ const utils_1 = require("./utils");
8
+ /**
9
+ * Check ordered array
10
+ *
11
+ * @param contents
12
+ * @param nodes
13
+ * @param specs
14
+ * @param options
15
+ * @param depth
16
+ * @returns
17
+ */
18
+ function order(contents, nodes, specs, options, depth) {
19
+ var _a, _b, _c, _d;
20
+ const orderLog = debug_1.cmLog.extend(`order#${depth}`);
21
+ const btLog = debug_1.cmLog.extend(`backtrack#${depth}`);
22
+ const patterns = (0, helpers_1.deepCopy)(contents);
23
+ const collection = new utils_1.Collection(nodes);
24
+ orderLog('Starts: %s', collection);
25
+ let result = undefined;
26
+ let backtrackMode = false;
27
+ let afterBacktrack = false;
28
+ const unmatchedResults = [];
29
+ while (patterns.length) {
30
+ result = (0, complex_branch_1.complexBranch)(patterns[0], collection.unmatched, specs, options, depth);
31
+ collection.addMatched(result.matched);
32
+ orderLog('stack: %s', collection);
33
+ if (result.type !== 'UNEXPECTED_EXTRA_NODE' && result.type !== 'MATCHED' && result.type !== 'MATCHED_ZERO') {
34
+ unmatchedResults.push(result);
35
+ if (backtrackMode) {
36
+ collection.back();
37
+ btLog('🔙');
38
+ backtrackMode = false;
39
+ afterBacktrack = true;
40
+ continue;
41
+ }
42
+ orderLog('conformed (%s): %s', result.type, collection.toString(true));
43
+ const barelyMatchedResult = unmatchedResults.sort((a, b) => b.matched.length - a.matched.length)[0];
44
+ if (!barelyMatchedResult) {
45
+ throw new Error('Unreachable code');
46
+ }
47
+ return {
48
+ type: barelyMatchedResult.type,
49
+ matched: collection.matched,
50
+ unmatched: collection.unmatched,
51
+ zeroMatch: barelyMatchedResult.zeroMatch,
52
+ query: barelyMatchedResult.query,
53
+ hint: barelyMatchedResult.hint,
54
+ };
55
+ }
56
+ if (afterBacktrack) {
57
+ collection.lock();
58
+ afterBacktrack = false;
59
+ }
60
+ if (result.zeroMatch) {
61
+ backtrackMode = true;
62
+ }
63
+ else {
64
+ backtrackMode = false;
65
+ }
66
+ patterns.shift();
67
+ }
68
+ if (collection.unmatched.length) {
69
+ orderLog('Conformed (UNEXPECTED_EXTRA_NODE): %s', collection.toString(true));
70
+ return {
71
+ type: 'UNEXPECTED_EXTRA_NODE',
72
+ matched: collection.matched,
73
+ unmatched: collection.unmatched,
74
+ zeroMatch: false,
75
+ query: (_a = result === null || result === void 0 ? void 0 : result.query) !== null && _a !== void 0 ? _a : 'N/A',
76
+ hint: (_b = result === null || result === void 0 ? void 0 : result.hint) !== null && _b !== void 0 ? _b : {},
77
+ };
78
+ }
79
+ orderLog('Conformed: %s', collection);
80
+ return {
81
+ type: collection.matched.length ? 'MATCHED' : 'MATCHED_ZERO',
82
+ matched: collection.matched,
83
+ unmatched: collection.unmatched,
84
+ zeroMatch: false,
85
+ query: (_c = result === null || result === void 0 ? void 0 : result.query) !== null && _c !== void 0 ? _c : 'N/A',
86
+ hint: (_d = result === null || result === void 0 ? void 0 : result.hint) !== null && _d !== void 0 ? _d : {},
87
+ };
88
+ }
89
+ exports.order = order;
@@ -0,0 +1,4 @@
1
+ import type { SelectorResult } from './matches-selector';
2
+ import type { ChildNode, Options, Specs } from './types';
3
+ import type { PermittedContentPattern, Model } from '@markuplint/ml-spec';
4
+ export declare function recursiveBranch(model: Model | PermittedContentPattern[], nodes: readonly ChildNode[], specs: Specs, options: Options, depth: number): SelectorResult;