Permalink
Browse files

Styling and error handling.

I fixed some stylistic things relating to single line if statements.

I moved some of the checks to the JS side of the house. Because we need
to pass NULL to xmlCreateIntSubset for the last two params for them to
be ignored, we have to do some checks in the C++ side. In order to deal
with this I used strdup to grab the content of the v8::String.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
1 parent 07d27ef commit 3a5ab4cabe96f639c8160c986fae9bb1dfb74bb5 @ncb000gt ncb000gt committed Jan 6, 2013
Showing with 40 additions and 18 deletions.
  1. +18 −0 lib/document.js
  2. +20 −16 src/xml_document.cc
  3. +2 −2 test/document.js
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
36 src/xml_document.cc
@@ -96,30 +96,34 @@ XmlDocument::SetDtd(const v8::Arguments& args)
XmlDocument* document = ObjectWrap::Unwrap<XmlDocument>(args.Holder());
assert(document);
- v8::String::Utf8Value name_(args[0]->ToString());
- v8::String::Utf8Value externalId_(args[1]->ToString());
- v8::String::Utf8Value systemId_(args[2]->ToString());
+ v8::String::Utf8Value name(args[0]->ToString());
+ //must be set to null in order for xmlCreateIntSubset to ignore them
+ char* externalId = NULL;
+ if (args.Length() > 1 && args[1]->IsString()) {
+ v8::String::Utf8Value externalId_(args[1]->ToString());
+ externalId = strdup(*externalId_);
+ }
+ char* systemId = NULL;
+ if (args.Length() > 2 && args[2]->IsString()) {
+ v8::String::Utf8Value systemId_(args[2]->ToString());
+ systemId = strdup(*systemId_);
+ }
//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);
- if (name_.length() == 0 || !args[0]->IsString())
- return ThrowException(v8::Exception::Error(
- v8::String::New("Name must be filled out, and must be a String")));
- char* name = strdup(*name_);
- char* externalId = NULL;
- if (args.Length() > 1 && args[1]->IsString() && externalId_.length() > 0) externalId = strdup(*externalId_);
- char* systemId = NULL;
- if (args.Length() > 2 && args[2]->IsString() && systemId_.length() > 0) systemId = strdup(*systemId_);
- xmlCreateIntSubset(document->xml_obj, (const xmlChar *)name, (const xmlChar *)externalId, (const xmlChar *)systemId);
+ xmlCreateIntSubset(document->xml_obj, (const xmlChar *) *name, (const xmlChar *) externalId, (const xmlChar *) systemId);
- if (name != NULL) free(name);
- if (externalId != NULL) free(externalId);
- if (systemId != NULL) free(systemId);
+ if (externalId != NULL) {
+ free(externalId);
+ }
+ if (systemId != NULL) {
+ free(systemId);
+ }
return scope.Close(args.Holder());
}
@@ -371,7 +375,7 @@ XmlDocument::Initialize(v8::Handle<v8::Object> target)
"_validate",
XmlDocument::Validate);
NODE_SET_PROTOTYPE_METHOD(constructor_template,
- "setDtd",
+ "_setDtd",
XmlDocument::SetDtd);
View
4 test/document.js
@@ -15,12 +15,12 @@ module.exports.setDtd = function(assert) {
doc.setDtd(5);
});
assert.ok(doc);
- assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n', doc.toString());
+ 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', doc.toString());
+ assert.equal('<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html>\n', doc.toString());
assert.done();
};

1 comment on commit 3a5ab4c

@defunctzombie

No tabs plz. 4 spaces :)

Please sign in to comment.