fix(viewer): pretty print fallback tokenizer quand highlight.js ne produit aucun token (txt/log/plaintext) (#89)
This commit is contained in:
@@ -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("<script>"), "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("<img"), "img escaped");
|
||||
|
||||
console.log("✅ All pretty-print tests passed successfully!");
|
||||
|
||||
Reference in New Issue
Block a user