Permalink
Browse files

Merge branch 'yyfrankyy-master'

Fixes #77
  • Loading branch information...
Leonidas-from-XIV committed May 10, 2013
2 parents 787f2ef + a1812c3 commit 5bbcde1318e5cbaeb2f9940960b5336591c2abab
Showing with 31 additions and 8 deletions.
  1. +17 −6 lib/xml2js.js
  2. +12 −2 src/xml2js.coffee
  3. +1 −0 test/fixtures/sample.xml
  4. +1 −0 test/xml2js.test.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -125,9 +125,12 @@ class exports.Parser extends events.EventEmitter
nodeName = obj["#name"]
delete obj["#name"]
+ cdata = obj.cdata
+ delete obj.cdata
+
s = stack[stack.length - 1]
# remove the '#' key altogether if it's blank
- if obj[charkey].match(/^\s*$/)
+ if obj[charkey].match(/^\s*$/) and not cdata
emptyStr = obj[charkey]
delete obj[charkey]
else
@@ -194,10 +197,17 @@ class exports.Parser extends events.EventEmitter
@resultObject = obj
@emit "end", @resultObject
- @saxParser.ontext = @saxParser.oncdata = (text) =>
+ ontext = (text) =>
s = stack[stack.length - 1]
if s
s[charkey] += text
+ s
+
+ @saxParser.ontext = ontext
+ @saxParser.oncdata = (text) =>
+ s = ontext text
+ if s
+ s.cdata = true
parseString: (str, cb) =>
if cb? and typeof cb is "function"
View
@@ -1,6 +1,7 @@
<sample>
<chartest desc="Test for CHARs">Character data here!</chartest>
<cdatatest desc="Test for CDATA" misc="true"><![CDATA[CDATA here!]]></cdatatest>
+ <cdatawhitespacetest desc="Test for CDATA with whitespace" misc="true"><![CDATA[ ]]></cdatawhitespacetest>
<nochartest desc="No data" misc="false" />
<nochildrentest desc="No data" misc="false" />
<whitespacetest desc="Test for normalizing and trimming">
View
@@ -169,6 +169,7 @@ module.exports =
equ r.sample.cdatatest[0].attrobj.desc, 'Test for CDATA'
equ r.sample.cdatatest[0].attrobj.misc, 'true'
equ r.sample.cdatatest[0].charobj, 'CDATA here!'
+ equ r.sample.cdatawhitespacetest[0].charobj, ' '
equ r.sample.nochartest[0].attrobj.desc, 'No data'
equ r.sample.nochartest[0].attrobj.misc, 'false')

0 comments on commit 5bbcde1

Please sign in to comment.