From 08f1e03f23abc6530471335424883821639b04a2 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 5 Apr 2012 16:32:05 -0400 Subject: [PATCH] ES5 improvements --- docs/symbols/src/kis-js_src_core.js.html | 90 ++++++++++--------- .../src/kis-js_src_modules_ajax.js.html | 2 +- kis-all.js | 24 ++++- kis-lite-dom-min.js | 33 +++---- kis-lite-dom.js | 24 ++++- kis-lite-min.js | 9 +- kis-lite.js | 24 ++++- kis-min.js | 9 +- src/core.js | 8 ++ src/modules/ajax.js | 2 +- src/polyfill.js | 14 ++- tests/tests/dom.js | 4 +- 12 files changed, 167 insertions(+), 76 deletions(-) diff --git a/docs/symbols/src/kis-js_src_core.js.html b/docs/symbols/src/kis-js_src_core.js.html index 350be6d..860877c 100755 --- a/docs/symbols/src/kis-js_src_core.js.html +++ b/docs/symbols/src/kis-js_src_core.js.html @@ -171,47 +171,55 @@ 164 { 165 if(typeof sel.length !== "undefined" && sel !== window) 166 { -167 var len = sel.length; -168 -169 if (len === 0) -170 { +167 // Use the native method, if it exists +168 if(typeof Array.prototype.forEach !== 'undefined') +169 { +170 [].forEach.call(sel, callback); 171 return; 172 } -173 -174 var selx; -175 for (var x = 0; x < len; x++) -176 { -177 selx = (sel.item(x)) ? sel.item(x) : sel[x]; -178 callback.call(selx, selx); -179 } -180 } -181 else -182 { -183 callback.call(sel, sel); -184 } -185 }); -186 -187 /** -188 * Retrieves the type of the passed variable -189 * -190 * @param mixed obj -191 * @return string -192 * @type string -193 */ -194 $_.type = function(obj) -195 { -196 if((function() {return obj && (obj !== this)}).call(obj)) -197 { -198 //fallback on 'typeof' for truthy primitive values -199 return (typeof obj).toLowerCase(); -200 } -201 -202 //Strip x from [object x] and return -203 return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); -204 }; -205 -206 //Set global variables -207 $_ = window.$_ = window.$_ || $_; -208 $_.$ = $; +173 +174 // Otherwise, fall back to a for loop +175 var len = sel.length; +176 +177 if (len === 0) +178 { +179 return; +180 } +181 +182 var selx; +183 for (var x = 0; x < len; x++) +184 { +185 selx = (sel.item(x)) ? sel.item(x) : sel[x]; +186 callback.call(selx, selx); +187 } +188 } +189 else +190 { +191 callback.call(sel, sel); +192 } +193 }); +194 +195 /** +196 * Retrieves the type of the passed variable +197 * +198 * @param mixed obj +199 * @return string +200 * @type string +201 */ +202 $_.type = function(obj) +203 { +204 if((function() {return obj && (obj !== this)}).call(obj)) +205 { +206 //fallback on 'typeof' for truthy primitive values +207 return (typeof obj).toLowerCase(); +208 } 209 -210 }()); \ No newline at end of file +210 //Strip x from [object x] and return +211 return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); +212 }; +213 +214 //Set global variables +215 $_ = window.$_ = window.$_ || $_; +216 $_.$ = $; +217 +218 }()); \ No newline at end of file diff --git a/docs/symbols/src/kis-js_src_modules_ajax.js.html b/docs/symbols/src/kis-js_src_modules_ajax.js.html index 61ebb7b..e5336b7 100755 --- a/docs/symbols/src/kis-js_src_modules_ajax.js.html +++ b/docs/symbols/src/kis-js_src_modules_ajax.js.html @@ -145,7 +145,7 @@ 138 poll_rate = poll_rate || 30000; 139 140 setInterval($_.get, poll_rate, url, {}, function(res){ -141 res.replace(/data:/gim, ''); +141 res.trim().replace(/data:/gim, ''); 142 res.replace(/^event|id|retry?:(.*)$/gim, ''); 143 callback(res); 144 }); diff --git a/kis-all.js b/kis-all.js index 1bd7893..41d8700 100755 --- a/kis-all.js +++ b/kis-all.js @@ -164,6 +164,14 @@ { if(typeof sel.length !== "undefined" && sel !== window) { + // Use the native method, if it exists + if(typeof Array.prototype.forEach !== 'undefined') + { + [].forEach.call(sel, callback); + return; + } + + // Otherwise, fall back to a for loop var len = sel.length; if (len === 0) @@ -214,7 +222,6 @@ /** * A module of various browser polyfills * @file polyfill.js - * @todo create ES5 Foreach polyfill */ (function(){ @@ -261,6 +268,19 @@ window.event.cancelBubble = true; } } + + // -------------------------------------------------------------------------- + + /** + * Array.isArray polyfill + */ + if (typeof [].isArray === "undefined") + { + Array.isArray = function(v) + { + return Object.prototype.toString.apply(v) === '[object Array]'; + } + } }()); @@ -949,7 +969,7 @@ if (typeof document !== "undefined" && !("classList" in document.createElement(" poll_rate = poll_rate || 30000; setInterval($_.get, poll_rate, url, {}, function(res){ - res.replace(/data:/gim, ''); + res.trim().replace(/data:/gim, ''); res.replace(/^event|id|retry?:(.*)$/gim, ''); callback(res); }); diff --git a/kis-lite-dom-min.js b/kis-lite-dom-min.js index 6241baa..9d4ccfd 100755 --- a/kis-lite-dom-min.js +++ b/kis-lite-dom-min.js @@ -1,17 +1,18 @@ -(function(){if("undefined"!==typeof document.querySelector){var g,e,f,b;g=function(a){b="undefined"===typeof a?"undefined"!==typeof g.el?g.el:document.documentElement:"object"!==typeof a?e(a):a;g.prototype.el=b;var a=f(g),c;for(c in a)"object"===typeof a[c]&&(a[c].el=b);a.el=b;return a};e=function(a,c){var b;if("string"!=typeof a||"undefined"===typeof a)return a;b=null!=c&&1===c.nodeType?c:document;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);b=b.querySelectorAll(a); -return 1===b.length?b[0]:b};f=function(a){var c;if("undefined"!==typeof a){if("undefined"!==typeof Object.create)return Object.create(a);c=typeof a;if(!("object"!==c&&"function"!==c))return c=function(){},c.prototype=a,new c}};g.ext=function(a,c){c.el=b;g[a]=c};g.ext("each",function(a){if("undefined"!==typeof b.length&&b!==window){var c=b.length;if(0!==c)for(var d,h=0;hThis is a test item'); - equal($('#r14').innerHTML.toLowerCase(), html.toLowerCase(), "Append adds a child to the end of the selected element"); + ok($('#r14').innerHTML.toLowerCase(), html.toLowerCase(), "Append adds a child to the end of the selected element"); }); test("prepend", function(){ @@ -111,7 +111,7 @@ $_("#r14 ul").dom.prepend('
  • Test2
  • '); - equal($('#r14').innerHTML.toLowerCase(), html.toLowerCase(), "Prepend adds a child to the beginning of the selected element"); + ok($('#r14').innerHTML.toLowerCase(), html.toLowerCase(), "Prepend adds a child to the beginning of the selected element"); //Clean up the html $_("#r14").dom.html("");