Skip to content

Commit

Permalink
boiling it down once again
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpvar committed Feb 7, 2011
1 parent e47c383 commit 66ff77a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/jsdom/level1/core.js
Expand Up @@ -834,6 +834,7 @@ core.Node.prototype = {

core.NamedNodeMap = function NamedNodeMap(document) {
this._nodes = {};
this._nsStore = {};
this.length = 0;
this._ownerDocument = document;
this._readonly = false;
Expand Down
59 changes: 41 additions & 18 deletions lib/jsdom/level2/core.js
Expand Up @@ -151,6 +151,11 @@ core.Node.prototype.hasAttributes = function() {
core.NamedNodeMap.prototype.getNamedItemNS = function(/* string */ namespaceURI,
/* string */ localName)
{
if (this._nsStore[namespaceURI] && this._nsStore[namespaceURI][localName]) {
return this._nsStore[namespaceURI][localName];
}
return null;


return this._map(function(item) {
var node = null, equal = item.localName === localName;
Expand Down Expand Up @@ -202,6 +207,25 @@ core.NamedNodeMap.prototype.setNamedItemNS = function(/* Node */ arg)
throw new core.DOMException(core.INUSE_ATTRIBUTE_ERR);
}

// readonly
if (this._readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}


if (!this._nsStore[arg.namespaceURI]) {
this._nsStore[arg.namespaceURI] = {};
}
var existing = null;
if (this._nsStore[arg.namespaceURI][arg.localName]) {
var existing = this._nsStore[arg.namespaceURI][arg.localName];
}

this._nsStore[arg.namespaceURI][arg.localName] = arg;

arg._specified = true;
arg._ownerDocument = this._ownerDocument;

return this.setNamedItem(arg);
};

Expand Down Expand Up @@ -229,21 +253,20 @@ core.NamedNodeMap.prototype.removeNamedItemNS = function(/*string */ namespaceUR
}

defaultEl = defaults.getNamedItemNS(parent._namespaceURI, parent._localName);
var found = null;

found = this._map(function(attr) {
if (namespaceURI === attr.namespaceURI &&
attr.localName === localName)
{
return true;
}
})[0] || null;
if (this._nsStore[namespaceURI] &&
this._nsStore[namespaceURI][localName])
{
found = this._nsStore[namespaceURI][localName];
this.removeNamedItem(found.qualifiedName);
delete this._nsStore[namespaceURI][localName];
}

if (!found) {
throw new core.DOMException(core.NOT_FOUND_ERR);
}

this.removeNamedItem(found.name);

if (defaultEl) {
defaultAttr = defaultEl._attributes.getNamedItemNS(namespaceURI, localName);

Expand Down Expand Up @@ -353,7 +376,7 @@ core.Element.prototype.removeAttributeNS = function(/* string */ namespaceURI,
}

var defaults = this.ownerDocument.doctype._attributes, clone,
found = this._attributes.getNamedItemNS(namespaceURI, localName),
found,
defaultEl = defaults.getNamedItemNS(namespaceURI, localName),
defaultAttr,
clone;
Expand All @@ -362,9 +385,7 @@ core.Element.prototype.removeAttributeNS = function(/* string */ namespaceURI,
defaultAttr = defaultEl._attributes.getNamedItemNS(namespaceURI, localName);
}

if (found) {
this._attributes.removeNamedItemNS(namespaceURI, localName);
}
found = this._attributes.removeNamedItemNS(namespaceURI, localName);

if (defaultAttr) {
clone = this.setAttributeNS(defaultAttr.namespaceURI,
Expand All @@ -390,16 +411,13 @@ core.Element.prototype.setAttributeNodeNS = function(/* Attr */ newAttr)
}

var existing = null;

try {
existing = this._attributes.removeNamedItemNS(newAttr.namespaceURI,
newAttr.localName);
} catch (e) { /* noop */}

newAttr._ownerElement = this;
this._attributes.setNamedItemNS(newAttr);

return existing || null;
return this._attributes.setNamedItemNS(newAttr);
};

core.Element.prototype.getElementsByTagNameNS = function(/* String */ namespaceURI,
Expand Down Expand Up @@ -440,7 +458,12 @@ core.Element.prototype.hasAttribute = function(/* string */name)
core.Element.prototype.hasAttributeNS = function(/* string */namespaceURI,
/* string */localName)
{
return !!this._attributes.getNamedItemNS(namespaceURI, localName);
if (this._attributes.getNamedItemNS(namespaceURI, localName)) {
return true;
} else if (this.hasAttribute(localName)) {
return true;
}
return false;
};

core.DocumentType.prototype.__defineGetter__("publicId", function() {
Expand Down
1 change: 1 addition & 0 deletions test/level2/core.js
Expand Up @@ -4392,6 +4392,7 @@ elementhasattributens02 : function () {
element = doc.createElementNS("http://www.w3.org/DOM","address");
attribute = doc.createAttributeNS("http://www.w3.org/DOM","domestic");
newAttribute = element.setAttributeNode(attribute);
console.log(element.attributes._nsStore);
state = element.hasAttributeNS("http://www.w3.org/DOM","domestic");
assertTrue("hasDomesticAttr",state);

Expand Down

0 comments on commit 66ff77a

Please sign in to comment.