Skip to content

Commit

Permalink
ensure that parser is closed when parsing a string, to guarantee well…
Browse files Browse the repository at this point in the history
… formed but not closed XML generates an error
  • Loading branch information
tsgautier committed Dec 6, 2013
1 parent 23b4776 commit 8d1a9a6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/xml2js.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/xml2js.coffee
Expand Up @@ -299,7 +299,7 @@ class exports.Parser extends events.EventEmitter
@emit "end", null @emit "end", null
return true return true


@saxParser.write bom.stripBOM str.toString() @saxParser.write(bom.stripBOM str.toString()).close()


exports.parseString = (str, a, b) -> exports.parseString = (str, a, b) ->
# let's determine what we got as arguments # let's determine what we got as arguments
Expand Down
12 changes: 10 additions & 2 deletions test/parser.test.coffee
Expand Up @@ -285,8 +285,10 @@ module.exports =
throw new Error 'error throwing in callback' throw new Error 'error throwing in callback'
throw new Error 'error throwing outside' throw new Error 'error throwing outside'
catch e catch e
# don't catch the exception that was thrown by callback # the stream is finished by the time the parseString method is called
equ e.message, 'error throwing outside' # so the callback, which is synchronous, will bubble the inner error
# out to here, make sure that happens
equ e.message, 'error throwing in callback'
test.finish() test.finish()


'test xmlns': skeleton(xmlns: true, (r) -> 'test xmlns': skeleton(xmlns: true, (r) ->
Expand Down Expand Up @@ -340,3 +342,9 @@ module.exports =
equ err, null equ err, null
assert.deepEqual resWithNew, resWithoutNew assert.deepEqual resWithNew, resWithoutNew
test.done() test.done()

'test not closed but well formed xml': (test) ->
xml = "<test>"
xml2js.parseString xml, (err, parsed) ->
assert.equal err.message, 'Unclosed root tag\nLine: 0\nColumn: 6\nChar: '
test.finish()

0 comments on commit 8d1a9a6

Please sign in to comment.