diff --git a/dist/moon.js b/dist/moon.js index a77cb91d..cf7c5fef 100644 --- a/dist/moon.js +++ b/dist/moon.js @@ -1412,7 +1412,8 @@ attrs: props }; - var directives = {}; + var hasDirectives = false; + var directiveProps = []; var hasSpecialDirectivesAfter = false; var specialDirectivesAfter = {}; @@ -1446,15 +1447,22 @@ hasSpecialDirectivesAfter = true; } - } else if (_name[0] === "m" && _name[1] === "-") {} else { + + if ((duringPropGenerate = specialDirective.duringPropGenerate) !== undefined) { + duringPropGenerate(_prop, node, state); + } + + node.meta.shouldRender = true; + } else if (_name[0] === "m" && _name[1] === "-") { + directiveProps.push(_prop); + hasDirectives = true; + node.meta.shouldRender = true; + } else { var _value2 = _prop.value; var compiled = compileTemplate(_value2, state.dependencies, true); if (_value2 !== compiled) { node.meta.shouldRender = true; - if (parent !== undefined) { - parent.meta.shouldRender = true; - } } if (state.hasAttrs === false) { @@ -1466,16 +1474,34 @@ } if (state.hasAttrs === true) { - propsCode = propsCode.substring(0, propsCode.length - 2) + "}}, "; + propsCode = propsCode.substring(0, propsCode.length - 2) + "}"; state.hasAttrs = false; } else { - propsCode += "}}, "; + propsCode += "}"; + } + + if (hasDirectives === true) { + propsCode += ", directives: {"; + + var directiveProp = null; + var directivePropValue = null; + for (var i = 0; i < directiveProps.length; i++) { + directiveProp = directiveProps[i]; + directivePropValue = directiveProp.value; + + compileTemplateExpression(directivePropValue, state.dependencies); + propsCode += '"' + directiveProp.name + '": ' + directivePropValue; + } + + propsCode += "}"; } if (hasSpecialDirectivesAfter === true) { state.specialDirectivesAfter = specialDirectivesAfter; } + propsCode += "}, "; + return propsCode; }; @@ -1487,13 +1513,13 @@ eventListenersCode += '"' + type + '": ['; for (var i = 0; i < handlers.length; i++) { - eventListenersCode += handlers[i]; + eventListenersCode += handlers[i] + ', '; } eventListenersCode = eventListenersCode.substring(0, eventListenersCode.length - 2) + "], "; } - eventListenersCode = eventListenersCode.substring(0, eventListenersCode.length - 2) + "}"; + eventListenersCode = eventListenersCode.substring(0, eventListenersCode.length - 2) + "}, "; return eventListenersCode; }; @@ -1531,7 +1557,6 @@ var propsCode = generateProps(node, parent, state); var specialDirectivesAfter = state.specialDirectivesAfter; - state.specialDirectivesAfter = null; var children = node.children; var childrenLength = children.length; @@ -1546,6 +1571,10 @@ childrenCode = childrenCode.substring(0, childrenCode.length - 2) + "]"; } + if (node.deep === true) { + childrenCode = '[].concat.apply([], ' + childrenCode + ')'; + } + if (node.meta.shouldRender === true && parent !== undefined) { parent.meta.shouldRender = true; } @@ -1595,6 +1624,7 @@ return noop; } }; + var compile = function (template) { var tokens = lex(template); var ast = parse(tokens); @@ -2277,10 +2307,15 @@ }; specialDirectives["m-literal"] = { - duringPropGenerate: function (value, meta, vnode, dependencies) { - var prop = meta.arg; - compileTemplateExpression(value, dependencies); - if (prop === "class") { + duringPropGenerate: function (prop, vnode, state) { + var propName = prop.meta.arg; + compileTemplateExpression(prop.value, state.dependencies); + + if (state.hasAttrs === false) { + state.hasAttrs = true; + } + + if (propName === "class") { // Detected class, use runtime class render helper return '"class": Moon.renderClass(' + value + '), '; } else { diff --git a/dist/moon.min.js b/dist/moon.min.js index e0bc9c3c..6067ac97 100644 --- a/dist/moon.min.js +++ b/dist/moon.min.js @@ -4,4 +4,4 @@ * Released under the MIT License * http://moonjs.ga */ -!function(e,t){"object"==typeof module&&module.exports?module.exports=t():e.Moon=t()}(this,function(){"use strict";function e(e){this.instance=e,this.cache={},this.setters={},this.clear={},this.target=null,this.map={}}function t(t){void 0===t&&(t={}),this.$options=t,k(this,"$name",t.name,"root");var n=t.data;this.$data=void 0===n?{}:"function"==typeof n?n():n,k(this,"$render",t.render,A),k(this,"$hooks",t.hooks,{});var r=t.methods;void 0!==r&&s(this,r),this.$events={},this.$dom={},this.$observer=new e(this),this.$destroyed=!0,this.$queued=!1;var i=t.computed;void 0!==i&&l(this,i),this.init()}var n={},r={},i={},o={stop:"event.stopPropagation();",prevent:"event.preventDefault();",ctrl:"if(event.ctrlKey === false) {return null;};",shift:"if(event.shiftKey === false) {return null;};",alt:"if(event.altKey === false) {return null;};",enter:"if(event.keyCode !== 13) {return null;};"},a={},s=function(e,t){for(var n in t)!function(t,n){e.$data[t]=function(){return n.apply(e,arguments)}}(n,t[n])},l=function(e,t){for(var n in t)!function(n){var r=e.$observer;r.observe(n),Object.defineProperty(e.$data,n,{get:function(){var i=null;return void 0===r.cache[n]?(r.target=n,i=t[n].get.call(e),r.target=null,r.cache[n]=i):i=r.cache[n],i},set:A});var i=null;void 0!==(i=t[n].set)&&(r.setters[n]=i)}(n)};e.prototype.observe=function(e){var t=this;this.clear[e]=function(){t.cache[e]=void 0}},e.prototype.notify=function(e,t){var n=null;if(void 0!==(n=this.map[e]))for(var r=0;rc?u:c,d=0,p=0;d"===o||" "===o)break;i+=o,r++}var a={type:"tag",value:i};return e.tokens.push(a),e.current=r,a},oe=function(e,t){for(var n=t.input,r=n.length,i=t.current,o=n.charAt(i),a=n.charAt(i+1),s=function(){i++,o=n.charAt(i),a=n.charAt(i+1)},l={};i"!==o&&("/"!==o||">"!==a);)if(" "!==o){for(var u="",c=!1;i"===o||"/"===o&&">"===a){c=!0;break}u+=o,s()}var v={name:u,value:"",meta:{}};if(!0!==c){s();var f=" ";for("'"!==o&&'"'!==o||(f=o,s());i1&&(s=v.shift(),c=v.join("(").slice(0,-1),J(c,r.dependencies));for(var f="",d=0;dc?u:c,d=0,p=0;d"===o||" "===o)break;i+=o,r++}var a={type:"tag",value:i};return e.tokens.push(a),e.current=r,a},oe=function(e,t){for(var n=t.input,r=n.length,i=t.current,o=n.charAt(i),a=n.charAt(i+1),l=function(){i++,o=n.charAt(i),a=n.charAt(i+1)},s={};i"!==o&&("/"!==o||">"!==a);)if(" "!==o){for(var u="",c=!1;i"===o||"/"===o&&">"===a){c=!0;break}u+=o,l()}var v={name:u,value:"",meta:{}};if(!0!==c){l();var f=" ";for("'"!==o&&'"'!==o||(f=o,l());i1&&(l=v.shift(),c=v.join("(").slice(0,-1),J(c,r.dependencies));for(var f="",d=0;d'); + var compilerCommentApp = new Moon({ + el: "#compilerComment" + }); + expect(el.innerHTML).to.equal(""); + }); + it("should compile self closing elements", function() { + var el = createTestElement("compilerSelfClosing", ''); + var app = new Moon({ + el: "#compilerSelfClosing", + template: "
" + }); + expect(app.$dom.children[0].type).to.equal("self-closing"); + }); + it("should compile self closing elements without a slash and consume children", function() { + var el = createTestElement("compilerSelfClosingNoSlash", ''); + var app = new Moon({ + el: "#compilerSelfClosingNoSlash", + template: "
hi
" + }); + expect(app.$dom.children[0].children[0].val).to.equal("hi"); + }); + it("should ignore just closing elements", function() { + var el = createTestElement("compilerJustClosing", ''); + var app = new Moon({ + el: "#compilerJustClosing", + template: "
" + }); + expect(app.$dom.children[0]).to.equal(undefined); + }); + it("should ignore just closing custom elements", function() { + var el = createTestElement("compilerJustClosingCustom", ''); + var app = new Moon({ + el: "#compilerJustClosingCustom", + template: "
" + }); + expect(app.$dom.children[0]).to.equal(undefined); + }); + it("should compile only text", function() { + var el = createTestElement("compilerOnlyText", ''); + var compilerCommentApp = new Moon({ + el: "#compilerOnlyText", + template: "
text
" + }); + expect(el.innerHTML).to.equal("text"); + }); + it("should double quotes in text", function() { + var el = createTestElement("compilerDoubleQuote", '"Hello Moon!"'); + var compilerCommentApp = new Moon({ + el: "#compilerDoubleQuote" + }); + expect(el.innerHTML).to.equal('"Hello Moon!"'); + }); + it("should compile an unclosed comment", function() { + var el = createTestElement("compilerUnclosedComment", ''); + var compilerCommentApp = new Moon({ + el: "#compilerUnclosedComment", + template: "
'); -// var compilerCommentApp = new Moon({ -// el: "#compilerComment" -// }); -// expect(el.innerHTML).to.equal(""); -// }); -// it("should compile self closing elements", function() { -// var el = createTestElement("compilerSelfClosing", ''); -// var app = new Moon({ -// el: "#compilerSelfClosing", -// template: "
" -// }); -// expect(app.$dom.children[0].type).to.equal("self-closing"); -// }); -// it("should compile self closing elements without a slash and consume children", function() { -// var el = createTestElement("compilerSelfClosingNoSlash", ''); -// var app = new Moon({ -// el: "#compilerSelfClosingNoSlash", -// template: "
hi
" -// }); -// expect(app.$dom.children[0].children[0].val).to.equal("hi"); -// }); -// it("should ignore just closing elements", function() { -// var el = createTestElement("compilerJustClosing", ''); -// var app = new Moon({ -// el: "#compilerJustClosing", -// template: "
" -// }); -// expect(app.$dom.children[0]).to.equal(undefined); -// }); -// it("should ignore just closing custom elements", function() { -// var el = createTestElement("compilerJustClosingCustom", ''); -// var app = new Moon({ -// el: "#compilerJustClosingCustom", -// template: "
" -// }); -// expect(app.$dom.children[0]).to.equal(undefined); -// }); -// it("should compile only text", function() { -// var el = createTestElement("compilerOnlyText", ''); -// var compilerCommentApp = new Moon({ -// el: "#compilerOnlyText", -// template: "
text
" -// }); -// expect(el.innerHTML).to.equal("text"); -// }); -// it("should double quotes in text", function() { -// var el = createTestElement("compilerDoubleQuote", '"Hello Moon!"'); -// var compilerCommentApp = new Moon({ -// el: "#compilerDoubleQuote" -// }); -// expect(el.innerHTML).to.equal('"Hello Moon!"'); -// }); -// it("should compile an unclosed comment", function() { -// var el = createTestElement("compilerUnclosedComment", ''); -// var compilerCommentApp = new Moon({ -// el: "#compilerUnclosedComment", -// template: "