Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
code refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoMorenoSirius committed Jun 18, 2024
1 parent 0f8e6d1 commit 8bed712
Showing 1 changed file with 60 additions and 64 deletions.
124 changes: 60 additions & 64 deletions apps/mocksi-lite/universalReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,32 @@ class UniversalReplace {
seekAndReplace(pattern: RegExp, newText: string) {
const body = document.querySelector("body");
if (body) {
// TODO createTreeWalker provides a filter function, see if we can filter chunk textNodes
const treeWalker = document.createTreeWalker(
body,
NodeFilter.SHOW_TEXT,
(node) => {
if (
node.parentElement instanceof HTMLScriptElement ||
node.parentElement instanceof HTMLStyleElement
)
return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
},
);
let textNode: Node;
const fragmentsToHighlight: Node[] = [];
const fragmentsToHighlight: Node[] = [];
const replacements: { nodeToReplace: Node; replacement: Node }[] = [];
do {
textNode = treeWalker.currentNode;
if (textNode.nodeValue === null || !textNode?.textContent?.trim())
continue;
const matches = [...textNode.nodeValue.matchAll(pattern)];
if (matches.length > 0) {
const fragmentedTextNode = fragmentTextNode(
fragmentsToHighlight,
matches,
textNode,
newText,
);
replacements.push({
nodeToReplace: textNode,
replacement: fragmentedTextNode,
});
saveModification(
textNode.parentElement as HTMLElement,
newText,
cleanPattern(pattern),
);
}
} while (treeWalker.nextNode());
createTreeWalker(
body,
(textNode) => {
//@ts-ignore textNode.nodeValue is not null.
const matches = [...textNode.nodeValue.matchAll(pattern)];
if (matches.length > 0) {
const fragmentedTextNode = fragmentTextNode(
fragmentsToHighlight,
matches,
textNode,
newText,
);
replacements.push({
nodeToReplace: textNode,
replacement: fragmentedTextNode,
});
saveModification(
textNode.parentElement as HTMLElement,
newText,
cleanPattern(pattern),
);
}
}
)
//biome-ignore lint/complexity/noForEach: I'll replace later
replacements.forEach(({ nodeToReplace, replacement }) => {
if (nodeToReplace.parentElement)
Expand All @@ -86,36 +73,24 @@ class UniversalReplace {
for (const mutation of mutations) {
if (mutation.addedNodes != null && mutation.addedNodes.length > 0) {
for (const node of mutation.addedNodes) {
if (
node instanceof Text &&
node.parentElement &&
!(node.parentElement instanceof HTMLScriptElement) &&
!(node.parentElement instanceof HTMLStyleElement) &&
node.textContent !== null &&
!/^\s*$/.test(node.textContent)
) {
const replace = this.matchReplacePattern(node.textContent);
if (replace) {
const treeWalker = document.createTreeWalker(
node,
NodeFilter.SHOW_TEXT,
);
let textNode: Node;
do {
textNode = treeWalker.currentNode;
if (textNode.nodeValue === null) continue;
textNode.nodeValue = textNode.nodeValue.replace(
replace.pattern,
replaceFirstLetterCase(replace.replace),
);
} while (treeWalker.nextNode());
}
}
createTreeWalker(
node,
(textNode) => {
//@ts-ignore textNode.nodeValue is not null.
const replace = this.matchReplacePattern(textNode.textContent);
if (replace) {
//@ts-ignore textNode.nodeValue is not null.
textNode.nodeValue = textNode.nodeValue.replace(
replace.pattern,
replaceFirstLetterCase(replace.replace),
);
}
}
)
}
}
}
});

this.observer.observe(document, { childList: true, subtree: true });
}

Expand All @@ -135,6 +110,27 @@ class UniversalReplace {
const cleanPattern = (pattern: RegExp) =>
pattern.toString().replaceAll("/", "").replace("gi", "");

const createTreeWalker = (rootElement: Node, iterator: (textNode: Node) => void) => {
const treeWalker = document.createTreeWalker(
rootElement,
NodeFilter.SHOW_TEXT,
(node) => {
if (
node.parentElement instanceof HTMLScriptElement ||
node.parentElement instanceof HTMLStyleElement
)
return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
}
)
let textNode: Node;
do {
textNode = treeWalker.currentNode;
if (textNode.nodeValue === null || !textNode?.textContent?.trim()) continue;
iterator(textNode)
} while (treeWalker.nextNode());
}

const replaceFirstLetterCase = (value: string) => {
return (match: string) => {
// Check if the first letter in the match is uppercase
Expand Down

0 comments on commit 8bed712

Please sign in to comment.