Permalink
Browse files

Merge pull request #143 from dpw/parse-buffers

Support for parsing from Buffers
  • Loading branch information...
2 parents 31fa8c4 + 1f3f5e5 commit 1d56d8190fccd6e6644ded59bdc4acb7c7008c6d @defunctzombie defunctzombie committed Jul 26, 2012
View
@@ -13,7 +13,7 @@ var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<sibling>with content!</sibling>' +
'</root>';
-var xmlDoc = libxmljs.parseXmlString(xml);
+var xmlDoc = libxmljs.parseXml(xml);
// xpath queries
var gchild = xmlDoc.get('//grandchild');
View
@@ -11,7 +11,7 @@ LibXML bindings for [node.js](http://nodejs.org/)
'<sibling>with content!</sibling>' +
'</root>';
- var xmlDoc = libxmljs.parseXmlString(xml);
+ var xmlDoc = libxmljs.parseXml(xml);
// xpath queries
var gchild = xmlDoc.get('//grandchild');
@@ -81,7 +81,7 @@ By file:
By String:
- var doc = libxml.parseXmlString([xmlString]);
+ var doc = libxml.parseXml([xmlString]);
#### SAX Parsing
View
@@ -6,7 +6,7 @@ the document as a set of javascript objects is returned
### Methods
-#### libxmljs.parseXmlString(string)
+#### libxmljs.parseXml(string)
>Parse an in-memory XML string
@@ -26,7 +26,7 @@ the document as a set of javascript objects is returned
>**returns** an XmlDocument object
-#### libxmljs.parseHtmlString(string)
+#### libxmljs.parseHtml(string)
>Parse an in-memory HTML string
View
@@ -7,14 +7,10 @@ var bindings = require('./lib/bindings');
var Document = require('./lib/document');
/// parse an xml string and return a Document
-module.exports.parseXmlString = function(string) {
- return Document.fromXmlString(string);
-}
+module.exports.parseXml = Document.fromXml;
/// parse an html string and return a Document
-module.exports.parseHtmlString = function(string) {
- return Document.fromHtmlString(string);
-}
+module.exports.parseHtml = Document.fromHtml;
// constants
module.exports.version = bindings.version;
@@ -26,6 +22,12 @@ module.exports.libxml_debug_enabled = bindings.libxml_debug_enabled;
module.exports.Document = Document;
module.exports.Element = require('./lib/element');
+// Compatibility synonyms
+Document.fromXmlString = Document.fromXml;
+Document.fromHtmlString = Document.fromHtmlString;
+module.exports.parseXmlString = module.exports.parseXml;
+module.exports.parseHtmlString = module.exports.parseHtml;
+
var sax_parser = require('./lib/sax_parser');
module.exports.SaxParser = sax_parser.SaxParser;
module.exports.SaxPushParser = sax_parser.SaxPushParser;
View
@@ -83,14 +83,14 @@ 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);
+module.exports.fromHtml = function(string) {
+ return bindings.fromHtml(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);
+module.exports.fromXml = function(string) {
+ return bindings.fromXml(string);
}
View
@@ -1,6 +1,7 @@
// Copyright 2009, Squish Tech, LLC.
#include <node.h>
+#include <node_buffer.h>
#include <libxml/HTMLparser.h>
#include <libxml/xmlschemas.h>
@@ -130,7 +131,7 @@ XmlDocument::New(xmlDoc* doc)
}
v8::Handle<v8::Value>
-XmlDocument::FromHtmlString(const v8::Arguments& args)
+XmlDocument::FromHtml(const v8::Arguments& args)
{
v8::HandleScope scope;
@@ -139,8 +140,18 @@ XmlDocument::FromHtmlString(const v8::Arguments& args)
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(*errors),
XmlSyntaxError::PushToArray);
- v8::String::Utf8Value str(args[0]->ToString());
- htmlDocPtr doc = htmlReadMemory(*str, str.length(), NULL, NULL, 0);
+ htmlDocPtr doc;
+ if (!node::Buffer::HasInstance(args[0])) {
+ // Parse a string
+ v8::String::Utf8Value str(args[0]->ToString());
+ doc = htmlReadMemory(*str, str.length(), NULL, NULL, 0);
+ }
+ else {
+ // Parse a buffer
+ v8::Local<v8::Object> buf = args[0]->ToObject();
+ doc = htmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
+ NULL, NULL, 0);
+ }
xmlSetStructuredErrorFunc(NULL, NULL);
@@ -161,7 +172,7 @@ XmlDocument::FromHtmlString(const v8::Arguments& args)
}
v8::Handle<v8::Value>
-XmlDocument::FromXmlString(const v8::Arguments& args)
+XmlDocument::FromXml(const v8::Arguments& args)
{
v8::HandleScope scope;
@@ -170,8 +181,18 @@ XmlDocument::FromXmlString(const v8::Arguments& args)
xmlSetStructuredErrorFunc(reinterpret_cast<void *>(*errors),
XmlSyntaxError::PushToArray);
- v8::String::Utf8Value str(args[0]->ToString());
- xmlDocPtr doc = xmlReadMemory(*str, str.length(), NULL, "UTF-8", 0);
+ xmlDocPtr doc;
+ if (!node::Buffer::HasInstance(args[0])) {
+ // Parse a string
+ v8::String::Utf8Value str(args[0]->ToString());
+ doc = xmlReadMemory(*str, str.length(), NULL, "UTF-8", 0);
+ }
+ else {
+ // Parse a buffer
+ v8::Local<v8::Object> buf = args[0]->ToObject();
+ doc = xmlReadMemory(node::Buffer::Data(buf), node::Buffer::Length(buf),
+ NULL, NULL, 0);
+ }
xmlSetStructuredErrorFunc(NULL, NULL);
@@ -290,8 +311,8 @@ XmlDocument::Initialize(v8::Handle<v8::Object> target)
"_validate",
XmlDocument::Validate);
- NODE_SET_METHOD(target, "fromXmlString", XmlDocument::FromXmlString);
- NODE_SET_METHOD(target, "fromHtmlString", XmlDocument::FromHtmlString);
+ NODE_SET_METHOD(target, "fromXml", XmlDocument::FromXml);
+ NODE_SET_METHOD(target, "fromHtml", XmlDocument::FromHtml);
// used to create new document handles
target->Set(v8::String::NewSymbol("Document"), t->GetFunction());
View
@@ -40,8 +40,8 @@ class XmlDocument : public node::ObjectWrap {
explicit XmlDocument(xmlDoc* doc);
static v8::Handle<v8::Value> New(const v8::Arguments& args);
- static v8::Handle<v8::Value> FromHtmlString(const v8::Arguments& args);
- static v8::Handle<v8::Value> FromXmlString(const v8::Arguments& args);
+ static v8::Handle<v8::Value> FromHtml(const v8::Arguments& args);
+ static v8::Handle<v8::Value> FromXml(const v8::Arguments& args);
// document handle methods
static v8::Handle<v8::Value> Root(const v8::Arguments& args);
View
@@ -4,7 +4,7 @@ var body = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<root><node attr-one-key='attr-one-value' attr-two-key='attr-two-value' attr-three-key='attr-three-value' /></root>";
module.exports.new = function(assert) {
- var doc = libxml.parseXmlString(body);
+ var doc = libxml.parseXml(body);
var node = doc.get('node');
// add new attribute to the node
@@ -14,7 +14,7 @@ module.exports.new = function(assert) {
};
module.exports.create_with_namespace = function(assert) {
- var doc = new libxml.parseXmlString(
+ var doc = new libxml.parseXml(
"<?xml version='1.0' encoding='UTF-8'?>\n"+
"<root><node attr-one-key='attr-one-value' attr-two-key='attr-two-value' attr-three-key='attr-three-value' /></root>"
);
@@ -29,7 +29,7 @@ module.exports.create_with_namespace = function(assert) {
};
module.exports.getters = function(assert) {
- var doc = libxml.parseXmlString(body);
+ var doc = libxml.parseXml(body);
var node = doc.get('node');
assert.equal('attr-one-key', node.attr('attr-one-key').name());
@@ -45,7 +45,7 @@ module.exports.getters = function(assert) {
};
module.exports.setters = function(assert) {
- var doc = libxml.parseXmlString(body);
+ var doc = libxml.parseXml(body);
var node = doc.get('node');
node.attr('attr-one-key').value('new-value');
@@ -55,7 +55,7 @@ module.exports.setters = function(assert) {
/*
module.exports.namespace = function(assert) {
- var doc = libxml.parseXmlString(body);
+ var doc = libxml.parseXml(body);
var node = doc.get('node');
var ns = new libxml.Namespace(node, 'ns-prefix', 'ns-uri');
@@ -68,7 +68,7 @@ module.exports.namespace = function(assert) {
*/
module.exports.remove = function(assert) {
- var doc = libxml.parseXmlString(body);
+ var doc = libxml.parseXml(body);
var node = doc.get('node');
var attr = node.attr('attr-one-key');
View
@@ -100,8 +100,8 @@ module.exports.add_child_nodes = function(assert) {
'<root><child to="wongfoo"></child><sibling>with content!</sibling></root>',
].join("\n");
- var doc1 = libxml.parseXmlString(doc1_string);
- var doc2 = libxml.parseXmlString(doc2_string);
+ var doc1 = libxml.parseXml(doc1_string);
+ var doc2 = libxml.parseXml(doc2_string);
doc2.child(0).addChild(doc1.child(0).child(0));
assert.equal(doc1.toString(), doc2.toString());
assert.done();
@@ -120,7 +120,7 @@ module.exports.add_cdata_nodes = function(assert) {
'' /* Why?!? */
].join("\n");
- var doc1 = libxml.parseXmlString(doc1_string);
+ var doc1 = libxml.parseXml(doc1_string);
doc1.child(0).cdata('<p>Bacon</p>');
assert.equal(doc1.toString(), expected_string);
assert.done();
@@ -140,8 +140,8 @@ module.exports.cloned_node = function(assert) {
''
].join("\n");
- var doc1 = libxml.parseXmlString(doc1_string);
- var doc2 = libxml.parseXmlString(doc2_string);
+ var doc1 = libxml.parseXml(doc1_string);
+ var doc2 = libxml.parseXml(doc2_string);
var gchild = doc1.child(0).child(0); //the element to operate on
@@ -163,9 +163,9 @@ module.exports.validate = function(assert) {
var xml_valid = '<?xml version="1.0"?><comment>A comment</comment>';
var xml_invalid = '<?xml version="1.0"?><commentt>A comment</commentt>';
- var xsdDoc = libxml.parseXmlString(xsd);
- var xmlDocValid = libxml.parseXmlString(xml_valid);
- var xmlDocInvalid = libxml.parseXmlString(xml_invalid);
+ var xsdDoc = libxml.parseXml(xsd);
+ var xmlDocValid = libxml.parseXml(xml_valid);
+ var xmlDocInvalid = libxml.parseXml(xml_invalid);
assert.equal(xmlDocValid.validate(xsdDoc), true);
assert.equal(xmlDocInvalid.validate(xsdDoc), false);
Binary file not shown.
View
@@ -13,15 +13,30 @@ function make_error(object) {
module.exports.parse = function(assert) {
var filename = __dirname + '/fixtures/parser.html';
- var str = fs.readFileSync(filename, 'utf8');
- var doc = libxml.parseHtmlString(str);
- assert.equal('html', doc.root().name());
- assert.equal('Test HTML document', doc.get('head/title').text());
- assert.equal('HTML content!', doc.get('body/span').text());
+ function attempt_parse(encoding) {
+ var str = fs.readFileSync(filename, encoding);
+
+ var doc = libxml.parseHtml(str);
+ assert.equal('html', doc.root().name());
+ assert.equal('Test HTML document', doc.get('head/title').text());
+ assert.equal('HTML content!', doc.get('body/span').text());
+ }
+
+ // Parse via a string
+ attempt_parse('utf-8');
+
+ // Parse via a Buffer
+ attempt_parse(null);
+
assert.done();
};
+module.exports.parse_synonym = function(assert) {
+ assert.strictEqual(libxml.parseHtml, libxml.parseHtmlString);
+ assert.done();
+}
+
module.exports.recoverable_parse = function(assert) {
var recoverableFile = __dirname +'/fixtures/warnings/amp.html';
var str = fs.readFileSync(recoverableFile, 'utf8');
@@ -52,7 +67,7 @@ module.exports.recoverable_parse = function(assert) {
column: 4 })
];
- var doc = libxml.parseHtmlString(str);
+ var doc = libxml.parseHtml(str);
assert.equal(4, doc.errors.length);
for(var i = 0; i < recoverableErrors.length; i++) {
assert.equal(recoverableErrors[i].domain, doc.errors[i].domain);
View
@@ -45,20 +45,20 @@ module.exports.with_prefix = function(assert) {
};
module.exports.from_parsing = function(assert) {
- var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>' +
+ var doc = libxml.parseXml('<?xml version="1.0" encoding="UTF-8"?>' +
'<name1 xmlns="http://my-namespace.com"/>');
var elem = doc.root();
assert.ok(elem.namespace());
assert.equal(null, elem.namespace().prefix());
assert.equal('http://my-namespace.com', elem.namespace().href());
// no prefix from parsing
- var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>' +
+ var doc = libxml.parseXml('<?xml version="1.0" encoding="UTF-8"?>' +
'<name1 xmlns:pref="http://my-namespace.com"/>');
var elem = doc.root();
assert.ok(!elem.namespace());
- var doc = libxml.parseXmlString('<?xml version="1.0" encoding="UTF-8"?>' +
+ var doc = libxml.parseXml('<?xml version="1.0" encoding="UTF-8"?>' +
'<pref:name1 xmlns:pref="http://my-namespace.com"/>');
var elem = doc.root();
assert.ok(elem.namespace());
View
@@ -17,7 +17,7 @@ module.exports.gc = function(assert) {
};
module.exports.references = function(assert) {
- var nodes = libxml.parseXmlString('<root> <child> <grandchildren/> </child> <child2/></root>').childNodes();
+ var nodes = libxml.parseXml('<root> <child> <grandchildren/> </child> <child2/></root>').childNodes();
gc();
View
@@ -30,7 +30,7 @@ module.exports.get_attr = function(assert) {
assert.equal('val', doc.get('child').get('@attr').value());
// check again after re-parsign the doc
- doc = libxml.parseXmlString(doc.toString())
+ doc = libxml.parseXml(doc.toString())
assert.equal('val', doc.get('//@attr').value());
assert.equal('val', doc.get('child').get('@attr').value());
assert.equal(doc.get('child'), doc.get('//@attr').node());
View
@@ -1,14 +1,14 @@
var libxml = require('../index');
module.exports.text = function(assert) {
- var doc = libxml.parseXmlString('<?xml version="1.0"?><root>child</root>');
+ var doc = libxml.parseXml('<?xml version="1.0"?><root>child</root>');
assert.equal('text', doc.child(0).type());
assert.equal('text', doc.child(0).name());
assert.done();
};
module.exports.comment = function(assert) {
- var doc = libxml.parseXmlString('<?xml version="1.0"?>' +
+ var doc = libxml.parseXml('<?xml version="1.0"?>' +
'<root><!-- comment --></root>');
assert.equal('comment', doc.child(0).type());
assert.equal('comment', doc.child(0).name());
@@ -17,7 +17,7 @@ module.exports.comment = function(assert) {
module.exports.cdata = function(assert) {
- var doc = libxml.parseXmlString('<?xml version="1.0"?>' +
+ var doc = libxml.parseXml('<?xml version="1.0"?>' +
'<root><![CDATA[cdata text]]></root>');
assert.equal('cdata', doc.child(0).type());
Oops, something went wrong.

0 comments on commit 1d56d81

Please sign in to comment.