import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import vm from 'node:vm';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
const resources = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../Source/XFESystem.Documents/Assets/Website');
const context = vm.createContext({ console });
for (const name of ['Highlight.min.js', 'Xaml.js', 'CSharp.js', 'Bash.js', 'PowerShell.js', 'Dos.js', 'Log.js', 'PlainText.js']) {
vm.runInContext(fs.readFileSync(path.join(resources, name), 'utf8'), context, { filename: name, timeout: 3000 });
}
const highlight = (source, language) => context.hljs.highlight(source, { language }).value;
test('Website JavaScript grammar colors keywords without executing the source', () => {
context.ShouldNotExecute = false;
const html = highlight('const text = "hello"; globalThis.ShouldNotExecute = true;', 'javascript');
assert.match(html, /hljs-keyword/);
assert.match(html, /hljs-string/);
assert.equal(context.ShouldNotExecute, false);
});
test('Website CSharp extension supports actual CSharp syntax highlighting', () => {
const html = highlight('public sealed class Sample { public string Name => "XFE"; }', 'csharp');
assert.match(html, /hljs-keyword/);
assert.match(html, /hljs-string/);
assert.match(html, /Sample/);
});
test('Website XAML extension escapes markup and provides token classes', () => {
const html = highlight('<Window Title="XFE"><TextBlock Text="Hello" /></Window>', 'xaml');
assert.match(html, /hljs-/);
assert.match(html, /</);
assert.doesNotMatch(html, /<Window/);
});
test('Highlighting untrusted code emits escaped HTML', () => {
const html = highlight('</script><img src=x onerror=alert(1)>', 'plaintext');
assert.doesNotMatch(html, /<img/);
assert.match(html, /<\/script>/);
});
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import vm from 'node:vm';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
const resources = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../Source/XFESystem.Documents/Assets/Website');
const context = vm.createContext({ console });
for (const name of ['Highlight.min.js', 'Xaml.js', 'CSharp.js', 'Bash.js', 'PowerShell.js', 'Dos.js', 'Log.js', 'PlainText.js']) {
vm.runInContext(fs.readFileSync(path.join(resources, name), 'utf8'), context, { filename: name, timeout: 3000 });
}
const highlight = (source, language) => context.hljs.highlight(source, { language }).value;
test('Website JavaScript grammar colors keywords without executing the source', () => {
context.ShouldNotExecute = false;
const html = highlight('const text = "hello"; globalThis.ShouldNotExecute = true;', 'javascript');
assert.match(html, /hljs-keyword/);
assert.match(html, /hljs-string/);
assert.equal(context.ShouldNotExecute, false);
});
test('Website CSharp extension supports actual CSharp syntax highlighting', () => {
const html = highlight('public sealed class Sample { public string Name => "XFE"; }', 'csharp');
assert.match(html, /hljs-keyword/);
assert.match(html, /hljs-string/);
assert.match(html, /Sample/);
});
test('Website XAML extension escapes markup and provides token classes', () => {
const html = highlight('<Window Title="XFE"><TextBlock Text="Hello" /></Window>', 'xaml');
assert.match(html, /hljs-/);
assert.match(html, /</);
assert.doesNotMatch(html, /<Window/);
});
test('Highlighting untrusted code emits escaped HTML', () => {
const html = highlight('</script><img src=x onerror=alert(1)>', 'plaintext');
assert.doesNotMatch(html, /<img/);
assert.match(html, /<\/script>/);
});