diff --git a/.gitignore b/.gitignore index e5eb442c..8af32a76 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ coverage node_modules jsconfig.json -.vscode npm-debug.log +.vscode +.DS_Store diff --git a/docs/lifecycle-methods.md b/docs/lifecycle-methods.md index 278cb3c8..ec2b6ceb 100644 --- a/docs/lifecycle-methods.md +++ b/docs/lifecycle-methods.md @@ -83,7 +83,7 @@ You should not modify model data synchronously from this method. Since `oninit` The `oncreate(vnode)` hook is called after a DOM element is created and attached to the document. `oncreate` is guaranteed to run at the end of the render cycle, so it is safe to read layout values such as `vnode.dom.offsetHeight` and `vnode.dom.getBoundingClientRect()` from this method. -This hook does not get called when an element is updated, but it does get called if an element is recycled. +This hook does not get called when an element is updated. Like in other hooks, the `this` keyword in the `oncreate` callback points to `vnode.state`. DOM elements whose vnodes have an `oncreate` hook do not get recycled. diff --git a/mithril.js b/mithril.js index 392fb36b..67dd7173 100644 --- a/mithril.js +++ b/mithril.js @@ -499,11 +499,14 @@ var _14 = function($window) { if (attrs2 != null) { setAttrs(vnode, attrs2, ns) } - if (vnode.text != null) { + if (vnode.attrs != null && vnode.attrs.contenteditable != null) { + setContentEditable(vnode) + } + else if (vnode.text != null) { if (vnode.text !== "") element.textContent = vnode.text else vnode.children = [Vnode("#", undefined, undefined, vnode.text, undefined, undefined)] } - if (vnode.children != null) { + else if (vnode.children != null) { var children = vnode.children createNodes(element, children, 0, children.length, hooks, null, ns) setLateAttrs(vnode) @@ -671,7 +674,10 @@ var _14 = function($window) { } } updateAttrs(vnode, old.attrs, vnode.attrs, ns) - if (old.text != null && vnode.text != null && vnode.text !== "") { + if (vnode.attrs != null && vnode.attrs.contenteditable != null) { + setContentEditable(vnode) + } + else if (old.text != null && vnode.text != null && vnode.text !== "") { if (old.text.toString() !== vnode.text.toString()) old.dom.firstChild.nodeValue = vnode.text } else { @@ -744,6 +750,14 @@ var _14 = function($window) { if (nextSibling && nextSibling.parentNode) parent0.insertBefore(dom, nextSibling) else parent0.appendChild(dom) } + function setContentEditable(vnode) { + var children = vnode.children + if (children != null && children.length === 1 && children[0].tag === "<") { + var content = children[0].children + if (vnode.dom.innerHTML !== content) vnode.dom.innerHTML = content + } + else if (children != null || vnode.text != null) throw new Error("Child node of a contenteditable must be trusted") + } //remove function removeNodes(parent0, vnodes, start, end, context) { for (var i = start; i < end; i++) { diff --git a/mithril.min.js b/mithril.min.js index c8aa22b6..67cb1887 100644 --- a/mithril.min.js +++ b/mithril.min.js @@ -1,43 +1,43 @@ -new function(){function y(b,f,p,g,k,h){return{tag:b,key:f,attrs:p,children:g,text:k,dom:h,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function A(b){if(null==b||"string"!==typeof b&&null==b.view)throw Error("The selector must be either a string or a component.");if("string"===typeof b&&void 0===K[b]){for(var f,p,g=[],k={};f=Q.exec(b);){var h=f[1],w=f[2];""===h&&""!==w?p=w:"#"===h?k.id=w:"."===h?g.push(w):"["===f[3][0]&&((h=f[6])&&(h=h.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")), -k[f[4]]=h||!0)}0d.filter(G).length)throw Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream"); -return v(f(),d,function(){var e=d.filter(B);if(0e.indexOf("?")?"?":"&";e+=g+f}return e}function k(e){try{return""!==e?JSON.parse(e):null}catch(l){throw Error(e);}}function h(e){return e.responseText}function w(e,b){if("function"===typeof e)if(b instanceof Array)for(var f=0;fu.status||304===u.status)l(w(e.type,b));else{var f=Error(u.responseText),g;for(g in b)f[g]=b[g];l.error(f)}}catch(D){l.error(D)}"function"===typeof m&&m()}};r?u.send(e.data):u.send();return l},jsonp:function(e){var h=f();void 0!==e.initialValue&&h(e.initialValue);var r=e.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+x++,k=b.document.createElement("script");b[r]=function(f){k.parentNode.removeChild(k);h(w(e.type,f));"function"===typeof m&&m();delete b[r]};k.onerror=function(){k.parentNode.removeChild(k); -h.error(Error("JSONP request failed"));"function"===typeof m&&m();delete b[r]};null==e.data&&(e.data={});e.url=p(e.url,e.data);e.data[e.callbackKey||"callback"]=r;k.src=g(e.url,e.data);b.document.documentElement.appendChild(k);return h},setCompletionCallback:function(b){m=b}}}(window,O),M=function(){var b=[];return{subscribe:b.push.bind(b),unsubscribe:function(f){f=b.indexOf(f);-1=n&&k>=B;){var t=a[n],v=d[B];if(t!==v||q)if(null!=t&&null!=v&&t.key===v.key)n++,B++,h(c,t,v,b,x(a,n,g),q,l),q&&t.tag===v.tag&&m(c,w(t),g);else if(t=a[r],t!==v||q)if(null!=t&&null!=v&&t.key===v.key)h(c,t,v,b,x(a,r+1,g),q,l),(q||B=n&&k>=B;){t=a[r];v=d[k];if(t!==v||q)if(null!=t&&null!=v&&t.key===v.key)h(c,t,v,b,x(a,r+1,g),q,l),q&&t.tag===v.tag&&m(c,w(t), -g),null!=t.dom&&(g=t.dom),r--;else{if(!u){u=a;var t=r,C={},E;for(E=0;Ee.filter(I).length)throw Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream"); +return t(f(),e,function(){var m=e.filter(d);if(0e.indexOf("?")?"?":"&";e+=g+f}return e}function k(e){try{return""!==e?JSON.parse(e):null}catch(l){throw Error(e);}}function h(e){return e.responseText}function y(e,c){if("function"===typeof e)if(c instanceof Array)for(var f=0;fx.status||304===x.status)l(y(e.type,c));else{var f=Error(x.responseText),g;for(g in c)f[g]=c[g];l.error(f)}}catch(T){l.error(T)}"function"===typeof n&&n()}};r?x.send(e.data):x.send();return l},jsonp:function(e){var l=f();void 0!==e.initialValue&&l(e.initialValue);var h=e.callbackName||"_mithril_"+Math.round(1E16*Math.random())+"_"+z++,k=c.document.createElement("script");c[h]=function(f){k.parentNode.removeChild(k);l(y(e.type,f));"function"===typeof n&&n();delete c[h]};k.onerror=function(){k.parentNode.removeChild(k); +l.error(Error("JSONP request failed"));"function"===typeof n&&n();delete c[h]};null==e.data&&(e.data={});e.url=p(e.url,e.data);e.data[e.callbackKey||"callback"]=h;k.src=g(e.url,e.data);c.document.documentElement.appendChild(k);return l},setCompletionCallback:function(c){n=c}}}(window,P),M=function(){var c=[];return{subscribe:c.push.bind(c),unsubscribe:function(f){f=c.indexOf(f);-1=k&&w>=A;){var t=a[k],u=d[A];if(t!==u||m)if(null!=t&&null!=u&&t.key===u.key)k++,A++,h(b,t,u,c,z(a,k,e),m,g),m&&t.tag===u.tag&&n(b,y(t),e);else if(t=a[r],t!==u||m)if(null!=t&&null!=u&&t.key===u.key)h(b,t,u,c,z(a,r+1,e),m,g),(m||A=k&&w>=A;){t=a[r];u=d[w];if(t!==u||m)if(null!=t&&null!=u&&t.key=== +u.key)h(b,t,u,c,z(a,r+1,e),m,g),m&&t.tag===u.tag&&n(b,y(t),e),null!=t.dom&&(e=t.dom),r--;else{if(!x){x=a;var t=r,C={},q;for(q=0;q