Skip to content

Commit

Permalink
Fix lookupNamespaceURI / lookupPrefix for Document / Attributes.
Browse files Browse the repository at this point in the history
This fixes jindw#213.
  • Loading branch information
DrRataplan committed Jun 23, 2017
1 parent 366159a commit cb9c1ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 8 additions & 2 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ Node.prototype = {
},
lookupPrefix:function(namespaceURI){
var el = this;
if(el.nodeType == DOCUMENT_NODE){
el = el.documentElement;
}
while(el){
var map = el._nsMap;
//console.dir(map)
Expand All @@ -378,13 +381,16 @@ Node.prototype = {
}
}
}
el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
el = el.nodeType == ATTRIBUTE_NODE?el.ownerElement : el.parentNode;
}
return null;
},
// Introduced in DOM Level 3:
lookupNamespaceURI:function(prefix){
var el = this;
if(el.nodeType == DOCUMENT_NODE){
el = el.documentElement;
}
while(el){
var map = el._nsMap;
//console.dir(map)
Expand All @@ -393,7 +399,7 @@ Node.prototype = {
return map[prefix] ;
}
}
el = el.nodeType == ATTRIBUTE_NODE?el.ownerDocument : el.parentNode;
el = el.nodeType == ATTRIBUTE_NODE?el.ownerElement : el.parentNode;
}
return null;
},
Expand Down
12 changes: 10 additions & 2 deletions test/parse/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ wows.describe('XML Namespace Parse').addBatch({
console.assert(root.firstChild.namespaceURI=='http://p.com')
console.assert(root.lastChild.namespaceURI=='http://test.com')
console.assert(root.firstChild.nextSibling.lookupNamespaceURI('p') == 'http://test.com')
}
}).run(); // Run it
},
'performing a lookup from a document node': function () {
var documentNode = new DOMParser().parseFromString('<xml xmlns="http://test.com"/>','text/xml');
console.assert(documentNode.lookupNamespaceURI('') == 'http://test.com')
},
'performing a lookup from an attribute node': function () {
var documentNode = new DOMParser().parseFromString('<xml xmlns="http://test.com" attr="1"/>','text/xml');
console.assert(documentNode.documentElement.getAttributeNode('attr').lookupNamespaceURI('') == 'http://test.com')
}
}).run(); // Run it

0 comments on commit cb9c1ab

Please sign in to comment.