@hortonstudio/main 1.6.0 → 1.6.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.
- package/autoInit/accessibility.js +93 -0
- package/index.js +1 -1
- package/package.json +1 -1
|
@@ -10,6 +10,7 @@ export function init() {
|
|
|
10
10
|
setupPreventDefault();
|
|
11
11
|
setupRichTextAccessibility();
|
|
12
12
|
setupSummaryAccessibility();
|
|
13
|
+
setupCustomValuesReplacement();
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
function setupListAccessibility() {
|
|
@@ -339,6 +340,98 @@ export function init() {
|
|
|
339
340
|
});
|
|
340
341
|
}
|
|
341
342
|
|
|
343
|
+
function setupCustomValuesReplacement() {
|
|
344
|
+
const customValuesList = document.querySelector('[data-hs-a11y="custom-values-list"]');
|
|
345
|
+
|
|
346
|
+
if (!customValuesList) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Collect all custom values data
|
|
351
|
+
const customValues = {};
|
|
352
|
+
const descendants = customValuesList.getElementsByTagName('*');
|
|
353
|
+
|
|
354
|
+
Array.from(descendants).forEach(descendant => {
|
|
355
|
+
const nameElement = descendant.querySelector('[data-hs-a11y="custom-values-name"]');
|
|
356
|
+
const valueElement = descendant.querySelector('[data-hs-a11y="custom-values-value"]');
|
|
357
|
+
|
|
358
|
+
if (nameElement && valueElement) {
|
|
359
|
+
const name = nameElement.textContent.trim();
|
|
360
|
+
const value = valueElement.textContent.trim();
|
|
361
|
+
|
|
362
|
+
if (name && value) {
|
|
363
|
+
customValues[name] = value;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// If no custom values found, exit early
|
|
369
|
+
if (Object.keys(customValues).length === 0) {
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Replace text content efficiently using TreeWalker
|
|
374
|
+
const walker = document.createTreeWalker(
|
|
375
|
+
document.body,
|
|
376
|
+
NodeFilter.SHOW_TEXT,
|
|
377
|
+
{
|
|
378
|
+
acceptNode: (node) => {
|
|
379
|
+
// Check if any custom value names exist in the text content
|
|
380
|
+
const text = node.textContent;
|
|
381
|
+
for (const name in customValues) {
|
|
382
|
+
if (text.includes(`{{${name}}}`)) {
|
|
383
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return NodeFilter.FILTER_SKIP;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
);
|
|
390
|
+
|
|
391
|
+
const textNodes = [];
|
|
392
|
+
let node;
|
|
393
|
+
while (node = walker.nextNode()) {
|
|
394
|
+
textNodes.push(node);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Replace text in collected nodes
|
|
398
|
+
textNodes.forEach(textNode => {
|
|
399
|
+
let newText = textNode.textContent;
|
|
400
|
+
let hasChanges = false;
|
|
401
|
+
|
|
402
|
+
for (const name in customValues) {
|
|
403
|
+
const placeholder = `{{${name}}}`;
|
|
404
|
+
if (newText.includes(placeholder)) {
|
|
405
|
+
newText = newText.replace(new RegExp(placeholder, 'g'), customValues[name]);
|
|
406
|
+
hasChanges = true;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (hasChanges) {
|
|
411
|
+
textNode.textContent = newText;
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// Replace link hrefs
|
|
416
|
+
const links = document.querySelectorAll('a[href]');
|
|
417
|
+
links.forEach(link => {
|
|
418
|
+
let href = link.getAttribute('href');
|
|
419
|
+
let hasChanges = false;
|
|
420
|
+
|
|
421
|
+
for (const name in customValues) {
|
|
422
|
+
const placeholder = `{{${name}}}`;
|
|
423
|
+
if (href.includes(placeholder)) {
|
|
424
|
+
href = href.replace(new RegExp(placeholder, 'g'), customValues[name]);
|
|
425
|
+
hasChanges = true;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (hasChanges) {
|
|
430
|
+
link.setAttribute('href', href);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
342
435
|
function setupRichTextAccessibility() {
|
|
343
436
|
const contentAreas = document.querySelectorAll('[data-hs-a11y="rich-content"]');
|
|
344
437
|
const tocLists = document.querySelectorAll('[data-hs-a11y="rich-toc"]');
|
package/index.js
CHANGED