Skip to content

Commit

Permalink
move to node::ObjectWrap over custom version
Browse files Browse the repository at this point in the history
- wrap some of the c++ classes in js (documented and easier to follow)
- no longer can attributes and namespaces objects be created
  • Loading branch information
defunctzombie committed Dec 20, 2011
1 parent 5e32b62 commit 38ded0b
Show file tree
Hide file tree
Showing 30 changed files with 917 additions and 921 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.swp
build/
node_modules/
npm-debug.log
36 changes: 31 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
module.exports = require('./build/Release/libxmljs');
// js acts as a wrapper to the c++ bindings
// prefer to do error handling and other abstrctions in the
// js layer and only go to c++ when we need to hit libxml
var bindings = require('./build/Release/libxmljs');

// document parsing for backwards compat
var Document = require('./lib/document');

/// parse an xml string and return a Document
module.exports.parseXmlString = function(string) {
return Document.fromXmlString(string);
}

/// parse an html string and return a Document
module.exports.parseHtmlString = function(string) {
return Document.fromHtmlString(string);
}

// constants
module.exports.version = bindings.version;
module.exports.libxml_version = bindings.libxml_version;
module.exports.libxml_parser_version = bindings.libxml_parser_version;
module.exports.libxml_debug_enabled = bindings.libxml_debug_enabled;

// lib exports
module.exports.Document = Document;
module.exports.Element = require('./lib/element');

var sax_parser = require('./lib/sax_parser');
module.exports.SaxParser = sax_parser.SaxParser;
module.exports.SaxPushParser = sax_parser.SaxPushParser;

// attach javascipt helpers to bound classes
require('./lib/xml_element');
require('./lib/xml_document');
require('./lib/xml_sax_parser');
86 changes: 86 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var bindings = require('../build/Release/libxmljs');

var Element = require('./element');

/// Create a new document
/// @param {string} version xml version, default 1.0
/// @param {string} encoding the encoding, default utf8
/// @constructor
var Document = function(version, encoding) {
version = version || '1.0';
var doc = new bindings.Document(version);
doc.encoding(encoding || 'utf8');
return doc;
};

Document.prototype = bindings.Document.prototype;

/// get or set the root element
/// if called without any arguments, this will return the document root
/// @param {Element} [elem] if specified, this will become the new document root
Document.prototype.root = function(elem) {
return this._root(elem);
};

/// add a child node to the document
/// this will set the document root
Document.prototype.node = function(name, content) {
return this.root(Element(this, name, content));
};

/// xpath search
/// @return array of matching elements
Document.prototype.find = function(xpath, ns_uri) {
return this.root().find(xpath, ns_uri);
};

/// xpath search
/// @return first element matching
Document.prototype.get = function(xpath, ns_uri) {
return this.find(xpath, ns_uri)[0];
};

/// @return a given child
Document.prototype.child = function(id) {
if (id === undefined || typeof id !== 'number') {
throw new Error('id argument required for #child');
}
return this.root().child(id);
};

/// @return an Array of child nodes of the document root
Document.prototype.childNodes = function() {
return this.root().childNodes();
};

/// @return a string representation of the document
Document.prototype.toString = function() {
return this._toString();
}

/// @return the document version
Document.prototype.version = function() {
return this._version();
}

/// @return the document encoding
Document.prototype.encoding = function(encoding) {
return this._encoding(encoding);
}

module.exports = Document;

/// parse a string into a html document
/// @param string html string to parse
/// @return a Document
module.exports.fromHtmlString = function(string) {
return bindings.fromHtmlString(string);
}

/// parse a string into a xml document
/// @param string xml string to parse
/// @return a Document
module.exports.fromXmlString = function(string) {
return bindings.fromXmlString(string);
}

72 changes: 72 additions & 0 deletions lib/element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var bindings = require('../build/Release/libxmljs');

var Document = require('./document');

/// create a new element on the given document
/// @param doc the Document to create the element for
/// @param name the element name
/// @param {String} [contenn] element content
/// @constructor
var Element = function(doc, name, content) {
if (!doc) {
throw new Error('document argument required');
} else if (! (doc instanceof bindings.Document)) {
throw new Error('document argument must be an ' +
'instance of Document');
} else if (!name) {
throw new Error('name argument required');
}

var elem = new bindings.Element(doc, name, content);
return elem;
};

Element.prototype = bindings.Element.prototype;

Element.prototype.attr = function() {
if (arguments.length === 1) {
var arg = arguments[0];
if (typeof arg === 'object') {
// object setter
// iterate keys/value to set attributes
for (var k in arg) {
this._attr(k, arg[k]);
};
return this;
} else if (typeof arg === 'string') {
// getter
return this._attr(arg);
}
} else if (arguments.length === 2) {
// 2 arg setter
var name = arguments[0];
var value = arguments[1];
this._attr(name, value);
return this;
}
};

/// helper method to attach a new node to this element
/// @param name the element name
/// @param {String} [content] element content
Element.prototype.node = function(name, content) {
var elem = Element(this.doc(), name, content);
this.addChild(elem);
return elem;
};

Element.prototype.get = function() {
return this.find.apply(this, arguments)[0];
};

Element.prototype.defineNamespace = function(prefix, href) {
// if no prefix specified
if (!href) {
href = prefix;
prefix = null;
}
return new bindings.Namespace(this, prefix, href);
};

module.exports = Element;

14 changes: 9 additions & 5 deletions lib/xml_sax_parser.js → lib/sax_parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var libxml = require('../build/Release/libxmljs');
var bindings = require('../build/Release/libxmljs');

libxml.SaxCallbacks = function() {
bindings.SaxCallbacks = function() {
var callbackList = {};

function addCallback(name, callback) {
Expand Down Expand Up @@ -64,11 +64,15 @@ libxml.SaxCallbacks = function() {

(function() {
var setCallbacks = function(callback) {
var callbacks = new libxml.SaxCallbacks();
var callbacks = new bindings.SaxCallbacks();
callback(callbacks);
return callbacks;
};

libxml.SaxParser.prototype.setCallbacks = setCallbacks;
libxml.SaxPushParser.prototype.setCallbacks = setCallbacks;
bindings.SaxParser.prototype.setCallbacks = setCallbacks;
bindings.SaxPushParser.prototype.setCallbacks = setCallbacks;
})();

module.exports.SaxParser = bindings.SaxParser;
module.exports.SaxPushParser = bindings.SaxPushParser;

22 changes: 0 additions & 22 deletions lib/xml_document.js

This file was deleted.

55 changes: 0 additions & 55 deletions lib/xml_element.js

This file was deleted.

44 changes: 0 additions & 44 deletions src/html_parser.cc

This file was deleted.

14 changes: 0 additions & 14 deletions src/html_parser.h

This file was deleted.

Loading

0 comments on commit 38ded0b

Please sign in to comment.