Skip to content
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
13 changes: 10 additions & 3 deletions src/lib/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ export class Parser {
/**
* Adds the given _text_ to the document, either by appending it to a
* preceding `XmlText` node (if possible) or by creating a new `XmlText` node.
*
* When _normalize_ is `true` (the default), line breaks in _text_ are
* normalized per section 2.11 of the XML spec. This must be `false` for text
* that comes from a character or entity reference, since references aren't
* subject to line break normalization.
*/
addText(text: string, charIndex: number) {
addText(text: string, charIndex: number, normalize = true) {
let { children } = this.currentNode;
let { length } = children;

text = normalizeLineBreaks(text);
if (normalize) {
text = normalizeLineBreaks(text);
}

if (length > 0) {
let prevNode = children[length - 1];
Expand Down Expand Up @@ -297,7 +304,7 @@ export class Parser {
let ref = this.consumeReference();

return ref
? this.addText(ref, startIndex)
? this.addText(ref, startIndex, false)
: false;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/lib/Parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ describe('Parser', () => {
assert.strictEqual(root.attributes.d, " a z ");
});

// A character reference for a carriage return (`
` / `
`) in content
// must be preserved as a literal `\r`. Line break normalization only applies
// to literal line breaks in the input, not to characters that come from a
// character reference.
// https://www.w3.org/TR/2008/REC-xml-20081126/#sec-line-ends
// https://www.w3.org/TR/2008/REC-xml-20081126/#entproc
it("doesn't normalize a character reference for a carriage return in content", () => {
assert.strictEqual(parseXml('<a>&#xD;</a>').root.children[0].text, '\r');
assert.strictEqual(parseXml('<a>&#13;</a>').root.children[0].text, '\r');

// A literal `\r` is normalized to `\n`, but a `&#xD;` reference following
// it must remain a `\r`.
assert.strictEqual(parseXml('<a>x\r&#xD;y</a>').root.children[0].text, 'x\n\ry');

// `&#xD;&#xA;` must remain `\r\n` rather than collapsing to `\n` or `\n\n`.
assert.strictEqual(parseXml('<a>&#xD;&#xA;</a>').root.children[0].text, '\r\n');
});

it('handles many character references in a single attribute', () => {
let { root } = parseXml('<a b="&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;"/>');
assert.strictEqual(root.attributes.b, "<".repeat(35));
Expand Down
Loading