Permalink
Browse files

Manually merged gluwer's changes

  • Loading branch information...
1 parent 38169fa commit 466fca445423d8bf07d9d9b5f03e8f569971121a @teknopaul committed Nov 4, 2011
Showing with 84 additions and 50 deletions.
  1. +17 −0 example/test-cdata-dom-js.js
  2. +64 −48 lib/dom-js.js
  3. +3 −2 package.json
@@ -0,0 +1,17 @@
+var util = require('util');
+var DomJS = require("dom-js").DomJS;
+var domjs = new DomJS();
+
+console.log("\nExample XML");
+var string = '<xml><elem attrib="val1"><![CDATA[Hello "World"]]></elem></xml>';
+
+console.log(string);
+
+domjs.parse(string, function(err, dom) {
+
+ console.log(util.inspect(dom, false, 23));
+ console.log("serializes to : " + dom.toXml());
+
+});
+
+domjs.reset(); //before reuse
View
@@ -53,17 +53,26 @@ DomJS.prototype.parse = function(string, cb) {
parser = sax.parser(strict);
parser.onerror = function (err) {
- this.error = true;
+ self.error = true;
cb(true, err);
};
parser.ontext = function (text) {
if (self.currElement == null) {
// console.log("Content in the prolog " + text);
return;
}
- textNode = new Text(text);
+ var textNode = new Text(text);
self.currElement.children.push(textNode);
};
+ parser.onopencdata = function () {
+ var cdataNode = new CDATASection();
+ self.currElement.children.push(cdataNode);
+ };
+ parser.oncdata = function (data) {
+ var cdataNode = self.currElement.children[self.currElement.children.length - 1];
+ cdataNode.appendData(data);
+ };
+ // do nothing on parser.onclosecdata
parser.onopentag = function (node) {
var elem = new Element(node.name, node.attributes);
if (self.root == null) {
@@ -84,7 +93,7 @@ DomJS.prototype.parse = function(string, cb) {
//console.log("Comments in the prolog discarded " + comment);
return;
}
- commentNode = new Comment(comment);
+ var commentNode = new Comment(comment);
self.currElement.children.push(commentNode);
};
@@ -117,64 +126,71 @@ Element = function(name, attributes, children ) {
this.name = name;
this.attributes = attributes || [];
this.children = children || [];
-
- this.toXml = function(sb) {
- if (typeof sb == 'undefined') {
- sb = {buf:''}; // Strings are pass by value in JS it seems
- }
- sb.buf += '<' + this.name;
- for (att in this.attributes) {
-
- sb.buf += ' ' + att + '="' + escape(this.attributes[att]) + '"';
- }
- if (this.children.length != 0) {
- sb.buf += '>';
- for (var i = 0 ; i < this.children.length ; i++) {
- this.children[i].toXml(sb);
- }
- sb.buf += '</' + this.name + '>';
- }
- else {
- sb.buf += '/>';
- }
- return sb.buf;
- };
-
- this.firstChild = function() {
- if ( this.children.length > 0) {
- return this.children[0];
- }
- return null;
- };
-
- this.text = function() {
- if ( this.children.length > 0) {
- if (typeof this.children[0].text == 'string') {
- return this.children[0].text;
- };
+}
+Element.prototype.toXml = function(sb) {
+ if (typeof sb == 'undefined') {
+ sb = {buf:''}; // Strings are pass by value in JS it seems
+ }
+ sb.buf += '<' + this.name;
+ for (att in this.attributes) {
+
+ sb.buf += ' ' + att + '="' + escape(this.attributes[att]) + '"';
+ }
+ if (this.children.length != 0) {
+ sb.buf += '>';
+ for (var i = 0 ; i < this.children.length ; i++) {
+ this.children[i].toXml(sb);
}
- return null;
- };
+ sb.buf += '</' + this.name + '>';
+ }
+ else {
+ sb.buf += '/>';
+ }
+ return sb.buf;
};
+Element.prototype.firstChild = function() {
+ if ( this.children.length > 0) {
+ return this.children[0];
+ }
+ return null;
+};
+Element.prototype.text = function() {
+ if ( this.children.length > 0) {
+ if (typeof this.children[0].text == 'string') {
+ return this.children[0].text;
+ };
+ }
+ return null;
+};
+
Text = function(data){
this.text = data;
-
- this.toXml = function(sb) {
- sb.buf += escape(this.text);
- };
};
+Text.prototype.toXml = function(sb) {
+ sb.buf += escape(this.text);
+};
+
Comment = function(comment) {
this.comment = comment;
-
- this.toXml = function(sb) {
- sb.buf += '<!--' + this.comment + '-->';
- };
+};
+Comment.prototype.toXml = function(sb) {
+ sb.buf += '<!--' + this.comment + '-->';
};
+CDATASection = function(data){
+ this.text = data || '';
+};
+CDATASection.prototype.toXml = function(sb) {
+ sb.buf += '<![CDATA[' + this.text + ']]>';
+};
+CDATASection.prototype.appendData = function(data) {
+ this.text += data;
+};
exports.Element = Element;
exports.Text = Text;
exports.Comment = Comment;
+exports.CDATASection = CDATASection;
exports.DomJS = DomJS;
exports.escape = escape;
View
@@ -1,6 +1,6 @@
{
"name": "dom-js",
- "version": "0.0.5",
+ "version": "0.0.6",
"description" : "XML DOM based on sax",
"dependencies": {
"sax": ">=0.1.5"
@@ -15,5 +15,6 @@
"repository" : {
"type" : "git" , "url" : "https://github.com/teknopaul/dom-js.git"
},
- "author": "teknopaul"
+ "author": "teknopaul",
+ "contributors": ["Rafal Jonca"]
}

0 comments on commit 466fca4

Please sign in to comment.