Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some oddities in xmlEscape #143

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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();
});