@stacksjs/stx 0.2.125 → 0.2.127

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.
package/dist/a11y.js CHANGED
@@ -41,6 +41,23 @@ export function getScreenReaderOnlyStyle() {
41
41
  export function hasDOMSupport() {
42
42
  return typeof globalThis.document < "u" && typeof globalThis.document.createElement === "function";
43
43
  }
44
+ function protectStxComponentTags(source) {
45
+ const componentNames = new Map, protectedSource = source.replace(/<(\/?)([A-Z][\w:.-]*)(?=[\s/>])/g, (_match, closing, componentName) => {
46
+ let protectedName = componentNames.get(componentName);
47
+ if (!protectedName) {
48
+ protectedName = `stx-a11y-component-${componentNames.size}`;
49
+ componentNames.set(componentName, protectedName);
50
+ }
51
+ return `<${closing}${protectedName}`;
52
+ }), originalNames = new Map([...componentNames.entries()].map(([originalName, protectedName]) => [protectedName, originalName]));
53
+ return {
54
+ protectedSource,
55
+ restore: (value) => value.replace(/<(\/?)(stx-a11y-component-\d+)(?=[\s/>])/g, (match, closing, protectedName) => {
56
+ const originalName = originalNames.get(protectedName);
57
+ return originalName ? `<${closing}${originalName}` : match;
58
+ })
59
+ };
60
+ }
44
61
  function checkA11yWithRegex(html, _filePath) {
45
62
  const violations = [], imgRegex = /<img\s+([^>]*)>/gi;
46
63
  let match;
@@ -214,12 +231,13 @@ function checkA11yWithRegex(html, _filePath) {
214
231
  return violations;
215
232
  }
216
233
  export async function checkA11y(html, filePath) {
234
+ const { protectedSource } = protectStxComponentTags(html);
217
235
  if (!hasDOMSupport())
218
- return checkA11yWithRegex(html, filePath);
236
+ return checkA11yWithRegex(protectedSource, filePath);
219
237
  const violations = [];
220
238
  try {
221
239
  const container = globalThis.document.createElement("div");
222
- container.innerHTML = html;
240
+ container.innerHTML = protectedSource;
223
241
  container.querySelectorAll("img").forEach((img) => {
224
242
  if (!img.hasAttribute("alt"))
225
243
  violations.push({
@@ -270,7 +288,7 @@ export async function checkA11y(html, filePath) {
270
288
  prevLevel = level;
271
289
  }
272
290
  const htmlElement = container.querySelector("html");
273
- if (html.includes("<html") && htmlElement && !htmlElement.hasAttribute("lang"))
291
+ if (protectedSource.includes("<html") && htmlElement && !htmlElement.hasAttribute("lang"))
274
292
  violations.push({
275
293
  type: "missing-lang",
276
294
  element: "<html>",
@@ -291,7 +309,7 @@ export async function checkA11y(html, filePath) {
291
309
  helpUrl: "https://web.dev/learn/accessibility/more-html/#links"
292
310
  });
293
311
  });
294
- if (html.includes("<nav") || html.includes("<header")) {
312
+ if (protectedSource.includes("<nav") || protectedSource.includes("<header")) {
295
313
  if (!container.querySelector('a[href="#main"], a[href="#content"], .skip-link, [class*="skip"]') && container.querySelectorAll("a").length > 5)
296
314
  violations.push({
297
315
  type: "missing-skip-link",
@@ -368,7 +386,7 @@ export async function checkA11y(html, filePath) {
368
386
  helpUrl: "https://web.dev/learn/accessibility/focus/"
369
387
  });
370
388
  });
371
- if (html.includes("<body") && !container.querySelector('main, [role="main"]'))
389
+ if (protectedSource.includes("<body") && !container.querySelector('main, [role="main"]'))
372
390
  violations.push({
373
391
  type: "missing-main-landmark",
374
392
  element: "<body>",
@@ -422,8 +440,8 @@ const defaultAutoFixConfig = {
422
440
  defaultLang: "en"
423
441
  };
424
442
  export function autoFixA11y(html, config = {}) {
425
- const mergedConfig = { ...defaultAutoFixConfig, ...config }, fixes = [];
426
- let fixed = html;
443
+ const mergedConfig = { ...defaultAutoFixConfig, ...config }, fixes = [], { protectedSource, restore } = protectStxComponentTags(html);
444
+ let fixed = protectedSource;
427
445
  if (mergedConfig.fixMissingAlt)
428
446
  fixed = fixed.replace(/<img([^>]*?)(?<!\balt\s*=\s*["'][^"']*["'])(\s*\/?>)/gi, (match, attrs, closing) => {
429
447
  if (/\balt\s*=/i.test(attrs))
@@ -534,7 +552,7 @@ export function autoFixA11y(html, config = {}) {
534
552
  });
535
553
  return {
536
554
  original: html,
537
- fixed,
555
+ fixed: restore(fixed),
538
556
  fixCount: fixes.length,
539
557
  fixes
540
558
  };