diff --git a/web/js/textHighlight.js b/web/js/textHighlight.js index 3d87b32..add657e 100644 --- a/web/js/textHighlight.js +++ b/web/js/textHighlight.js @@ -220,12 +220,12 @@ async function syncText(inputEl, overlayEl, tries = 1) { const processNode = (node) => { if (node.nodeType === Node.TEXT_NODE) { - // Split and wrap text nodes return node.textContent .split(",") .map((tag) => { if (!tag.trim()) return tag; - return `${tag}`; + const escapedTag = tag.trim().replace(/"/g, """); + return `${tag}`; }) .join(","); } else if (node.nodeType === Node.ELEMENT_NODE) { @@ -234,8 +234,19 @@ async function syncText(inputEl, overlayEl, tries = 1) { const innerProcessed = Array.from(node.childNodes) .map((child) => processNode(child)) .join(""); - clone.innerHTML = innerProcessed; - return clone.outerHTML; + // Use textContent for text nodes to avoid HTML parsing + if ( + node.childNodes.length === 1 && + node.childNodes[0].nodeType === Node.TEXT_NODE + ) { + clone.textContent = node.textContent; + return `${clone.outerHTML}`; + } else { + clone.innerHTML = innerProcessed; + return clone.outerHTML; + } } return ""; }; diff --git a/web/js/util.js b/web/js/util.js index 99bb3cd..987cd03 100644 --- a/web/js/util.js +++ b/web/js/util.js @@ -33,15 +33,13 @@ export function leadingEdgeDebounce(func, wait) { }; } -export function escapeHtml(char) { - switch (char) { - case "<": - return "<"; - case ">": - return ">"; - default: - return char; - } +export function escapeHtml(text) { + return text + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); } export function interpolateColor(color1, color2, factor) {