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

xml entity encoding for carriage return and line feed charaters #3733

Merged
merged 1 commit into from
Apr 29, 2021
Merged
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
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;');
});
});