Added reverse_key_sort method to util module, updated tests and qunit

This commit is contained in:
Timothy Warren 2011-10-21 09:57:46 -04:00
parent db76e16393
commit 69ee39623d
6 changed files with 344 additions and 120 deletions

View File

@ -29,27 +29,16 @@ if($dir = opendir($folder))
//Define files that aren't modules
$special_files = array(
'core.js',
'module_vars.js',
);
//Filter out special files
$src_files = array_diff($files, $special_files);
$syntax_start = array(
'//Function to maintain module scope',
'(function(){',
'',
' "use strict";',
);
//Start with the core
$new_file = file_get_contents($folder."/core.js") . "\n";
//Add the opening of the function for the modules
$new_file .= "\n// --------------------------------------------------------------------------\n\n".implode("\n", $syntax_start);
//Add the module-global variables
$new_file .= "\n\n".file_get_contents($folder."/module_vars.js")."\n";
$new_file .= "\n// --------------------------------------------------------------------------\n\n";
//Add the modules
foreach($src_files as $f)
@ -71,9 +60,6 @@ foreach($src_files as $f)
}
//Add the close of the module function
$new_file .= "\n}());";
//Output the full file
file_put_contents("kis-custom.js", $new_file);

View File

@ -185,13 +185,6 @@
// --------------------------------------------------------------------------
//Function to maintain module scope
(function(){
"use strict";
//Fix $_ is not defined errors
var $_ = $_ || window.$_;
// --------------------------------------------------------------------------
@ -716,6 +709,11 @@
}());
// --------------------------------------------------------------------------
//Fix $_ is not defined errors
var $_ = $_ || window.$_;
// --------------------------------------------------------------------------
/**
@ -805,17 +803,18 @@
* Various object and string manipulation functions
*/
(function(){
var u = {
object_keys: function(o)
{
var keys = [],
key;
k;
for(key in o)
for(k in o)
{
if(o.hasOwnProperty(key))
if(o.hasOwnProperty(k))
{
keys.push(key);
keys.push(k);
}
}
@ -832,8 +831,64 @@
}
return vals;
}
},
object_merge: function()
{
},
reverse_key_sort: function(o)
{
//Define some variables
var keys = [],
num_keys = 0,
new_o = {},
i,
k,
x;
//Extract the keys
keys = this.object_keys(o);
//Sort the keys
keys.sort(function (b, a) {
var aFloat = parseFloat(a),
bFloat = parseFloat(b),
aNumeric = aFloat + '' === a,
bNumeric = bFloat + '' === b;
if (aNumeric && bNumeric)
{
return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
}
else if (aNumeric && !bNumeric)
{
return 1;
}
else if (!aNumeric && bNumeric)
{
return -1;
}
return a > b ? 1 : a < b ? -1 : 0;
});
//cache object/array size
num_keys = keys.length;
//Recreate the object/array
for(i=0; i < num_keys; i++)
{
k = keys[i];
new_o[k] = o[k];
}
return new_o;
},
str_trans: function(string, from, to)
{
}
};
//Add it to the $_ object
@ -1019,5 +1074,3 @@
$_.ext('event', e);
}());
}());

37
kis-min.js vendored
View File

@ -1,17 +1,20 @@
(function(){if(document.querySelectorAll){var g,c,f,d;c=function(a){if(typeof a!=="string"||typeof a==="undefined")return a;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);else a=a.match(/^([\w\-]+)$/)?document.getElementsByTagName(a):document.querySelectorAll(a);return a.length===1?a[0]:a};g=function(a){d=typeof a==="undefined"?typeof g.el!=="undefined"?g.el:document.documentElement:typeof a!=="object"?c(a):a;g.prototype.el=d;var a=f(g),b;for(b in a)if(typeof a[b]==="object")a[b].el=
d;a.el=d;return a};f=function(a){var b;if(typeof a!=="undefined"){if(typeof Object.create!=="undefined")return Object.create(a);b=typeof a;if(!(b!=="object"&&b!=="function"))return b=function(){},b.prototype=a,new b}};g.ext=function(a,b){b.el=d;g[a]=b};g.ext("each",function(a){if(typeof d.length!=="undefined"&&d!==window){var b=d.length;if(b!==0)for(var e,h=0;h<b;h++)e=d.item(h)?d.item(h):d[h],a(e)}else a(d)});g.type=function(a){return function(){return a&&a!==this}.call(a)?(typeof a).toLowerCase():
{}.toString.call(a).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()};g=window.$_=window.$_||g;g.$=c;if(typeof window.console==="undefined")window.console={log:function(){}};if(typeof String.prototype.trim==="undefined")String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}})();
(function(){var g=g||window.$_;typeof document!=="undefined"&&!("classList"in document.createElement("a"))&&function(c){var c=(c.HTMLElement||c.Element).prototype,f=Object,d=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},a=Array.prototype.indexOf||function(a){for(var b=0,e=this.length;b<e;b++)if(b in this&&this[b]===a)return b;return-1},b=function(a,b){this.name=a;this.code=DOMException[a];this.message=b},e=function(e,h){if(h==="")throw new b("SYNTAX_ERR","An invalid or illegal string was specified");
if(/\s/.test(h))throw new b("INVALID_CHARACTER_ERR","String contains an invalid character");return a.call(e,h)},h=function(a){for(var b=d.call(a.className),b=b?b.split(/\s+/):[],e=0,h=b.length;e<h;e++)this.push(b[e]);this._updateClassName=function(){a.className=this.toString()}},i=h.prototype=[],g=function(){return new h(this)};b.prototype=Error.prototype;i.item=function(a){return this[a]||null};i.contains=function(a){a+="";return e(this,a)!==-1};i.add=function(a){a+="";e(this,a)===-1&&(this.push(a),
this._updateClassName())};i.remove=function(a){a+="";a=e(this,a);a!==-1&&(this.splice(a,1),this._updateClassName())};i.toggle=function(a){a+="";e(this,a)===-1?this.add(a):this.remove(a)};i.toString=function(){return this.join(" ")};if(f.defineProperty){i={get:g,enumerable:true,configurable:true};try{f.defineProperty(c,"classList",i)}catch(j){if(j.number===-2146823252)i.enumerable=false,f.defineProperty(c,"classList",i)}}else f.prototype.__defineGetter__&&c.__defineGetter__("classList",g)}(self);(function(){function c(a,
b,e){var h,c;if(typeof a.hasAttribute!=="undefined")a.hasAttribute(b)&&(h=a.getAttribute(b)),c=true;else if(typeof a[b]!=="undefined")h=a[b],c=false;else if(b==="class"&&typeof a.className!=="undefined")b="className",h=a.className,c=false;if(typeof h==="undefined"&&(typeof e==="undefined"||e===null))console.log(e),console.log(a),console.log("Element does not have the selected attribute");else{if(typeof e==="undefined")return h;typeof e!=="undefined"&&e!==null?c===true?a.setAttribute(b,e):a[b]=e:e===
null&&(c===true?a.removeAttribute(b):delete a[b]);return typeof e!=="undefined"?e:h}}function f(a){return a.replace(/(\-[a-z])/g,function(a){return a.toUpperCase().replace("-","")})}function d(a,b,e){var h,b=f(b);h={outerHeight:"offsetHeight",outerWidth:"offsetWidth",top:"posTop"};if(typeof e==="undefined"&&a.style[b]!=="undefined")return a.style[b];else if(typeof e==="undefined"&&a.style[h[b]]!=="undefined")return a.style[h[b]];typeof a.style[b]!=="undefined"?a.style[b]=e:a.style[h[b]]?a.style[h[b]]=
e:console.log("Property "+b+" nor an equivalent seems to exist")}g.ext("dom",{addClass:function(a){g.each(function(b){b.classList.add(a)})},removeClass:function(a){g.each(function(b){b.classList.remove(a)})},hide:function(){this.css("display","none")},show:function(a){typeof a==="undefined"&&(a="block");this.css("display",a)},attr:function(a,b){var e=this.el;if(e.length>1&&typeof b==="undefined")console.log(e),console.log("Must be a singular element");else if(e.length>1&&typeof b!=="undefined")g.each(function(e){return c(e,
a,b)});else return c(e,a,b)},text:function(a){var b,e,h;h=this.el;e=typeof h.innerText!=="undefined"?"innerText":typeof h.textContent!=="undefined"?"textContent":"innerHTML";b=h[e];return typeof a!=="undefined"?h[e]=a:b},css:function(a,b){if(typeof b==="undefined")return d(this.el,a);g.each(function(e){d(e,a,b)})}})})();(function(){g.ext("store",{get:function(c){return JSON.parse(localStorage.getItem(c))},set:function(c,f){typeof f!=="string"&&(f=JSON.stringify(f));localStorage.setItem(c,f)},remove:function(c){localStorage.removeItem(c)},
getAll:function(){var c,f,d;f=localStorage.length;d={};for(c=0;c<f;c++){var a=localStorage.key(c),b=localStorage.getItem(a);d[a]=b}return d}})})();(function(){g.hb=history.pushState?false:true;g.ext("qs",{parse:function(c){var c=c||g.hb,f,d,a,b;a={};if(c===true)c=location.hash.split("#!/"),c=c.length>1?c[1]:"";else if(c===false||c===void 0)c=window.location.search.substring(1);else return false;f=c.split("&");d=f.length;for(c=0;c<d;c++){b=f[c].split("=");if(b.length<2)break;a[b[0]]=b[1]}return a},
set:function(c,f,d){var d=d||g.hb,a=this.parse(d);c!==void 0&&f!==void 0&&(a[c]=f);var c=[],b;for(b in a)a.hasOwnProperty(b)&&c.push(b+"="+a[b]);a=c.join("&");if(d===true)a="!/"+a,location.hash=a;return a},get:function(c,f){var f=f||g.hb,d=this.parse(f);return d[c]?d[c]:""}})})();(function(){var c={_do:function(c,d,a,b){typeof a==="undefined"&&(a=function(){});var e=typeof window.XMLHttpRequest!=="undefined"?new XMLHttpRequest:false,b=b?"POST":"GET";c+=b==="GET"?"?"+this._serialize(d):"";e.open(b,
c);e.onreadystatechange=function(){e.readyState===4&&a(e.responseText)};b==="POST"?(e.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),e.send(this._serialize(d))):e.send(null)},_serialize:function(c){var d=[],a;for(a in c)if(c.hasOwnProperty(a)&&typeof c[a]!=="function"){var b=c[a].toString();a=encodeURIComponent(a);b=encodeURIComponent(b);d.push(a+"="+b)}return d.join("&")}};g.ext("get",function(f,d,a){c._do(f,d,a,false)});g.ext("post",function(f,d,a){c._do(f,d,a,true)})})();
(function(){g.ext("util",{object_keys:function(c){var f=[],d;for(d in c)c.hasOwnProperty(d)&&f.push(d);return f},object_values:function(c){var f=[],d;for(d in c)f.push(c[d]);return f}})})();(function(){var c,f,d,a,b;typeof document.addEventListener!=="undefined"?(c=function(a,b,c){typeof a.addEventListener!=="undefined"&&a.addEventListener(b,c,false)},f=function(a,b,c){typeof a.removeEventListener!=="undefined"&&a.removeEventListener(b,c,false)}):typeof document.attachEvent!=="undefined"&&(c=function(a,
b,c){var e;function d(){c.apply(arguments)}typeof a.attachEvent!=="undefined"?(f(b,c),a.attachEvent("on"+b,d),e=a.KIS_0_3_0=a.KIS_0_3_0||{},a=e,a.listeners=a.listeners||{},a.listeners[b]=a.listeners[b]||[],a.listeners[b].push({callback:c,listener:d})):console.log("Failed to attach event:"+b+" on "+a)},f=function(a,b,c){if(typeof a.detachEvent!=="undefined"){var d=a.KIS_0_3_0;if(d&&d.listeners&&d.listeners[b])for(var f=d.listeners[b],g=f.length,k=0;k<g;k++)if(f[k].callback===c){a.detachEvent("on"+
b,f[k].listener);f.splice(k,1);f.length===0&&delete d.listeners[b];break}}});d=function(a,b,g,m){var j,l;if(typeof a==="undefined")return console.log(arguments),console.log(b),false;if(b.match(/^([\w\-]+)$/))m===true?c(a,b,g):f(a,b,g);else{b=b.split(" ");l=b.length;for(j=0;j<l;j++)d(a,b[j],g,m)}};a=function(a,b,c){d(a,c,function(){a=g.$(a)},true)};b=function(b,c,d){a(document.documentElement,b,c,d)};g.ext("event",{add:function(a,b){g.each(function(c){d(c,a,b,true)})},remove:function(a,b){g.each(function(c){d(c,
a,b,false)})},live:function(a,c){g.each(function(d){b(d,a,c)})},delegate:function(b,c,d){g.each(function(f){a(f,b,c,d)})}})})()})();
(function(){if(document.querySelectorAll){var b,e,f,c;e=function(a){if(typeof a!=="string"||typeof a==="undefined")return a;if(a.match(/^#([\w\-]+$)/))return document.getElementById(a.split("#")[1]);else a=a.match(/^([\w\-]+)$/)?document.getElementsByTagName(a):document.querySelectorAll(a);return a.length===1?a[0]:a};b=function(a){c=typeof a==="undefined"?typeof b.el!=="undefined"?b.el:document.documentElement:typeof a!=="object"?e(a):a;b.prototype.el=c;var a=f(b),d;for(d in a)if(typeof a[d]==="object")a[d].el=
c;a.el=c;return a};f=function(a){var d;if(typeof a!=="undefined"){if(typeof Object.create!=="undefined")return Object.create(a);d=typeof a;if(!(d!=="object"&&d!=="function"))return d=function(){},d.prototype=a,new d}};b.ext=function(a,d){d.el=c;b[a]=d};b.ext("each",function(a){if(typeof c.length!=="undefined"&&c!==window){var d=c.length;if(d!==0)for(var b,e=0;e<d;e++)b=c.item(e)?c.item(e):c[e],a(b)}else a(c)});b.type=function(a){return function(){return a&&a!==this}.call(a)?(typeof a).toLowerCase():
{}.toString.call(a).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()};b=window.$_=window.$_||b;b.$=e;if(typeof window.console==="undefined")window.console={log:function(){}};if(typeof String.prototype.trim==="undefined")String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}}})();
typeof document!=="undefined"&&!("classList"in document.createElement("a"))&&function(b){var b=(b.HTMLElement||b.Element).prototype,e=Object,f=String.prototype.trim||function(){return this.replace(/^\s+|\s+$/g,"")},c=Array.prototype.indexOf||function(a){for(var d=0,c=this.length;d<c;d++)if(d in this&&this[d]===a)return d;return-1},a=function(a,d){this.name=a;this.code=DOMException[a];this.message=d},d=function(d,b){if(b==="")throw new a("SYNTAX_ERR","An invalid or illegal string was specified");if(/\s/.test(b))throw new a("INVALID_CHARACTER_ERR",
"String contains an invalid character");return c.call(d,b)},l=function(a){for(var d=f.call(a.className),d=d?d.split(/\s+/):[],c=0,b=d.length;c<b;c++)this.push(d[c]);this._updateClassName=function(){a.className=this.toString()}},g=l.prototype=[],j=function(){return new l(this)};a.prototype=Error.prototype;g.item=function(a){return this[a]||null};g.contains=function(a){a+="";return d(this,a)!==-1};g.add=function(a){a+="";d(this,a)===-1&&(this.push(a),this._updateClassName())};g.remove=function(a){a+=
"";a=d(this,a);a!==-1&&(this.splice(a,1),this._updateClassName())};g.toggle=function(a){a+="";d(this,a)===-1?this.add(a):this.remove(a)};g.toString=function(){return this.join(" ")};if(e.defineProperty){g={get:j,enumerable:true,configurable:true};try{e.defineProperty(b,"classList",g)}catch(h){if(h.number===-2146823252)g.enumerable=false,e.defineProperty(b,"classList",g)}}else e.prototype.__defineGetter__&&b.__defineGetter__("classList",j)}(self);
(function(){function b(c,a,d){var b,e;if(typeof c.hasAttribute!=="undefined")c.hasAttribute(a)&&(b=c.getAttribute(a)),e=true;else if(typeof c[a]!=="undefined")b=c[a],e=false;else if(a==="class"&&typeof c.className!=="undefined")a="className",b=c.className,e=false;if(typeof b==="undefined"&&(typeof d==="undefined"||d===null))console.log(d),console.log(c),console.log("Element does not have the selected attribute");else{if(typeof d==="undefined")return b;typeof d!=="undefined"&&d!==null?e===true?c.setAttribute(a,
d):c[a]=d:d===null&&(e===true?c.removeAttribute(a):delete c[a]);return typeof d!=="undefined"?d:b}}function e(c){return c.replace(/(\-[a-z])/g,function(a){return a.toUpperCase().replace("-","")})}function f(c,a,d){var b,a=e(a);b={outerHeight:"offsetHeight",outerWidth:"offsetWidth",top:"posTop"};if(typeof d==="undefined"&&c.style[a]!=="undefined")return c.style[a];else if(typeof d==="undefined"&&c.style[b[a]]!=="undefined")return c.style[b[a]];typeof c.style[a]!=="undefined"?c.style[a]=d:c.style[b[a]]?
c.style[b[a]]=d:console.log("Property "+a+" nor an equivalent seems to exist")}$_.ext("dom",{addClass:function(c){$_.each(function(a){a.classList.add(c)})},removeClass:function(c){$_.each(function(a){a.classList.remove(c)})},hide:function(){this.css("display","none")},show:function(c){typeof c==="undefined"&&(c="block");this.css("display",c)},attr:function(c,a){var d=this.el;if(d.length>1&&typeof a==="undefined")console.log(d),console.log("Must be a singular element");else if(d.length>1&&typeof a!==
"undefined")$_.each(function(d){return b(d,c,a)});else return b(d,c,a)},text:function(c){var a,d,b;b=this.el;d=typeof b.innerText!=="undefined"?"innerText":typeof b.textContent!=="undefined"?"textContent":"innerHTML";a=b[d];return typeof c!=="undefined"?b[d]=c:a},css:function(b,a){if(typeof a==="undefined")return f(this.el,b);$_.each(function(d){f(d,b,a)})}})})();
(function(){$_.ext("store",{get:function(b){return JSON.parse(localStorage.getItem(b))},set:function(b,e){typeof e!=="string"&&(e=JSON.stringify(e));localStorage.setItem(b,e)},remove:function(b){localStorage.removeItem(b)},getAll:function(){var b,e,f;e=localStorage.length;f={};for(b=0;b<e;b++){var c=localStorage.key(b),a=localStorage.getItem(c);f[c]=a}return f}})})();
(function(){$_.hb=history.pushState?false:true;$_.ext("qs",{parse:function(b){var b=b||$_.hb,e,f,c,a;c={};if(b===true)b=location.hash.split("#!/"),b=b.length>1?b[1]:"";else if(b===false||b===void 0)b=window.location.search.substring(1);else return false;e=b.split("&");f=e.length;for(b=0;b<f;b++){a=e[b].split("=");if(a.length<2)break;c[a[0]]=a[1]}return c},set:function(b,e,f){var f=f||$_.hb,c=this.parse(f);b!==void 0&&e!==void 0&&(c[b]=e);var b=[],a;for(a in c)c.hasOwnProperty(a)&&b.push(a+"="+c[a]);
c=b.join("&");if(f===true)c="!/"+c,location.hash=c;return c},get:function(b,e){var e=e||$_.hb,f=this.parse(e);return f[b]?f[b]:""}})})();var $_=$_||window.$_;
(function(){var b={_do:function(b,f,c,a){typeof c==="undefined"&&(c=function(){});var d=typeof window.XMLHttpRequest!=="undefined"?new XMLHttpRequest:false,a=a?"POST":"GET";b+=a==="GET"?"?"+this._serialize(f):"";d.open(a,b);d.onreadystatechange=function(){d.readyState===4&&c(d.responseText)};a==="POST"?(d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.send(this._serialize(f))):d.send(null)},_serialize:function(b){var f=[],c;for(c in b)if(b.hasOwnProperty(c)&&typeof b[c]!==
"function"){var a=b[c].toString();c=encodeURIComponent(c);a=encodeURIComponent(a);f.push(c+"="+a)}return f.join("&")}};$_.ext("get",function(e,f,c){b._do(e,f,c,false)});$_.ext("post",function(e,f,c){b._do(e,f,c,true)})})();
(function(){$_.ext("util",{object_keys:function(b){var e=[],f;for(f in b)b.hasOwnProperty(f)&&e.push(f);return e},object_values:function(b){var e=[],f;for(f in b)e.push(b[f]);return e},object_merge:function(){},reverse_key_sort:function(b){var e=[],f=0,c={},a,d,e=this.object_keys(b);e.sort(function(a,d){var b=parseFloat(d),c=parseFloat(a),e=b+""===d,f=c+""===a;if(e&&f)return b>c?1:b<c?-1:0;else if(e&&!f)return 1;else if(!e&&f)return-1;return d>a?1:d<a?-1:0});f=e.length;for(a=0;a<f;a++)d=e[a],c[d]=
b[d];return c},str_trans:function(){}})})();
(function(){var b,e,f,c,a;typeof document.addEventListener!=="undefined"?(b=function(a,b,c){typeof a.addEventListener!=="undefined"&&a.addEventListener(b,c,false)},e=function(a,b,c){typeof a.removeEventListener!=="undefined"&&a.removeEventListener(b,c,false)}):typeof document.attachEvent!=="undefined"&&(b=function(a,b,c){var d;function f(){c.apply(arguments)}typeof a.attachEvent!=="undefined"?(e(b,c),a.attachEvent("on"+b,f),d=a.KIS_0_3_0=a.KIS_0_3_0||{},a=d,a.listeners=a.listeners||{},a.listeners[b]=
a.listeners[b]||[],a.listeners[b].push({callback:c,listener:f})):console.log("Failed to attach event:"+b+" on "+a)},e=function(a,b,c){if(typeof a.detachEvent!=="undefined"){var e=a.KIS_0_3_0;if(e&&e.listeners&&e.listeners[b])for(var f=e.listeners[b],k=f.length,i=0;i<k;i++)if(f[i].callback===c){a.detachEvent("on"+b,f[i].listener);f.splice(i,1);f.length===0&&delete e.listeners[b];break}}});f=function(a,c,g,j){var h,k;if(typeof a==="undefined")return console.log(arguments),console.log(c),false;if(c.match(/^([\w\-]+)$/))j===
true?b(a,c,g):e(a,c,g);else{c=c.split(" ");k=c.length;for(h=0;h<k;h++)f(a,c[h],g,j)}};c=function(a,b,c){f(a,c,function(){a=$_.$(a)},true)};a=function(a,b,e){c(document.documentElement,a,b,e)};$_.ext("event",{add:function(a,b){$_.each(function(c){f(c,a,b,true)})},remove:function(a,b){$_.each(function(c){f(c,a,b,false)})},live:function(b,c){$_.each(function(e){a(e,b,c)})},delegate:function(a,b,e){$_.each(function(f){c(f,a,b,e)})}})})();

View File

@ -4,17 +4,18 @@
* Various object and string manipulation functions
*/
(function(){
var u = {
object_keys: function(o)
{
var keys = [],
key;
k;
for(key in o)
for(k in o)
{
if(o.hasOwnProperty(key))
if(o.hasOwnProperty(k))
{
keys.push(key);
keys.push(k);
}
}
@ -31,8 +32,64 @@
}
return vals;
}
},
object_merge: function()
{
},
reverse_key_sort: function(o)
{
//Define some variables
var keys = [],
num_keys = 0,
new_o = {},
i,
k,
x;
//Extract the keys
keys = this.object_keys(o);
//Sort the keys
keys.sort(function (b, a) {
var aFloat = parseFloat(a),
bFloat = parseFloat(b),
aNumeric = aFloat + '' === a,
bNumeric = bFloat + '' === b;
if (aNumeric && bNumeric)
{
return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
}
else if (aNumeric && !bNumeric)
{
return 1;
}
else if (!aNumeric && bNumeric)
{
return -1;
}
return a > b ? 1 : a < b ? -1 : 0;
});
//cache object/array size
num_keys = keys.length;
//Recreate the object/array
for(i=0; i < num_keys; i++)
{
k = keys[i];
new_o[k] = o[k];
}
return new_o;
},
str_trans: function(string, from, to)
{
}
};
//Add it to the $_ object

View File

@ -1,11 +1,13 @@
/**
* QUnit - A JavaScript Unit Testing Framework
* QUnit 1.2.0pre - A JavaScript Unit Testing Framework
*
* http://docs.jquery.com/QUnit
*
* Copyright (c) 2011 John Resig, Jörn Zaefferer
* Dual licensed under the MIT (MIT-LICENSE.txt)
* or GPL (GPL-LICENSE.txt) licenses.
* Pulled Live from Git Fri Oct 21 15:55:01 UTC 2011
* Last Commit: ee156923cdb01820e35e6bb579d5cf6bf55736d4
*/
(function(window) {
@ -21,7 +23,9 @@ var defined = {
})()
};
var testId = 0;
var testId = 0,
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty;
var Test = function(name, testName, expected, testEnvironmentArg, async, callback) {
this.name = name;
@ -48,7 +52,7 @@ Test.prototype = {
setup: function() {
if (this.module != config.previousModule) {
if ( config.previousModule ) {
QUnit.moduleDone( {
runLoggingCallbacks('moduleDone', QUnit, {
name: config.previousModule,
failed: config.moduleStats.bad,
passed: config.moduleStats.all - config.moduleStats.bad,
@ -57,7 +61,7 @@ Test.prototype = {
}
config.previousModule = this.module;
config.moduleStats = { all: 0, bad: 0 };
QUnit.moduleStart( {
runLoggingCallbacks( 'moduleStart', QUnit, {
name: this.module
} );
}
@ -71,9 +75,10 @@ Test.prototype = {
extend(this.testEnvironment, this.testEnvironmentArg);
}
QUnit.testStart( {
name: this.testName
} );
runLoggingCallbacks( 'testStart', QUnit, {
name: this.testName,
module: this.module
});
// allow utility functions to access the current test environment
// TODO why??
@ -90,6 +95,7 @@ Test.prototype = {
}
},
run: function() {
config.current = this;
if ( this.async ) {
QUnit.stop();
}
@ -108,11 +114,12 @@ Test.prototype = {
// Restart the tests if they're blocking
if ( config.blocking ) {
start();
QUnit.start();
}
}
},
teardown: function() {
config.current = this;
try {
this.testEnvironment.teardown.call(this.testEnvironment);
checkPollution();
@ -121,7 +128,8 @@ Test.prototype = {
}
},
finish: function() {
if ( this.expected && this.expected != this.assertions.length ) {
config.current = this;
if ( this.expected != null && this.expected != this.assertions.length ) {
QUnit.ok( false, "Expected " + this.expected + " assertions, but " + this.assertions.length + " were run" );
}
@ -210,8 +218,9 @@ Test.prototype = {
fail("reset() failed, following Test " + this.testName + ", exception and reset fn follows", e, QUnit.reset);
}
QUnit.testDone( {
runLoggingCallbacks( 'testDone', QUnit, {
name: this.testName,
module: this.module,
failed: bad,
passed: this.assertions.length - bad,
total: this.assertions.length
@ -243,7 +252,7 @@ Test.prototype = {
if (bad) {
run();
} else {
synchronize(run);
synchronize(run, true);
};
}
@ -260,7 +269,7 @@ var QUnit = {
asyncTest: function(testName, expected, callback) {
if ( arguments.length === 2 ) {
callback = expected;
expected = 0;
expected = null;
}
QUnit.test(testName, expected, callback, true);
@ -310,8 +319,8 @@ var QUnit = {
result: a,
message: msg
};
msg = escapeHtml(msg);
QUnit.log(details);
msg = escapeInnerText(msg);
runLoggingCallbacks( 'log', QUnit, details );
config.current.assertions.push({
result: a,
message: msg
@ -387,8 +396,8 @@ var QUnit = {
QUnit.ok(ok, message);
},
start: function() {
config.semaphore--;
start: function(count) {
config.semaphore -= count || 1;
if (config.semaphore > 0) {
// don't start until equal number of stop-calls
return;
@ -408,28 +417,38 @@ var QUnit = {
}
config.blocking = false;
process();
process(true);
}, 13);
} else {
config.blocking = false;
process();
process(true);
}
},
stop: function(timeout) {
config.semaphore++;
stop: function(count) {
config.semaphore += count || 1;
config.blocking = true;
if ( timeout && defined.setTimeout ) {
if ( config.testTimeout && defined.setTimeout ) {
clearTimeout(config.timeout);
config.timeout = window.setTimeout(function() {
QUnit.ok( false, "Test timed out" );
config.semaphore = 1;
QUnit.start();
}, timeout);
}, config.testTimeout);
}
}
};
//We want access to the constructor's prototype
(function() {
function F(){};
F.prototype = QUnit;
QUnit = new F();
//Make F QUnit's constructor so that we can add to the prototype later
QUnit.constructor = F;
})();
// Backwards compatibility, deprecated
QUnit.equals = QUnit.equal;
QUnit.same = QUnit.deepEqual;
@ -453,7 +472,16 @@ var config = {
// by default, modify document.title when suite is done
altertitle: true,
urlConfig: ['noglobals', 'notrycatch']
urlConfig: ['noglobals', 'notrycatch'],
//logging callback queues
begin: [],
done: [],
log: [],
testStart: [],
testDone: [],
moduleStart: [],
moduleDone: []
};
// Load paramaters
@ -586,8 +614,7 @@ extend(QUnit, {
return "null";
}
var type = Object.prototype.toString.call( obj )
.match(/^\[object\s(.*)\]$/)[1] || '';
var type = toString.call( obj ).match(/^\[object\s(.*)\]$/)[1] || '';
switch (type) {
case 'Number':
@ -618,10 +645,10 @@ extend(QUnit, {
expected: expected
};
message = escapeHtml(message) || (result ? "okay" : "failed");
message = escapeInnerText(message) || (result ? "okay" : "failed");
message = '<span class="test-message">' + message + "</span>";
expected = escapeHtml(QUnit.jsDump.parse(expected));
actual = escapeHtml(QUnit.jsDump.parse(actual));
expected = escapeInnerText(QUnit.jsDump.parse(expected));
actual = escapeInnerText(QUnit.jsDump.parse(actual));
var output = message + '<table><tr class="test-expected"><th>Expected: </th><td><pre>' + expected + '</pre></td></tr>';
if (actual != expected) {
output += '<tr class="test-actual"><th>Result: </th><td><pre>' + actual + '</pre></td></tr>';
@ -631,12 +658,12 @@ extend(QUnit, {
var source = sourceFromStacktrace();
if (source) {
details.source = source;
output += '<tr class="test-source"><th>Source: </th><td><pre>' + escapeHtml(source) + '</pre></td></tr>';
output += '<tr class="test-source"><th>Source: </th><td><pre>' + escapeInnerText(source) + '</pre></td></tr>';
}
}
output += "</table>";
QUnit.log(details);
runLoggingCallbacks( 'log', QUnit, details );
config.current.assertions.push({
result: !!result,
@ -649,6 +676,9 @@ extend(QUnit, {
var querystring = "?",
key;
for ( key in params ) {
if ( !hasOwn.call( params, key ) ) {
continue;
}
querystring += encodeURIComponent( key ) + "=" +
encodeURIComponent( params[ key ] ) + "&";
}
@ -657,23 +687,28 @@ extend(QUnit, {
extend: extend,
id: id,
addEvent: addEvent,
addEvent: addEvent
});
//QUnit.constructor is set to the empty F() above so that we can add to it's prototype later
//Doing this allows us to tell if the following methods have been overwritten on the actual
//QUnit object, which is a deprecated way of using the callbacks.
extend(QUnit.constructor.prototype, {
// Logging callbacks; all receive a single argument with the listed properties
// run test/logs.html for any related changes
begin: function() {},
begin: registerLoggingCallback('begin'),
// done: { failed, passed, total, runtime }
done: function() {},
done: registerLoggingCallback('done'),
// log: { result, actual, expected, message }
log: function() {},
log: registerLoggingCallback('log'),
// testStart: { name }
testStart: function() {},
testStart: registerLoggingCallback('testStart'),
// testDone: { name, failed, passed, total }
testDone: function() {},
testDone: registerLoggingCallback('testDone'),
// moduleStart: { name }
moduleStart: function() {},
moduleStart: registerLoggingCallback('moduleStart'),
// moduleDone: { name, failed, passed, total }
moduleDone: function() {}
moduleDone: registerLoggingCallback('moduleDone')
});
if ( typeof document === "undefined" || document.readyState === "complete" ) {
@ -681,7 +716,7 @@ if ( typeof document === "undefined" || document.readyState === "complete" ) {
}
QUnit.load = function() {
QUnit.begin({});
runLoggingCallbacks( 'begin', QUnit, {} );
// Initialize the config, saving the execution queue
var oldconfig = extend({}, config);
@ -756,12 +791,23 @@ QUnit.load = function() {
addEvent(window, "load", QUnit.load);
// addEvent(window, "error") gives us a useless event object
window.onerror = function( message, file, line ) {
if ( QUnit.config.current ) {
ok( false, message + ", " + file + ":" + line );
} else {
test( "global failure", function() {
ok( false, message + ", " + file + ":" + line );
});
}
};
function done() {
config.autorun = true;
// Log the last module results
if ( config.currentModule ) {
QUnit.moduleDone( {
runLoggingCallbacks( 'moduleDone', QUnit, {
name: config.currentModule,
failed: config.moduleStats.bad,
passed: config.moduleStats.all - config.moduleStats.bad,
@ -803,7 +849,7 @@ function done() {
].join(" ");
}
QUnit.done( {
runLoggingCallbacks( 'done', QUnit, {
failed: config.stats.bad,
passed: passed,
total: config.stats.all,
@ -855,16 +901,14 @@ function sourceFromStacktrace() {
}
}
function escapeHtml(s) {
function escapeInnerText(s) {
if (!s) {
return "";
}
s = s + "";
return s.replace(/[\&"<>\\]/g, function(s) {
return s.replace(/[\&<>]/g, function(s) {
switch(s) {
case "&": return "&amp;";
case "\\": return "\\\\";
case '"': return '\"';
case "<": return "&lt;";
case ">": return "&gt;";
default: return s;
@ -872,26 +916,30 @@ function escapeHtml(s) {
});
}
function synchronize( callback ) {
function synchronize( callback, last ) {
config.queue.push( callback );
if ( config.autorun && !config.blocking ) {
process();
process(last);
}
}
function process() {
var start = (new Date()).getTime();
function process( last ) {
var start = new Date().getTime();
config.depth = config.depth ? config.depth + 1 : 1;
while ( config.queue.length && !config.blocking ) {
if ( config.updateRate <= 0 || (((new Date()).getTime() - start) < config.updateRate) ) {
if ( !defined.setTimeout || config.updateRate <= 0 || ( ( new Date().getTime() - start ) < config.updateRate ) ) {
config.queue.shift()();
} else {
window.setTimeout( process, 13 );
window.setTimeout( function(){
process( last );
}, 13 );
break;
}
}
if (!config.blocking && !config.queue.length) {
config.depth--;
if ( last && !config.blocking && !config.queue.length && config.depth === 0 ) {
done();
}
}
@ -901,6 +949,9 @@ function saveGlobal() {
if ( config.noglobals ) {
for ( var key in window ) {
if ( !hasOwn.call( window, key ) ) {
continue;
}
config.pollution.push( key );
}
}
@ -951,7 +1002,9 @@ function extend(a, b) {
for ( var prop in b ) {
if ( b[prop] === undefined ) {
delete a[prop];
} else {
// Avoid "Member not found" error in IE8 caused by setting window.constructor
} else if ( prop !== "constructor" || a !== window ) {
a[prop] = b[prop];
}
}
@ -974,9 +1027,27 @@ function id(name) {
document.getElementById( name );
}
function registerLoggingCallback(key){
return function(callback){
config[key].push( callback );
};
}
// Supports deprecated method of completely overwriting logging callbacks
function runLoggingCallbacks(key, scope, args) {
//debugger;
var callbacks;
if ( QUnit.hasOwnProperty(key) ) {
QUnit[key].call(scope, args);
} else {
callbacks = config[key];
for( var i = 0; i < callbacks.length; i++ ) {
callbacks[i].call( scope, args );
}
}
}
// Test for equality any JavaScript type.
// Discussions and reference: http://philrathe.com/articles/equiv
// Test suites: http://philrathe.com/tests/equiv
// Author: Philippe Rathé <prathe@gmail.com>
QUnit.equiv = function () {
@ -1226,7 +1297,12 @@ QUnit.jsDump = (function() {
type = "document";
} else if (obj.nodeType) {
type = "node";
} else if (typeof obj === "object" && typeof obj.length === "number" && obj.length >= 0) {
} else if (
// native arrays
toString.call( obj ) === "[object Array]" ||
// NodeList objects
( typeof obj.length === "number" && typeof obj.item !== "undefined" && ( obj.length ? obj.item(0) === obj[0] : ( obj.item( 0 ) === null && typeof obj[0] === "undefined" ) ) )
) {
type = "array";
} else {
type = typeof obj;
@ -1408,6 +1484,9 @@ QUnit.diff = (function() {
}
for (var i in ns) {
if ( !hasOwn.call( ns, i ) ) {
continue;
}
if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) {
n[ns[i].rows[0]] = {
text: n[ns[i].rows[0]],
@ -1507,4 +1586,4 @@ QUnit.diff = (function() {
};
})();
})(this);
})(this);

View File

@ -176,22 +176,68 @@
equal(ele.style.display, "block", "Setting CSS");
equal($test.dom.css("display"), "block", "Getting CSS");
});
/*test("Children", function(){
var $test = $_("section");
var ele = $("section");
var ele2 = $_("section aside").el;
equal($_("section").dom.children().el, ele.children, "Returns children without parameters");
equal($_("section").dom.children('#r14').el, document.getElementById('r14'), "Finds id");
equal($_("section").dom.children("aside").el, ele2, "Finds children by tag name");
equal($_("section aside").dom.children(".child").el, $_("#classChild .child").el, "Finds children by class");
});*/
// --------------------------------------------------------------------------
module("util");
test("Object keys", function(){
expect(1);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6
};
var test_keys = ["x", "a", "q", "r"];
deepEqual($_.util.object_keys(test_o), test_keys, "Retrieves object keys correctly");
});
test("Object values", function(){
expect(1);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6,
"p": "q"
};
var test_values = [2,4,3,6,"q"];
deepEqual($_.util.object_values(test_o), test_values, "Retrieves object values correctly");
});
test("Reverse Key Sort", function(){
expect(2);
var test_o = {
"x": 2,
"a": 4,
"q": 3,
"r": 6
};
var test_sorted = {
"x": 2,
"r": 6,
"q": 3,
"a": 4
};
var test_array = [7, 2, 6, 3];
var test_array_sorted = [3, 6, 2, 7];
deepEqual($_.util.reverse_key_sort(test_o), test_sorted, "Object sort");
deepEqual($_.util.object_values($_.util.reverse_key_sort(test_array)), test_array_sorted, "Array Sort");
});
}());