Skip to content

Commit

Permalink
Fix null CDATA to comply with undefined behavior (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelgors authored Dec 20, 2024
1 parent e132656 commit 280cd63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/j2x_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ describe("XMLBuilder", function() {
expect(result).toEqual(expected);
});

it('should parse to XML with empty CDATA', function() {
const jObj = {
a: {
$cdata: null,
},
b: {
$cdata: undefined,
},
};
const builder = new XMLBuilder({ cdataPropName: '$cdata' });
const result = builder.build(jObj);
//console.log(result);
const expected = `<a></a><b></b>`;
expect(result).toEqual(expected);
});

it("should parse to XML with attributes as separate node", function() {
const jObj = {
a: {
Expand Down
2 changes: 2 additions & 0 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Builder.prototype.j2x = function(jObj, level, ajPath) {
// null attribute should be ignored by the attribute list, but should not cause the tag closing
if (this.isAttribute(key)) {
val += '';
} else if (key === this.options.cdataPropName) {
val += '';
} else if (key[0] === '?') {
val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
} else {
Expand Down

0 comments on commit 280cd63

Please sign in to comment.