Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'GH-172'

  • Loading branch information...
commit 5bae57dcbe6828b71b44201578851c49b6d5a3f7 2 parents ceb6f23 + 4e72d02
@ncb000gt ncb000gt authored
View
18 lib/document.js
@@ -73,6 +73,24 @@ Document.prototype.validate = function(xsd) {
return this._validate(xsd);
};
+Document.prototype.setDtd = function(name, ext, sys) {
+ if (!name) {
+ throw new Error('Must pass in a DTD name');
+ } else if (typeof name !== 'string') {
+ throw new Error('Must pass in a valid DTD name');
+ }
+
+ var params = [name];
+ if (typeof ext !== 'undefined') {
+ params.push(ext);
+ }
+ if (ext && typeof sys !== 'undefined') {
+ params.push(sys);
+ }
+
+ return this._setDtd.apply(this, params);
+};
+
/// @return array of namespaces in document
Document.prototype.namespaces = function() {
return this.root().namespaces();
View
43 src/xml_document.cc
@@ -3,6 +3,8 @@
#include <node.h>
#include <node_buffer.h>
+#include <cstring>
+
#include <libxml/HTMLparser.h>
#include <libxml/xmlschemas.h>
@@ -87,6 +89,43 @@ XmlDocument::Root(const v8::Arguments& args)
}
v8::Handle<v8::Value>
+XmlDocument::SetDtd(const v8::Arguments& args)
+{
+ v8::HandleScope scope;
+
+ XmlDocument* document = ObjectWrap::Unwrap<XmlDocument>(args.Holder());
+ assert(document);
+
+ v8::String::Utf8Value name(args[0]);
+
+ v8::Handle<v8::Value> extIdOpt;
+ v8::Handle<v8::Value> sysIdOpt;
+ if (args.Length() > 1 && args[1]->IsString()) {
+ extIdOpt = args[1];
+ }
+ if (args.Length() > 2 && args[2]->IsString()) {
+ sysIdOpt = args[2];
+ }
+
+ v8::String::Utf8Value extIdRaw(extIdOpt);
+ v8::String::Utf8Value sysIdRaw(sysIdOpt);
+
+ //must be set to null in order for xmlCreateIntSubset to ignore them
+ const char* extId = (extIdRaw.length()) ? *extIdRaw : NULL;
+ const char* sysId = (sysIdRaw.length()) ? *sysIdRaw : NULL;
+
+ //No good way of unsetting the doctype if it is previously set...this allows us to.
+ xmlDtdPtr dtd = xmlGetIntSubset(document->xml_obj);
+
+ xmlUnlinkNode((xmlNodePtr) dtd);
+ xmlFreeNode((xmlNodePtr) dtd);
+
+ xmlCreateIntSubset(document->xml_obj, (const xmlChar *) *name, (const xmlChar *) extId, (const xmlChar *) sysId);
+
+ return scope.Close(args.Holder());
+}
+
+v8::Handle<v8::Value>
XmlDocument::ToString(const v8::Arguments& args)
{
v8::HandleScope scope;
@@ -332,6 +371,10 @@ XmlDocument::Initialize(v8::Handle<v8::Object> target)
NODE_SET_PROTOTYPE_METHOD(constructor_template,
"_validate",
XmlDocument::Validate);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template,
+ "_setDtd",
+ XmlDocument::SetDtd);
+
NODE_SET_METHOD(target, "fromXml", XmlDocument::FromXml);
NODE_SET_METHOD(target, "fromHtml", XmlDocument::FromHtml);
View
1  src/xml_document.h
@@ -42,6 +42,7 @@ class XmlDocument : public node::ObjectWrap {
static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> FromHtml(const v8::Arguments& args);
static v8::Handle<v8::Value> FromXml(const v8::Arguments& args);
+ static v8::Handle<v8::Value> SetDtd(const v8::Arguments& args);
// document handle methods
static v8::Handle<v8::Value> Root(const v8::Arguments& args);
View
24 test/document.js
@@ -1,5 +1,29 @@
var libxml = require('../index');
+module.exports.setDtd = function(assert) {
+ var doc = libxml.Document();
+ doc.setDtd("html");
+ assert.ok(doc);
+ assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
+ doc.setDtd("html", "bacon", "bacon");
+ assert.ok(doc);
+ assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html PUBLIC "bacon" "bacon">\n', doc.toString());
+ doc.setDtd("html", null);
+ assert.ok(doc);
+ assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
+ assert.throws(function() {
+ doc.setDtd(5);
+ });
+ assert.ok(doc);
+ assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
+ assert.throws(function() {
+ doc.setDtd();
+ });
+ assert.ok(doc);
+ assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
+ assert.done();
+};
+
module.exports.blank = function(assert) {
var doc = libxml.Document();
assert.ok(doc);
Please sign in to comment.
Something went wrong with that request. Please try again.