Skip to content

Commit

Permalink
xml entity encoding for more characoters (#3733)
Browse files Browse the repository at this point in the history
XML entity encoding for carriage return, line feed, next line, and line separator characters
  • Loading branch information
AllanZhengYP authored Apr 29, 2021
1 parent bfb22c7 commit e9352e4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-Serializer-1f02d1b1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "Serializer",
"description": "XML entity encoding for carriage return, line feed, next line, and line separator characters"
}
8 changes: 7 additions & 1 deletion lib/xml/escape-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
* Escapes characters that can not be in an XML element.
*/
function escapeElement(value) {
return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
return value.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\r/g, '&#x0D;')
.replace(/\n/g, '&#x0A;')
.replace(/\u0085/g, '&#x85;')
.replace(/\u2028/, '&#x2028;');
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/xml/escape-element.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var escapeElement = require('../../lib/xml/escape-element').escapeElement;

describe('escape-element', function() {
it('escapes: & < >', function() {
var value = 'abc 123 &<>"%';
expect(escapeElement(value)).to.equal('abc 123 &amp;&lt;&gt;"%');
it('escapes: & < > \\r \\n \u0085\u2028', function() {
var value = 'abc 123 &<>"%\r\n\u0085\u2028';
expect(escapeElement(value)).to.equal('abc 123 &amp;&lt;&gt;"%&#x0D;&#x0A;&#x85;&#x2028;');
});
});

0 comments on commit e9352e4

Please sign in to comment.