Skip to content

Commit 280d274

Browse files
committed
ugh, getting sloppy
1 parent 9ccbd62 commit 280d274

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

Doc/improve-page.rst

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,30 @@ Improve a documentation page
1313
.. raw:: html
1414

1515
<script>
16+
function applyReplacements(text, params) {
17+
console.log('Applying replacements to:', text);
18+
return text
19+
.replace(/PAGETITLE/g, params.get('pagetitle'))
20+
.replace(/PAGEURL/g, params.get('pageurl'))
21+
.replace(/PAGESOURCE/g, params.get('pagesource'));
22+
}
23+
1624
document.addEventListener('DOMContentLoaded', () => {
1725
const params = new URLSearchParams(window.location.search);
18-
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null);
26+
const walker = document.createTreeWalker(
27+
document.body,
28+
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT,
29+
null
30+
);
1931
20-
// Replace .textContent to be safe. innerHTML will execute XSS code.
2132
while (walker.nextNode()) {
22-
const textNode = walker.currentNode;
23-
textNode.textContent = textNode.textContent
24-
.replace(/PAGETITLE/g, params.get('pagetitle'))
25-
.replace(/PAGEURL/g, params.get('pageurl'))
26-
.replace(/PAGESOURCE/g, params.get('pagesource'));
33+
const node = walker.currentNode;
34+
35+
if (node.nodeType === Node.TEXT_NODE) {
36+
node.textContent = applyReplacements(node.textContent, params)
37+
} else if (node.nodeName === 'A' && node.href) {
38+
node.setAttribute('href', applyReplacements(node.getAttribute('href'), params));
39+
}
2740
}
2841
});
2942
</script>

0 commit comments

Comments
 (0)