@@ -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