From b55f09fcf9f1af090896e5011b997543379cccdf Mon Sep 17 00:00:00 2001 From: Kabir Shah Date: Tue, 22 May 2018 22:07:54 -0700 Subject: [PATCH] clean up conditional implementation --- dist/moon.js | 25 +++++++++++++------------ dist/moon.min.js | 2 +- src/compiler/generator/create.js | 25 +++++++++++++------------ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/dist/moon.js b/dist/moon.js index bc5e40d6..95ce09e0 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -355,45 +355,46 @@ var insertBefore = function (element, reference, parent) { return ("m.ib(" + (getElement(element)) + "," + (getElement(reference)) + "," + (getElement(parent)) + ");"); }; var generateCreate = function (element, index, parent, root, insert) { - var createCode; + var createCode, mountCode = "", mountElement = element.index; switch (element.type) { case "#if": var siblings = parent.children; + var ifReference = root.nextIndex++; var ifCreates = ""; var ifBranches = ""; for (var i = index; i < siblings.length;) { var sibling = siblings[i]; - var siblingType = sibling.type; - var ifCreate = root.nextIndex++; var keyword = (void 0); - if (siblingType === "#if") { + if (sibling.type === "#if") { keyword = "if(" + (attributeValue(sibling.attributes[0])) + ")"; - } else if (siblingType === "#elseif") { + } else if (sibling.type === "#elseif") { keyword = "else if(" + (attributeValue(sibling.attributes[0])) + ")"; - } else if (siblingType === "#else") { + } else if (sibling.type === "#else") { keyword = "else"; } else { break; } - ifCreates += setElement(ifCreate, ("function(){" + (mapReduce(sibling.children, function (child, index) { return generateCreate(child, index, parent, root, element.index); })) + "};")); - ifBranches += keyword + "{" + (getElement(ifCreate)) + "();}"; + ifCreates += setElement(sibling.index, ("function(){" + (mapReduce(sibling.children, function (child, index) { return generateCreate(child, index, parent, root, ifReference); })) + "};")); + ifBranches += keyword + "{" + (getElement(sibling.index)) + "();}"; siblings.splice(i, 1); } - createCode = ifCreates + ifBranches + setElement(element.index, createComment()); + createCode = setElement(ifReference, createComment()); + mountCode = ifCreates + ifBranches; + mountElement = ifReference; break; case "#text": - createCode = setElement(element.index, createTextNode(attributeValue(element.attributes[0]))); + createCode = setElement(mountElement, createTextNode(attributeValue(element.attributes[0]))); break; default: - createCode = setElement(element.index, createElement(element.type)) + mapReduce(element.attributes, function (attribute) { return attribute.key[0] === "@" ? addEventListener(element.index, attribute) : setAttribute(element.index, attribute); }) + mapReduce(element.children, function (child, index) { return generateCreate(child, index, element, root); }); + createCode = setElement(mountElement, createElement(element.type)) + mapReduce(element.attributes, function (attribute) { return attribute.key[0] === "@" ? addEventListener(mountElement, attribute) : setAttribute(mountElement, attribute); }) + mapReduce(element.children, function (child, index) { return generateCreate(child, index, element, root); }); } - return createCode + (insert === undefined ? appendChild(element.index, parent.index) : insertBefore(element.index, insert, parent.index)); + return createCode + (insert === undefined ? appendChild(mountElement, parent.index) : insertBefore(mountElement, insert, parent.index)) + mountCode; }; var generateUpdate = function (element, index, parent, root) { diff --git a/dist/moon.min.js b/dist/moon.min.js index bb8212cf..9bd4e8f5 100644 --- a/dist/moon.min.js +++ b/dist/moon.min.js @@ -4,4 +4,4 @@ * Released under the MIT License * https://kbrsh.github.io/moon */ -!function(e,t){"undefined"==typeof module?e.Moon=t():module.exports=t()}(this,function(){"use strict";var u=/"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g,a=["NaN","event","false","in","m","null","this","true","typeof","undefined"],s=function(e,t){for(var n,i=!1;null!==(n=u.exec(e));){var r=n[1];void 0!==r&&-1===a.indexOf(r)&&"$"!==r[0]&&(i=!0,-1===t.indexOf(r)&&t.push(r))}return i},e={silent:!0},d=/\s+/,v=function(e,t,n,i,r){for(;e"===u)break;if(d.test(u))e+=1;else{for(var a="",o="",c=!1;e"===u||d.test(u)){o=a;break}if("="===u){e+=1;break}a+=u,e+=1}if(0===o.length){var f=void 0;for('"'===(u=t[e])||"'"===u?(f=u,e+=1):"{"===u?(f="}",c=!0,e+=1):f=d;e"!==u;){if("object"==typeof f&&f.test(u)||u===f){e+=1;break}o+=u,e+=1}}r.push({key:a,value:o,expression:c,dynamic:c&&s(o,i)})}}return e},o=function(e,t,n,i,r){for(var u={index:i[0].nextIndex++,type:"",attributes:[],children:[]};e"===a){var o=u.attributes;i.push(u);for(var c=0;c"===t[e+1]){i[i.length-1].children.push(u),e+=2;break}d.test(a)&&(e+=1)||"="===a?e=v(e,t,n,r,u.attributes):(u.type+=a,e+=1)}return e},c=function(e,t,n,i){for(;e"===r){e+=1;break}r}var u=i.pop();return u.type,e},f=function(e,t,n){for(;e"===u){e+=3;break}e+=1}}return e},h=/(?:(?:&(?:amp|gt|lt|nbsp|quot);)|"|\\|\n)/g,l={"&":"&",">":">","<":"<"," ":" ",""":'\\"',"\\":"\\\\",'"':'\\"',"\n":"\\n"},p=function(e,t,n,i){for(var r="";e"===u)break;if(d.test(u))e+=1;else{for(var a="",o="",c=!1;e"===u||d.test(u)){o=a;break}if("="===u){e+=1;break}a+=u,e+=1}if(0===o.length){var f=void 0;for('"'===(u=t[e])||"'"===u?(f=u,e+=1):"{"===u?(f="}",c=!0,e+=1):f=d;e"!==u;){if("object"==typeof f&&f.test(u)||u===f){e+=1;break}o+=u,e+=1}}r.push({key:a,value:o,expression:c,dynamic:c&&s(o,i)})}}return e},o=function(e,t,n,i,r){for(var u={index:i[0].nextIndex++,type:"",attributes:[],children:[]};e"===a){var o=u.attributes;i.push(u);for(var c=0;c"===t[e+1]){i[i.length-1].children.push(u),e+=2;break}d.test(a)&&(e+=1)||"="===a?e=v(e,t,n,r,u.attributes):(u.type+=a,e+=1)}return e},c=function(e,t,n,i){for(;e"===r){e+=1;break}r}var u=i.pop();return u.type,e},f=function(e,t,n){for(;e"===u){e+=3;break}e+=1}}return e},h=/(?:(?:&(?:amp|gt|lt|nbsp|quot);)|"|\\|\n)/g,l={"&":"&",">":">","<":"<"," ":" ",""":'\\"',"\\":"\\\\",'"':'\\"',"\n":"\\n"},p=function(e,t,n,i){for(var r="";e { - let createCode; + let createCode, mountCode = "", mountElement = element.index; switch (element.type) { case "#if": const siblings = parent.children; + const ifReference = root.nextIndex++; let ifCreates = ""; let ifBranches = ""; for (let i = index; i < siblings.length;) { const sibling = siblings[i]; - const siblingType = sibling.type; - const ifCreate = root.nextIndex++; let keyword; - if (siblingType === "#if") { + if (sibling.type === "#if") { keyword = `if(${attributeValue(sibling.attributes[0])})`; - } else if (siblingType === "#elseif") { + } else if (sibling.type === "#elseif") { keyword = `else if(${attributeValue(sibling.attributes[0])})`; - } else if (siblingType === "#else") { + } else if (sibling.type === "#else") { keyword = "else"; } else { break; } - ifCreates += setElement(ifCreate, `function(){${mapReduce(sibling.children, (child, index) => generateCreate(child, index, parent, root, element.index))}};`); - ifBranches += `${keyword}{${getElement(ifCreate)}();}`; + ifCreates += setElement(sibling.index, `function(){${mapReduce(sibling.children, (child, index) => generateCreate(child, index, parent, root, ifReference))}};`); + ifBranches += `${keyword}{${getElement(sibling.index)}();}`; siblings.splice(i, 1); } - createCode = ifCreates + ifBranches + setElement(element.index, createComment()); + createCode = setElement(ifReference, createComment()); + mountCode = ifCreates + ifBranches; + mountElement = ifReference; break; case "#text": - createCode = setElement(element.index, createTextNode(attributeValue(element.attributes[0]))); + createCode = setElement(mountElement, createTextNode(attributeValue(element.attributes[0]))); break; default: - createCode = setElement(element.index, createElement(element.type)) + mapReduce(element.attributes, (attribute) => attribute.key[0] === "@" ? addEventListener(element.index, attribute) : setAttribute(element.index, attribute)) + mapReduce(element.children, (child, index) => generateCreate(child, index, element, root)); + createCode = setElement(mountElement, createElement(element.type)) + mapReduce(element.attributes, (attribute) => attribute.key[0] === "@" ? addEventListener(mountElement, attribute) : setAttribute(mountElement, attribute)) + mapReduce(element.children, (child, index) => generateCreate(child, index, element, root)); } - return createCode + (insert === undefined ? appendChild(element.index, parent.index) : insertBefore(element.index, insert, parent.index)); + return createCode + (insert === undefined ? appendChild(mountElement, parent.index) : insertBefore(mountElement, insert, parent.index)) + mountCode; };