Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'JacksonTian-fix_spaces_node'

  • Loading branch information...
commit 787f2efca086ccd95486450b29dbcb85117399e6 2 parents 590d619 + 26c8677
@Leonidas-from-XIV authored
Showing with 15 additions and 7 deletions.
  1. +4 −3 lib/xml2js.js
  2. +3 −2 src/xml2js.coffee
  3. +8 −2 test/xml2js.test.coffee
View
7 lib/xml2js.js
@@ -146,12 +146,13 @@
return stack.push(obj);
};
this.saxParser.onclosetag = function() {
- var node, nodeName, obj, old, s, xpath;
+ var node, nodeName, obj, old, s, xpath, emptyStr;
obj = stack.pop();
nodeName = obj["#name"];
delete obj["#name"];
s = stack[stack.length - 1];
if (obj[charkey].match(/^\s*$/)) {
+ emptyStr = obj[charkey];
delete obj[charkey];
} else {
if (_this.options.trim) {
@@ -164,8 +165,8 @@
obj = obj[charkey];
}
}
- if ((isEmpty(obj)) && stack.length > 0) {
- obj = _this.options.emptyTag !== void 0 ? _this.options.emptyTag : '';
+ if ((isEmpty(obj))) {
+ obj = _this.options.emptyTag !== void 0 ? _this.options.emptyTag : emptyStr;
}
if (_this.options.validator != null) {
xpath = "/" + ((function() {
View
5 src/xml2js.coffee
@@ -128,6 +128,7 @@ class exports.Parser extends events.EventEmitter
s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
if obj[charkey].match(/^\s*$/)
+ emptyStr = obj[charkey]
delete obj[charkey]
else
obj[charkey] = obj[charkey].trim() if @options.trim
@@ -137,11 +138,11 @@ class exports.Parser extends events.EventEmitter
if Object.keys(obj).length == 1 and charkey of obj and not @EXPLICIT_CHARKEY
obj = obj[charkey]
- if (isEmpty obj) and stack.length > 0
+ if (isEmpty obj)
obj = if @options.emptyTag != undefined
@options.emptyTag
else
- ''
+ emptyStr
if @options.validator?
xpath = "/" + (node["#name"] for node in stack).concat(nodeName).join("/")
View
10 test/xml2js.test.coffee
@@ -144,10 +144,10 @@ module.exports =
'test enabled root node elimination': skeleton(__xmlString: '<root></root>', explicitRoot: false, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
- assert.deepEqual r, {})
+ assert.deepEqual r, '')
'test disabled root node elimination': skeleton(__xmlString: '<root></root>', explicitRoot: true, (r) ->
- assert.deepEqual r, {root: {}})
+ assert.deepEqual r, {root: ''})
'test default empty tag result': skeleton(undefined, (r) ->
assert.deepEqual r.sample.emptytest, [''])
@@ -296,6 +296,12 @@ module.exports =
equ parsed.xml.Label[0], ''
test.finish()
+ 'test CDATA whitespaces result': (test) ->
+ xml = '<spacecdatatest><![CDATA[ ]]></spacecdatatest>'
+ xml2js.parseString xml, (err, parsed) ->
+ equ parsed.spacecdatatest, ' '
+ test.finish()
+
'test non-strict parsing': (test) ->
html = '<html><head></head><body><br></body></html>'
xml2js.parseString html, strict: false, (err, parsed) ->
Please sign in to comment.
Something went wrong with that request. Please try again.