Skip to content

Commit da9ad20

Browse files
1 parent 1043b03 commit da9ad20

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

scripts/aem.js

+44
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,48 @@ function decorateTemplateAndTheme() {
372372
if (theme) addClasses(document.body, theme);
373373
}
374374

375+
/**
376+
* Wrap inline text content of block cells within a <p> tag.
377+
* @param {Element} block the block element
378+
*/
379+
function wrapTextNodes(block) {
380+
const validWrappers = [
381+
'P',
382+
'PRE',
383+
'UL',
384+
'OL',
385+
'PICTURE',
386+
'TABLE',
387+
'H1',
388+
'H2',
389+
'H3',
390+
'H4',
391+
'H5',
392+
'H6',
393+
];
394+
395+
const wrap = (el) => {
396+
const wrapper = document.createElement('p');
397+
wrapper.append(...el.childNodes);
398+
el.append(wrapper);
399+
};
400+
401+
block.querySelectorAll(':scope > div > div').forEach((blockColumn) => {
402+
if (blockColumn.hasChildNodes()) {
403+
const hasWrapper = !!blockColumn.firstElementChild
404+
&& validWrappers.some((tagName) => blockColumn.firstElementChild.tagName === tagName);
405+
if (!hasWrapper) {
406+
wrap(blockColumn);
407+
} else if (
408+
blockColumn.firstElementChild.tagName === 'PICTURE'
409+
&& (blockColumn.children.length > 1 || !!blockColumn.textContent.trim())
410+
) {
411+
wrap(blockColumn);
412+
}
413+
}
414+
});
415+
}
416+
375417
/**
376418
* Decorates paragraphs containing a single link as buttons.
377419
* @param {Element} element container element
@@ -635,6 +677,7 @@ function decorateBlock(block) {
635677
block.classList.add('block');
636678
block.dataset.blockName = shortBlockName;
637679
block.dataset.blockStatus = 'initialized';
680+
wrapTextNodes(block);
638681
const blockWrapper = block.parentElement;
639682
blockWrapper.classList.add(`${shortBlockName}-wrapper`);
640683
const section = block.closest('.section');
@@ -723,4 +766,5 @@ export {
723766
toClassName,
724767
updateSectionsStatus,
725768
waitForLCP,
769+
wrapTextNodes,
726770
};

0 commit comments

Comments
 (0)