Skip to content

Commit d90cb92

Browse files
Fix(MWPW-175129): url.canParse support for legacy browsers. (#4451)
* support for legacy browsers that do not support url.canParse * added additional check to validate url protocol * validated url * reverted formatting changes * code enhancement * mock for unit tests
1 parent c75eaa0 commit d90cb92

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

libs/blocks/caas-config/caas-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import {
1414
getConfig,
1515
parseEncodedConfig,
1616
loadStyle,
17+
isValidHtmlUrl,
1718
} from '../../utils/utils.js';
1819
import Accordion from '../../ui/controls/Accordion.js';
1920
import {
2021
decodeCompressedString,
2122
defaultState,
2223
initCaas,
23-
isValidHtmlUrl,
2424
isValidUuid,
2525
loadCaasFiles,
2626
loadCaasTags,

libs/blocks/caas/utils.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,6 @@ export function getPageLocale(currentPath, locales = pageLocales) {
189189
return '';
190190
}
191191

192-
export const isValidHtmlUrl = (url) => {
193-
const regex = /^https:\/\/[^\s]+$/;
194-
return regex.test(url);
195-
};
196192
export const isValidUuid = (id) => /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(id);
197193

198194
export const loadStrings = async (

libs/utils/utils.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,18 @@ export function decorateSVG(a) {
785785
}
786786
}
787787

788+
export const isValidHtmlUrl = (url) => {
789+
const regex = /^https:\/\/[^\s]+$/;
790+
return regex.test(url);
791+
};
792+
788793
export function decorateImageLinks(el) {
789794
const images = el.querySelectorAll('img[alt*="|"]');
790795
if (!images.length) return;
791796
[...images].forEach((img) => {
792797
const [source, alt, icon] = img.alt.split('|');
793798
try {
794-
if (!URL.canParse(source.trim())) return;
799+
if (!isValidHtmlUrl(source.trim())) return;
795800
const url = new URL(source.trim());
796801
const href = (url.hostname.includes('.aem.') || url.hostname.includes('.hlx.')) ? `${url.pathname}${url.search}${url.hash}` : url.href;
797802
img.alt = alt?.trim() || '';

test/blocks/caas/mocks/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function getMetadata(name, doc = document) {
2020
return meta && meta.content;
2121
}
2222

23+
export const isValidHtmlUrl = (url) => {
24+
const regex = /^https:\/\/[^\s]+$/;
25+
return regex.test(url);
26+
};
27+
2328
export function createIntersectionObserver({ el, callback /* , once = true, options = {} */ }) {
2429
// fire immediately
2530
callback(el, { target: el });

0 commit comments

Comments
 (0)