Skip to content

Commit a6732f0

Browse files
committed
test: decorate buttons test
1 parent 8f80c98 commit a6732f0

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

scripts/aem.js

+21-25
Original file line numberDiff line numberDiff line change
@@ -356,37 +356,33 @@ function decorateTemplateAndTheme() {
356356
}
357357

358358
/**
359-
* Decorates paragraphs containing a single link as buttons.
359+
* Decorates single links in paragraphs within a container element as buttons.
360360
* @param {Element} element container element
361361
*/
362362
function decorateButtons(element) {
363-
element.querySelectorAll('a').forEach((a) => {
363+
element.querySelectorAll('a[href]').forEach((a) => {
364364
a.title = a.title || a.textContent;
365365
if (a.href !== a.textContent) {
366-
const up = a.parentElement;
367-
const twoup = a.parentElement.parentElement;
368-
if (!a.querySelector('img')) {
369-
if (up.childNodes.length === 1 && (up.tagName === 'P' || up.tagName === 'DIV')) {
370-
a.className = 'button'; // default
371-
up.classList.add('button-container');
372-
}
373-
if (
374-
up.childNodes.length === 1
375-
&& up.tagName === 'STRONG'
376-
&& twoup.childNodes.length === 1
377-
&& twoup.tagName === 'P'
378-
) {
379-
a.className = 'button primary';
380-
twoup.classList.add('button-container');
366+
const linkedImg = a.querySelector('img');
367+
if (!linkedImg) {
368+
let wrapper = a.closest('p');
369+
if (!wrapper) {
370+
const inList = a.closest('li');
371+
const inBlock = a.closest('[class]');
372+
if (!inList && inBlock) {
373+
// wrap unwrapped block links in paragraph tag
374+
const cell = a.closest('div');
375+
const p = document.createElement('p');
376+
while (cell.firstChild) p.append(cell.firstChild);
377+
cell.replaceChildren(p);
378+
wrapper = p;
379+
}
381380
}
382-
if (
383-
up.childNodes.length === 1
384-
&& up.tagName === 'EM'
385-
&& twoup.childNodes.length === 1
386-
&& twoup.tagName === 'P'
387-
) {
388-
a.className = 'button secondary';
389-
twoup.classList.add('button-container');
381+
if (wrapper && wrapper.childNodes.length === 1) {
382+
wrapper.className = 'button-container';
383+
a.className = 'button';
384+
if (wrapper.querySelector('strong')) a.classList.add('primary');
385+
if (wrapper.querySelector('em')) a.classList.add('secondary');
390386
}
391387
}
392388
}

0 commit comments

Comments
 (0)