From 96a8fae6d0b49f812b432417429b65d63d63e668 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Fri, 15 Nov 2019 07:09:34 +0100 Subject: [PATCH] Discover node.remove capability at runtime --- cjs/index.js | 4 ---- cjs/utils.js | 29 ++++++++++------------------- esm/index.js | 4 ---- esm/utils.js | 29 ++++++++++------------------- index.js | 36 ++++++++++++++++-------------------- min.js | 4 ++-- 6 files changed, 38 insertions(+), 68 deletions(-) diff --git a/cjs/index.js b/cjs/index.js index 59104be..2345036 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -72,7 +72,6 @@ const domdiff = ( if (futureSame && currentStart < currentEnd) { remove( get, - parentNode, currentNodes, currentStart, currentEnd @@ -131,14 +130,12 @@ const domdiff = ( if (-1 < i) { remove( get, - parentNode, currentNodes, currentStart, i ); remove( get, - parentNode, currentNodes, i + futureChanges, currentEnd @@ -161,7 +158,6 @@ const domdiff = ( ); remove( get, - parentNode, currentNodes, currentStart, currentEnd diff --git a/cjs/utils.js b/cjs/utils.js index c2e8cb1..5785a55 100644 --- a/cjs/utils.js +++ b/cjs/utils.js @@ -86,9 +86,9 @@ const next = (get, list, i, length, before) => i < length ? before); exports.next = next; -const remove = (get, parent, children, start, end) => { +const remove = (get, children, start, end) => { while (start < end) - removeChild(get(children[start++], -1), parent); + drop(get(children[start++], -1)); }; exports.remove = remove; @@ -311,7 +311,6 @@ const applyDiff = ( else remove( get, - parentNode, currentNodes, currentStart++, currentStart @@ -381,19 +380,11 @@ const smartDiff = ( }; exports.smartDiff = smartDiff; -let removeChild = (child, parentNode) => { - /* istanbul ignore if */ - if ('remove' in child) { - removeChild = child => { - child.remove(); - }; - } - else { - removeChild = (child, parentNode) => { - /* istanbul ignore else */ - if (child.parentNode === parentNode) - parentNode.removeChild(child); - }; - } - removeChild(child, parentNode); -}; +const drop = node => (node.remove || dropChild).call(node); + +function dropChild() { + const {parentNode} = this; + /* istanbul ignore else */ + if (parentNode) + parentNode.removeChild(this); +} diff --git a/esm/index.js b/esm/index.js index 28e7719..703a20a 100644 --- a/esm/index.js +++ b/esm/index.js @@ -73,7 +73,6 @@ const domdiff = ( if (futureSame && currentStart < currentEnd) { remove( get, - parentNode, currentNodes, currentStart, currentEnd @@ -132,14 +131,12 @@ const domdiff = ( if (-1 < i) { remove( get, - parentNode, currentNodes, currentStart, i ); remove( get, - parentNode, currentNodes, i + futureChanges, currentEnd @@ -162,7 +159,6 @@ const domdiff = ( ); remove( get, - parentNode, currentNodes, currentStart, currentEnd diff --git a/esm/utils.js b/esm/utils.js index ff5664a..4f55904 100644 --- a/esm/utils.js +++ b/esm/utils.js @@ -79,9 +79,9 @@ export const next = (get, list, i, length, before) => i < length ? get(list[i - 1], -0).nextSibling : before); -export const remove = (get, parent, children, start, end) => { +export const remove = (get, children, start, end) => { while (start < end) - removeChild(get(children[start++], -1), parent); + drop(get(children[start++], -1)); }; // - - - - - - - - - - - - - - - - - - - @@ -303,7 +303,6 @@ const applyDiff = ( else remove( get, - parentNode, currentNodes, currentStart++, currentStart @@ -372,19 +371,11 @@ export const smartDiff = ( ); }; -let removeChild = (child, parentNode) => { - /* istanbul ignore if */ - if ('remove' in child) { - removeChild = child => { - child.remove(); - }; - } - else { - removeChild = (child, parentNode) => { - /* istanbul ignore else */ - if (child.parentNode === parentNode) - parentNode.removeChild(child); - }; - } - removeChild(child, parentNode); -}; +const drop = node => (node.remove || dropChild).call(node); + +function dropChild() { + const {parentNode} = this; + /* istanbul ignore else */ + if (parentNode) + parentNode.removeChild(this); +} diff --git a/index.js b/index.js index 1433141..59bef1c 100644 --- a/index.js +++ b/index.js @@ -98,9 +98,9 @@ var next = function next(get, list, i, length, before) { return i < length ? get(list[i], 0) : 0 < i ? get(list[i - 1], -0).nextSibling : before; }; -var remove = function remove(get, parent, children, start, end) { +var remove = function remove(get, children, start, end) { while (start < end) { - _removeChild(get(children[start++], -1), parent); + drop(get(children[start++], -1)); } }; @@ -273,7 +273,7 @@ var applyDiff = function applyDiff(diff, get, parentNode, futureNodes, futureSta break; case DELETION: // TODO: bulk removes for sequential nodes - if (live.has(currentNodes[currentStart])) currentStart++;else remove(get, parentNode, currentNodes, currentStart++, currentStart); + if (live.has(currentNodes[currentStart])) currentStart++;else remove(get, currentNodes, currentStart++, currentStart); break; } } @@ -293,21 +293,17 @@ var smartDiff = function smartDiff(get, parentNode, futureNodes, futureStart, fu applyDiff(OND(futureNodes, futureStart, futureChanges, currentNodes, currentStart, currentChanges, compare) || HS(futureNodes, futureStart, futureEnd, futureChanges, currentNodes, currentStart, currentEnd, currentChanges), get, parentNode, futureNodes, futureStart, currentNodes, currentStart, currentLength, before); }; -var _removeChild = function removeChild(child, parentNode) { - /* istanbul ignore if */ - if ('remove' in child) { - _removeChild = function removeChild(child) { - child.remove(); - }; - } else { - _removeChild = function removeChild(child, parentNode) { - /* istanbul ignore else */ - if (child.parentNode === parentNode) parentNode.removeChild(child); - }; - } - _removeChild(child, parentNode); +var drop = function drop(node) { + return (node.remove || dropChild).call(node); }; +function dropChild() { + var parentNode = this.parentNode; + /* istanbul ignore else */ + + if (parentNode) parentNode.removeChild(this); +} + /*! (c) 2018 Andrea Giammarchi (ISC) */ var domdiff = function domdiff(parentNode, // where changes happen @@ -357,7 +353,7 @@ options // optional object with one of the following properties // only stuff to remove if (futureSame && currentStart < currentEnd) { - remove(get, parentNode, currentNodes, currentStart, currentEnd); + remove(get, currentNodes, currentStart, currentEnd); return futureNodes; } @@ -380,8 +376,8 @@ options // optional object with one of the following properties i = indexOf(currentNodes, currentStart, currentEnd, futureNodes, futureStart, futureEnd, compare); // outer diff if (-1 < i) { - remove(get, parentNode, currentNodes, currentStart, i); - remove(get, parentNode, currentNodes, i + futureChanges, currentEnd); + remove(get, currentNodes, currentStart, i); + remove(get, currentNodes, i + futureChanges, currentEnd); return futureNodes; } } @@ -391,7 +387,7 @@ options // optional object with one of the following properties /* istanbul ignore else */ if (currentChanges < 2 || futureChanges < 2) { append(get, parentNode, futureNodes, futureStart, futureEnd, get(currentNodes[currentStart], 0)); - remove(get, parentNode, currentNodes, currentStart, currentEnd); + remove(get, currentNodes, currentStart, currentEnd); return futureNodes; } diff --git a/min.js b/min.js index 30f28cf..759d0d0 100644 --- a/min.js +++ b/min.js @@ -1,2 +1,2 @@ -var domdiff=function(){"use strict";/*! (c) Andrea Giammarchi - ISC */ -var r={};try{r.Map=Map}catch(Map){r.Map=function(){function r(r){return-1<(n=e.indexOf(r))}var n=0,e=[],t=[];return{"delete":function(o){var f=r(o);return f&&(e.splice(n,1),t.splice(n,1)),f},forEach:function(r,n){e.forEach(function(e,o){r.call(n,t[o],e,this)},this)},get:function(e){return r(e)?t[n]:void 0},has:function(n){return r(n)},set:function(o,f){return t[r(o)?n:e.push(o)-1]=f,this}}}}var n=r.Map,e=[].indexOf,t=function(r,n,t,o,f,i){for(var u=("selectedIndex"in n),a=u;o=u;){for(var a=n,c=o;au;)--c;v=a+o-c;var x=Array(v),y=l[c];for(--t;y;){for(var k=y,m=k.newi,A=k.oldi;t>m;)x[--v]=1,--t;for(;u>A;)x[--v]=-1,--u;x[--v]=0,--t,--u,y=y.prev}for(;t>=e;)x[--v]=1,--t;for(;u>=i;)x[--v]=-1,--u;return x},l=function(r,n,e,t,o,f,i){var u=e+f,a=[],c=void 0,v=void 0,l=void 0,s=void 0,d=void 0,h=void 0,p=void 0;r:for(c=0;c<=u;c++){if(c>50)return null;for(p=c-1,d=c?a[c-1]:[0,0],h=a[c]=[],v=-c;v<=c;v+=2){for(s=v===-c||v!==c&&d[p+v-1]=0;c--){for(;s>0&&l>0&&i(t[o+s-1],r[n+l-1]);)b[g--]=0,s--,l--;if(!c)break;p=c-1,d=c?a[c-1]:[0,0],v=s-l,v===-c||v!==c&&d[p+v-1]>>0;e=u;){for(var a=n,c=o;au;)--c;v=a+o-c;var x=Array(v),y=l[c];for(--t;y;){for(var k=y,A=k.newi,M=k.oldi;t>A;)x[--v]=1,--t;for(;u>M;)x[--v]=-1,--u;x[--v]=0,--t,--u,y=y.prev}for(;t>=n;)x[--v]=1,--t;for(;u>=i;)x[--v]=-1,--u;return x},s=function(r,n,e,t,o,f,i){var u=e+f,a=[],c=void 0,v=void 0,l=void 0,s=void 0,d=void 0,h=void 0,p=void 0;r:for(c=0;c<=u;c++){if(c>50)return null;for(p=c-1,d=c?a[c-1]:[0,0],h=a[c]=[],v=-c;v<=c;v+=2){for(s=v===-c||v!==c&&d[p+v-1]=0;c--){for(;s>0&&l>0&&i(t[o+s-1],r[n+l-1]);)b[g--]=0,s--,l--;if(!c)break;p=c-1,d=c?a[c-1]:[0,0],v=s-l,v===-c||v!==c&&d[p+v-1]>>0;e