8000 jsdom/lib/jsdom/level1/core.js at level3 · gavinuhma/jsdom · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
1611 lines (1350 loc) · 45.2 KB

core.js

File metadata and controls

1611 lines (1350 loc) · 45.2 KB
object = this.ownerDocument.createDocumentFragment();
break;
case this.NOTATION_NODE:
object = this.ownerDocument.createNotationNode(this._name,
this._publicId,
this._systemId);
object = attrCopy(this,object);
object.nodeValue = this.nodeValue;
break;
default:
throw new core.DOMException(NOT_FOUND_ERR);
break;
}
if (deep || this.nodeType === this.ATTRIBUTE_NODE) {
var clone = null;
for (var i=0;i<this.children.length;i++)
{
clone = this.children.item(i).cloneNode(true);
if (!clone) {
debug(this.children.item(i).nodeType);
}
if (clone.nodeType === this.ATTRIBUTE_NODE) {
object.setAttributeNode(clone);
} else {
object.appendChild(clone);
}
}
}
return object;
},
/* returns void */
normalize: function() {
var prevChild, child, attr,i;
if (this._attributes && this._attributes.length) {
for (i=0;i<this._attributes.length;i++)
{
if (attr = this._attributes.item(i).normalize) {
attr = this._attributes.item(i).normalize();
}
}
}
for (i=0;i<this._children.length;i++)
{
child = this._children[i];
if (child.normalize) {
child.normalize();
}
if (i>0) {
prevChild = this._children[i-1];
if (child.nodeType === this.TEXT_NODE &&
prevChild.nodeType === this.TEXT_NODE)
{
// remove the child and decrement i
prevChild.appendData(child.value);
this.removeChild(child);
i--;
}
}
}
},
toString: function() {
var id = '';
if (this.id) {
id = '#' + this.id;
}
if (this.className) {
id += '.' + this.className;
}
return '[ ' + this.tagName + id + ' ]';
}
};
core.NamedNodeMap = function(document) {
this._nodes = {};
this._length = 0;
this._ownerDocument = document;
this._readonly = false;
};
core.NamedNodeMap.prototype = {
get readonly() { return this._readonly;},
get ownerDocument() { this._ownerDocument;},
get length() {
var count = 0;
for (var i in this._nodes) {
count++;
}
this._length = count;
return this._length;
},
exists : function(name) {
return (this._nodes[name] || this._nodes[name] === null) ? true : false;
},
/* returns Node */
getNamedItem: function(/* string */ name) {
return this._nodes[name] || null;
},
/* returns Node */
setNamedItem: function(/* Node */ arg) {
// readonly
if (this._readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}
// arg is from a different document
if (arg && arg._ownerDocument !== this._ownerDocument) {
throw new core.DOMException(WRONG_DOCUMENT_ERR);
}
// if this argument is already in use..
if (arg && arg._parentNode) {
throw new core.DOMException(INUSE_ATTRIBUTE_ERR);
}
var ret;
if (!this._nodes[arg.name] || this._nodes[arg.name] === null) {
this._length++;
ret = null;
} else {
ret = this._nodes[arg.name];
}
arg._parentNode = this;
arg._specified = true;
this._nodes[arg.name] = arg;
return ret;
}, // raises: function(DOMException) {},
/* returns Node */
removeNamedItem: function(/* string */ name) {
// readonly
if (this.readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}
if (!this._nodes.hasOwnProperty(name)) {
throw new core.DOMException(NOT_FOUND_ERR);
}
var prev = this._nodes[name] || null;
this._nodes[name] = null;
delete this._nodes[name];
this._length--;
return prev;
}, // raises: function(DOMException) {},
/* returns Node */
item: function(/* int */ index) {
var current = 0;
for (var member in this._nodes) {
if (this._nodes.hasOwnProperty(member)) {
if (current === index && this._nodes[member]) {
return this._nodes[member];
}
current++;
}
}
return null;
}
};
core.AttrNodeMap = function(document, parentNode) {
core.NamedNodeMap.call(this,document);
this._parentNode = parentNode;
};
core.AttrNodeMap.prototype = {
get parentNode() { return this._parentNode;},
/* returns Node */
removeNamedItem: function(/* string */ name) {
var prev = core.NamedNodeMap.prototype.removeNamedItem.call(this, name);
var doc = this._ownerDocument;
// set default value if available
if (doc && doc.doctype && doc.doctype.name.toLowerCase() !== "html") {
var defaultValue = false;
var elem = doc.doctype._attributes.getNamedItem(this.parentNode.nodeName);
var defaultValue = elem.attributes.getNamedItem(name);
if (defaultValue) {
var attr = doc.createAttribute(name);
attr.value = defaultValue.value;
attr._specified = false;
this._nodes[name] = attr;
this._length++;
}
}
return prev;
}, // raises: function(DOMException) {},
};
core.AttrNodeMap.prototype.__proto__ = core.NamedNodeMap.prototype;
core.NotationNodeMap = function(document) {
core.NamedNodeMap.call(this,document);
this._readonly = false;
for (var i=1;i<arguments.length;i++) {
this.setNamedItem(arguments[i]);
}
this._readonly = true;
};
core.NotationNodeMap.prototype = {};
core.NotationNodeMap.prototype.__proto__ = core.NamedNodeMap.prototype;
core.EntityNodeMap = function(document) {
core.NamedNodeMap.call(this,document);
this._readonly = false;
for (var i=1;i<arguments.length;i++) {
this.setNamedItem(arguments[i]);
}
this._readonly = true;
};
core.EntityNodeMap.prototype = {};
core.EntityNodeMap.prototype.__proto__ = core.NamedNodeMap.prototype;
core.Element = function (document, tagName) {
this._attributes = null;
this._ownerDocument = document;
core.Node.call(this, document);
this._nodeName = tagName;
this._nodeType = this.ELEMENT_NODE;
this._tagName = tagName;
};
core.Element.prototype = {
get nodeValue() { return null;},
set nodeValue(value) { /* do nothing */ },
get tagName() {
if (this.nodeType === this.ELEMENT_NODE &&
this.ownerDocument &&
this.ownerDocument.doctype &&
this.ownerDocument.doctype.name.indexOf("html") !== -1)
{
return this.nodeName.toUpperCase();
}
return this.nodeName;
},
get nodeType() { return this._nodeType;},
get attributes() { return this._attributes;},
get name() { return this.nodeName;},
/* returns string */
getAttribute: function(/* string */ name) {
var attribute = this._attributes.getNamedItem(name);
if (attribute) {
return attribute.value;
}
return "";
},
/* returns string */
setAttribute: function(/* string */ name, /* string */ value) {
// readonly
if (this.readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}
if (this._attributes === null) {
this._attributes = new core.AttrNodeMap(this.ownerDocument, this);
}
if (this.ownerDocument) {
var attr = this.ownerDocument.createAttribute(name);
attr.value = value;
if (this.attributes.exists(name)) {
this._attributes.removeNamedItem(name);
}
this._attributes.setNamedItem(attr);
if (name === 'id') {
this._ownerDocument._ids[value] = this;
}
}
return value;
}, //raises: function(DOMException) {},
/* returns string */
removeAttribute: function(/* string */ name) {
// readonly
if (this.readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}
if (!this._attributes.exists(name)) {
return name;
}
this._attributes.removeNamedItem(name);
return name;
}, // raises: function(DOMException) {},
/* returns Attr */
getAttributeNode: function(/* string */ name) {
if (!this._attributes.exists(name)) {
return null;
}
return this._attributes.getNamedItem(name);
},
/* returns Attr */
setAttributeNode: function(/* Attr */ newAttr) {
// readonly
if (this.readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}
if (this._attributes === null) {
this._attributes = new core.AttrNodeMap(this.ownerDocument, this);
}
// only attributes created with this.ownerDocument can be added here
if (newAttr.ownerDocument !== this.ownerDocument) {
throw new core.DOMException(WRONG_DOCUMENT_ERR);
}
var prevNode = this._attributes.getNamedItem(newAttr.name);
if (prevNode) {
prevNode._parentNode = null;
}
this._attributes.setNamedItem(newAttr);
return (prevNode && prevNode.specified) ? prevNode : null;
}, // raises: function(DOMException) {},
/* returns Attr */
removeAttributeNode: function(/* Attr */ oldAttr) {
// readonly
if (this.readonly === true) {
throw new core.DOMException(NO_MODIFICATION_ALLOWED_ERR);
}
var existingAttr = this._attributes.getNamedItem(oldAttr.name);
if (this._attributes && existingAttr === oldAttr) {
this._attributes.removeNamedItem(oldAttr.name);
return oldAttr;
}
throw new core.DOMException(NOT_FOUND_ERR);
}, //raises: function(DOMException) {},
/* returns NodeList */
getElementsByTagName: function(/* string */ name) {
var queryFunction = function(child) {
if (child.nodeType && child.nodeType ===
core.Node.prototype.ENTITY_REFERENCE_NODE)
{
child = child._entity;
}
if (child.nodeName && child.nodeType === core.Node.prototype.ELEMENT_NODE)
{
if (name === "*") {
return true;
// case insensitivity for html
} else if (child.ownerDocument && child.ownerDocument.doctype &&
child.ownerDocument.doctype.name === "html" &&
child.nodeName.toLowerCase() === name.toLowerCase())
{
return true;
} else if (child.nodeName.toLowerCase() === name.toLowerCase()) {
return true;
}
}
return false;
}
if (this.ownerDocument &&
this.ownerDocument.implementation &&
this.ownerDocument.implementation.hasFeature("DisableLiveLists"))
{
return core.mapDOMNodes(this, true, queryFunction);
} else {
return new core.LiveNodeList(this._document, this, queryFunction);
}
},
};
core.Element.prototype.__proto__ = core.Node.prototype;
core.DocumentFragment = function(document) {
core.Element.call(this, document, "#document-fragment");
this._nodeType = this.DOCUMENT_FRAGMENT_NODE;
};
core.DocumentFragment.prototype = {
get nodeValue() { return null;},
set nodeValue() { /* do nothing */ },
get attributes() { return null;}
};
0