Skip to content

Commit b8e703b

Browse files
committed
Update aem.js decorateButtons function
1 parent 0ac1782 commit b8e703b

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

scripts/aem.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -356,34 +356,45 @@ function wrapTextNodes(block) {
356356
/**
357357
* Decorates paragraphs containing a single link as buttons.
358358
* @param {Element} element container element
359+
* Updated function to support Universal Editor and Document Authoring in-preview editing.
359360
*/
360361
function decorateButtons(element) {
361362
element.querySelectorAll('a').forEach((a) => {
362363
a.title = a.title || a.textContent;
363364
if (a.href !== a.textContent) {
364365
const up = a.parentElement;
365366
const twoup = a.parentElement.parentElement;
367+
368+
// Check if the <a> does not contain an <img> and it is inside a valid container
366369
if (!a.querySelector('img')) {
367-
if (up.childNodes.length === 1 && (up.tagName === 'P' || up.tagName === 'DIV')) {
368-
a.className = 'button'; // default
370+
// Case 1: When <em> is directly inside the <a> tag
371+
if (a.querySelector('em') && up.childNodes.length === 1 && up.tagName === 'P') {
372+
a.className = 'button secondary';
369373
up.classList.add('button-container');
370-
}
371-
if (
374+
// Case 2: When <strong> is directly inside the <a> tag
375+
} else if (a.querySelector('strong') && up.childNodes.length === 1 && up.tagName === 'P') {
376+
a.className = 'button primary';
377+
up.classList.add('button-container');
378+
// Case 3: When <em> is inside a parent <p> or <div>, but not directly within <a>
379+
} else if (
372380
up.childNodes.length === 1
373-
&& up.tagName === 'STRONG'
374-
&& twoup.childNodes.length === 1
375-
&& twoup.tagName === 'P'
381+
&& (up.tagName === 'P' || up.tagName === 'DIV')
382+
&& !a.querySelector('em')
376383
) {
377-
a.className = 'button primary';
378-
twoup.classList.add('button-container');
379-
}
380-
if (
384+
a.className = 'button'; // default
385+
up.classList.add('button-container');
386+
// Case 4: When <em> is inside a parent <p>, and <a> is inside <strong> or <em> as a sibling
387+
} else if (
381388
up.childNodes.length === 1
382-
&& up.tagName === 'EM'
389+
&& (up.tagName === 'EM' || up.tagName === 'STRONG')
383390
&& twoup.childNodes.length === 1
384391
&& twoup.tagName === 'P'
385392
) {
386-
a.className = 'button secondary';
393+
if (up.tagName === 'EM') {
394+
a.className = 'button secondary';
395+
} else if (up.tagName === 'STRONG') {
396+
a.className = 'button primary';
397+
}
387398
twoup.classList.add('button-container');
388399
}
389400
}

0 commit comments

Comments
 (0)