From 325a91bce27e1914d20b63af1a618e370decfcc7 Mon Sep 17 00:00:00 2001 From: rdrew Date: Tue, 10 Nov 2026 11:10:04 -0400 Subject: [PATCH] annotation spaces --- js/fix-annotation-spaces.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/js/fix-annotation-spaces.js b/js/fix-annotation-spaces.js index 5f1322b..64d2c9e 100644 --- a/js/fix-annotation-spaces.js +++ b/js/fix-annotation-spaces.js @@ -1,28 +1,35 @@ (function () { - // Guard against running multiple observers if the script is loaded more than once if (window.__miradorSpaceFixInitialized) return; window.__miradorSpaceFixInitialized = true; const observer = new MutationObserver((mutations) => { - // Target any third-party HTML content blocks in Mirador that haven't been cleaned yet const elements = document.querySelectorAll( ".mirador-third-party-html:not([data-fixed])" ); - if (elements.length > 0) { - elements.forEach((el) => { - // Swap non-breaking spaces for normal breakable spaces - el.innerHTML = el.innerHTML - .replace(/ /g, " ") - .replace(/\u00A0/g, " "); + elements.forEach((el) => { + // Create a TreeWalker to find all text nodes inside this container + const walker = document.createTreeWalker( + el, + NodeFilter.SHOW_TEXT, + null, + false + ); + let node; - // Mark element so we don't re-process it on subsequent mutations - el.setAttribute("data-fixed", "true"); - }); - } + while ((node = walker.nextNode())) { + // Replace both non-breaking space characters and entity text if present + if (node.nodeValue) { + node.nodeValue = node.nodeValue + .replace(/\u00A0/g, " ") + .replace(/ /g, " "); + } + } + + el.setAttribute("data-fixed", "true"); + }); }); - // Start observing the document body for dynamically added Mirador elements observer.observe(document.body, { childList: true, subtree: true,