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
45 changes: 45 additions & 0 deletions spec/cdata_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,49 @@ patronymic</person></root>`;
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});

it("should preserve carriage returns in CDATA", function () {
const xmlData = `<properties object="" engine="">
<property type="string" name="x" state="changed">
<![CDATA[This is a carriage return \r...]]>
</property>
<property type="string" name="y" state="changed">
<![CDATA[\r]]>
</property>
</properties>`;

const expected = {
"properties": {
"property": [
{
"#value": "This is a carriage return \r...",
"@type": "string",
"@name": "x",
"@state": "changed"
},
{
"#value": "\r",
"@type": "string",
"@name": "y",
"@state": "changed"
}
],
"@object": "",
"@engine": ""
}
};

const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
parseAttributeValue: false,
parseTagValue: false,
textNodeName: "#value",
};

const parser = new XMLParser(options);
let result = parser.parse(xmlData);

expect(result).toEqual(expected);
});
});
51 changes: 49 additions & 2 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,55 @@ function buildAttributesMap(attrStr, jPath, tagName, force = false) {
return attrs;
}
}
function normalizeLineEndingsOutsideCDATA(xmlData) {
if (xmlData.indexOf("\r") === -1) return xmlData;

let normalizedXml = "";
let segmentStart = 0;
let insideTag = false;
let quoteChar = "";

for (let i = 0; i < xmlData.length; i++) {
const ch = xmlData[i];

if (insideTag) {
if (quoteChar) {
if (ch === quoteChar) quoteChar = "";
} else if (ch === '"' || ch === "'") {
quoteChar = ch;
} else if (ch === ">") {
insideTag = false;
}
} else if (ch === "<") {
if (xmlData.startsWith("<![CDATA[", i)) {
normalizedXml += xmlData.substring(segmentStart, i).replace(/\r\n?/g, "\n");

const cdataEnd = xmlData.indexOf("]]>", i + 9);
if (cdataEnd === -1) {
normalizedXml += xmlData.substring(i);
return normalizedXml;
}

const cdataCloseIndex = cdataEnd + 3;
normalizedXml += xmlData.substring(i, cdataCloseIndex);
i = cdataCloseIndex - 1;
segmentStart = cdataCloseIndex;
} else if (xmlData.startsWith("<!--", i)) {
const commentEnd = xmlData.indexOf("-->", i + 4);
if (commentEnd === -1) break;
i = commentEnd + 2;
} else {
insideTag = true;
}
}
}

normalizedXml += xmlData.substring(segmentStart).replace(/\r\n?/g, "\n");
return normalizedXml;
}

const parseXml = function (xmlData) {
xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
xmlData = normalizeLineEndingsOutsideCDATA(xmlData);
const xmlObj = new xmlNode('!xml');
let currentNode = xmlObj;
let textData = "";
Expand Down Expand Up @@ -835,4 +882,4 @@ function sanitizeName(name, options) {
return options.onDangerousProperty(name);
}
return name;
}
}