OSDN Git Service

insertBeforeメソッドの軽量化
authordhrname <dhrname@users.sourceforge.jp>
Tue, 15 May 2012 14:33:07 +0000 (23:33 +0900)
committerdhrname <dhrname@users.sourceforge.jp>
Tue, 15 May 2012 14:33:07 +0000 (23:33 +0900)
org/w3c/core.js
org/w3c/dom/events.js

index 12c5ab1..0f2474a 100644 (file)
@@ -248,6 +248,7 @@ Node.prototype = {
  */
 /*Node*/ insertBefore : function( /*Node*/ n, ref) {
   var tp = this.parentNode,
+      np = n.parentNode,
       rp;
   if (tp) {
     while (!tp) {                              //先祖をたどっていく
@@ -260,9 +261,13 @@ Node.prototype = {
   if (this.ownerDocument !== n.ownerDocument) { //所属Documentの生成元が違うならば
     throw (new DOMException(DOMException.WRONG_DOCUMENT_ERR));
   }
-  if (n.parentNode === this) {                  //入力した要素が子要素ならば
+  if (np === this) {                  //入力した要素が子要素ならば
     this.removeChild(n);
   }
+  if (np) {
+    (n === np.firstChild) && (np.firstChild = n.nextSibling);
+    (n === np.lastChild ) && (np.lastChild = n.previousSibling);
+  }
   if (!ref) {                                   //参照要素がNULLの場合、要素を追加する(appendChildと同じ効果)
     n._num = this.childNodes.length;
     this.childNodes[n._num] = n;
@@ -285,7 +290,7 @@ Node.prototype = {
   this.firstChild = this.childNodes[0];
   this.lastChild = this.childNodes[this.childNodes.length-1];
   n.parentNode = this;
-  tp = ref = rp = void 0;
+  tp = np = ref = rp = void 0;
   return n;
 },
 /*replaceChildメソッド
index 00618c8..b5a63c3 100644 (file)
@@ -395,6 +395,7 @@ MutationEvent.prototype = new Event();
 
 /*Node*/ Node.prototype.insertBefore = function( /*Node*/ n, ref) {
   var tp = this.parentNode,
+      np = n.parentNode,
       rp, evt,
       te = this,
       s, descend, di;
@@ -409,12 +410,12 @@ MutationEvent.prototype = new Event();
   if (this.ownerDocument !== n.ownerDocument) { //所属Documentの生成元が違うならば
     throw (new DOMException(DOMException.WRONG_DOCUMENT_ERR));
   }
-  if (n.parentNode === this) {                  //入力した要素が子要素ならば
+  if (np === this) {                  //入力した要素が子要素ならば
     this.removeChild(n);
   }
-  if (n.parentNode) {
-    (n === n.parentNode.firstChild) && (n.parentNode.firstChild = n.nextSibling);
-    (n === n.parentNode.lastChild ) && (n.parentNode.lastChild = n.previousSibling);
+  if (np) {
+    (n === np.firstChild) && (np.firstChild = n.nextSibling);
+    (n === np.lastChild ) && (np.lastChild = n.previousSibling);
   }
   if (!ref) {                                   //参照要素がNULLの場合、要素を追加する(appendChildと同じ効果)
     n._num = this.childNodes.length;
@@ -483,7 +484,7 @@ MutationEvent.prototype = new Event();
     }
   }
   evt = descend = void 0;
-  tp = rp = te = s = di = void 0;
+  tp = np = rp = te = s = di = void 0;
   return n;
 };