Permalink
Browse files

set toString() to output formatted output

  • Loading branch information...
1 parent 669fc4b commit 6cf50a222936fa4ee60e9aaf67b98be448110777 @mgcrea mgcrea committed Feb 8, 2013
Showing with 20 additions and 13 deletions.
  1. +4 −4 lib/document.js
  2. +3 −3 src/xml_document.cc
  3. +13 −6 test/document.js
View
8 lib/document.js
@@ -54,8 +54,8 @@ Document.prototype.childNodes = function() {
};
/// @return a string representation of the document
-Document.prototype.toString = function() {
- return this._toString();
+Document.prototype.toString = function(formatted) {
+ return this._toString(formatted !== undefined ? formatted : true);
};
/// @return the document version
@@ -79,15 +79,15 @@ Document.prototype.setDtd = function(name, ext, sys) {
} 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);
};
View
6 src/xml_document.cc
@@ -96,8 +96,8 @@ XmlDocument::SetDtd(const v8::Arguments& args)
XmlDocument* document = ObjectWrap::Unwrap<XmlDocument>(args.Holder());
assert(document);
- v8::String::Utf8Value name(args[0]);
-
+ v8::String::Utf8Value name(args[0]);
+
v8::Handle<v8::Value> extIdOpt;
v8::Handle<v8::Value> sysIdOpt;
if (args.Length() > 1 && args[1]->IsString()) {
@@ -135,7 +135,7 @@ XmlDocument::ToString(const v8::Arguments& args)
xmlChar* buffer = NULL;
int len = 0;
- xmlDocDumpFormatMemoryEnc(document->xml_obj, &buffer, &len, "UTF-8", 0);
+ xmlDocDumpFormatMemoryEnc(document->xml_obj, &buffer, &len, "UTF-8", args[0]->BooleanValue() ? 1 : 0);
v8::Local<v8::String> str = v8::String::New((const char*)buffer, len);
xmlFree(buffer);
View
19 test/document.js
@@ -99,7 +99,12 @@ module.exports.xpath_child = function(assert) {
module.exports.toString = function(assert) {
var control = [
'<?xml version="1.0" encoding="UTF-8"?>',
- '<root><child to="wongfoo"><grandchild from="julie numar">with love</grandchild></child><sibling>with content!</sibling></root>',
+ '<root>',
+ ' <child to="wongfoo">',
+ ' <grandchild from="julie numar">with love</grandchild>',
+ ' </child>',
+ ' <sibling>with content!</sibling>',
+ '</root>',
''
].join("\n");
@@ -140,7 +145,9 @@ module.exports.add_cdata_nodes = function(assert) {
var expected_string = [
'<?xml version="1.0" encoding="UTF-8"?>',
- '<root><child to="wongfoo"><![CDATA[<p>Bacon</p>]]></child></root>',
+ '<root>',
+ ' <child to="wongfoo"><![CDATA[<p>Bacon</p>]]></child>',
+ '</root>',
'' /* Why?!? */
].join("\n");
@@ -177,20 +184,20 @@ module.exports.cloned_node = function(assert) {
gchild.remove();
- assert.equal(doc2_string, doc1.toString()); //doc1 should be the same as doc2 str
- assert.equal(doc1_string, doc2.toString()); //doc2 should be the same as doc1 str
+ assert.equal(doc2_string, doc1.toString(false)); //doc1 should be the same as doc2 str (raw output)
+ assert.equal(doc1_string, doc2.toString(false)); //doc2 should be the same as doc1 str (raw output)
assert.done();
};
module.exports.validate = function(assert) {
var xsd = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="comment" type="xs:string"/></xs:schema>';
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.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);

0 comments on commit 6cf50a2

Please sign in to comment.