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-157292: CC and DC: Phone number links wrong #2841

Merged
merged 3 commits into from
Sep 5, 2024
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
13 changes: 9 additions & 4 deletions libs/features/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export async function replaceText(
// The .shift method is very slow, thus using normal iterator
let i = 0;
// eslint-disable-next-line no-plusplus
const finalText = text.replaceAll(regex, () => placeholders[i++]);
let finalText = text.replaceAll(regex, () => placeholders[i++]);
finalText = finalText.replace(/ /g, '\u00A0');
return finalText;
}

Expand All @@ -130,9 +131,13 @@ export async function decoratePlaceholderArea({
if (!nodes.length) return;
const config = getConfig();
await fetchPlaceholders({ placeholderPath, config, placeholderRequest });
const replaceNodes = nodes.map(async (textNode) => {
textNode.nodeValue = await replaceText(textNode.nodeValue, config);
textNode.nodeValue = textNode.nodeValue.replace(/ /g, '\u00A0');
const replaceNodes = nodes.map(async (nodeEl) => {
if (nodeEl.nodeType === Node.TEXT_NODE) {
nodeEl.nodeValue = await replaceText(nodeEl.nodeValue, config);
} else if (nodeEl.nodeType === Node.ELEMENT_NODE) {
const hrefVal = await replaceText(nodeEl.getAttribute('href'), config);
nodeEl.setAttribute('href', hrefVal);
}
});
await Promise.all(replaceNodes);
}
26 changes: 12 additions & 14 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,23 +760,21 @@ export async function customFetch({ resource, withCacheRules }) {

const findReplaceableNodes = (area) => {
const regex = /{{(.*?)}}|%7B%7B(.*?)%7D%7D/g;
const walker = document.createTreeWalker(
area,
NodeFilter.SHOW_TEXT,
{
acceptNode(node) {
const a = regex.test(node.nodeValue)
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT;
regex.lastIndex = 0;
return a;
},
},
);
const walker = document.createTreeWalker(area, NodeFilter.SHOW_ALL);
const nodes = [];
let node = walker.nextNode();
while (node !== null) {
nodes.push(node);
let matchFound = false;
if (node.nodeType === Node.TEXT_NODE) {
matchFound = regex.test(node.nodeValue);
} else if (node.nodeType === Node.ELEMENT_NODE && node.hasAttribute('href')) {
const hrefValue = node.getAttribute('href');
matchFound = regex.test(hrefValue);
}
if (matchFound) {
nodes.push(node);
regex.lastIndex = 0;
}
node = walker.nextNode();
}
return nodes;
Expand Down
3 changes: 3 additions & 0 deletions test/utils/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<p>I'm not a blockhead.</p>
<p>{{&nbsp;inkl. MwSt.}}</p>
</div>
<div>
<a href="tel:%7B%7Bphone-number-substance%7D%7D" class="placeholder"><sup>{{phone-number-substance}}</sup></a>
</div>
<!-- picture element with poster video -->
<div>
<picture>
Expand Down
3 changes: 3 additions & 0 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ describe('Utils', () => {
const paragraphs = [...document.querySelectorAll('p')];
const lastPara = paragraphs.pop();
expect(lastPara.textContent).to.equal(' inkl. MwSt.');
const plceholderhref = document.querySelector('.placeholder');
const hrefValue = plceholderhref.getAttribute('href');
expect(hrefValue).to.equal('tel:phone number substance');
});

it('Decorates meta helix url', () => {
Expand Down