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
28 changes: 28 additions & 0 deletions spec/entities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,31 @@ describe("External Entities", function () {
expect(output.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
});

describe("Carriage return", function () {
const CR = String.fromCharCode(13); // U+000D
const LF = String.fromCharCode(10); // U+000A
const TAB = String.fromCharCode(9); // U+0009

it("escapes a carriage return as 
 in text and attributes", function () {
const builder = new XMLBuilder({ ignoreAttributes: false });
expect(builder.build({ n: "A" + CR + "B" })).toEqual("<n>A&#13;B</n>");
expect(builder.build({ n: { "@_a": "A" + CR + "B" } })).toEqual(`<n a="A&#13;B"></n>`);
});

it("preserves a carriage return through a build -> parse round trip", function () {
// A raw CR would be silently folded to LF on parse (XML 1.0 2.11).
const builder = new XMLBuilder({ ignoreAttributes: false });
const parser = new XMLParser({ ignoreAttributes: false, htmlEntities: true });
for (const value of ["A" + CR + "B", "line1" + CR + LF + "line2"]) {
expect(parser.parse(builder.build({ n: value })).n).toEqual(value);
expect(parser.parse(builder.build({ n: { "@_a": value } })).n["@_a"]).toEqual(value);
}
});

it("leaves LF and TAB unescaped", function () {
const builder = new XMLBuilder();
expect(builder.build({ n: "A" + LF + "B" })).toEqual("<n>A" + LF + "B</n>");
expect(builder.build({ n: "A" + TAB + "B" })).toEqual("<n>A" + TAB + "B</n>");
});
});
3 changes: 2 additions & 1 deletion src/fxb.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const defaultOptions = {
{ regex: new RegExp(">", "g"), val: "&gt;" },
{ regex: new RegExp("<", "g"), val: "&lt;" },
{ regex: new RegExp("\'", "g"), val: "&apos;" },
{ regex: new RegExp("\"", "g"), val: "&quot;" }
{ regex: new RegExp("\"", "g"), val: "&quot;" },
{ regex: new RegExp("\\r", "g"), val: "&#13;" }//a raw CR is folded to LF on parse (XML 1.0 2.11)
],
processEntities: true,
stopNodes: [],
Expand Down