Skip to content

Commit

Permalink
Stripping BOM if at the beginning of file
Browse files Browse the repository at this point in the history
Closes #99
  • Loading branch information
Leonidas-from-XIV committed Sep 4, 2013
1 parent 36f2c5b commit 6e83733
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/bom.js

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

6 changes: 4 additions & 2 deletions lib/xml2js.js

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

8 changes: 8 additions & 0 deletions src/bom.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
xml2js = require '../lib/xml2js'

exports.stripBOM = (str) ->
if str[0] == '\uFEFF'
str.substring(1)
else
str

3 changes: 2 additions & 1 deletion src/xml2js.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
sax = require 'sax'
events = require 'events'
bom = require './bom'

# Underscore has a nice function for this, but we try to go without dependencies
isEmpty = (thing) ->
Expand Down Expand Up @@ -232,7 +233,7 @@ class exports.Parser extends events.EventEmitter
@emit "end", null
return true

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

exports.parseString = (str, a, b) ->
# let's determine what we got as arguments
Expand Down
11 changes: 11 additions & 0 deletions test/bom.test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
xml2js = require '../lib/xml2js'
assert = require 'assert'
equ = assert.equal

module.exports =
'test decoded BOM': (test) ->
demo = '\uFEFF<xml><foo>bar</foo></xml>'
xml2js.parseString demo, (err, res) ->
equ err, undefined
equ res.xml.foo[0], 'bar'
test.done()

0 comments on commit 6e83733

Please sign in to comment.