Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions blocks/hero-heritage-cc/hero-heritage-cc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { decorateButtons, pickPicturePreloadUrl } from '../../scripts/aem.js';
import { loadFragment } from '../../scripts/scripts.js';
import { pickPicturePreloadUrl } from '../../scripts/aem.js';
import { decorateButtons, loadFragment } from '../../scripts/scripts.js';

/* eslint-disable secure-coding/no-hardcoded-credentials -- CSS classes/style props only */

Expand Down
3 changes: 2 additions & 1 deletion blocks/modal/modal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
buildBlock, decorateBlock, loadBlock, loadCSS,
} from '../../scripts/aem.js';
import { loadFragment, DOMPURIFY } from '../../scripts/scripts.js';
import { loadFragment, DOMPURIFY, decorateButtons } from '../../scripts/scripts.js';

/*
This is not a traditional block, so there is no decorate function.
Expand Down Expand Up @@ -417,6 +417,7 @@ export async function createModal(contentNodes, options = {}) {
const block = buildBlock('modal', '');
document.querySelector('main').append(block);
decorateBlock(block);
decorateButtons(block);
await loadBlock(block);

// close on click outside the dialog
Expand Down
67 changes: 0 additions & 67 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,70 +462,6 @@ function wrapTextNodes(block) {
});
}

/**
* Decorates paragraphs containing a single link as buttons.
* @param {Element} element container element
*/
function decorateButtons(element) {
element.querySelectorAll('a').forEach((a) => {
a.title = a.title || a.textContent;
const shouldSkip = a.href === a.textContent && a.textContent.trim().length > 0
&& !a.closest('.button-container') && !a.classList.contains('button');
if (!shouldSkip) {
const up = a.parentElement;
const twoup = a.parentElement.parentElement;
const threeup = a.parentElement.parentElement.parentElement;
const child = a.querySelector(':scope > *:first-child');
if (!a.querySelector('img')) {
if (up.childNodes.length === 1 && (up.tagName === 'P' || up.tagName === 'DIV')) {
a.className = 'button'; // default
up.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'STRONG'
&& twoup.childNodes.length === 1
&& twoup.tagName === 'P'
) {
a.className = 'button primary';
twoup.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'EM'
&& twoup.childNodes.length === 1
&& twoup.tagName === 'P'
) {
a.className = 'button secondary';
twoup.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'STRONG'
&& twoup.childNodes.length === 1
&& (twoup.tagName === 'EM' || child?.tagName === 'EM')
&& threeup.childNodes.length === 1
&& threeup.tagName === 'P'
) {
a.className = 'button tertiary';
threeup.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'EM'
&& twoup.childNodes.length === 1
&& twoup.tagName === 'STRONG'
&& threeup.childNodes.length === 1
&& threeup.tagName === 'P'
) {
a.className = 'button tertiary';
threeup.classList.add('button-container');
}
}
}
});
}

/**
* Add <img> for icon, prefixed with codeBasePath and optional prefix.
* @param {Element} [span] span element with icon classes
Expand Down Expand Up @@ -705,8 +641,6 @@ function decorateBlock(block) {
blockWrapper.classList.add(`${shortBlockName}-wrapper`);
const section = block.closest('.section');
if (section) section.classList.add(`${shortBlockName}-container`);
// eslint-disable-next-line no-use-before-define
decorateButtons(block);
}
}

Expand Down Expand Up @@ -812,7 +746,6 @@ export {
createOptimizedPicture,
decorateBlock,
decorateBlocks,
decorateButtons,
decorateIcons,
decorateTemplateAndTheme,
fetchPlaceholders,
Expand Down
2 changes: 1 addition & 1 deletion scripts/editor-support.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
decorateBlock,
decorateBlocks,
decorateButtons,
decorateIcons,
loadBlock,
loadSections,
Expand All @@ -13,6 +12,7 @@ import { decorateRichtext } from './editor-support-rte.js';
import {
decorateSections,
decorateMain,
decorateButtons,
loadFragment,
moveAllAttributes,
} from './scripts.js';
Expand Down
78 changes: 75 additions & 3 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
loadFooter,
buildBlock,
decorateBlock,
decorateButtons,
decorateIcons,
decorateBlocks,
decorateTemplateAndTheme,
Expand All @@ -22,6 +21,72 @@ import {

export { DOMPURIFY };

/**
* Decorates paragraphs containing a single link as buttons.
* @param {Element} element container element
*/

/* eslint-disable sonarjs/cognitive-complexity */
export function decorateButtons(element) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

element.querySelectorAll('a').forEach((a) => {
a.title = a.title || a.textContent;
const shouldSkip = a.href === a.textContent && a.textContent.trim().length > 0
&& !a.closest('.button-container') && !a.classList.contains('button');
if (!shouldSkip) {
const up = a.parentElement;
const twoup = a.parentElement.parentElement;
const threeup = a.parentElement.parentElement.parentElement;
const child = a.querySelector(':scope > *:first-child');
if (!a.querySelector('img')) {
if (up.childNodes.length === 1 && (up.tagName === 'P' || up.tagName === 'DIV')) {
a.className = 'button'; // default
up.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'STRONG'
&& twoup.childNodes.length === 1
&& twoup.tagName === 'P'
) {
a.className = 'button primary';
twoup.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'EM'
&& twoup.childNodes.length === 1
&& twoup.tagName === 'P'
) {
a.className = 'button secondary';
twoup.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'STRONG'
&& twoup.childNodes.length === 1
&& (twoup.tagName === 'EM' || child?.tagName === 'EM')
&& threeup.childNodes.length === 1
&& threeup.tagName === 'P'
) {
a.className = 'button tertiary';
threeup.classList.add('button-container');
}
if (
up.childNodes.length === 1
&& up.tagName === 'EM'
&& twoup.childNodes.length === 1
&& twoup.tagName === 'STRONG'
&& threeup.childNodes.length === 1
&& threeup.tagName === 'P'
) {
a.className = 'button tertiary';
threeup.classList.add('button-container');
}
}
}
});
}

// Max collection size before iteration to prevent DoS from excessive loops (CWE-400)
const MAX_ITERATION_LIMIT = 500;

Expand Down Expand Up @@ -473,6 +538,7 @@ export function buildEmbedBlocks(main) {
const embedBlock = buildBlock('embed', a.cloneNode(true));
a.replaceWith(embedBlock);
decorateBlock(embedBlock);
decorateButtons(embedBlock);
}
});
}
Expand Down Expand Up @@ -1025,6 +1091,7 @@ export function buildMultiSection(main) {
appendMultiSectionBlock(section, items, blockClass, true);
// Decorate blocks inside nested sections so they get .block and are loaded by loadSection
decorateBlocks(section);
section.querySelectorAll('div.block').forEach((block) => decorateButtons(block));
});
}

Expand Down Expand Up @@ -1073,6 +1140,7 @@ export function decorateMain(main) {
buildAutoBlocks(main);
initEntranceAnimationObserver(main);
decorateBlocks(main);
main.querySelectorAll('div.block').forEach((block) => decorateButtons(block));
decorateButtonGroups(main);
buildEmbedBlocks(main);
decorateLinkedPictures(main);
Expand Down Expand Up @@ -1764,7 +1832,9 @@ async function loadLazy(doc) {
const main = doc.querySelector('main');

// Load header first so nav-wrapper is available for category navbar
await loadHeader(doc.querySelector('header'));
const headerEl = doc.querySelector('header');
await loadHeader(headerEl);
if (headerEl) decorateButtons(headerEl);

// Load get app banner fragment and append to header
await loadGetAppBannerFragment();
Expand All @@ -1788,7 +1858,9 @@ async function loadLazy(doc) {
const element = hash ? doc.getElementById(hash.substring(1)) : false;
if (hash && element) element.scrollIntoView();

loadFooter(doc.querySelector('footer'));
const footerEl = doc.querySelector('footer');
loadFooter(footerEl);
if (footerEl) decorateButtons(footerEl);

// Rewrite relative links to prod origin (avoid ww2 links)
doc.querySelectorAll('a[href]').forEach((a) => {
Expand Down
Loading