Skip to content

Commit

Permalink
*AttributeNS now treat empty namespaces as no namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
lddubeau authored and domenic committed Feb 2, 2014
1 parent 93d0083 commit f0046e0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/jsdom/level2/core.js
Expand Up @@ -325,6 +325,10 @@ core.NamedNodeMap.prototype._map = function(fn) {
core.Element.prototype.getAttributeNS = function(/* string */ namespaceURI,
/* string */ localName)
{
if (namespaceURI === "") {
namespaceURI = null;
}

var attr = this._attributes.getNamedItemNS(namespaceURI, localName);
return attr && attr.nodeValue;
};
Expand All @@ -333,6 +337,10 @@ core.Element.prototype.setAttributeNS = function(/* string */ namespaceURI,
/* string */ qualifiedName,
/* string */ value)
{
if (namespaceURI === "") {
namespaceURI = null;
}

var s = qualifiedName.split(':'),
local = s.pop(),
prefix = s.pop() || null,
Expand Down Expand Up @@ -469,6 +477,10 @@ core.Element.prototype.hasAttribute = function(/* string */name)
core.Element.prototype.hasAttributeNS = function(/* string */namespaceURI,
/* string */localName)
{
if (namespaceURI === "") {
namespaceURI = null;
}

if (this._attributes.getNamedItemNS(namespaceURI, localName)) {
return true;
} else if (this.hasAttribute(localName)) {
Expand Down
66 changes: 66 additions & 0 deletions test/level2/core.js
Expand Up @@ -3537,7 +3537,29 @@ exports['elementgetattributens'] = testcase({
attrValue = element.getAttributeNS(nullNS,"defaultAttr");
test.equal(attrValue, "defaultVal", "elementgetattributens02");
test.done();
},
/**
*
The method getAttributeNS treats an empty string for the namespace
URI as meaning no namespace.
Using getAttributeNS, verify that we get the value of an attribute
which is not in any namespace if we pass a namespace URI equal to
"".
* @author Louis-Dominique Dubeau
* @see http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElGetAttrNS
* @see http://dom.spec.whatwg.org/#dom-element-getattributens
*/
elementgetattributens03: function(test) {
var doc = require('./core/files/staffNS.xml').staffNS();
var childList = doc.getElementsByTagNameNS("http://www.nist.gov", "employee");
var element = childList.item(1);
test.equal(element.getAttributeNS("", "defaultAttr"), "defaultVal",
"value should be 'defaultVal'");
test.done();
}

})

exports['elementgetelementsbytagnamens'] = testcase({
Expand Down Expand Up @@ -4381,6 +4403,29 @@ exports['elementsetattributens'] = testcase({
test.ok(success, 'elementsetattributens08_Err2');
}
test.done();
},
/**
*
The method setAttributeNS adds a new attribute in no namespace if
the namespace URI is set to "".
* @author Louis-Dominique Dubeau
* @see http://www.w3.org/TR/DOM-Level-2-Core/core#ID-ElSetAttrNS
* @see http://dom.spec.whatwg.org/#dom-element-setattributens
*/
elementsetattributens09: function(test) {
var element;

var doc = require('./core/files/staffNS.xml').staffNS();
element = doc.createElement("elem");

element.setAttributeNS("","x","test");
test.equal(element.getAttribute("x"), "test", "getAttribute");
test.equal(element.getAttributeNS("", "x"), "test",
"getAttributeNS with ''");
test.equal(element.getAttributeNS(null, "x"), "test",
"getAttributeNS with null");
test.done();
}
})

Expand Down Expand Up @@ -5615,7 +5660,28 @@ exports['hasAttributeNS'] = testcase({
state = testNode.hasAttributeNS(namespaceURI,localName);
test.ok(state, 'hasAttribute');
test.done();
},
/**
*
The method hasAttributeNS checks in no namespace if the namespace
URI is set to "".
* @author Louis-Dominique Dubeau
* @see http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttrNS
* @see http://dom.spec.whatwg.org/#dom-element-hasattributens
*/
hasAttributeNS06: function(test) {
var element;

var doc = require('./core/files/staffNS.xml').staffNS();
element = doc.createElement("elem");

element.setAttribute("x","test");
test.ok(element.hasAttributeNS("", "x"), "getAttributeNS with ''");
test.ok(element.hasAttributeNS(null, "x"), "getAttributeNS with null");
test.done();
}

})

exports['hasAttributes'] = testcase({
Expand Down

0 comments on commit f0046e0

Please sign in to comment.