Skip to content

Commit

Permalink
Fixes #270, replaces #271
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Nov 17, 2015
1 parent 17c8858 commit 34d55d2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/xml_node.cc
Expand Up @@ -89,7 +89,12 @@ NAN_METHOD(XmlNode::Namespaces) {
XmlNode* node = Nan::ObjectWrap::Unwrap<XmlNode>(info.Holder());
assert(node);

return info.GetReturnValue().Set(node->get_all_namespaces());
// ignore everything but a literal true; different from IsFalse
if ((info.Length() == 0) || !info[0]->IsTrue()) {
return info.GetReturnValue().Set(node->get_all_namespaces());
}

return info.GetReturnValue().Set(node->get_local_namespaces());
}

NAN_METHOD(XmlNode::Parent) {
Expand Down Expand Up @@ -354,6 +359,22 @@ XmlNode::get_all_namespaces() {
return scope.Escape(namespaces);
}

v8::Local<v8::Value>
XmlNode::get_local_namespaces() {
Nan::EscapableHandleScope scope;

// Iterate through local namespaces
v8::Local<v8::Array> namespaces = Nan::New<v8::Array>();
xmlNs* nsDef = xml_obj->nsDef;
for(int i=0; nsDef; i++, nsDef = nsDef->next) {
v8::Local<v8::Number> index = Nan::New<v8::Number>(i);
v8::Local<v8::Object> ns = XmlNamespace::New(nsDef);
Nan::Set(namespaces, index, ns);
}

return scope.Escape(namespaces);
}

v8::Local<v8::Value>
XmlNode::get_parent() {
Nan::EscapableHandleScope scope;
Expand Down
1 change: 1 addition & 0 deletions src/xml_node.h
Expand Up @@ -47,6 +47,7 @@ class XmlNode : public Nan::ObjectWrap {
void set_namespace(xmlNs* ns);
xmlNs * find_namespace(const char * search_str);
v8::Local<v8::Value> get_all_namespaces();
v8::Local<v8::Value> get_local_namespaces();
v8::Local<v8::Value> get_parent();
v8::Local<v8::Value> get_prev_sibling();
v8::Local<v8::Value> get_next_sibling();
Expand Down
40 changes: 40 additions & 0 deletions test/namespace.js
Expand Up @@ -168,3 +168,43 @@ module.exports.custom_ns = function(assert) {
assert.equal(div.toString(), exp.toString());
assert.done();
}

module.exports.local_namespaces = function(assert) {
var str = '<html xmlns="urn:example" xmlns:ex1="urn:example:1"><body xmlns:ex2="urn:example:2"/></html>';
var doc = libxml.parseXmlString(str);
assert.ok(doc);
var root = doc.root();
assert.ok(root);
var decls = root.namespaces(true)
assert.ok(decls);
assert.equal(2, decls.length);
decls.forEach(function(n) {
if (n.prefix()==null) {
assert.equal("urn:example", n.href());
}
else if (n.prefix() == "ex1") {
assert.equal("urn:example:1", n.href());
}
else {
assert.ok(false);
}
});
// body has a namespace, from the default declaration on html.
var body = root.get('ex:body', {ex: 'urn:example'});
assert.ok(body);
decls = body.namespaces(true);
assert.equal(1, decls.length);
assert.equal("urn:example:2", decls[0].href())

// Make sure default behavior still works,
// and doesn't get turned on by mistake
decls = body.namespaces();
assert.equal(3, decls.length);
decls = body.namespaces(false);
assert.equal(3, decls.length);
decls = body.namespaces(0);
assert.equal(3, decls.length);
decls = body.namespaces(1);
assert.equal(3, decls.length);
assert.done();
};

0 comments on commit 34d55d2

Please sign in to comment.