Browse files

fix error when parsing CDATA with only whitespace(s), #77

  • Loading branch information...
1 parent 3e176cd commit a1812c32dc071e9cfbb7d10e27a908128f2602b4 @yyfrankyy yyfrankyy committed Apr 15, 2013
Showing with 37 additions and 17 deletions.
  1. +23 −15 lib/xml2js.js
  2. +12 −2 src/xml2js.coffee
  3. +1 −0 test/fixtures/sample.xml
  4. +1 −0 test/xml2js.test.coffee
View
38 lib/xml2js.js
@@ -1,9 +1,9 @@
-// Generated by CoffeeScript 1.6.1
+// Generated by CoffeeScript 1.3.3
(function() {
var events, isEmpty, sax,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
- _this = this;
+ __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
sax = require('sax');
@@ -71,14 +71,11 @@
__extends(Parser, _super);
function Parser(opts) {
- var key, value, _ref,
- _this = this;
- this.parseString = function(str, cb) {
- return Parser.prototype.parseString.apply(_this, arguments);
- };
- this.reset = function() {
- return Parser.prototype.reset.apply(_this, arguments);
- };
+ this.parseString = __bind(this.parseString, this);
+
+ this.reset = __bind(this.reset, this);
+
+ var key, value, _ref;
this.options = {};
_ref = exports.defaults["0.2"];
for (key in _ref) {
@@ -98,7 +95,7 @@
}
Parser.prototype.reset = function() {
- var attrkey, charkey, err, stack,
+ var attrkey, charkey, err, ontext, stack,
_this = this;
this.removeAllListeners();
this.saxParser = sax.parser(this.options.strict, {
@@ -146,12 +143,14 @@
return stack.push(obj);
};
this.saxParser.onclosetag = function() {
- var node, nodeName, obj, old, s, xpath;
+ var cdata, node, nodeName, obj, old, s, xpath;
obj = stack.pop();
nodeName = obj["#name"];
delete obj["#name"];
+ cdata = obj.cdata;
+ delete obj.cdata;
s = stack[stack.length - 1];
- if (obj[charkey].match(/^\s*$/)) {
+ if (obj[charkey].match(/^\s*$/) && !cdata) {
delete obj[charkey];
} else {
if (_this.options.trim) {
@@ -225,11 +224,20 @@
return _this.emit("end", _this.resultObject);
}
};
- return this.saxParser.ontext = this.saxParser.oncdata = function(text) {
+ ontext = function(text) {
var s;
s = stack[stack.length - 1];
if (s) {
- return s[charkey] += text;
+ s[charkey] += text;
+ return s;
+ }
+ };
+ this.saxParser.ontext = ontext;
+ return this.saxParser.oncdata = function(text) {
+ var s;
+ s = ontext(text);
+ if (s) {
+ return s.cdata = true;
}
};
};
View
14 src/xml2js.coffee
@@ -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
delete obj[charkey]
else
obj[charkey] = obj[charkey].trim() if @options.trim
@@ -193,10 +196,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 test/fixtures/sample.xml
@@ -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
1 test/xml2js.test.coffee
@@ -167,6 +167,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 a1812c3

Please sign in to comment.