From 71706af7541e84ded4b100f4ad0d7880f82d1ffb Mon Sep 17 00:00:00 2001 From: BorisMoore Date: Wed, 15 Sep 2010 10:27:20 -0700 Subject: [PATCH] Some minor edits following JSLint verification. --- demos/samplesCore/conditional.html | 27 ++++++++------------ jquery.tmpl.js | 40 +++++++++++++++--------------- jquery.tmplPlus.js | 31 ++++++++++++----------- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/demos/samplesCore/conditional.html b/demos/samplesCore/conditional.html index 8d21c3d..924c13f 100644 --- a/demos/samplesCore/conditional.html +++ b/demos/samplesCore/conditional.html @@ -21,23 +21,16 @@ diff --git a/jquery.tmpl.js b/jquery.tmpl.js index 3a5e040..fa0cb4c 100644 --- a/jquery.tmpl.js +++ b/jquery.tmpl.js @@ -4,7 +4,7 @@ * Dual licensed under the MIT or GPL Version 2 licenses. */ (function( jQuery, undefined ){ - var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$|{{! /, + var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]*(<[\w\W]+>)[^>]*$|\{\{\! /, newTmplItems = {}, wrappedItems = {}, appendToTmplItems, topTmplItem = { key: 0, data: {} }, itemKey = 0, cloneIndex = 0, stack = []; function newTmplItem( options, parentItem, fn, data ) { @@ -46,7 +46,7 @@ replaceAll: "replaceWith" }, function( name, original ) { jQuery.fn[ name ] = function( selector ) { - var ret = [], insert = jQuery( selector ), + var ret = [], insert = jQuery( selector ), elems, i, l, tmplItems, parent = this.length === 1 && this[0].parentNode; appendToTmplItems = newTmplItems || {}; @@ -54,16 +54,16 @@ insert[ original ]( this[0] ); ret = this; } else { - for ( var i = 0, l = insert.length; i < l; i++ ) { + for ( i = 0, l = insert.length; i < l; i++ ) { cloneIndex = i; - var elems = (i > 0 ? this.clone(true) : this).get(); + elems = (i > 0 ? this.clone(true) : this).get(); jQuery.fn[ original ].apply( jQuery(insert[i]), elems ); ret = ret.concat( elems ); } cloneIndex = 0; ret = this.pushStack( ret, name, insert.selector ); } - var tmplItems = appendToTmplItems; + tmplItems = appendToTmplItems; appendToTmplItems = null; jQuery.tmpl.complete( tmplItems ); return ret; @@ -92,7 +92,7 @@ // it should be doing .call() instead of .apply(). See #6227 if ( args[0] && args[0].nodeType ) { var dmArgs = jQuery.makeArray( arguments ), argsLength = args.length, i = 0, tmplItem; - while ( i < argsLength && !(tmplItem = jQuery.data( args[i++], "tmplItem" ))) {}; + while ( i < argsLength && !(tmplItem = jQuery.data( args[i++], "tmplItem" ))) {} if ( argsLength > 1 ) { dmArgs[0] = [jQuery.makeArray( args )]; } @@ -100,7 +100,7 @@ dmArgs[2] = function( fragClone ) { // Handler called by oldManip when rendered template has been inserted into DOM. jQuery.tmpl.afterManip( this, fragClone, callback ); - } + }; } oldManip.apply( this, dmArgs ); } else { @@ -322,8 +322,8 @@ jQuery.trim(markup) .replace( /([\\'])/g, "\\$1" ) .replace( /[\r\t\n]/g, " " ) - .replace( /\${([^}]*)}/g, "{{= $1}}" ) - .replace( /{{(\/?)(\w+|.)(?:\(((?:.(?!}}))*?)?\))?(?:\s+(.*?)?)?(\((.*?)\))?\s*}}/g, + .replace( /\$\{([^\}]*)\}/g, "{{= $1}}" ) + .replace( /\{\{(\/?)(\w+|.)(?:\(((?:.(?!\}\}))*?)?\))?(?:\s+(.*?)?)?(\((.*?)\))?\s*\}\}/g, function( all, slash, type, fnargs, target, parens, args ) { var tag = jQuery.tmpl.tag[ type ], def, expr, exprAutoFnDetect; if ( !tag ) { @@ -342,7 +342,7 @@ expr = parens ? (target.indexOf(".") > -1 ? target + parens : ("(" + target + ").call($item" + args)) : target; exprAutoFnDetect = parens ? expr : "(typeof(" + target + ")==='function'?(" + target + ").call($item):(" + target + "))"; } else { - exprAutoFnDetect = expr = def["$1"] || "null"; + exprAutoFnDetect = expr = def.$1 || "null"; } fnargs = unescape( fnargs ); return "');" + @@ -355,7 +355,7 @@ params = params ? ("," + params + ")") : (parens ? ")" : ""); return params ? ("(" + name + ").call($item" + params) : all; }) - : (def["$2"]||"") + : (def.$2||"") ) + "_.push('"; }) + @@ -381,13 +381,13 @@ // Store template items in jQuery.data(), ensuring a unique tmplItem data data structure for each rendered template instance. function storeTmplItems( content ) { - var keySuffix = "_" + cloneIndex, elem, elems, newClonedItems = {}; - for ( var i = 0, l = content.length; i < l; i++ ) { + var keySuffix = "_" + cloneIndex, elem, elems, newClonedItems = {}, i, l, m; + for ( i = 0, l = content.length; i < l; i++ ) { if ( (elem = content[i]).nodeType !== 1 ) { continue; } elems = elem.getElementsByTagName("*"); - for ( var m = elems.length - 1; m >= 0; m-- ) { + for ( m = elems.length - 1; m >= 0; m-- ) { processItemKey( elems[m] ); } processItemKey( elem ); @@ -395,7 +395,7 @@ function processItemKey( el ) { var pntKey, pntNode = el, pntItem, tmplItem, key; // Ensure that each rendered template inserted into the DOM has its own template item, - if ( key = el.getAttribute( tmplItmAtt )) { + if ( (key = el.getAttribute( tmplItmAtt ))) { while ((pntNode = pntNode.parentNode).nodeType === 1 && !(pntKey = pntNode.getAttribute( tmplItmAtt ))) { } if ( pntKey !== key ) { // The next ancestor with a _tmplitem expando is on a different key than this one. @@ -423,8 +423,9 @@ } if ( tmplItem ) { pntItem = tmplItem; - // Find the template item of the parent element - while ( pntItem && pntItem.key != pntNode ) { + // Find the template item of the parent element. + // (Using !=, not !==, since pntItem.key is number, and pntNode may be a string) + while ( pntItem && pntItem.key != pntNode ) { // Add this element as a top-level node for this rendered template item, as well as for any // ancestor items between this item and the item of its parent element pntItem.nodes.push( el ); @@ -438,8 +439,8 @@ } function cloneTmplItem( key ) { key = key + keySuffix; - tmplItem = newClonedItems[key] - = (newClonedItems[key] || newTmplItem( tmplItem, newTmplItems[tmplItem.parent.key + keySuffix] || tmplItem.parent, null, true )); + tmplItem = newClonedItems[key] = + (newClonedItems[key] || newTmplItem( tmplItem, newTmplItems[tmplItem.parent.key + keySuffix] || tmplItem.parent, null, true )); } } } @@ -450,7 +451,6 @@ if ( !content ) { return stack.pop(); } - var l = stack.length; stack.push({ _: content, tmpl: tmpl, item:this, data: data, options: options }); } diff --git a/jquery.tmplPlus.js b/jquery.tmplPlus.js index ffa8893..7631a5e 100644 --- a/jquery.tmplPlus.js +++ b/jquery.tmplPlus.js @@ -22,11 +22,25 @@ tmplItem.rendered( tmplItem ); } } - } + }; jQuery.extend({ tmplCmd: function( command, data, tmplItems ) { var retTmplItems = [], before; + function find( data, tmplItems ) { + var found = [], tmplItem, ti, tl = tmplItems.length, dataItem, di = 0, dl = data.length; + for ( ; di < dl; ) { + dataItem = data[di++]; + for ( ti = 0; ti < tl; ) { + tmplItem = tmplItems[ti++]; + if ( tmplItem.data === dataItem ) { + found.push( tmplItem ); + } + } + } + return found; + } + data = jQuery.isArray( data ) ? data : [ data ]; switch ( command ) { case "find": @@ -54,19 +68,6 @@ } }); return retTmplItems; - function find( data, tmplItems ) { - var found = [], tmplItem, ti, tl = tmplItems.length, dataItem, di = 0, dl = data.length; - for ( ; di < dl; ) { - dataItem = data[di++]; - for ( ti = 0; ti < tl; ) { - tmplItem = tmplItems[ti++]; - if ( tmplItem.data === dataItem ) { - found.push( tmplItem ); - } - } - } - return found; - } } }); @@ -83,7 +84,7 @@ dmArgs[2] = function( fragClone ) { // Handler called by oldManip when rendered template has been inserted into DOM. jQuery.tmpl.afterManip( this, fragClone, callback ); - } + }; return oldManip.apply( this, dmArgs ); } return oldManip.apply( this, arguments );