forked from BorisMoore/jquery-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tmpl.min.js
22 lines (22 loc) · 6.33 KB
/
jquery.tmpl.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
* jQuery Templates Plugin 1.0.0pre
* http://github.com/jquery/jquery-tmpl
* Requires jQuery 1.4.2
*
* Copyright 2011, Software Freedom Conservancy, Inc.
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function(){function m(d){function q(a,b,e,c){c={data:c||0===c||!1===c?c:b?b.data:{},_wrap:b?b._wrap:null,tmpl:null,parent:b||null,nodes:[],calls:D,nest:E,wrap:F,html:G,update:H};a&&d.extend(c,a,{nodes:[],parent:b});e&&(c.tmpl=e,c._ctnt=c._ctnt||c.tmpl(d,c),c.key=++r,(w.length?s:j)[r]=c);return c}function t(a,b,e){var c,e=e?d.map(e,function(b){return"string"===typeof b?a.key?b.replace(/(<\w+)(?=[\s>])(?![^>]*_tmplitem)([^>]*)/g,"$1 "+n+'="'+a.key+'" $2'):b:t(b,a,b._ctnt)}):a;if(b)return e;e=e.join("");
e.replace(/^\s*([^<\s][^<]*)?(<[\w\W]+>)([^>]*[^>\s])?\s*$/,function(b,a,e,i){c=d(e).get();z(c);a&&(c=x(a).concat(c));i&&(c=c.concat(x(i)))});return c?c:x(e)}function x(a){var b=document.createElement("div");b.innerHTML=a;return d.makeArray(b.childNodes)}function m(a){return new Function("jQuery","$item","var $=jQuery,call,__=[],$data=$item.data;with($data){__.push('"+d.trim(a).replace(/([\\'])/g,"\\$1").replace(/[\r\t\n]/g," ").replace(/\$\{([^\}]*)\}/g,"{{= $1}}").replace(/\{\{(\/?)(\w+|.)(?:\(((?:[^\}]|\}(?!\}))*?)?\))?(?:\s+(.*?)?)?(\(((?:[^\}]|\}(?!\}))*?)\))?\s*\}\}/g,
function(a,e,c,g,h,f,i){a=d.tmpl.tag[c];if(!a)throw"Unknown template tag: "+c;c=a._default||[];f&&!/\w$/.test(h)&&(h+=f,f="");h?(h=u(h),i=i?","+u(i)+")":f?")":"",i=f?-1<h.indexOf(".")?h+u(f):"("+h+").call($item"+i:h,f=f?i:"(typeof("+h+")==='function'?("+h+").call($item):("+h+"))"):f=i=c.$1||"null";g=u(g);return"');"+a[e?"close":"open"].split("$notnull_1").join(h?"typeof("+h+")!=='undefined' && ("+h+")!=null":"true").split("$1a").join(f).split("$1").join(i).split("$2").join(g||c.$2||"")+"__.push('"})+
"');}return __;")}function A(a,b){a._wrap=t(a,!0,d.isArray(b)?b:[B.test(b)?b:d(b).html()]).join("")}function u(a){return a?a.replace(/\\'/g,"'").replace(/\\\\/g,"\\"):null}function z(a){function b(a){function b(a){a+=e;f=h[a]=h[a]||q(f,j[f.parent.key+e]||f.parent)}var c,g=a,f,i;if(i=a.getAttribute(n)){for(;g.parentNode&&1===(g=g.parentNode).nodeType&&!(c=g.getAttribute(n)););if(c!==i){g=g.parentNode?11===g.nodeType?0:g.getAttribute(n)||0:0;if(!(f=j[i]))f=s[i],f=q(f,j[g]||s[g]),f.key=++r,j[r]=f;k&&
b(i)}if(i=a.getAttribute("_tmplelement"))p[i]?unshift.call(p[i],a):p[i]=(window[a.getAttribute("_tmplclass")]||$)(a),a.removeAttribute("_tmplelement"),a.removeAttribute("_tmplclass");a.removeAttribute(n)}else if(k&&(f=d.data(a,"tmplItem")))b(f.key),j[f.key]=f,g=(g=d.data(a.parentNode,"tmplItem"))?g.key:0;if(f){c=f;for(f.elements=p;c&&c.key!=g;)c.nodes.push(a),c=c.parent;delete f._ctnt;delete f._wrap;d.data(a,"tmplItem",f)}}var e="_"+k,c,g,h={},f,i,l,p;f=0;for(i=a.length;f<i;f++)if(1===(c=a[f]).nodeType){g=
c.getElementsByTagName("*");p={};for(l=g.length-1;0<=l;l--)b(g[l]);b(c)}}function D(a,b,d,c){if(!a)return w.pop();w.push({_:a,tmpl:b,item:this,data:d,options:c})}function E(a,b,e){return d.tmpl(d.template(a),b,e,this)}function F(a,b){var e=a.options||{};e.wrapped=b;return d.tmpl(d.template(a.tmpl),a.data,e,a.item)}function G(a,b){var e=this._wrap;return d.map(d(d.isArray(e)?e.join(""):e).filter(a||"*"),function(a){if(b)a=a.innerText||a.textContent;else{var d;if(!(d=a.outerHTML))d=document.createElement("div"),
d.appendChild(a.cloneNode(!0)),d=d.innerHTML;a=d}return a})}function H(){var a=this.nodes;d.tmpl(null,null,null,this).insertBefore(a[0]);d(a).remove()}var C=d.fn.domManip,n="_tmplitem",B=/^[^<]*(<[\w\W]+>)[^>]*$|\{\{\! /,j={},s={},v,y={key:0,data:{}},r=0,k=0,w=[];d.each({appendTo:"append",prependTo:"prepend",insertBefore:"before",insertAfter:"after",replaceAll:"replaceWith"},function(a,b){d.fn[a]=function(e){var c=[],e=d(e),g,h,f;g=1===this.length&&this[0].parentNode;v=j||{};if(g&&11===g.nodeType&&
1===g.childNodes.length&&1===e.length)e[b](this[0]),c=this;else{h=0;for(f=e.length;h<f;h++)k=h,g=(0<h?this.clone(!0):this).get(),d(e[h])[b](g),c=c.concat(g);k=0;c=this.pushStack(c,a,e.selector)}e=v;v=null;d.tmpl.complete(e);return c}});d.fn.extend({tmpl:function(a,b,e){return d.tmpl(this[0],a,b,e)},tmplItem:function(){return d.tmplItem(this[0])},template:function(a){return d.template(a,this[0])},domManip:function(a,b,e,c){if(a[0]&&d.isArray(a[0])){for(var g=d.makeArray(arguments),h=a[0],f=h.length,
i=0,l;i<f&&!(l=d.data(h[i++],"tmplItem")););l&&k&&(g[2]=function(a){d.tmpl.afterManip(this,a,e)});C.apply(this,g)}else C.apply(this,arguments);k=0;v||d.tmpl.complete(j);return this}});d.extend({tmpl:function(a,b,e,c){var g=!c;if(g)c=y,a=d.template[a]||d.template(null,a),s={};else if(!a)return a=c.tmpl,j[c.key]=c,c.nodes=[],c.wrapped&&A(c,c.wrapped),d(t(c,null,c.tmpl(d,c)));if(!a)return[];"function"===typeof b&&(b=b.call(c||{}));e&&e.wrapped&&A(e,e.wrapped);b=d.isArray(b)?d.map(b,function(b){return b?
q(e,c,a,b):null}):[q(e,c,a,b)];return g?d(t(c,null,b)):b},tmplItem:function(a,b){var e;for(a instanceof d&&(a=a[0]);a&&1===a.nodeType&&!(e=d.data(a,"tmplItem"))&&(a=a.parentNode););return b?(e||y)[b]:e||y},template:function(a,b){return b?("string"===typeof b?b=m(b):b instanceof d&&(b=b[0]||{}),b.nodeType&&(b=d.data(b,"tmpl")||d.data(b,"tmpl",m(b.innerHTML))),"string"===typeof a?d.template[a]=b:b):a?"string"!==typeof a?d.template(null,a):d.template[a]||d.template(null,B.test(a)?a:d(a)):null},encode:function(a){return(""+
a).split("<").join("<").split(">").join(">").split('"').join(""").split("'").join("'")}});d.extend(d.tmpl,{tag:{tmpl:{_default:{$2:"null"},open:"if($notnull_1){__=__.concat($item.nest($1,$2));}"},wrap:{_default:{$2:"null"},open:"$item.calls(__,$1,$2);__=[];",close:"call=$item.calls();__=call._.concat($item.wrap(call,__));"},each:{_default:{$2:"$index, $value"},open:"if($notnull_1){$.each($1a,function($2){with(this){",close:"}});}"},"if":{open:"if(($notnull_1) && $1a){",close:"}"},"else":{_default:{$1:"true"},
open:"}else if(($notnull_1) && $1a){"},html:{open:"if($notnull_1){__.push($1a);}"},"=":{_default:{$1:"$data"},open:"if($notnull_1){__.push($.encode($1a));}"},"!":{open:""},elem:{_default:{$2:"jQuery"},open:"if($notnull_1){__.push(' _tmplclass=\"$2\" _tmplelement=\"'+$.encode($1a)+'\"');}"}},complete:function(){j={}},afterManip:function(a,b,e){var c=11===b.nodeType?d.makeArray(b.childNodes):1===b.nodeType?[b]:[];e.call(a,b);z(c);k++}})}return"function"===typeof define&&define.amd?define("jquery/tmpl",
["jquery"],m):m(jQuery)})();