fix(viewer): pretty print fallback tokenizer quand highlight.js ne produit aucun token (txt/log/plaintext) (#89)
CI / lint (push) Successful in 1m18s
CI / security (push) Successful in 56s
CI / test (push) Successful in 2m57s
CI / build (push) Successful in 48s
CI / e2e (push) Successful in 10m47s

This commit is contained in:
2026-09-14 19:43:50 -04:00
parent d3f7a04411
commit bb0c8e7731
4 changed files with 100 additions and 4 deletions
+18 -2
View File
@@ -40,8 +40,11 @@ function escapeHtml(s) {
const sandbox = { escapeHtml };
vm.createContext(sandbox);
vm.runInContext(`${extractFn("detectPrettyLang")}\n${extractFn("buildPrettyHtml")}\nglobalThis.detectPrettyLang = detectPrettyLang; globalThis.buildPrettyHtml = buildPrettyHtml;`, sandbox);
const { detectPrettyLang, buildPrettyHtml } = sandbox;
vm.runInContext(
`${extractFn("detectPrettyLang")}\n${extractFn("buildPrettyHtml")}\n${src.slice(src.indexOf("const PP_TOKEN_RE"), src.indexOf("function applyPrettyHighlight"))}\nglobalThis.fallbackTokenize = fallbackTokenize;`,
sandbox,
);
const { detectPrettyLang, buildPrettyHtml, fallbackTokenize } = sandbox;
console.log("Testing detectPrettyLang...");
assert.equal(detectPrettyLang({ is_json: true }), "json");
@@ -68,4 +71,17 @@ const xss = buildPrettyHtml({ extension: ".py", html: "" }, 'print("<script>aler
assert.ok(!xss.includes("<script>"), "no raw script tag injected");
assert.ok(xss.includes("&lt;script&gt;"), "script tag escaped");
console.log("Testing fallbackTokenize (log levels, strings, comments)...");
const fb = fallbackTokenize('2026-09-14 ERROR failed to open "/var/db"\n# note simple\nvalue=42');
assert.ok(fb.includes("pp-err"), "ERROR level colored");
assert.ok(fb.includes("hljs-string"), "quoted string colored");
assert.ok(fb.includes("hljs-comment"), "comment colored");
assert.ok(fb.includes("hljs-number"), "number colored");
assert.ok(fb.includes("hljs-attr"), "key= colored");
console.log("Testing fallbackTokenize (no html injection)...");
const fbXss = fallbackTokenize('bad <img src=x onerror="alert(1)">');
assert.ok(!/<img/.test(fbXss), "no raw img tag");
assert.ok(fbXss.includes("&lt;img"), "img escaped");
console.log("✅ All pretty-print tests passed successfully!");