1 | /* |
---|
2 | * QUnit - jQuery unit testrunner |
---|
3 | * |
---|
4 | * http://docs.jquery.com/QUnit |
---|
5 | * |
---|
6 | * Copyright (c) 2008 John Resig, Jörn Zaefferer |
---|
7 | * Dual licensed under the MIT (MIT-LICENSE.txt) |
---|
8 | * and GPL (GPL-LICENSE.txt) licenses. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | (function($) { |
---|
14 | |
---|
15 | // Tests for equality any JavaScript type and structure without unexpected results. |
---|
16 | // Discussions and reference: http://philrathe.com/articles/equiv |
---|
17 | // Test suites: http://philrathe.com/tests/equiv |
---|
18 | // Author: Philippe Rathé <prathe@gmail.com> |
---|
19 | var equiv = function () { |
---|
20 | |
---|
21 | var innerEquiv; // the real equiv function |
---|
22 | var callers = []; // stack to decide between skip/abort functions |
---|
23 | |
---|
24 | // Determine what is o. |
---|
25 | function hoozit(o) { |
---|
26 | if (typeof o === "string") { |
---|
27 | return "string"; |
---|
28 | |
---|
29 | } else if (typeof o === "boolean") { |
---|
30 | return "boolean"; |
---|
31 | |
---|
32 | } else if (typeof o === "number") { |
---|
33 | |
---|
34 | if (isNaN(o)) { |
---|
35 | return "nan"; |
---|
36 | } else { |
---|
37 | return "number"; |
---|
38 | } |
---|
39 | |
---|
40 | } else if (typeof o === "undefined") { |
---|
41 | return "undefined"; |
---|
42 | |
---|
43 | // consider: typeof null === object |
---|
44 | } else if (o === null) { |
---|
45 | return "null"; |
---|
46 | |
---|
47 | // consider: typeof [] === object |
---|
48 | } else if (o instanceof Array) { |
---|
49 | return "array"; |
---|
50 | |
---|
51 | // consider: typeof new Date() === object |
---|
52 | } else if (o instanceof Date) { |
---|
53 | return "date"; |
---|
54 | |
---|
55 | // consider: /./ instanceof Object; |
---|
56 | // /./ instanceof RegExp; |
---|
57 | // typeof /./ === "function"; // => false in IE and Opera, |
---|
58 | // true in FF and Safari |
---|
59 | } else if (o instanceof RegExp) { |
---|
60 | return "regexp"; |
---|
61 | |
---|
62 | } else if (typeof o === "object") { |
---|
63 | return "object"; |
---|
64 | |
---|
65 | } else if (o instanceof Function) { |
---|
66 | return "function"; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | // Call the o related callback with the given arguments. |
---|
71 | function bindCallbacks(o, callbacks, args) { |
---|
72 | var prop = hoozit(o); |
---|
73 | if (prop) { |
---|
74 | if (hoozit(callbacks[prop]) === "function") { |
---|
75 | return callbacks[prop].apply(callbacks, args); |
---|
76 | } else { |
---|
77 | return callbacks[prop]; // or undefined |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | var callbacks = function () { |
---|
83 | |
---|
84 | // for string, boolean, number and null |
---|
85 | function useStrictEquality(b, a) { |
---|
86 | return a === b; |
---|
87 | } |
---|
88 | |
---|
89 | return { |
---|
90 | "string": useStrictEquality, |
---|
91 | "boolean": useStrictEquality, |
---|
92 | "number": useStrictEquality, |
---|
93 | "null": useStrictEquality, |
---|
94 | "undefined": useStrictEquality, |
---|
95 | |
---|
96 | "nan": function (b) { |
---|
97 | return isNaN(b); |
---|
98 | }, |
---|
99 | |
---|
100 | "date": function (b, a) { |
---|
101 | return hoozit(b) === "date" && a.valueOf() === b.valueOf(); |
---|
102 | }, |
---|
103 | |
---|
104 | "regexp": function (b, a) { |
---|
105 | return hoozit(b) === "regexp" && |
---|
106 | a.source === b.source && // the regex itself |
---|
107 | a.global === b.global && // and its modifers (gmi) ... |
---|
108 | a.ignoreCase === b.ignoreCase && |
---|
109 | a.multiline === b.multiline; |
---|
110 | }, |
---|
111 | |
---|
112 | // - skip when the property is a method of an instance (OOP) |
---|
113 | // - abort otherwise, |
---|
114 | // initial === would have catch identical references anyway |
---|
115 | "function": function () { |
---|
116 | var caller = callers[callers.length - 1]; |
---|
117 | return caller !== Object && |
---|
118 | typeof caller !== "undefined"; |
---|
119 | }, |
---|
120 | |
---|
121 | "array": function (b, a) { |
---|
122 | var i; |
---|
123 | var len; |
---|
124 | |
---|
125 | // b could be an object literal here |
---|
126 | if ( ! (hoozit(b) === "array")) { |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | len = a.length; |
---|
131 | if (len !== b.length) { // safe and faster |
---|
132 | return false; |
---|
133 | } |
---|
134 | for (i = 0; i < len; i++) { |
---|
135 | if( ! innerEquiv(a[i], b[i])) { |
---|
136 | return false; |
---|
137 | } |
---|
138 | } |
---|
139 | return true; |
---|
140 | }, |
---|
141 | |
---|
142 | "object": function (b, a) { |
---|
143 | var i; |
---|
144 | var eq = true; // unless we can proove it |
---|
145 | var aProperties = [], bProperties = []; // collection of strings |
---|
146 | |
---|
147 | // comparing constructors is more strict than using instanceof |
---|
148 | if ( a.constructor !== b.constructor) { |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | // stack constructor before traversing properties |
---|
153 | callers.push(a.constructor); |
---|
154 | |
---|
155 | for (i in a) { // be strict: don't ensures hasOwnProperty and go deep |
---|
156 | |
---|
157 | aProperties.push(i); // collect a's properties |
---|
158 | |
---|
159 | if ( ! innerEquiv(a[i], b[i])) { |
---|
160 | eq = false; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | callers.pop(); // unstack, we are done |
---|
165 | |
---|
166 | for (i in b) { |
---|
167 | bProperties.push(i); // collect b's properties |
---|
168 | } |
---|
169 | |
---|
170 | // Ensures identical properties name |
---|
171 | return eq && innerEquiv(aProperties.sort(), bProperties.sort()); |
---|
172 | } |
---|
173 | }; |
---|
174 | }(); |
---|
175 | |
---|
176 | innerEquiv = function () { // can take multiple arguments |
---|
177 | var args = Array.prototype.slice.apply(arguments); |
---|
178 | if (args.length < 2) { |
---|
179 | return true; // end transition |
---|
180 | } |
---|
181 | |
---|
182 | return (function (a, b) { |
---|
183 | if (a === b) { |
---|
184 | return true; // catch the most you can |
---|
185 | |
---|
186 | } else if (typeof a !== typeof b || a === null || b === null || typeof a === "undefined" || typeof b === "undefined") { |
---|
187 | return false; // don't lose time with error prone cases |
---|
188 | |
---|
189 | } else { |
---|
190 | return bindCallbacks(a, callbacks, [b, a]); |
---|
191 | } |
---|
192 | |
---|
193 | // apply transition with (1..n) arguments |
---|
194 | })(args[0], args[1]) && arguments.callee.apply(this, args.splice(1, args.length -1)); |
---|
195 | }; |
---|
196 | |
---|
197 | return innerEquiv; |
---|
198 | }(); // equiv |
---|
199 | |
---|
200 | var config = { |
---|
201 | stats: { |
---|
202 | all: 0, |
---|
203 | bad: 0 |
---|
204 | }, |
---|
205 | queue: [], |
---|
206 | // block until document ready |
---|
207 | blocking: true, |
---|
208 | //restrict modules/tests by get parameters |
---|
209 | filters: location.search.length > 1 && $.map( location.search.slice(1).split('&'), decodeURIComponent ), |
---|
210 | isLocal: !!(window.location.protocol == 'file:') |
---|
211 | }; |
---|
212 | |
---|
213 | // public API as global methods |
---|
214 | $.extend(window, { |
---|
215 | test: test, |
---|
216 | module: module, |
---|
217 | expect: expect, |
---|
218 | ok: ok, |
---|
219 | equals: equals, |
---|
220 | start: start, |
---|
221 | stop: stop, |
---|
222 | reset: reset, |
---|
223 | isLocal: config.isLocal, |
---|
224 | same: function(a, b, message) { |
---|
225 | push(equiv(a, b), a, b, message); |
---|
226 | }, |
---|
227 | QUnit: { |
---|
228 | equiv: equiv |
---|
229 | }, |
---|
230 | // legacy methods below |
---|
231 | isSet: isSet, |
---|
232 | isObj: isObj, |
---|
233 | compare: function() { |
---|
234 | throw "compare is deprecated - use same() instead"; |
---|
235 | }, |
---|
236 | compare2: function() { |
---|
237 | throw "compare2 is deprecated - use same() instead"; |
---|
238 | }, |
---|
239 | serialArray: function() { |
---|
240 | throw "serialArray is deprecated - use jsDump.parse() instead"; |
---|
241 | }, |
---|
242 | q: q, |
---|
243 | t: t, |
---|
244 | url: url, |
---|
245 | triggerEvent: triggerEvent |
---|
246 | }); |
---|
247 | |
---|
248 | $(window).load(function() { |
---|
249 | $('#userAgent').html(navigator.userAgent); |
---|
250 | var head = $('<div class="testrunner-toolbar"><label for="filter">Hide passed tests</label></div>').insertAfter("#userAgent"); |
---|
251 | $('<input type="checkbox" id="filter" />').attr("disabled", true).prependTo(head).click(function() { |
---|
252 | $('li.pass')[this.checked ? 'hide' : 'show'](); |
---|
253 | }); |
---|
254 | runTest(); |
---|
255 | }); |
---|
256 | |
---|
257 | function synchronize(callback) { |
---|
258 | config.queue.push(callback); |
---|
259 | if(!config.blocking) { |
---|
260 | process(); |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | function process() { |
---|
265 | while(config.queue.length && !config.blocking) { |
---|
266 | config.queue.shift()(); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | function stop(timeout) { |
---|
271 | config.blocking = true; |
---|
272 | if (timeout) |
---|
273 | config.timeout = setTimeout(function() { |
---|
274 | ok( false, "Test timed out" ); |
---|
275 | start(); |
---|
276 | }, timeout); |
---|
277 | } |
---|
278 | function start() { |
---|
279 | // A slight delay, to avoid any current callbacks |
---|
280 | setTimeout(function() { |
---|
281 | if(config.timeout) |
---|
282 | clearTimeout(config.timeout); |
---|
283 | config.blocking = false; |
---|
284 | process(); |
---|
285 | }, 13); |
---|
286 | } |
---|
287 | |
---|
288 | function validTest( name ) { |
---|
289 | var filters = config.filters; |
---|
290 | if( !filters ) |
---|
291 | return true; |
---|
292 | |
---|
293 | var i = filters.length, |
---|
294 | run = false; |
---|
295 | while( i-- ){ |
---|
296 | var filter = filters[i], |
---|
297 | not = filter.charAt(0) == '!'; |
---|
298 | if( not ) |
---|
299 | filter = filter.slice(1); |
---|
300 | if( name.indexOf(filter) != -1 ) |
---|
301 | return !not; |
---|
302 | if( not ) |
---|
303 | run = true; |
---|
304 | } |
---|
305 | return run; |
---|
306 | } |
---|
307 | |
---|
308 | function runTest() { |
---|
309 | config.blocking = false; |
---|
310 | var started = +new Date; |
---|
311 | config.fixture = document.getElementById('main').innerHTML; |
---|
312 | config.ajaxSettings = $.ajaxSettings; |
---|
313 | synchronize(function() { |
---|
314 | $('<p id="testresult" class="result">').html(['Tests completed in ', |
---|
315 | +new Date - started, ' milliseconds.<br/>', |
---|
316 | '<span class="bad">', config.stats.bad, '</span> tests of <span class="all">', config.stats.all, '</span> failed.</p>'] |
---|
317 | .join('')) |
---|
318 | .appendTo("body"); |
---|
319 | $("#banner").addClass(config.stats.bad ? "fail" : "pass"); |
---|
320 | }); |
---|
321 | } |
---|
322 | |
---|
323 | function test(name, callback) { |
---|
324 | if(config.currentModule) |
---|
325 | name = config.currentModule + " module: " + name; |
---|
326 | var lifecycle = $.extend({ |
---|
327 | setup: function() {}, |
---|
328 | teardown: function() {} |
---|
329 | }, config.moduleLifecycle); |
---|
330 | |
---|
331 | if ( !validTest(name) ) |
---|
332 | return; |
---|
333 | |
---|
334 | synchronize(function() { |
---|
335 | config.assertions = []; |
---|
336 | config.expected = null; |
---|
337 | try { |
---|
338 | lifecycle.setup(); |
---|
339 | callback(); |
---|
340 | lifecycle.teardown(); |
---|
341 | } catch(e) { |
---|
342 | if( typeof console != "undefined" && console.error && console.warn ) { |
---|
343 | console.error("Test " + name + " died, exception and test follows"); |
---|
344 | console.error(e); |
---|
345 | console.warn(callback.toString()); |
---|
346 | } |
---|
347 | config.assertions.push( { |
---|
348 | result: false, |
---|
349 | message: "Died on test #" + (config.assertions.length + 1) + ": " + e.message |
---|
350 | }); |
---|
351 | } |
---|
352 | }); |
---|
353 | synchronize(function() { |
---|
354 | try { |
---|
355 | reset(); |
---|
356 | } catch(e) { |
---|
357 | if( typeof console != "undefined" && console.error && console.warn ) { |
---|
358 | console.error("reset() failed, following Test " + name + ", exception and reset fn follows"); |
---|
359 | console.error(e); |
---|
360 | console.warn(reset.toString()); |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | if(config.expected && config.expected != config.assertions.length) { |
---|
365 | config.assertions.push({ |
---|
366 | result: false, |
---|
367 | message: "Expected " + config.expected + " assertions, but " + config.assertions.length + " were run" |
---|
368 | }); |
---|
369 | } |
---|
370 | |
---|
371 | var good = 0, bad = 0; |
---|
372 | var ol = $("<ol/>").hide(); |
---|
373 | config.stats.all += config.assertions.length; |
---|
374 | for ( var i = 0; i < config.assertions.length; i++ ) { |
---|
375 | var assertion = config.assertions[i]; |
---|
376 | $("<li/>").addClass(assertion.result ? "pass" : "fail").text(assertion.message || "(no message)").appendTo(ol); |
---|
377 | assertion.result ? good++ : bad++; |
---|
378 | } |
---|
379 | config.stats.bad += bad; |
---|
380 | |
---|
381 | var b = $("<strong/>").html(name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + config.assertions.length + ")</b>") |
---|
382 | .click(function(){ |
---|
383 | $(this).next().toggle(); |
---|
384 | }) |
---|
385 | .dblclick(function(event) { |
---|
386 | var target = $(event.target).filter("strong").clone(); |
---|
387 | if ( target.length ) { |
---|
388 | target.children().remove(); |
---|
389 | location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text())); |
---|
390 | } |
---|
391 | }); |
---|
392 | |
---|
393 | $("<li/>").addClass(bad ? "fail" : "pass").append(b).append(ol).appendTo("#tests"); |
---|
394 | |
---|
395 | if(bad) { |
---|
396 | $("#filter").attr("disabled", null); |
---|
397 | } |
---|
398 | }); |
---|
399 | } |
---|
400 | |
---|
401 | // call on start of module test to prepend name to all tests |
---|
402 | function module(name, lifecycle) { |
---|
403 | config.currentModule = name; |
---|
404 | config.moduleLifecycle = lifecycle; |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. |
---|
409 | */ |
---|
410 | function expect(asserts) { |
---|
411 | config.expected = asserts; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * Resets the test setup. Useful for tests that modify the DOM. |
---|
416 | */ |
---|
417 | function reset() { |
---|
418 | $("#main").html( config.fixture ); |
---|
419 | $.event.global = {}; |
---|
420 | $.ajaxSettings = $.extend({}, config.ajaxSettings); |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * Asserts true. |
---|
425 | * @example ok( $("a").size() > 5, "There must be at least 5 anchors" ); |
---|
426 | */ |
---|
427 | function ok(a, msg) { |
---|
428 | config.assertions.push({ |
---|
429 | result: !!a, |
---|
430 | message: msg |
---|
431 | }); |
---|
432 | } |
---|
433 | |
---|
434 | /** |
---|
435 | * Asserts that two arrays are the same |
---|
436 | */ |
---|
437 | function isSet(a, b, msg) { |
---|
438 | function serialArray( a ) { |
---|
439 | var r = []; |
---|
440 | |
---|
441 | if ( a && a.length ) |
---|
442 | for ( var i = 0; i < a.length; i++ ) { |
---|
443 | var str = a[i].nodeName; |
---|
444 | if ( str ) { |
---|
445 | str = str.toLowerCase(); |
---|
446 | if ( a[i].id ) |
---|
447 | str += "#" + a[i].id; |
---|
448 | } else |
---|
449 | str = a[i]; |
---|
450 | r.push( str ); |
---|
451 | } |
---|
452 | |
---|
453 | return "[ " + r.join(", ") + " ]"; |
---|
454 | } |
---|
455 | var ret = true; |
---|
456 | if ( a && b && a.length != undefined && a.length == b.length ) { |
---|
457 | for ( var i = 0; i < a.length; i++ ) |
---|
458 | if ( a[i] != b[i] ) |
---|
459 | ret = false; |
---|
460 | } else |
---|
461 | ret = false; |
---|
462 | config.assertions.push({ |
---|
463 | result: ret, |
---|
464 | message: !ret ? (msg + " expected: " + serialArray(b) + " result: " + serialArray(a)) : msg |
---|
465 | }); |
---|
466 | } |
---|
467 | |
---|
468 | /** |
---|
469 | * Asserts that two objects are equivalent |
---|
470 | */ |
---|
471 | function isObj(a, b, msg) { |
---|
472 | var ret = true; |
---|
473 | |
---|
474 | if ( a && b ) { |
---|
475 | for ( var i in a ) |
---|
476 | if ( a[i] != b[i] ) |
---|
477 | ret = false; |
---|
478 | |
---|
479 | for ( i in b ) |
---|
480 | if ( a[i] != b[i] ) |
---|
481 | ret = false; |
---|
482 | } else |
---|
483 | ret = false; |
---|
484 | |
---|
485 | config.assertions.push({ |
---|
486 | result: ret, |
---|
487 | message: msg |
---|
488 | }); |
---|
489 | } |
---|
490 | |
---|
491 | /** |
---|
492 | * Returns an array of elements with the given IDs, eg. |
---|
493 | * @example q("main", "foo", "bar") |
---|
494 | * @result [<div id="main">, <span id="foo">, <input id="bar">] |
---|
495 | */ |
---|
496 | function q() { |
---|
497 | var r = []; |
---|
498 | for ( var i = 0; i < arguments.length; i++ ) |
---|
499 | r.push( document.getElementById( arguments[i] ) ); |
---|
500 | return r; |
---|
501 | } |
---|
502 | |
---|
503 | /** |
---|
504 | * Asserts that a select matches the given IDs |
---|
505 | * @example t("Check for something", "//[a]", ["foo", "baar"]); |
---|
506 | * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar' |
---|
507 | */ |
---|
508 | function t(a,b,c) { |
---|
509 | var f = $(b); |
---|
510 | var s = ""; |
---|
511 | for ( var i = 0; i < f.length; i++ ) |
---|
512 | s += (s && ",") + '"' + f[i].id + '"'; |
---|
513 | isSet(f, q.apply(q,c), a + " (" + b + ")"); |
---|
514 | } |
---|
515 | |
---|
516 | /** |
---|
517 | * Add random number to url to stop IE from caching |
---|
518 | * |
---|
519 | * @example url("data/test.html") |
---|
520 | * @result "data/test.html?10538358428943" |
---|
521 | * |
---|
522 | * @example url("data/test.php?foo=bar") |
---|
523 | * @result "data/test.php?foo=bar&10538358345554" |
---|
524 | */ |
---|
525 | function url(value) { |
---|
526 | return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000); |
---|
527 | } |
---|
528 | |
---|
529 | /** |
---|
530 | * Checks that the first two arguments are equal, with an optional message. |
---|
531 | * Prints out both actual and expected values. |
---|
532 | * |
---|
533 | * Prefered to ok( actual == expected, message ) |
---|
534 | * |
---|
535 | * @example equals( $.format("Received {0} bytes.", 2), "Received 2 bytes." ); |
---|
536 | * |
---|
537 | * @param Object actual |
---|
538 | * @param Object expected |
---|
539 | * @param String message (optional) |
---|
540 | */ |
---|
541 | function equals(actual, expected, message) { |
---|
542 | push(expected == actual, actual, expected, message); |
---|
543 | } |
---|
544 | |
---|
545 | function push(result, actual, expected, message) { |
---|
546 | message = message || (result ? "okay" : "failed"); |
---|
547 | config.assertions.push({ |
---|
548 | result: result, |
---|
549 | message: result ? message + ": " + expected : message + ", expected: " + jsDump.parse(expected) + " result: " + jsDump.parse(actual) |
---|
550 | }); |
---|
551 | } |
---|
552 | |
---|
553 | /** |
---|
554 | * Trigger an event on an element. |
---|
555 | * |
---|
556 | * @example triggerEvent( document.body, "click" ); |
---|
557 | * |
---|
558 | * @param DOMElement elem |
---|
559 | * @param String type |
---|
560 | */ |
---|
561 | function triggerEvent( elem, type, event ) { |
---|
562 | if ( $.browser.mozilla || $.browser.opera ) { |
---|
563 | event = document.createEvent("MouseEvents"); |
---|
564 | event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView, |
---|
565 | 0, 0, 0, 0, 0, false, false, false, false, 0, null); |
---|
566 | elem.dispatchEvent( event ); |
---|
567 | } else if ( $.browser.msie ) { |
---|
568 | elem.fireEvent("on"+type); |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | })(jQuery); |
---|
573 | |
---|
574 | /** |
---|
575 | * jsDump |
---|
576 | * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com |
---|
577 | * Licensed under BSD (http://www.opensource.org/licenses/bsd-license.php) |
---|
578 | * Date: 5/15/2008 |
---|
579 | * @projectDescription Advanced and extensible data dumping for Javascript. |
---|
580 | * @version 1.0.0 |
---|
581 | * @author Ariel Flesler |
---|
582 | * @link {http://flesler.blogspot.com/2008/05/jsdump-pretty-dump-of-any-javascript.html} |
---|
583 | */ |
---|
584 | (function(){ |
---|
585 | function quote( str ){ |
---|
586 | return '"' + str.toString().replace(/"/g, '\\"') + '"'; |
---|
587 | }; |
---|
588 | function literal( o ){ |
---|
589 | return o + ''; |
---|
590 | }; |
---|
591 | function join( pre, arr, post ){ |
---|
592 | var s = jsDump.separator(), |
---|
593 | base = jsDump.indent(); |
---|
594 | inner = jsDump.indent(1); |
---|
595 | if( arr.join ) |
---|
596 | arr = arr.join( ',' + s + inner ); |
---|
597 | if( !arr ) |
---|
598 | return pre + post; |
---|
599 | return [ pre, inner + arr, base + post ].join(s); |
---|
600 | }; |
---|
601 | function array( arr ){ |
---|
602 | var i = arr.length, ret = Array(i); |
---|
603 | this.up(); |
---|
604 | while( i-- ) |
---|
605 | ret[i] = this.parse( arr[i] ); |
---|
606 | this.down(); |
---|
607 | return join( '[', ret, ']' ); |
---|
608 | }; |
---|
609 | |
---|
610 | var reName = /^function (\w+)/; |
---|
611 | |
---|
612 | var jsDump = window.jsDump = { |
---|
613 | parse:function( obj, type ){//type is used mostly internally, you can fix a (custom)type in advance |
---|
614 | var parser = this.parsers[ type || this.typeOf(obj) ]; |
---|
615 | type = typeof parser; |
---|
616 | |
---|
617 | return type == 'function' ? parser.call( this, obj ) : |
---|
618 | type == 'string' ? parser : |
---|
619 | this.parsers.error; |
---|
620 | }, |
---|
621 | typeOf:function( obj ){ |
---|
622 | var type = typeof obj, |
---|
623 | f = 'function';//we'll use it 3 times, save it |
---|
624 | return type != 'object' && type != f ? type : |
---|
625 | !obj ? 'null' : |
---|
626 | obj.exec ? 'regexp' :// some browsers (FF) consider regexps functions |
---|
627 | obj.getHours ? 'date' : |
---|
628 | obj.scrollBy ? 'window' : |
---|
629 | obj.nodeName == '#document' ? 'document' : |
---|
630 | obj.nodeName ? 'node' : |
---|
631 | obj.item ? 'nodelist' : // Safari reports nodelists as functions |
---|
632 | obj.callee ? 'arguments' : |
---|
633 | obj.call || obj.constructor != Array && //an array would also fall on this hack |
---|
634 | (obj+'').indexOf(f) != -1 ? f : //IE reports functions like alert, as objects |
---|
635 | 'length' in obj ? 'array' : |
---|
636 | type; |
---|
637 | }, |
---|
638 | separator:function(){ |
---|
639 | return this.multiline ? this.HTML ? '<br />' : '\n' : this.HTML ? ' ' : ' '; |
---|
640 | }, |
---|
641 | indent:function( extra ){// extra can be a number, shortcut for increasing-calling-decreasing |
---|
642 | if( !this.multiline ) |
---|
643 | return ''; |
---|
644 | var chr = this.indentChar; |
---|
645 | if( this.HTML ) |
---|
646 | chr = chr.replace(/\t/g,' ').replace(/ /g,' '); |
---|
647 | return Array( this._depth_ + (extra||0) ).join(chr); |
---|
648 | }, |
---|
649 | up:function( a ){ |
---|
650 | this._depth_ += a || 1; |
---|
651 | }, |
---|
652 | down:function( a ){ |
---|
653 | this._depth_ -= a || 1; |
---|
654 | }, |
---|
655 | setParser:function( name, parser ){ |
---|
656 | this.parsers[name] = parser; |
---|
657 | }, |
---|
658 | // The next 3 are exposed so you can use them |
---|
659 | quote:quote, |
---|
660 | literal:literal, |
---|
661 | join:join, |
---|
662 | // |
---|
663 | _depth_: 1, |
---|
664 | // This is the list of parsers, to modify them, use jsDump.setParser |
---|
665 | parsers:{ |
---|
666 | window: '[Window]', |
---|
667 | document: '[Document]', |
---|
668 | error:'[ERROR]', //when no parser is found, shouldn't happen |
---|
669 | unknown: '[Unknown]', |
---|
670 | 'null':'null', |
---|
671 | undefined:'undefined', |
---|
672 | 'function':function( fn ){ |
---|
673 | var ret = 'function', |
---|
674 | name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE |
---|
675 | if( name ) |
---|
676 | ret += ' ' + name; |
---|
677 | ret += '('; |
---|
678 | |
---|
679 | ret = [ ret, this.parse( fn, 'functionArgs' ), '){'].join(''); |
---|
680 | return join( ret, this.parse(fn,'functionCode'), '}' ); |
---|
681 | }, |
---|
682 | array: array, |
---|
683 | nodelist: array, |
---|
684 | arguments: array, |
---|
685 | object:function( map ){ |
---|
686 | var ret = [ ]; |
---|
687 | this.up(); |
---|
688 | for( var key in map ) |
---|
689 | ret.push( this.parse(key,'key') + ': ' + this.parse(map[key]) ); |
---|
690 | this.down(); |
---|
691 | return join( '{', ret, '}' ); |
---|
692 | }, |
---|
693 | node:function( node ){ |
---|
694 | var open = this.HTML ? '<' : '<', |
---|
695 | close = this.HTML ? '>' : '>'; |
---|
696 | |
---|
697 | var tag = node.nodeName.toLowerCase(), |
---|
698 | ret = open + tag; |
---|
699 | |
---|
700 | for( var a in this.DOMAttrs ){ |
---|
701 | var val = node[this.DOMAttrs[a]]; |
---|
702 | if( val ) |
---|
703 | ret += ' ' + a + '=' + this.parse( val, 'attribute' ); |
---|
704 | } |
---|
705 | return ret + close + open + '/' + tag + close; |
---|
706 | }, |
---|
707 | functionArgs:function( fn ){//function calls it internally, it's the arguments part of the function |
---|
708 | var l = fn.length; |
---|
709 | if( !l ) return ''; |
---|
710 | |
---|
711 | var args = Array(l); |
---|
712 | while( l-- ) |
---|
713 | args[l] = String.fromCharCode(97+l);//97 is 'a' |
---|
714 | return ' ' + args.join(', ') + ' '; |
---|
715 | }, |
---|
716 | key:quote, //object calls it internally, the key part of an item in a map |
---|
717 | functionCode:'[code]', //function calls it internally, it's the content of the function |
---|
718 | attribute:quote, //node calls it internally, it's an html attribute value |
---|
719 | string:quote, |
---|
720 | date:quote, |
---|
721 | regexp:literal, //regex |
---|
722 | number:literal, |
---|
723 | 'boolean':literal |
---|
724 | }, |
---|
725 | DOMAttrs:{//attributes to dump from nodes, name=>realName |
---|
726 | id:'id', |
---|
727 | name:'name', |
---|
728 | 'class':'className' |
---|
729 | }, |
---|
730 | HTML:false,//if true, entities are escaped ( <, >, \t, space and \n ) |
---|
731 | indentChar:' ',//indentation unit |
---|
732 | multiline:true //if true, items in a collection, are separated by a \n, else just a space. |
---|
733 | }; |
---|
734 | |
---|
735 | })(); |
---|