/** * IST: Indented Selector Templating * version 0.6.6 * * Copyright (c) 2012-2014 Nicolas Joyard * Released under the MIT license. * * Author: Nicolas Joyard * http://njoyard.github.com/ist */ !function(e){var t,n,r="function"==typeof e.define&&e.define.amd,i="undefined"!=typeof process&&process.versions&&!!process.versions.node,o="undefined"!=typeof window&&window.navigator&&window.document;n={require:e.require},n.misc=function(){return{jsEscape:function(e){return e.replace(/(['\\])/g,"\\$1").replace(/[\f]/g,"\\f").replace(/[\b]/g,"\\b").replace(/[\t]/g,"\\t").replace(/[\n]/g,"\\n").replace(/[\r]/g,"\\r")},findScript:function(e){var t,n,r,i,o;try{o=document.querySelectorAll("script#"+e)}catch(c){return}if(o)for(t=0,n=o.length;n>t;t++)if(r=o[t],"text/x-ist"===r.getAttribute("type"))return r.innerHTML;return i},appendNodeSegment:function(e,t,n){for(var r,i=e,o=t?t.nextSibling:null;i&&i!=o;)r=i.nextSibling,n.appendChild(i),i=r},insertNodeSegmentBefore:function(e,t,n,r){for(var i,o=e,c=t?t.nextSibling:null;o&&o!=c;)i=o.nextSibling,n.insertBefore(o,r),o=i}}}(),n.codegen=function(e){var t=/{{\s*((?:}(?!})|[^}])*?)\s*}}/,n={},r=function(){};return{expression:function(e){var t="{{ "+e+" }}";return t in n||(n[t]="function(document,_istScope){if(this!==null&&this!==undefined){with(this){with(_istScope){return "+e+";}}}else{with(_istScope){return "+e+";}}}"),n[t]},interpolation:function(r){return r in n||(n[r]=this.expression(r.split(t).map(function(t,n){return n%2?"("+t+")":"'"+e.jsEscape(t)+"'"}).filter(function(e){return"''"!==e}).join("+"))),n[r]},directiveEvaluator:function(e){return"expr"in e?new Function("document,_istScope","return ("+this.expression(e.expr)+").call(this,document,_istScope);"):r},elementUpdater:function(e){var t=[],n=this,r=e.attributes,i=e.properties,o=e.events;return Object.keys(r).forEach(function(e){t.push('element.setAttribute("'+e+'",('+n.interpolation(r[e])+").call(this,document,_istScope));")}),i.forEach(function(e){for(var r=[],i=0,o=e.path.length;o>i;i++){var c=e.path[i];i===o-1?r.push('current["'+c+'"] = value;'):r.push('if (!("'+c+'" in current)) {current["'+c+'"] = {};}current = current["'+c+'"];')}t.push("(function(value) {var current = element;"+r.join("")+"})(("+n.interpolation(e.value)+").call(this,document,_istScope));")}),Object.keys(o).forEach(function(e){t.push('element.addEventListener("'+e+'",('+n.expression(o[e])+").call(this,document,_istScope),false);")}),new Function("document,_istScope,element",t.join(""))},textUpdater:function(e){return new Function("document,_istScope,textNode",'textNode.textContent=""+('+this.interpolation(e.text)+").call(this,document,_istScope);")}}}(n.misc),n.context=function(){function e(t,n){this.value=t,this.values=[t],this.doc=n||document,this.rootScope=this.scope=e.globalScope}return e.globalScope={},e.prototype={importNode:function(e,t){return e.ownerDocument===this.doc?e.cloneNode(t):this.doc.importNode(e,t)},createDocumentFragment:function(){return this.doc.createDocumentFragment()},createElement:function(e,t){return"undefined"!=typeof t?this.doc.createElementNS(t,e):this.doc.createElement(e)},createTextNode:function(e){return this.doc.createTextNode(e)},createComment:function(e){return this.doc.createComment(e)},pushScope:function(e){var t=Object.create(this.scope);Object.keys(e).forEach(function(n){t[n]=e[n]}),this.scope=t},popScope:function(){var e=this.scope;if(e===this.rootScope)throw new Error("No scope left to pop out");this.scope=Object.getPrototypeOf(e)},pushValue:function(e){this.values.unshift(e),this.value=e,void 0!==e&&null!==e&&"string"!=typeof e&&"number"!=typeof e?this.pushScope(e):this.pushScope({})},popValue:function(){this.popScope(),this.values.shift(),this.value=this.values[0]},createContext:function(t){return new e(t,this.doc)},scopedCall:function(e,t){return e.call(this.value,this.doc,this.scope,t)}},e}(),n.directives=function(){function e(e,t,n,r){var i=r.extractRenderedFragment();t&&(i?i.update(e):i=n.render(e),r.appendRenderedFragment(i))}function t(e,t,n,r,i,o){var c=e.value,a=o.extractRenderedFragments(),s=a.keys,u=a.fragments;t.forEach(function(a,l){e.pushValue(a);var d={first:0===l,index:l,last:l==t.length-1,length:t.length,outer:c};r&&Object.keys(r).forEach(function(e){d[e]=r[e]}),e.pushScope({loop:d});var p,f=s.indexOf(n[l]);-1===f?p=i.render(e):(p=u[f],p.update(e)),e.popScope(),e.popValue(),o.appendRenderedFragment(p,n[l])})}var n,r,i={};return r={"if":function(t,n,r,i){e.call(null,t,n,r,i)},unless:function(t,n,r,i){e.call(null,t,!n,r,i)},"with":function(e,t,n,r){var i=r.extractRenderedFragment();e.pushValue(t),i?i.update(e):i=n.render(e),e.popValue(),r.appendChild(n.render(t))},each:function(e,n,r,i){if(!Array.isArray(n))throw new Error(n+" is not an array");t(e,n,n,null,r,i)},eachkey:function(){function e(e){return{key:e,value:this[e]}}return function(n,r,i,o){var c,a=Object.keys(r);c=a.map(e,r),t(n,c,a,{object:r},i,o)}}(),dom:function(e,t,n,r){for(t.ownerDocument!==e.doc&&(t=e.doc.importNode(t,!0));r.hasChildNodes();)r.removeChild(r.firstChild);r.appendChild(t)},define:function(e,t,n){i[t]=n},use:function(e,t,n,r){var o=i[t];if(!o)throw new Error("Template '"+t+"' has not been @defined");var c=r.extractRenderedFragment();c?c.update(e):c=o.render(e),r.appendRenderedFragment(c)}},n={register:function(e,t){r[e]=t},get:function(e){return r[e]}}}(),n.rendereddirective=function(e){function t(e,t){this._istKeyIndex.push(t),this._istFragIndex.push({firstChild:e.firstChild,lastChild:e.lastChild,update:e.update}),this.appendChild(e)}function n(){var t=this._istContext,n=this._istKeyIndex,r=this._istFragIndex,i={keys:n.slice(),fragments:r.map(function(n){var r=t.createDocumentFragment();return e.appendNodeSegment(n.firstChild,n.lastChild,r),r.update=n.update,r})};return n.splice(0,n.length),r.splice(0,r.length),i}function r(t){var n,r,i=this._istContext,o=this._istKeyIndex,c=this._istFragIndex,a=o.indexOf(t);return-1!==a?(n=c[a],r=i.createDocumentFragment(),e.appendNodeSegment(n.firstChild,n.lastChild,r),r.update=n.update,o.splice(a,1),c.splice(a,1),r):void 0}function i(){this.firstChild=null,this.lastChild=null,this.keyIndex=[],this.fragIndex=[]}return i.prototype.createFragment=function(i){var o=i.createDocumentFragment();return o._istContext=i,o._istKeyIndex=this.keyIndex,o._istFragIndex=this.fragIndex,o.appendRenderedFragment=t,o.extractRenderedFragment=r,o.extractRenderedFragments=n,e.appendNodeSegment(this.firstChild,this.lastChild,o),o},i.prototype.updateFromFragment=function(e){this.firstChild=e.firstChild,this.lastChild=e.lastChild},i}(n.misc),n.renderedtree=function(e,t){function n(e,t){this.element=e,this.childrenIndex=t||[],this.appendDone=!1}return n.prototype.forEach=function(e,t,n){var r=this.childrenIndex;e.forEach(function(e,n){r[n]=t.call(this,e,r[n])},n)},n.prototype.updateParent=function(){var t=this.childrenIndex[0];t&&(this.element=null,this.element=t instanceof n?t.element.parentNode:t instanceof e?t.firstChild.parentNode:t.parentNode)},n.prototype.appendChildren=function(){var r=this.element,i=this.childrenIndex;if(r)if(this.appendDone)for(var o=null,c=i.length-1;c>=0;c--){var a=i[c];a instanceof n?o=a.element:a instanceof e?(t.insertNodeSegmentBefore(a.firstChild,a.lastChild,r,o),o=a.firstChild||o):o=a}else i.forEach(function(i){i instanceof n?r.appendChild(i.element):i instanceof e?t.appendNodeSegment(i.firstChild,i.lastChild,r):r.appendChild(i)}),this.appendDone=!0},n}(n.rendereddirective,n.misc),n.renderer=function(e,t,n,r){function i(e){this.template=e,this.context=void 0}return i.prototype.setContext=function(t,n){n=n||(this.context?this.context.doc:document),this.context=t instanceof e?t:new e(t,n)},i.prototype._completeError=function(e,t){return this.template._completeError(e,t)},i.prototype._renderTextNode=function(e,t){var n=this.context;if(t||(t="pr"in e?n.importNode(e.pr,!1):n.createTextNode("")),!("pr"in e))try{n.scopedCall(e.updater,t)}catch(r){throw this._completeError(r,e)}return t},i.prototype._renderElement=function(e,t){var n=this.context;t||(t=n.importNode(e.pr,!1));try{n.scopedCall(e.updater,t)}catch(r){throw this._completeError(r,e)}return t},i.prototype._renderDirective=function(e,n){var i=this.context,o=e.pr,c=t.get(e.directive);if("function"!=typeof c)throw new Error("No directive helper for @"+e.directive+" has been registered");n||(n=new r);var a=n.createFragment(i);a.firstChild&&a.firstChild._isISTPlaceHolder&&a.removeChild(a.firstChild);try{c.call(null,i,i.scopedCall(o.evaluator),o.template,a)}catch(s){throw this._completeError(s,e)}if(0===a.childNodes.length){var u=i.createComment("");u._isISTPlaceHolder=!0,a.appendChild(u)}return n.updateFromFragment(a),n},i.prototype._renderRec=function(e,t){return"text"in e&&(t=this._renderTextNode(e,t)),"tagName"in e&&(t?t.element=this._renderElement(e,t.element):t=new n(this._renderElement(e)),this._renderNodes(e.children,t)),"directive"in e&&(t=this._renderDirective(e,t)),t},i.prototype._renderNodes=function(e,t){t.forEach(e,this._renderRec,this),t.appendChildren()},i.prototype.render=function(){var e=this,t=this.context.createDocumentFragment(),r=this.template.nodes,i=new n(t);return this._renderNodes(r,i),t.update=function(t){t&&e.setContext(t),i.updateParent(),e._renderNodes(r,i)},t},i}(n.context,n.directives,n.renderedtree,n.rendereddirective),n.template=function(e,t,n){function r(e,t){var n,i,o,c=t.filter(function(t){return t.partial===e});if(c.length)return c[0];for(i=0,o=t.length;o>i;i++)if("undefined"!=typeof t[i].children&&(n=r(e,t[i].children)))return n}function i(e,t){this.name=e||"",this.nodes=t,"undefined"!=typeof document&&this.nodes.forEach(this._preRenderRec,this)}var o=/{{((?:}(?!})|[^}])*)}}/;return i.prototype._preRenderRec=function(t){var n;if(!("pr"in t||"updater"in t)){if("children"in t&&t.children.forEach(this._preRenderRec,this),"text"in t)if(o.test(t.text))try{t.updater=e.textUpdater(t)}catch(r){throw this._completeError(r,t)}else t.pr=document.createTextNode(t.text);if("tagName"in t){t.pr=n=document.createElement(t.tagName),t.classes.forEach(function(e){n.classList.add(e)}),"undefined"!=typeof t.id&&(n.id=t.id);try{t.updater=e.elementUpdater(t)}catch(r){throw this._completeError(r,t)}}if("directive"in t)try{t.pr={template:new i(this.name,t.children),evaluator:e.directiveEvaluator(t)}}catch(r){throw this._completeError(r,t)}}},i.prototype._completeError=function(e,t){var n="in '"+this.name+"' on line "+(t.line||"");return"undefined"==typeof e.istStack&&(e.message+=" "+n,e.istStack=[]),e.istStack.push(n),e},i.prototype.findPartial=function(e){return console&&(console.warn||console.log)("Warning: Template#findPartial is deprecated, use Template#partial instead"),this.partial(e)},i.prototype.partial=function(e){var t;if("undefined"!=typeof e)return t=r(e,this.nodes),"undefined"!=typeof t?new i(this.name,[t]):void 0},i.prototype.render=function(e,t){var r=this,i=new n(r);return i.setContext(e,t),i.render()},i.prototype.getCode=function(e){return"new ist.Template("+JSON.stringify(this.name)+", "+JSON.stringify(this.nodes,null,e?1:0)+")"},i}(n.codegen,n.context,n.renderer),n.parsehelpers=function(){var e,t,n,r="U",i="I",o="D",c={};return e=function(){return{text:this.text,line:this.line}},t=function(){var e={tagName:this.tagName,line:this.line,classes:this.classes,attributes:this.attributes,properties:this.properties,events:this.events,children:this.children};return"undefined"!=typeof this.id&&(e.id=this.id),"undefined"!=typeof this.partial&&(e.partial=this.partial),e},n=function(){return{directive:this.directive,expr:this.expr,line:this.line,children:this.children}},c.generateNodeTree=function(e,t){var n,i,c,a,s={children:[]},u=[s],l=0;if(!e)return s.children;for(i=function(){return u[u.length-1]},c=function(e){l++,u.push(e)},a=function(){var e,t,n;if(u.length<2)throw new Error("Could not pop node from stack");if(e=u.pop(),t=i(),"undefined"!=typeof t.text)throw n=new Error("Cannot add children to text node"),n.line=e.line,n;if("else"===e.directive){var r=t.children[t.children.length-1];if(r&&!r.wasElse&&"if"===r.directive)e.directive="unless";else{if(!r||r.wasElse||"unless"!==r.directive)throw n=new Error("@else directive has no matching @if or @unless directive"),n.line=e.line,n;e.directive="if"}e.expr=r.expr,e.wasElse=!0}return t.children.push(e),e},n=t.map(function(e){return e.pop()}),n.unshift(e),n.forEach(function(e){var t=e.indent,n=e.item;if(t[0]instanceof Error)throw t[0];if(l>0)if(t[0]===r)a();else if(t[0]===o)for(a();t.length>0;)t.pop(),a();c(n)});u.length>1;)a();return s.children},c.parseIndent=function(e,t,n){var c,a=t.length,s=[];if(0===a.length&&e.push(a),a==e[0])return[r];if(a>e[0])return e.unshift(a),[i];for(;a1?String.fromCharCode(parseInt(e,16)):{f:"\f",b:"\b",t:" ",n:"\n",r:"\r"}[e]||e},c}(),n.parser=function(e){var t;return t=function(){function t(e,t){function n(){this.constructor=e}n.prototype=t.prototype,e.prototype=new n}function n(e,t,n,r,i,o){this.message=e,this.expected=t,this.found=n,this.offset=r,this.line=i,this.column=o,this.name="SyntaxError"}function r(t){function r(){return i(hn).line}function i(e){function n(e,n,r){var i,o;for(i=n;r>i;i++)o=t.charAt(i),"\n"===o?(e.seenCR||e.line++,e.column=1,e.seenCR=!1):"\r"===o||"\u2028"===o||"\u2029"===o?(e.line++,e.column=1,e.seenCR=!0):(e.column++,e.seenCR=!1)}return vn!==e&&(vn>e&&(vn=0,mn={line:1,column:1,seenCR:!1}),n(mn,vn,e),vn=e),mn}function o(e){gn>fn||(fn>gn&&(gn=fn,yn=[]),yn.push(e))}function c(e,r,o){function c(e){var t=1;for(e.sort(function(e,t){return e.descriptiont.description?1:0});t1?c.slice(0,-1).join(", ")+" or "+c[e.length-1]:c[0],i=t?'"'+n(t)+'"':"end of input","Expected "+r+" but "+i+" found."}var s=i(o),u=o1?arguments[1]:{},U={},H={templateLines:a},B=a,J=U,L=null,V=function(t,n){return e.generateNodeTree(t,n)},$={type:"other",description:"whitespace"},K=/^[ \t]/,W={type:"class",value:"[ \\t]",description:"[ \\t]"},M=function(e,t){return{indent:e,item:t}},G={type:"other",description:"indent"},X=function(t){return e.parseIndent(Cn,t,r())},Q={type:"other",description:"new line"},Y="\n",Z={type:"literal",value:"\n",description:'"\\n"'},et={type:"other",description:"character"},tt=/^[^\n]/,nt={type:"class",value:"[^\\n]",description:"[^\\n]"},rt={type:"other",description:"identifier"},it=/^[a-z_]/i,ot={type:"class",value:"[a-z_]i",description:"[a-z_]i"},ct=/^[a-z0-9_\-]/i,at={type:"class",value:"[a-z0-9_\\-]i",description:"[a-z0-9_\\-]i"},st=function(e,t){return e+t.join("")},ut={type:"other",description:"dotted path"},lt=".",dt={type:"literal",value:".",description:'"."'},pt=function(e,t){return t.length?[e].concat(t.map(function(e){return e[1]})):[e]},ft="!",ht={type:"literal",value:"!",description:'"!"'},vt=function(e){return e},mt="#",gt={type:"literal",value:"#",description:'"#"'},yt=function(e){return{id:e}},xt=function(e){return{className:e}},Ct=/^[^\\\n\]]/,wt={type:"class",value:"[^\\\\\\n\\]]",description:"[^\\\\\\n\\]]"},At=function(e){return e.join("")},Et="[",St={type:"literal",value:"[",description:'"["'},Nt="=",_t={type:"literal",value:"=",description:'"="'},Ft="]",bt={type:"literal",value:"]",description:'"]"'},Rt=function(e,t){return{attr:e,value:t}},Tt=function(e,t){return{prop:e,value:t}},It=function(e,t){return{event:e,value:t}},jt={type:"other",description:"element qualifier"},Ot={type:"other",description:"element"},kt=function(t,n){return e.createElement("div",t,n,r())},Dt=function(t,n,i){return e.createElement(t,n,i,r())},qt=function(e){return e},zt=function(e){return e},Pt=function(e,t){return{textnode:e,partial:t}},Ut={type:"other",description:"text node"},Ht=function(t){return e.createTextNode(t,r())},Bt="u",Jt={type:"literal",value:"u",description:'"u"'},Lt=/^[0-9a-z]/i,Vt={type:"class",value:"[0-9a-z]i",description:"[0-9a-z]i"},$t=function(e,t,n,r){return""+e+t+n+r},Kt="x",Wt={type:"literal",value:"x",description:'"x"'},Mt=function(e,t){return""+e+t},Gt="\\",Xt={type:"literal",value:"\\",description:'"\\\\"'},Qt=function(t){return e.escapedCharacter(t)},Yt='"',Zt={type:"literal",value:'"',description:'"\\""'},en=/^[^\\\n"]/,tn={type:"class",value:'[^\\\\\\n"]',description:'[^\\\\\\n"]'},nn="'",rn={type:"literal",value:"'",description:'"\'"'},on=/^[^\\\n']/,cn={type:"class",value:"[^\\\\\\n']",description:"[^\\\\\\n']"},an={type:"other",description:"quoted text"},sn={type:"other",description:"directive"},un="@",ln={type:"literal",value:"@",description:'"@"'},dn=function(t){return e.createDirective(t,void 0,r())},pn=function(t,n){return e.createDirective(t,n.join(""),r())},fn=0,hn=0,vn=0,mn={line:1,column:1,seenCR:!1},gn=0,yn=[],xn=0;if("startRule"in P){if(!(P.startRule in H))throw new Error("Can't start parsing from rule \""+P.startRule+'".');B=H[P.startRule]}var Cn=[0];if(z=B(),z!==U&&fn===t.length)return z;throw z!==U&&fn";try{r=c.parse(a(t))}catch(i){throw i.message+=" in '"+n+"' on line "+i.line+("undefined"!=typeof i.column?", character "+i.column:""),i}return new e(n,r)}return l.Template=e,l.fromScriptTag=function(e){return console&&(console.warn||console.log)("Warning: ist.fromScriptTag is deprecated, use ist.script instead"),l.script(e)},l.registerHelper=function(e,t){console&&(console.warn||console.log)("Warning: ist.registerHelper is deprecated, use ist.helper instead"),l.helper(e,t)},l.createNode=function(e,t,n){return console&&(console.warn||console.log)("Warning: ist.createNode is deprecated, use ist.create instead"),l.create(e,t,n)},l.create=function(e,t,n){var r,i=e.split(">").map(function(e){return e.trim()}),o="",c="";return i.forEach(function(e){c+="\n"+o+e,o+=" "}),r=l(c).render(t,n),1===r.childNodes.length?r.firstChild:r},l.script=function(e){var t=u.findScript(e);return t?l(t):void 0},l.helper=function(e,n){t.register(e,n)},l.helper("include",function(e,t,n,i){var o,c,a=t,s=a.replace(/\.ist$/,"");if(o=u.findScript(a),r)for(c=[s,s+".ist","ist!"+s,"text!"+s+".ist"];!o&&c.length;)try{o=requirejs(c.shift())}catch(d){}if(!o)throw new Error("Cannot find included template '"+a+"'");if("string"==typeof o&&(o=l(o,s)),"function"!=typeof o.render)throw new Error("Invalid included template '"+a+"'");i.appendChild(o.render(e))}),l.global=function(e,t){n.globalScope[e]=t},(i||o&&r)&&s(l),l}(n.template,n.directives,n.context,n.parser,n.preprocessor,n.amdplugin,n.misc),r||i?define("ist",[],function(){return n.ist}):(t=e.ist,e.ist=n.ist,e.ist.noConflict=function(){var n=e.ist;return e.ist=t,n})}(this);