Fix /mirror subtab clicks (HTML-escaped < in highlightCode regex broke entire script)

#9
by ArthurZ HF Staff - opened
SlyFox org

What this fixes

The Buddy mirror / AI emotions sub-tab buttons silently did nothing after PR #7 merged.

Root cause

In highlightCode(), the lookbehind regex was written into the HTML as (?&lt;![a-zA-Z_])(\d+). The < got HTML-escaped to &lt; and ended up inside the JS regex literal. When the browser parses the script, the JS engine sees (?&lt;!...) inside the regex source, treats it as invalid, and throws a SyntaxError on script parse.

A parse-time SyntaxError kills the entire <script> block. None of it ever runs. That includes the line that registers the subtab click handler:

$("subtabs").querySelectorAll("button").forEach(b => b.addEventListener("click", () => setTab(b.dataset.tab)));

So clicks did nothing.

Fix

One-character change in mirror.html:

- .replace(/(?&lt;![a-zA-Z_])(\d+)\b/g, '<span class="n">$1</span>');
+ .replace(/(?<![a-zA-Z_])(\d+)\b/g, '<span class="n">$1</span>');

Verified the script now parses cleanly. The remaining &lt;/&gt; occurrences in the file are only inside the escapeHtml() mapping table where they belong as the actual entity strings.

Why a new PR

The fix was pushed to refs/pr/7 after PR #7 was merged, so it never reached main. This PR re-applies it on top of current main (92cd143c).

ArthurZ changed pull request status to open
ArthurZ changed pull request status to merged

Sign up or log in to comment