Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added _nodes cache to _ownerDocument and added RegEx caching

  • Loading branch information...
commit f140a22ffa4db8f33afba58e5eb8d9ddf7198cf4 1 parent 5755c8d
Dav Glass davglass authored tmpvar committed
Showing with 21 additions and 9 deletions.
  1. +21 −9 lib/jsdom/level1/core.js
30 lib/jsdom/level1/core.js
View
@@ -1032,8 +1032,14 @@ core.Document = function(name, doctype, implementation, /* string */contentType)
this._ownerDocument = this;
this._readonly = false;
};
+
+
+var tagRegEx = /[^\w:\d_-]+/i;
+var entRegEx = /[^\w\d_\-&;]+/;
+
core.Document.prototype = {
_elementBuilders : {},
+ _nodes: null,
get contentType() { return this._contentType;},
get doctype() { return this._doctype || null;},
set doctype(doctype) { this._doctype = doctype;},
@@ -1071,9 +1077,11 @@ core.Document.prototype = {
/* returns Element */
createElement: function(/* string */ tagName) {
var c = [], lower = tagName.toLowerCase(), element;
- if (!tagName || !tagName.match || (c = tagName.match(/[^\w:\d_-]+/i))) {
+
+ if (!tagName || !tagName.match || (c = tagName.match(tagRegEx))) {
throw new core.DOMException(INVALID_CHARACTER_ERR, 'Invalid character in tag name: ' + c.pop());
}
+
if (this._elementBuilders[lower]) {
element = this._elementBuilders[lower](this, tagName);
@@ -1087,8 +1095,8 @@ core.Document.prototype = {
if (attrElement && attrElement.children) {
attrs = attrElement.attributes;
- var attr;
- for (var i=0;i<attrs.length;i++) {
+ var attr, len = attrs.length;
+ for (var i = 0; i < len; i++) {
attr = this.createAttribute(attrs.item(i).name);
attr.value = attrs.item(i).value;
element.setAttributeNode(attr);
@@ -1096,6 +1104,10 @@ core.Document.prototype = {
}
}
}
+ if (!this._nodes) {
+ this._nodes = [];
+ }
+ this._nodes.push(this);
return element;
}, //raises: function(DOMException) {},
@@ -1130,7 +1142,7 @@ core.Document.prototype = {
throw new core.DOMException(NOT_SUPPORTED_ERR);
}
- if (target.match(/[^\w\d_-]+/) || !target || !target.length) {
+ if (target.match(tagRegEx) || !target || !target.length) {
throw new core.DOMException(INVALID_CHARACTER_ERR);
}
@@ -1139,7 +1151,7 @@ core.Document.prototype = {
/* returns Attr */
createAttribute: function(/* string */ name) {
- if (!name || !name.length || name.match(/[^\w:\d_\.-]+/) ) {
+ if (!name || !name.length || name.match(tagRegEx) ) {
throw new core.DOMException(INVALID_CHARACTER_ERR);
}
return new core.Attr(this, name,false);
@@ -1157,7 +1169,7 @@ core.Document.prototype = {
throw new core.DOMException(INVALID_CHARACTER_ERR);
}
- if (name.match(/[^\w\d_-]+/)) {
+ if (name.match(tagRegEx)) {
throw new core.DOMException(INVALID_CHARACTER_ERR);
}
@@ -1173,7 +1185,7 @@ core.Document.prototype = {
createEntityNode : function(/* string */ name)
{
- if (name.match(/[^\w\d_\-&;]+/) || !name || !name.length) {
+ if (name.match(entRegEx) || !name || !name.length) {
throw new core.DOMException(INVALID_CHARACTER_ERR);
}
@@ -1194,7 +1206,7 @@ core.Document.prototype = {
createNotationNode : function(/* string */ name,/* string */ publicId,/* string */ systemId)
{
- if (name.match(/[^\w\d_\-&;]+/) || !name || !name.length) {
+ if (name.match(entRegEx) || !name || !name.length) {
throw new core.DOMException(INVALID_CHARACTER_ERR);
}
@@ -1213,7 +1225,7 @@ core.Document.prototype = {
/* returns Node */
- appendChild : function(/* Node */ newChild){
+ appendChild: function(/* Node */ newChild){
// readonly
if (this.readonly === true) {
Please sign in to comment.
Something went wrong with that request. Please try again.