Skip to content

Commit 4ab773c

Browse files
committed
j2x: suppress empty nodes to self closing node if configured
1 parent 19446ab commit 4ab773c

File tree

2 files changed

+88
-17
lines changed

2 files changed

+88
-17
lines changed

spec/j2x_spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,42 @@ describe("XMLParser", function () {
235235
expect(result).toEqual(expected);
236236
});
237237

238+
it("should supress empty node to self closing node when parsing to XML", function () {
239+
var jObj = {
240+
a : {
241+
"@": {
242+
b : "val>1",
243+
c : "val<2"
244+
},
245+
"#text": "text\\cvalue>\\c",
246+
tag: {
247+
k: 34,
248+
g: "",
249+
nested: {
250+
"@": {
251+
b : "val>1",
252+
c : "val<2"
253+
}
254+
}
255+
},
256+
"__cdata": [
257+
"this text is > from CDATA",
258+
""
259+
]
260+
}
261+
};
262+
var parser = new Parser({
263+
cdataTagName : "__cdata",
264+
attrNodeName : "@",
265+
encodeHTMLchar: true,
266+
supressEmptyNode: true
267+
});
268+
var result = parser.parse(jObj);
269+
//console.log(result);
270+
var expected = '<a b="val&gt;1" c="val&lt;2"><tag><k>34</k><g/><nested b="val&gt;1" c="val&lt;2"/></tag>text<![CDATA[this text is > from CDATA]]>value&gt;<![CDATA[]]></a>';
271+
expect(result).toEqual(expected);
272+
});
273+
238274

239275
/* it("should format when parsing to XML", function () {
240276
var jObj = {

src/j2x.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ var defaultOptions = {
1010
cdataTagName: false,
1111
cdataPositionChar: "\\c",
1212
format: false,
13-
indentBy: " "
13+
indentBy: " ",
14+
supressEmptyNode: false
1415
};
1516

1617
function Parser(options){
@@ -44,6 +45,17 @@ function Parser(options){
4445
this.newLine = "";
4546
}
4647

48+
if(this.options.supressEmptyNode){
49+
this.buildTextNode = buildEmptyTextNode;
50+
this.buildObjNode = buildEmptyObjNode;
51+
}else{
52+
this.buildTextNode = buildTextValNode;
53+
this.buildObjNode = buildObjectNode;
54+
}
55+
56+
this.buildTextValNode = buildTextValNode;
57+
this.buildObjectNode = buildObjectNode;
58+
4759
}
4860

4961

@@ -75,7 +87,7 @@ Parser.prototype.j2x = function(jObj,level){
7587
val += this.encodeHTMLchar(jObj[key]);
7688
}
7789
}else{
78-
val += this.indentate(level) + "<" + key + ">" +this.encodeHTMLchar(jObj[key])+"</"+key+ this.tagEndChar;
90+
val += this.buildTextNode(jObj[key],key,"",level);
7991
}
8092
}
8193
}else if(Array.isArray(jObj[key])){//repeated nodes
@@ -94,15 +106,10 @@ Parser.prototype.j2x = function(jObj,level){
94106
var item = jObj[key][j];
95107
if(typeof item === "object"){
96108
var result = this.j2x(item,level+1);
97-
val += this.indentate(level)
98-
+ "<"+key + result.attrStr
99-
+ this.tagEndChar
100-
+ result.val
101-
+ this.newLine
102-
+ this.indentate(level)
103-
+ "</"+key+this.tagEndChar;
109+
val += this.buildObjNode(result.val,key,result.attrStr,level);
104110
}else{
105-
val += this.indentate(level) + "<"+key+">"+ this.encodeHTMLchar(item) + "</"+key+this.tagEndChar;
111+
val += this.buildTextNode(item,key,"",level);
112+
//val += this.indentate(level) + "<"+key+">"+ this.encodeHTMLchar(item) + "</"+key+this.tagEndChar;
106113
}
107114
}
108115
}
@@ -116,13 +123,7 @@ Parser.prototype.j2x = function(jObj,level){
116123
}
117124
}else{
118125
var result = this.j2x(jObj[key],level+1);
119-
val += this.indentate(level)
120-
+ "<" + key + result.attrStr
121-
+ this.tagEndChar
122-
+ result.val
123-
+ this.newLine
124-
+ this.indentate(level)
125-
+ "</"+key+this.tagEndChar;
126+
val += this.buildObjNode(result.val,key,result.attrStr,level);
126127
}
127128
}
128129
}
@@ -150,6 +151,40 @@ function replaceCDATAarr(str,cdata){
150151
}
151152
}
152153

154+
function buildObjectNode(val,key,attrStr,level){
155+
return this.indentate(level)
156+
+ "<" + key + attrStr
157+
+ this.tagEndChar
158+
+ val
159+
+ this.newLine
160+
+ this.indentate(level)
161+
+ "</"+key+this.tagEndChar;
162+
}
163+
164+
function buildEmptyObjNode(val,key,attrStr,level){
165+
if(val !== "" ){
166+
return this.buildObjectNode(val,key,attrStr,level);
167+
}else{
168+
return this.indentate(level)
169+
+ "<" + key + attrStr
170+
+ "/"
171+
+ this.tagEndChar
172+
//+ this.newLine
173+
}
174+
}
175+
176+
function buildTextValNode(val,key,attrStr,level){
177+
return this.indentate(level) + "<" + key + attrStr + ">" +this.encodeHTMLchar(val)+"</"+key+ this.tagEndChar;
178+
}
179+
180+
function buildEmptyTextNode(val,key,attrStr,level){
181+
if(val !== ""){
182+
return this.buildTextValNode(val,key,attrStr,level);
183+
}else{
184+
return this.indentate(level) + "<" + key + attrStr + "/" + this.tagEndChar;
185+
}
186+
}
187+
153188
function indentate(level){
154189
return this.options.indentBy.repeat(level);
155190
}

0 commit comments

Comments
 (0)