Skip to content

Commit

Permalink
Call function for emptyTag if specified
Browse files Browse the repository at this point in the history
In case there are multiple empty tags in xml,
they all share reference to the same default object. This causes unwanted
duplication, when we assign something to any of these empty objects.
When we provide factory function for emptyObject, these objects no longer
share reference and works as expected.
  • Loading branch information
Jan Stepanek committed May 21, 2019
1 parent 0f0a298 commit 19a4c2f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/parser.coffee
Expand Up @@ -145,7 +145,7 @@ class exports.Parser extends events.EventEmitter
obj = obj[charkey]

if (isEmpty obj)
obj = if @options.emptyTag != '' then @options.emptyTag else emptyStr
obj = @options.emptyTag?() ? (if @options.emptyTag != '' then @options.emptyTag else emptyStr)

if @options.validator?
xpath = "/" + (node["#name"] for node in stack).concat(nodeName).join("/")
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/sample.xml
Expand Up @@ -55,4 +55,7 @@
<tagNameProcessTest/>
<valueProcessTest>some value</valueProcessTest>
<textordertest>this is text with <b>markup</b> <em>like this</em> in the middle</textordertest>
<emptytestanother>

</emptytestanother>
</sample>
5 changes: 5 additions & 0 deletions test/parser.test.coffee
Expand Up @@ -72,6 +72,11 @@ module.exports =
# determine number of items in object
equ Object.keys(r.sample.tagcasetest[0]).length, 3)

'test parse with empty objects and functions': skeleton({emptyTag: ()=> new Object()}, (r)->
console.log 'Result object: ' + util.inspect r, false, 10
bool = r.sample.emptytestanother[0] is r.sample.emptytest[0]
equ bool, false)

'test parse with explicitCharkey': skeleton(explicitCharkey: true, (r) ->
console.log 'Result object: ' + util.inspect r, false, 10
equ r.sample.chartest[0].$.desc, 'Test for CHARs'
Expand Down

0 comments on commit 19a4c2f

Please sign in to comment.