From 3433cabf8874f387211b14f66ba02baa131f0981 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Fri, 2 May 2014 22:20:51 -0400 Subject: [PATCH] fix cloning when undefined present in tree --- archive/v0.1.11/mithril-tests.js | 37 ++++++++++++++++++++++--------- archive/v0.1.11/mithril.js | 7 +++--- archive/v0.1.11/mithril.min.js | 2 +- archive/v0.1.11/mithril.min.map | 2 +- archive/v0.1.11/mithril.min.zip | Bin 42026 -> 41993 bytes mithril.js | 7 +++--- tests/mithril-tests.js | 30 ++++++++++++++++++++----- 7 files changed, 59 insertions(+), 26 deletions(-) diff --git a/archive/v0.1.11/mithril-tests.js b/archive/v0.1.11/mithril-tests.js index 1d8f1c5f..7f40924c 100644 --- a/archive/v0.1.11/mithril-tests.js +++ b/archive/v0.1.11/mithril-tests.js @@ -33,10 +33,7 @@ Mithril = m = new function app(window) { return cell } function build(parentElement, parentTag, data, cached, shouldReattach, index, editable, namespace) { - if (data === null || data === undefined) { - if (cached) clear(cached.nodes) - return - } + if (data === null || data === undefined) data = "" if (data.subtree === "retain") return var cachedType = type.call(cached), dataType = type.call(data) @@ -56,7 +53,9 @@ Mithril = m = new function app(window) { cached[cacheCount++] = item } if (!intact) { + //console.log(123, nodes, cached.nodes, 666,cached[0], cached[1], data.length) for (var i = 0; i < data.length; i++) if (cached[i] !== undefined) nodes = nodes.concat(cached[i].nodes) + //console.log(234, nodes, cached.nodes, cached[i]) for (var i = nodes.length, node; node = cached.nodes[i]; i++) if (node.parentNode !== null) node.parentNode.removeChild(node) for (var i = cached.nodes.length, node; node = nodes[i]; i++) if (node.parentNode === null) parentElement.appendChild(node) if (data.length < cached.length) cached.length = data.length @@ -715,7 +714,7 @@ function testMithril(mock) { test(function() { var root = mock.document.createElement("div") m.render(root, m("div", [undefined])) - return root.childNodes[0].childNodes.length === 0 + return root.childNodes[0].childNodes[0].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") @@ -738,19 +737,19 @@ function testMithril(mock) { var root = mock.document.createElement("div") m.render(root, m("ul", [m("li")])) m.render(root, m("ul", [m("li"), undefined])) - return root.childNodes[0].childNodes.length === 1 + return root.childNodes[0].childNodes[1].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") m.render(root, m("ul", [m("li"), m("li")])) m.render(root, m("ul", [m("li"), undefined])) - return root.childNodes[0].childNodes.length === 1 + return root.childNodes[0].childNodes[1].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") m.render(root, m("ul", [m("li")])) m.render(root, m("ul", [undefined])) - return root.childNodes[0].childNodes.length === 0 + return root.childNodes[0].childNodes[0].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") @@ -936,6 +935,24 @@ function testMithril(mock) { m.render(root, m("div", "foo")) return root.childNodes.length == 1 }) + test(function() { + var root = mock.document.createElement("div") + m.render(root, m("div", [m("button"), m("ul")])) + var valueBefore = root.childNodes[0].childNodes[0].nodeName + m.render(root, m("div", [undefined, m("ul")])) + var valueAfter = root.childNodes[0].childNodes[0].nodeValue + return valueBefore === "BUTTON" && valueAfter === "" + }) + test(function() { + var root = mock.document.createElement("div") + m.render(root, m("div", [m("ul"), undefined])) + var valueBefore1 = root.childNodes[0].childNodes[0].nodeName + var valueBefore2 = root.childNodes[0].childNodes[1].nodeValue + m.render(root, m("div", [undefined, m("ul")])) + var valueAfter1 = root.childNodes[0].childNodes[0].nodeValue + var valueAfter2 = root.childNodes[0].childNodes[1].nodeName + return valueBefore1 === "UL" && valueAfter1 === "" && valueBefore2 === "" && valueAfter2 === "UL" + }) //end m.render //m.redraw @@ -949,11 +966,11 @@ function testMithril(mock) { }) controller.value = "foo" m.redraw() - var lengthBefore = root.childNodes.length + var valueBefore = root.childNodes[0].nodeValue mock.performance.$elapse(50) m.redraw() mock.performance.$elapse(50) //teardown - return lengthBefore === 0 && root.childNodes[0].nodeValue === "foo" + return valueBefore === "" && root.childNodes[0].nodeValue === "foo" }) test(function() { mock.performance.$elapse(50) //setup diff --git a/archive/v0.1.11/mithril.js b/archive/v0.1.11/mithril.js index 390859dd..18a2bc1b 100644 --- a/archive/v0.1.11/mithril.js +++ b/archive/v0.1.11/mithril.js @@ -33,10 +33,7 @@ Mithril = m = new function app(window) { return cell } function build(parentElement, parentTag, data, cached, shouldReattach, index, editable, namespace) { - if (data === null || data === undefined) { - if (cached) clear(cached.nodes) - return - } + if (data === null || data === undefined) data = "" if (data.subtree === "retain") return var cachedType = type.call(cached), dataType = type.call(data) @@ -56,7 +53,9 @@ Mithril = m = new function app(window) { cached[cacheCount++] = item } if (!intact) { + //console.log(123, nodes, cached.nodes, 666,cached[0], cached[1], data.length) for (var i = 0; i < data.length; i++) if (cached[i] !== undefined) nodes = nodes.concat(cached[i].nodes) + //console.log(234, nodes, cached.nodes, cached[i]) for (var i = nodes.length, node; node = cached.nodes[i]; i++) if (node.parentNode !== null) node.parentNode.removeChild(node) for (var i = cached.nodes.length, node; node = nodes[i]; i++) if (node.parentNode === null) parentElement.appendChild(node) if (data.length < cached.length) cached.length = data.length diff --git a/archive/v0.1.11/mithril.min.js b/archive/v0.1.11/mithril.min.js index bbaf6e4c..d3e258a6 100644 --- a/archive/v0.1.11/mithril.min.js +++ b/archive/v0.1.11/mithril.min.js @@ -4,5 +4,5 @@ http://github.com/lhorie/mithril.js (c) Leo Horie License: MIT */ -Mithril=m=new function(a){function b(){var a=arguments,b="[object Object]"==u.call(a[1]),c=b?a[1]:{},d="class"in c?"class":"className",e=t[a[0]];if(void 0===e){t[a[0]]=e={tag:"div",attrs:{}};for(var f,h=[];f=v.exec(a[0]);)if(""==f[1])e.tag=f[2];else if("#"==f[1])e.attrs.id=f[2];else if("."==f[1])h.push(f[2]);else if("["==f[3][0]){var i=w.exec(f[3]);e.attrs[i[1]]=i[3]||!0}h.length>0&&(e.attrs[d]=h.join(" "))}e=g(e),e.attrs=g(e.attrs),e.children=b?a[2]:a[1];for(var j in c)e.attrs[j]=j==d?(e.attrs[j]||"")+" "+c[j]:c[j];return e}function c(b,g,h,i,j,k,l,m){if(null===h||void 0===h)return void(i&&e(i.nodes));if("retain"!==h.subtree){var n=u.call(i),o=u.call(h);if(n!=o&&(null!==i&&void 0!==i&&e(i.nodes),i=new h.constructor,i.nodes=[]),"[object Array]"==o){for(var p=[],q=i.length===h.length,r=0,s=0,t=0;s-1?new h.constructor(h):h,i.nodes=[w];else if(i.valueOf()!==h.valueOf()||j===!0){if(!l||l!==a.document.activeElement)if(h.$trusted){var y=i.nodes[0],p=[y];if(y){for(;y=y.nextSibling;)p.push(y);e(p),w=f(b,k,h)}else b.innerHTML=h}else w=i.nodes[0],"textarea"===g?b.value=h:l?l.innerHTML=h:(b.insertBefore(w,b.childNodes[k]||null),w.nodeValue=h);i=new h.constructor(h),i.nodes=[w]}else i.nodes.intact=!0}return i}}function d(b,c,d,e,f){for(var g in d){var i=d[g],j=e[g];if(!(g in e)||j!==i||b===a.document.activeElement){if(e[g]=i,"config"===g)continue;if("function"==typeof i&&0==g.indexOf("on"))b[g]=h(i,b);else if("style"===g)for(var k in i)console.log(b),(void 0===j||j[k]!==i[k])&&(b.style[k]=i[k]);else void 0!==f?"href"===g?b.setAttributeNS("http://www.w3.org/1999/xlink","href",i):"className"===g?b.setAttribute("class",i):b.setAttribute(g,i):"value"===g&&"input"===c?b.value!==i&&(b.value=i):g in b?b[g]=i:b.setAttribute(g,i)}}return e}function e(a){for(var b=a.length-1;b>-1;b--)a[b].parentNode.removeChild(a[b]);a.length=0}function f(a,b,c){var d=a.childNodes[b];return d?d.insertAdjacentHTML("beforebegin",c):a.insertAdjacentHTML("beforeend",c),d?d.previousSibling:a.firstChild}function g(a){var b={};for(var c in a)b[c]=a[c];return b}function h(a,c){return function(d){b.startComputation();var e=a.call(c,d);return b.endComputation(),e}}function i(){for(var a=0;a0&&("GET"==a.method?a.url=a.url+(a.url.indexOf("?")<0?"?":"&")+o(b):a.data=c(b)),a}function q(a,b){var c=a.match(/:[a-z]\w+/gi);if(c&&b)for(var d=0;de?y.push(b)-1:e,g=b==a.document||b==a.document.documentElement?x:b;z[f]=c(g,null,d,z[f],!1,0,null,void 0)},b.trust=function(a){return a=new String(a),a.$trusted=!0,a};var A=[],B=[],C=[],D=0,E=0,F=0,G=null;b.module=function(a,c){b.startComputation();var d=A.indexOf(a);0>d&&(d=A.length),A[d]=a,B[d]=c,C[d]=new c.controller,b.endComputation()},b.redraw=function(){if(D=a.performance&&a.performance.now?a.performance.now():(new a.Date).getTime(),D-E>16)i();else{var b=a.cancelAnimationFrame||a.clearTimeout,c=a.requestAnimationFrame||a.setTimeout;b(F),F=c(i,0)}};var H=0;b.startComputation=function(){H++},b.endComputation=function(){H=Math.max(H-1,0),0==H&&b.redraw()},b.withAttr=function(a,b){return function(c){b(a in c.currentTarget?c.currentTarget[a]:c.currentTarget.getAttribute(a))}};var I,J={pathname:"",hash:"#",search:"?"},K=function(){},L={};return b.route=function(){if(0===arguments.length)return I;if(3===arguments.length){I=a.location[b.route.mode].slice(J[b.route.mode].length);var c=arguments[0],d=arguments[1],e=arguments[2];K=function(a){var f=a.slice(J[b.route.mode].length);j(c,e,f)||b.route(d,!0)};var f="hash"==b.route.mode?"onhashchange":"onpopstate";a[f]=function(){K(a.location[b.route.mode])},G=l,a[f]()}else if(arguments[0].addEventListener){var g=arguments[0],h=arguments[1];g.href=J[b.route.mode]+g.pathname,h||(g.removeEventListener("click",k),g.addEventListener("click",k))}else if("string"==typeof arguments[0]){I=arguments[0];var i=arguments[1]===!0;a.history.pushState?(G=function(){a.history[i?"replaceState":"pushState"](null,a.document.title,J[b.route.mode]+I),l()},K(J[b.route.mode]+I)):a.location[b.route.mode]=I}},b.route.param=function(a){return L[a]},b.route.mode="search",b.prop=function(a){var b=function(){return arguments.length&&(a=arguments[0]),a};return b.toJSON=function(){return a},b},b.deferred=function(){var a=[],c=[],d={resolve:function(b){for(var c=0;c0&&(e.attrs[d]=h.join(" "))}e=g(e),e.attrs=g(e.attrs),e.children=b?a[2]:a[1];for(var j in c)e.attrs[j]=j==d?(e.attrs[j]||"")+" "+c[j]:c[j];return e}function c(b,g,h,i,j,k,l,m){if((null===h||void 0===h)&&(h=""),"retain"!==h.subtree){var n=u.call(i),o=u.call(h);if(n!=o&&(null!==i&&void 0!==i&&e(i.nodes),i=new h.constructor,i.nodes=[]),"[object Array]"==o){for(var p=[],q=i.length===h.length,r=0,s=0,t=0;s-1?new h.constructor(h):h,i.nodes=[w];else if(i.valueOf()!==h.valueOf()||j===!0){if(!l||l!==a.document.activeElement)if(h.$trusted){var y=i.nodes[0],p=[y];if(y){for(;y=y.nextSibling;)p.push(y);e(p),w=f(b,k,h)}else b.innerHTML=h}else w=i.nodes[0],"textarea"===g?b.value=h:l?l.innerHTML=h:(b.insertBefore(w,b.childNodes[k]||null),w.nodeValue=h);i=new h.constructor(h),i.nodes=[w]}else i.nodes.intact=!0}return i}}function d(b,c,d,e,f){for(var g in d){var i=d[g],j=e[g];if(!(g in e)||j!==i||b===a.document.activeElement){if(e[g]=i,"config"===g)continue;if("function"==typeof i&&0==g.indexOf("on"))b[g]=h(i,b);else if("style"===g)for(var k in i)console.log(b),(void 0===j||j[k]!==i[k])&&(b.style[k]=i[k]);else void 0!==f?"href"===g?b.setAttributeNS("http://www.w3.org/1999/xlink","href",i):"className"===g?b.setAttribute("class",i):b.setAttribute(g,i):"value"===g&&"input"===c?b.value!==i&&(b.value=i):g in b?b[g]=i:b.setAttribute(g,i)}}return e}function e(a){for(var b=a.length-1;b>-1;b--)a[b].parentNode.removeChild(a[b]);a.length=0}function f(a,b,c){var d=a.childNodes[b];return d?d.insertAdjacentHTML("beforebegin",c):a.insertAdjacentHTML("beforeend",c),d?d.previousSibling:a.firstChild}function g(a){var b={};for(var c in a)b[c]=a[c];return b}function h(a,c){return function(d){b.startComputation();var e=a.call(c,d);return b.endComputation(),e}}function i(){for(var a=0;a0&&("GET"==a.method?a.url=a.url+(a.url.indexOf("?")<0?"?":"&")+o(b):a.data=c(b)),a}function q(a,b){var c=a.match(/:[a-z]\w+/gi);if(c&&b)for(var d=0;de?y.push(b)-1:e,g=b==a.document||b==a.document.documentElement?x:b;z[f]=c(g,null,d,z[f],!1,0,null,void 0)},b.trust=function(a){return a=new String(a),a.$trusted=!0,a};var A=[],B=[],C=[],D=0,E=0,F=0,G=null;b.module=function(a,c){b.startComputation();var d=A.indexOf(a);0>d&&(d=A.length),A[d]=a,B[d]=c,C[d]=new c.controller,b.endComputation()},b.redraw=function(){if(D=a.performance&&a.performance.now?a.performance.now():(new a.Date).getTime(),D-E>16)i();else{var b=a.cancelAnimationFrame||a.clearTimeout,c=a.requestAnimationFrame||a.setTimeout;b(F),F=c(i,0)}};var H=0;b.startComputation=function(){H++},b.endComputation=function(){H=Math.max(H-1,0),0==H&&b.redraw()},b.withAttr=function(a,b){return function(c){b(a in c.currentTarget?c.currentTarget[a]:c.currentTarget.getAttribute(a))}};var I,J={pathname:"",hash:"#",search:"?"},K=function(){},L={};return b.route=function(){if(0===arguments.length)return I;if(3===arguments.length){I=a.location[b.route.mode].slice(J[b.route.mode].length);var c=arguments[0],d=arguments[1],e=arguments[2];K=function(a){var f=a.slice(J[b.route.mode].length);j(c,e,f)||b.route(d,!0)};var f="hash"==b.route.mode?"onhashchange":"onpopstate";a[f]=function(){K(a.location[b.route.mode])},G=l,a[f]()}else if(arguments[0].addEventListener){var g=arguments[0],h=arguments[1];g.href=J[b.route.mode]+g.pathname,h||(g.removeEventListener("click",k),g.addEventListener("click",k))}else if("string"==typeof arguments[0]){I=arguments[0];var i=arguments[1]===!0;a.history.pushState?(G=function(){a.history[i?"replaceState":"pushState"](null,a.document.title,J[b.route.mode]+I),l()},K(J[b.route.mode]+I)):a.location[b.route.mode]=I}},b.route.param=function(a){return L[a]},b.route.mode="search",b.prop=function(a){var b=function(){return arguments.length&&(a=arguments[0]),a};return b.toJSON=function(){return a},b},b.deferred=function(){var a=[],c=[],d={resolve:function(b){for(var c=0;cv8_W!p_I*Yg<_m4;hODa2-hTx)z%r>Oao2h zv(gFAD#DCMR0iIpY9MS%!W=QIBr}4B2rGz6G7Excg{6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index 390859dd..18a2bc1b 100644 --- a/mithril.js +++ b/mithril.js @@ -33,10 +33,7 @@ Mithril = m = new function app(window) { return cell } function build(parentElement, parentTag, data, cached, shouldReattach, index, editable, namespace) { - if (data === null || data === undefined) { - if (cached) clear(cached.nodes) - return - } + if (data === null || data === undefined) data = "" if (data.subtree === "retain") return var cachedType = type.call(cached), dataType = type.call(data) @@ -56,7 +53,9 @@ Mithril = m = new function app(window) { cached[cacheCount++] = item } if (!intact) { + //console.log(123, nodes, cached.nodes, 666,cached[0], cached[1], data.length) for (var i = 0; i < data.length; i++) if (cached[i] !== undefined) nodes = nodes.concat(cached[i].nodes) + //console.log(234, nodes, cached.nodes, cached[i]) for (var i = nodes.length, node; node = cached.nodes[i]; i++) if (node.parentNode !== null) node.parentNode.removeChild(node) for (var i = cached.nodes.length, node; node = nodes[i]; i++) if (node.parentNode === null) parentElement.appendChild(node) if (data.length < cached.length) cached.length = data.length diff --git a/tests/mithril-tests.js b/tests/mithril-tests.js index 345226d5..b0067fa3 100644 --- a/tests/mithril-tests.js +++ b/tests/mithril-tests.js @@ -105,7 +105,7 @@ function testMithril(mock) { test(function() { var root = mock.document.createElement("div") m.render(root, m("div", [undefined])) - return root.childNodes[0].childNodes.length === 0 + return root.childNodes[0].childNodes[0].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") @@ -128,19 +128,19 @@ function testMithril(mock) { var root = mock.document.createElement("div") m.render(root, m("ul", [m("li")])) m.render(root, m("ul", [m("li"), undefined])) - return root.childNodes[0].childNodes.length === 1 + return root.childNodes[0].childNodes[1].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") m.render(root, m("ul", [m("li"), m("li")])) m.render(root, m("ul", [m("li"), undefined])) - return root.childNodes[0].childNodes.length === 1 + return root.childNodes[0].childNodes[1].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") m.render(root, m("ul", [m("li")])) m.render(root, m("ul", [undefined])) - return root.childNodes[0].childNodes.length === 0 + return root.childNodes[0].childNodes[0].nodeValue === "" }) test(function() { var root = mock.document.createElement("div") @@ -326,6 +326,24 @@ function testMithril(mock) { m.render(root, m("div", "foo")) return root.childNodes.length == 1 }) + test(function() { + var root = mock.document.createElement("div") + m.render(root, m("div", [m("button"), m("ul")])) + var valueBefore = root.childNodes[0].childNodes[0].nodeName + m.render(root, m("div", [undefined, m("ul")])) + var valueAfter = root.childNodes[0].childNodes[0].nodeValue + return valueBefore === "BUTTON" && valueAfter === "" + }) + test(function() { + var root = mock.document.createElement("div") + m.render(root, m("div", [m("ul"), undefined])) + var valueBefore1 = root.childNodes[0].childNodes[0].nodeName + var valueBefore2 = root.childNodes[0].childNodes[1].nodeValue + m.render(root, m("div", [undefined, m("ul")])) + var valueAfter1 = root.childNodes[0].childNodes[0].nodeValue + var valueAfter2 = root.childNodes[0].childNodes[1].nodeName + return valueBefore1 === "UL" && valueAfter1 === "" && valueBefore2 === "" && valueAfter2 === "UL" + }) //end m.render //m.redraw @@ -339,11 +357,11 @@ function testMithril(mock) { }) controller.value = "foo" m.redraw() - var lengthBefore = root.childNodes.length + var valueBefore = root.childNodes[0].nodeValue mock.performance.$elapse(50) m.redraw() mock.performance.$elapse(50) //teardown - return lengthBefore === 0 && root.childNodes[0].nodeValue === "foo" + return valueBefore === "" && root.childNodes[0].nodeValue === "foo" }) test(function() { mock.performance.$elapse(50) //setup