forked from fishilico/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 38ded0b
Showing
30 changed files
with
917 additions
and
921 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
*.swp | ||
build/ | ||
node_modules/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.