Skip to content

Commit 3344f16

Browse files
committed
Fix trim option for empty tags
1 parent 7bc3c5d commit 3344f16

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/parser.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class exports.Parser extends events
133133
s = stack[stack.length - 1]
134134
# remove the '#' key altogether if it's blank
135135
if obj[charkey].match(/^\s*$/) and not cdata
136-
emptyStr = obj[charkey]
136+
emptyStr = if @options.trim then obj[charkey].trim() else obj[charkey]
137137
delete obj[charkey]
138138
else
139139
obj[charkey] = obj[charkey].trim() if @options.trim

test/parser.test.coffee

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ module.exports =
257257
'test text no trimming, no normalize': skeleton(trim: false, normalize: false, (r) ->
258258
equ r.sample.whitespacetest[0]._, '\n Line One\n Line Two\n ')
259259

260+
'test trimming on empty tag with whitespace': skeleton(trim: true, explicitArray: false, (r) ->
261+
equ r.sample.emptytestanother, '')
262+
260263
'test enabled root node elimination': skeleton(__xmlString: '<root></root>', explicitRoot: false, (r) ->
261264
console.log 'Result object: ' + util.inspect r, false, 10
262265
assert.deepEqual r, '')
@@ -599,7 +602,7 @@ module.exports =
599602
'test valueProcessors key param': skeleton(valueProcessors: [replaceValueByName], (r)->
600603
console.log 'Result object: ' + util.inspect r, false, 10
601604
equ r.sample.valueProcessTest[0], 'valueProcessTest')
602-
605+
603606
'test parseStringPromise parsing': (test) ->
604607
x2js = new xml2js.Parser()
605608
readFilePromise(fileName).then (data) ->
@@ -610,7 +613,7 @@ module.exports =
610613
test.finish()
611614
.catch (err) ->
612615
test.fail('Should not error')
613-
616+
614617
'test parseStringPromise with bad input': (test) ->
615618
x2js = new xml2js.Parser()
616619
x2js.parseStringPromise("< a moose bit my sister>").then (r) ->
@@ -640,7 +643,7 @@ module.exports =
640643
test.finish()
641644
.catch (err) ->
642645
test.fail('Should not error')
643-
646+
644647
'test global parseStringPromise with bad input': (test) ->
645648
xml2js.parseStringPromise("< a moose bit my sister>").then (r) ->
646649
test.fail('Should fail')

0 commit comments

Comments
 (0)