Skip to content

Commit 280cd63

Browse files
authored
Fix null CDATA to comply with undefined behavior (#701)
1 parent e132656 commit 280cd63

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

spec/j2x_spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ describe("XMLBuilder", function() {
108108
expect(result).toEqual(expected);
109109
});
110110

111+
it('should parse to XML with empty CDATA', function() {
112+
const jObj = {
113+
a: {
114+
$cdata: null,
115+
},
116+
b: {
117+
$cdata: undefined,
118+
},
119+
};
120+
const builder = new XMLBuilder({ cdataPropName: '$cdata' });
121+
const result = builder.build(jObj);
122+
//console.log(result);
123+
const expected = `<a></a><b></b>`;
124+
expect(result).toEqual(expected);
125+
});
126+
111127
it("should parse to XML with attributes as separate node", function() {
112128
const jObj = {
113129
a: {

src/xmlbuilder/json2xml.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Builder.prototype.j2x = function(jObj, level, ajPath) {
9292
// null attribute should be ignored by the attribute list, but should not cause the tag closing
9393
if (this.isAttribute(key)) {
9494
val += '';
95+
} else if (key === this.options.cdataPropName) {
96+
val += '';
9597
} else if (key[0] === '?') {
9698
val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
9799
} else {

0 commit comments

Comments
 (0)