Permalink
Browse files

move to node::ObjectWrap over custom version

- 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...
1 parent 5e32b62 commit 38ded0b2b1b224ef630484a65dcfe73db166ea87 @defunctzombie defunctzombie committed Dec 20, 2011
View
@@ -2,3 +2,4 @@
*.swp
build/
node_modules/
+npm-debug.log
View
@@ -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');
View
@@ -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);
+}
+
View
@@ -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;
+
@@ -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) {
@@ -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;
+
View
@@ -1,22 +0,0 @@
-var libxml = require('../build/Release/libxmljs');
-
-libxml.Document.prototype.node = function() {
- args = libxml.Element.Arguments(arguments);
- return this.root(new libxml.Element(this, args[0], args[1], args[2], args[3]));
-};
-
-libxml.Document.prototype.find = function() {
- return this.root().find.apply(this.root(), arguments);
-};
-
-libxml.Document.prototype.get = function() {
- return this.find.apply(this, arguments)[0];
-};
-
-libxml.Document.prototype.child = function() {
- return this.root().child.apply(this.root(), arguments);
-};
-
-libxml.Document.prototype.childNodes = function() {
- return this.root().childNodes();
-};
View
@@ -1,55 +0,0 @@
-var libxml = require('../build/Release/libxmljs');
-
-// name, attrs, content, callback
-libxml.Element.Arguments = function(args) {
- var ret_args = null;
-
- switch (args.length) {
- case 1:
- ret_args = [args[0], null, null, null];
- break;
-
- case 2: // name, callback; name, attrs; name, content
- if (typeof args[1] == 'function')
- ret_args = [args[0], null, null, args[1]];
- else if (typeof args[1] == 'string')
- ret_args = [args[0], null, args[1], null];
- else
- ret_args = [args[0], args[1], null, null];
- break;
-
- case 3: // name, attrs, content; name, attrs, callback
- if (typeof args[2] == 'function')
- ret_args = [args[0], args[1], null, args[2]];
- else
- ret_args = [args[0], args[1], args[2], null];
- break;
-
- case 4: // name, attrs, content, callback
- ret_args = [args[0], args[1], args[2], args[3]];
- }
-
- return ret_args;
-};
-
-libxml.Element.prototype.node = function() {
- var args = libxml.Element.Arguments(arguments);
- var elem = null;
-
- if (args[0] instanceof libxml.Element)
- elem = args[0];
- else
- elem = new libxml.Element(this.doc(), args[0], args[1], args[2], args[3]);
-
- this.addChild(elem);
- return elem;
-};
-
-libxml.Element.prototype.get = function() {
- return this.find.apply(this, arguments)[0];
-};
-
-libxml.Element.prototype.defineNamespace = function() {
- var args = arguments.length == 1 ? [null, arguments[0]] : [arguments[0], arguments[1]];
- return new libxml.Namespace(this, args[0], args[1]);
-};
View
@@ -1,44 +0,0 @@
-// Copyright 2009, Squish Tech, LLC.
-
-#include <libxml/HTMLparser.h>
-
-#include "xml_syntax_error.h"
-#include "html_parser.h"
-#include "html_document.h"
-
-namespace libxmljs {
-
-v8::Handle<v8::Value>
-ParseHtmlString(const v8::Arguments& args) {
- v8::HandleScope scope;
-
- if (!args[0]->IsString())
- return v8::ThrowException(v8::Exception::Error(
- v8::String::New("Must supply parseHtmlString with a string")));
-
- v8::Local<v8::Array> errors = v8::Array::New();
- xmlResetLastError();
- xmlSetStructuredErrorFunc(reinterpret_cast<void *>(*errors),
- XmlSyntaxError::PushToArray);
-
- v8::String::Utf8Value str(args[0]->ToString());
- htmlDocPtr doc = htmlReadMemory(*str, str.length(), NULL, NULL, 0);
-
- xmlSetStructuredErrorFunc(NULL, NULL);
-
- if (!doc) {
- xmlError* error = xmlGetLastError();
- if (error) {
- return v8::ThrowException(XmlSyntaxError::BuildSyntaxError(error));
- }
- return v8::ThrowException(v8::Exception::Error(
- v8::String::New("Could not parse XML string")));
- }
-
- v8::Handle<v8::Object> built_doc = LibXmlObj::GetMaybeBuild<HtmlDocument, xmlDoc>(doc);
- built_doc->Set(v8::String::New("errors"), errors);
-
- return scope.Close(built_doc);
-}
-
-} // namespace libxmljs
View
@@ -1,14 +0,0 @@
-// Copyright 2009, Squish Tech, LLC.
-#ifndef SRC_HTML_PARSER_H_
-#define SRC_HTML_PARSER_H_
-
-#include "libxmljs.h"
-
-namespace libxmljs {
-
-v8::Handle<v8::Value>
-ParseHtmlString(const v8::Arguments& args);
-
-} // namespace libxmljs
-
-#endif // SRC_HTML_PARSER_H_
Oops, something went wrong.

0 comments on commit 38ded0b

Please sign in to comment.