Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/util/xml-escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ const log = require('./log');
*/
const xmlEscape = function (unsafe) {
if (typeof unsafe !== 'string') {
if (Array.isArray(unsafe)) {
// This happens when we have hacked blocks from 2.0
// See #1030
unsafe = String(unsafe);
} else {
log.error('Unexpected input recieved in replaceUnsafeChars');
return unsafe;
}
log.error('Unexpected input recieved in xmlEscape');
unsafe = String(unsafe);
}
return unsafe.replace(/[<>&'"]/g, c => {
switch (c) {
Expand Down
15 changes: 15 additions & 0 deletions test/unit/tw-xml-escape-returns-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const {test} = require('tap');
const xmlEscape = require('../../src/util/xml-escape');

test('xmlEscape always returns a string', t => {
// Logging errors during this test is expected.
t.equal(xmlEscape('<< >> "\'&"\'&'), '&lt;&lt; &gt;&gt; &quot;&apos;&amp;&quot;&apos;&amp;');
t.equal(xmlEscape(null), 'null');
t.equal(xmlEscape(undefined), 'undefined');
t.equal(xmlEscape(5), '5');
t.equal(xmlEscape(true), 'true');
t.equal(xmlEscape(false), 'false');
t.equal(xmlEscape(['<', '>']), '&lt;,&gt;');
t.equal(xmlEscape({a: 'whatever'}), '[object Object]');
t.end();
});