Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-158142 [MEP] check for MEP placeholders as fragmentless content is inserted #2919

Merged
merged 10 commits into from
Sep 19, 2024
47 changes: 35 additions & 12 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,33 @@
return frag;
};

export const checkCustomPlaceholders = (content) => {
let newContent = content;

const config = getConfig();
const regex = /{{(.*?)}}/g;

newContent = newContent.replace(regex, (match, prop) => {
if (Object.prototype.hasOwnProperty.call(config.placeholders, prop)) {
return config.placeholders[prop];
}
return match;
});

return newContent;

Check warning on line 164 in libs/features/personalization/personalization.js

View check run for this annotation

Codecov / codecov/patch

libs/features/personalization/personalization.js#L152-L164

Added lines #L152 - L164 were not covered by tests
};

export function replacePlaceholders(value, placeholders) {
let val = value;
const matches = val.match(/{{(.*?)}}/g);
if (!matches) return val;
matches.forEach((match) => {
const key = match.replace(/{{|}}/g, '').trim();
if (placeholders[key]) val = val.replace(match, placeholders[key]);
});
return val;
}

export const createContent = (el, content, manifestId, targetManifestId, action, modifiers) => {
if (action === 'replace') {
addIds(el, manifestId, targetManifestId);
Expand All @@ -158,11 +185,16 @@
return el;
}
if (getSelectorType(content) !== 'fragment') {
const config = getConfig();
const newContent = replacePlaceholders(content, config.placeholders);

if (action === 'replace') {
el.innerHTML = content;
el.innerHTML = newContent;

return el;
}
const container = createTag('div', {}, content);

const container = createTag('div', {}, newContent);
addIds(container, manifestId, targetManifestId);
return container;
}
Expand Down Expand Up @@ -926,16 +958,7 @@
return false;
}

function replacePlaceholders(value, placeholders) {
let val = value;
const matches = val.match(/{{(.*?)}}/g);
if (!matches) return val;
matches.forEach((match) => {
const key = match.replace(/{{|}}/g, '').trim();
if (placeholders[key]) val = val.replace(match, placeholders[key]);
});
return val;
}


export function parseNestedPlaceholders({ placeholders }) {
if (!placeholders) return;
Expand Down
Loading