From 5b1f83a7077e6c66bfeb329479ec1280bb08305e Mon Sep 17 00:00:00 2001 From: kings9527 <3350474162@qq.com> Date: Fri, 29 May 2026 13:17:22 +0800 Subject: [PATCH] fix: preserve carriage return characters in CDATA and text nodes Remove the line that globally replaced \r and \r\n with \n in the input XML data. This replacement was incorrectly converting carriage returns inside CDATA sections and text nodes, which should be preserved as-is. The existing TODO comment even suggested removing this line. Closes #512 - Removed: xmlData.replace(/\r\n?/g, "\n") from OrderedObjParser.js - Added unit test in cdata_spec.js to verify \r preservation in CDATA and text nodes - All 315 tests pass with this change --- spec/cdata_spec.js | 25 ++++++++++++++++++------- src/xmlparser/OrderedObjParser.js | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spec/cdata_spec.js b/spec/cdata_spec.js index 821179a5..c93b9fce 100644 --- a/spec/cdata_spec.js +++ b/spec/cdata_spec.js @@ -368,19 +368,30 @@ patronymic`; expect(result).toEqual(expected); }); - it("should not process entities in CDATA", function () { - const xmlData = ``; + it("should preserve carriage return characters in CDATA and text nodes", function () { + const xmlData = ` + + + line1\rline2 + `; - const expected = { xml: '<text>' }; + const expected = { + "properties": { + "property": [ + "This is a carriage return \r...", + "\r" + ], + "text": "line1\rline2" + } + }; const options = { - ignoreAttributes: false, + parseTagValue: false, + trimValues: false }; - const parser = new XMLParser(options); let result = parser.parse(xmlData); - - // console.log(JSON.stringify(result,null,4)); + // console.log(JSON.stringify(result, null, 4)); expect(result).toEqual(expected); }); }); diff --git a/src/xmlparser/OrderedObjParser.js b/src/xmlparser/OrderedObjParser.js index ec8d24d4..83f2ae3b 100644 --- a/src/xmlparser/OrderedObjParser.js +++ b/src/xmlparser/OrderedObjParser.js @@ -275,7 +275,7 @@ function buildAttributesMap(attrStr, jPath, tagName, force = false) { } } const parseXml = function (xmlData) { - xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line + // xmlData = xmlData.replace(/\r\n?/g, "\n"); // Removed: incorrectly replaces \r in CDATA and text nodes const xmlObj = new xmlNode('!xml'); let currentNode = xmlObj; let textData = "";