Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trim option for empty tags #643

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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