Skip to content

Commit

Permalink
Fix trim option for empty tags
Browse files Browse the repository at this point in the history
  • Loading branch information
AHarman committed Apr 19, 2022
1 parent 7bc3c5d commit 3344f16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class exports.Parser extends events
s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
if obj[charkey].match(/^\s*$/) and not cdata
emptyStr = obj[charkey]
emptyStr = if @options.trim then obj[charkey].trim() else obj[charkey]
delete obj[charkey]
else
obj[charkey] = obj[charkey].trim() if @options.trim
Expand Down
9 changes: 6 additions & 3 deletions test/parser.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ module.exports =
'test text no trimming, no normalize': skeleton(trim: false, normalize: false, (r) ->
equ r.sample.whitespacetest[0]._, '\n Line One\n Line Two\n ')

'test trimming on empty tag with whitespace': skeleton(trim: true, explicitArray: false, (r) ->
equ r.sample.emptytestanother, '')

'test enabled root node elimination': skeleton(__xmlString: '<root></root>', explicitRoot: false, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
assert.deepEqual r, '')
Expand Down Expand Up @@ -599,7 +602,7 @@ module.exports =
'test valueProcessors key param': skeleton(valueProcessors: [replaceValueByName], (r)->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.valueProcessTest[0], 'valueProcessTest')

'test parseStringPromise parsing': (test) ->
x2js = new xml2js.Parser()
readFilePromise(fileName).then (data) ->
Expand All @@ -610,7 +613,7 @@ module.exports =
test.finish()
.catch (err) ->
test.fail('Should not error')

'test parseStringPromise with bad input': (test) ->
x2js = new xml2js.Parser()
x2js.parseStringPromise("< a moose bit my sister>").then (r) ->
Expand Down Expand Up @@ -640,7 +643,7 @@ module.exports =
test.finish()
.catch (err) ->
test.fail('Should not error')

'test global parseStringPromise with bad input': (test) ->
xml2js.parseStringPromise("< a moose bit my sister>").then (r) ->
test.fail('Should fail')
Expand Down

0 comments on commit 3344f16

Please sign in to comment.