49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
const {test} = require('node:test');
|
|
const assert = require('node:assert');
|
|
const xml = require('../../src/util/xml-escape');
|
|
|
|
test('escape', () => {
|
|
const input = '<foo bar="he & llo \'"></foo>';
|
|
const output = '<foo bar="he & llo '"></foo>';
|
|
assert.strictEqual(xml(input), output);
|
|
});
|
|
|
|
test('xmlEscape (more)', () => {
|
|
const empty = '';
|
|
assert.strictEqual(xml(empty), empty);
|
|
|
|
const safe = 'hello';
|
|
assert.strictEqual(xml(safe), safe);
|
|
|
|
const unsafe = '< > & \' "';
|
|
assert.strictEqual(xml(unsafe), '< > & ' "');
|
|
|
|
const single = '&';
|
|
assert.strictEqual(xml(single), '&');
|
|
|
|
const mix = '<a>b& c\'def_-"';
|
|
assert.strictEqual(xml(mix), '<a>b& c'def_-"');
|
|
|
|
const dupes = '<<&_"_"_&>>';
|
|
assert.strictEqual(xml(dupes), '<<&_"_"_&>>');
|
|
|
|
const emoji = '(>^_^)>';
|
|
assert.strictEqual(xml(emoji), '(>^_^)>');
|
|
});
|
|
|
|
test('xmlEscape should handle non strings', () => {
|
|
const array = ['hello', 'world'];
|
|
assert.strictEqual(xml(array), String(array));
|
|
|
|
const arrayWithSpecialChar = ['hello', '<world>'];
|
|
assert.strictEqual(xml(arrayWithSpecialChar), 'hello,<world>');
|
|
|
|
const arrayWithNumbers = [1, 2, 3];
|
|
assert.strictEqual(xml(arrayWithNumbers), '1,2,3');
|
|
|
|
// Objects shouldn't get provided to replaceUnsafeChars, but in the event
|
|
// they do, it should just return the object (and log an error)
|
|
const object = {hello: 'world'};
|
|
assert.strictEqual(xml(object), object);
|
|
});
|