From be51114635d0cb24d1b8a0a1001f02291211401a Mon Sep 17 00:00:00 2001 From: Louis-Dominique Dubeau Date: Fri, 11 Oct 2019 13:59:00 -0400 Subject: [PATCH] fix: implement attribute normalization Section 3.3.3 of the XML specification. Fixes #24 --- lib/saxes.js | 9 ++++++++- test/eol-handling.js | 12 ++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/saxes.js b/lib/saxes.js index aa5bca16..e127fc57 100644 --- a/lib/saxes.js +++ b/lib/saxes.js @@ -1907,8 +1907,10 @@ class SaxesParser { this.state = S_ENTITY; this.entityReturnState = S_ATTRIB_VALUE_QUOTED; return; + case NL: case NL_LIKE: - this.text += `${chunk.slice(start, this.prevI)}\n`; + case TAB: + this.text += `${chunk.slice(start, this.prevI)} `; start = this.i; break; case LESS: @@ -1947,6 +1949,11 @@ class SaxesParser { /** @private */ sAttribValueUnquoted() { + // We don't do anything regarding EOL or space handling for unquoted + // attributes. We already have failed by the time we get here, and the + // contract that saxes upholds states that upon failure, it is not safe to + // rely on the data passed to event handlers (other than + // ``onerror``). Passing "bad" data is not a problem. const c = this.captureTo(ATTRIB_VALUE_UNQUOTED_TERMINATOR); switch (c) { case AMP: diff --git a/test/eol-handling.js b/test/eol-handling.js index 0565886a..c8dd3933 100644 --- a/test/eol-handling.js +++ b/test/eol-handling.js @@ -25,7 +25,7 @@ describe("eol handling", () => { ["opentag", { name: "moo", attributes: { - a: "12\n 3", + a: "12 3", }, isSelfClosing: false, }], @@ -33,7 +33,7 @@ describe("eol handling", () => { ["closetag", { name: "moo", attributes: { - a: "12\n 3", + a: "12 3", }, isSelfClosing: false, }], @@ -145,8 +145,8 @@ SYSTEM ["opentag", { name: "moo", attributes: { - a: "12\n 3", - b: "\nz\n", + a: "12 3", + b: " z ", }, isSelfClosing: false, }], @@ -178,8 +178,8 @@ abc ["closetag", { name: "moo", attributes: { - a: "12\n 3", - b: "\nz\n", + a: "12 3", + b: " z ", }, isSelfClosing: false, }],