├── package.json ├── tests ├── index.html └── core.js ├── README.md ├── demos ├── contenteditable.html └── index.html ├── undo.js └── vendor ├── qunit.css └── qunit.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "undo.js", 3 | "version": "0.2.0", 4 | "description": "Undo.js http://jzaefferer.github.com/undo/demos/", 5 | "main": "undo.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/jzaefferer/undo.git" 9 | }, 10 | "keywords": [ 11 | "undo", 12 | "redo" 13 | ], 14 | "author": "Jörn Zaefferer ", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/jzaefferer/undo/issues" 18 | }, 19 | "homepage": "https://github.com/jzaefferer/undo" 20 | } 21 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Undo.js Test Suite 5 | 6 | 7 | 8 | 9 | 10 | 11 |

Undo.js Test Suite

12 |

13 |
14 |

15 |
    16 |
    test markup
    17 | 18 | 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Undo.js 2 | 3 | **This is only a proof of concept, not a proper library.** 4 | 5 | Undo.js provides an abstraction for undoing and redoing any task. It can run both in the browser and on the server (targetted at node.js). 6 | It can be used to add undo and redo features to a custom application, e.g. undo and redo changing the priority of a task in a TODO list. 7 | It can also be used to undo and redo native browser features: Clicking a checkbox, editing a textbox and eventually, editing a contenteditable element. 8 | 9 | The base abstraction will stay independent of any framework, while plugins, like a command editing a sortable list, will likely depend on libraries such as jQuery. 10 | 11 | ## Installation 12 | 13 | Via npm: `npm install undo.js` 14 | 15 | ## Roadmap for demos to add: 16 | 17 | - Extend sortable list demo with jQuery UI sortable 18 | - form: track all form changes: text input, checkbox change, radio change, select change 19 | - add undo/redo to Backbone TODO demo 20 | - contenteditable with bold command 21 | 22 | ## Demos: 23 | 24 | http://jzaefferer.github.io/undo/demos/ 25 | http://jzaefferer.github.io/undo/demos/contenteditable.html 26 | 27 | ## To suggest a feature, report a bug, or general discussion: 28 | 29 | http://github.com/jzaefferer/undo/issues/ 30 | -------------------------------------------------------------------------------- /tests/core.js: -------------------------------------------------------------------------------- 1 | module("core"); 2 | 3 | var NameChange = Undo.Command.extend({ 4 | constructor: function(object, newName) { 5 | NameChange.__super__.constructor("Name change to " + newName); 6 | this.object = object; 7 | this.oldName = object.name; 8 | this.newName = newName; 9 | }, 10 | execute: function() { 11 | this.object.name = this.newName; 12 | }, 13 | undo: function() { 14 | this.object.name = this.oldName; 15 | } 16 | }); 17 | 18 | test("Command.extend", function() { 19 | var object = { 20 | name: "Peter" 21 | } 22 | var command = new NameChange(object, "Pan"); 23 | equal(command.name, "Name change to Pan"); 24 | equal(object.name, "Peter"); 25 | 26 | command.execute(); 27 | equal(object.name, "Pan"); 28 | 29 | command.undo(); 30 | equal(object.name, "Peter"); 31 | 32 | command.redo(); 33 | equal(object.name, "Pan"); 34 | }); 35 | 36 | test("Stack", function() { 37 | var object = { 38 | name: "Peter" 39 | } 40 | var command = new NameChange(object, "Pawn"); 41 | var stack = new Undo.Stack(); 42 | 43 | 44 | stack.execute(command); 45 | equal(object.name, "Pawn"); 46 | ok( stack.dirty() ); 47 | ok( stack.canUndo() ); 48 | ok( !stack.canRedo() ); 49 | 50 | stack.save(); 51 | ok( !stack.dirty() ); 52 | ok( stack.canUndo() ); 53 | ok( !stack.canRedo() ); 54 | 55 | stack.undo(); 56 | equal(object.name, "Peter"); 57 | ok( stack.dirty() ); 58 | ok( !stack.canUndo() ); 59 | ok( stack.canRedo() ); 60 | 61 | stack.redo(); 62 | equal(object.name, "Pawn"); 63 | ok( !stack.dirty() ); 64 | ok( stack.canUndo() ); 65 | ok( !stack.canRedo() ); 66 | }); 67 | 68 | test("Stack._clearRedo", function() { 69 | var object = { 70 | name: "Peter" 71 | } 72 | var command = new NameChange(object, "p0wn"); 73 | var stack = new Undo.Stack(); 74 | 75 | stack.execute(command) 76 | stack.execute(command) 77 | stack.execute(command) 78 | stack.undo(); 79 | stack.undo(); 80 | stack.undo(); 81 | 82 | stack.execute(command); 83 | ok( !stack.canRedo() ); 84 | }); 85 | -------------------------------------------------------------------------------- /demos/contenteditable.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Undo.js demo 8 | 12 | 88 | 89 | 90 | 91 | 92 | 93 | 94 |
    Some content, edit me!
    95 | 96 | -------------------------------------------------------------------------------- /demos/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Undo.js demo 8 | 11 | 66 | 67 | 68 | 69 | 70 | 71 | 98 | 99 | Contenteditable Demo 100 | 101 | -------------------------------------------------------------------------------- /undo.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Undo.js - A undo/redo framework for JavaScript 3 | * 4 | * http://jzaefferer.github.com/undo 5 | * 6 | * Copyright (c) 2011 Jörn Zaefferer 7 | * 8 | * MIT licensed. 9 | */ 10 | (function() { 11 | 12 | // based on Backbone.js' inherits 13 | var ctor = function(){}; 14 | var inherits = function(parent, protoProps) { 15 | var child; 16 | 17 | if (protoProps && protoProps.hasOwnProperty('constructor')) { 18 | child = protoProps.constructor; 19 | } else { 20 | child = function(){ return parent.apply(this, arguments); }; 21 | } 22 | 23 | ctor.prototype = parent.prototype; 24 | child.prototype = new ctor(); 25 | 26 | if (protoProps) extend(child.prototype, protoProps); 27 | 28 | child.prototype.constructor = child; 29 | child.__super__ = parent.prototype; 30 | return child; 31 | }; 32 | 33 | function extend(target, ref) { 34 | var name, value; 35 | for ( name in ref ) { 36 | value = ref[name]; 37 | if (value !== undefined) { 38 | target[ name ] = value; 39 | } 40 | } 41 | return target; 42 | }; 43 | 44 | var Undo = { 45 | version: '0.1.15' 46 | }; 47 | 48 | Undo.Stack = function() { 49 | this.commands = []; 50 | this.stackPosition = -1; 51 | this.savePosition = -1; 52 | }; 53 | 54 | extend(Undo.Stack.prototype, { 55 | execute: function(command) { 56 | this._clearRedo(); 57 | command.execute(); 58 | this.commands.push(command); 59 | this.stackPosition++; 60 | this.changed(); 61 | }, 62 | undo: function() { 63 | this.commands[this.stackPosition].undo(); 64 | this.stackPosition--; 65 | this.changed(); 66 | }, 67 | canUndo: function() { 68 | return this.stackPosition >= 0; 69 | }, 70 | redo: function() { 71 | this.stackPosition++; 72 | this.commands[this.stackPosition].redo(); 73 | this.changed(); 74 | }, 75 | canRedo: function() { 76 | return this.stackPosition < this.commands.length - 1; 77 | }, 78 | save: function() { 79 | this.savePosition = this.stackPosition; 80 | this.changed(); 81 | }, 82 | dirty: function() { 83 | return this.stackPosition != this.savePosition; 84 | }, 85 | _clearRedo: function() { 86 | // TODO there's probably a more efficient way for this 87 | this.commands = this.commands.slice(0, this.stackPosition + 1); 88 | }, 89 | changed: function() { 90 | // do nothing, override 91 | } 92 | }); 93 | 94 | Undo.Command = function(name) { 95 | this.name = name; 96 | } 97 | 98 | var up = new Error("override me!"); 99 | 100 | extend(Undo.Command.prototype, { 101 | execute: function() { 102 | throw up; 103 | }, 104 | undo: function() { 105 | throw up; 106 | }, 107 | redo: function() { 108 | this.execute(); 109 | } 110 | }); 111 | 112 | Undo.Command.extend = function(protoProps) { 113 | var child = inherits(this, protoProps); 114 | child.extend = Undo.Command.extend; 115 | return child; 116 | }; 117 | 118 | // AMD support 119 | if (typeof define === "function" && define.amd) { 120 | // Define as an anonymous module 121 | define(Undo); 122 | } else if(typeof module != "undefined" && module.exports){ 123 | module.exports = Undo 124 | }else { 125 | this.Undo = Undo; 126 | } 127 | }).call(this); 128 | -------------------------------------------------------------------------------- /vendor/qunit.css: -------------------------------------------------------------------------------- 1 | /** Font Family and Sizes */ 2 | 3 | #qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult { 4 | font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial; 5 | } 6 | 7 | #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; } 8 | #qunit-tests { font-size: smaller; } 9 | 10 | 11 | /** Resets */ 12 | 13 | #qunit-tests, #qunit-tests ol, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult { 14 | margin: 0; 15 | padding: 0; 16 | } 17 | 18 | 19 | /** Header */ 20 | 21 | #qunit-header { 22 | padding: 0.5em 0 0.5em 1em; 23 | 24 | color: #8699a4; 25 | background-color: #0d3349; 26 | 27 | font-size: 1.5em; 28 | line-height: 1em; 29 | font-weight: normal; 30 | 31 | border-radius: 15px 15px 0 0; 32 | -moz-border-radius: 15px 15px 0 0; 33 | -webkit-border-top-right-radius: 15px; 34 | -webkit-border-top-left-radius: 15px; 35 | } 36 | 37 | #qunit-header a { 38 | text-decoration: none; 39 | color: #c2ccd1; 40 | } 41 | 42 | #qunit-header a:hover, 43 | #qunit-header a:focus { 44 | color: #fff; 45 | } 46 | 47 | #qunit-banner { 48 | height: 5px; 49 | } 50 | 51 | #qunit-testrunner-toolbar { 52 | padding: 0.5em 0 0.5em 2em; 53 | color: #5E740B; 54 | background-color: #eee; 55 | } 56 | 57 | #qunit-userAgent { 58 | padding: 0.5em 0 0.5em 2.5em; 59 | background-color: #2b81af; 60 | color: #fff; 61 | text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px; 62 | } 63 | 64 | 65 | /** Tests: Pass/Fail */ 66 | 67 | #qunit-tests { 68 | list-style-position: inside; 69 | } 70 | 71 | #qunit-tests li { 72 | padding: 0.4em 0.5em 0.4em 2.5em; 73 | border-bottom: 1px solid #fff; 74 | list-style-position: inside; 75 | } 76 | 77 | #qunit-tests.hidepass li.pass { 78 | display: none; 79 | } 80 | 81 | #qunit-tests li strong { 82 | cursor: pointer; 83 | } 84 | 85 | #qunit-tests ol { 86 | margin-top: 0.5em; 87 | padding: 0.5em; 88 | 89 | background-color: #fff; 90 | 91 | border-radius: 15px; 92 | -moz-border-radius: 15px; 93 | -webkit-border-radius: 15px; 94 | 95 | box-shadow: inset 0px 2px 13px #999; 96 | -moz-box-shadow: inset 0px 2px 13px #999; 97 | -webkit-box-shadow: inset 0px 2px 13px #999; 98 | } 99 | 100 | #qunit-tests table { 101 | border-collapse: collapse; 102 | margin-top: .2em; 103 | } 104 | 105 | #qunit-tests th { 106 | text-align: right; 107 | vertical-align: top; 108 | padding: 0 .5em 0 0; 109 | } 110 | 111 | #qunit-tests td { 112 | vertical-align: top; 113 | } 114 | 115 | #qunit-tests pre { 116 | margin: 0; 117 | white-space: pre-wrap; 118 | word-wrap: break-word; 119 | } 120 | 121 | #qunit-tests del { 122 | background-color: #e0f2be; 123 | color: #374e0c; 124 | text-decoration: none; 125 | } 126 | 127 | #qunit-tests ins { 128 | background-color: #ffcaca; 129 | color: #500; 130 | text-decoration: none; 131 | } 132 | 133 | /*** Test Counts */ 134 | 135 | #qunit-tests b.counts { color: black; } 136 | #qunit-tests b.passed { color: #5E740B; } 137 | #qunit-tests b.failed { color: #710909; } 138 | 139 | #qunit-tests li li { 140 | margin: 0.5em; 141 | padding: 0.4em 0.5em 0.4em 0.5em; 142 | background-color: #fff; 143 | border-bottom: none; 144 | list-style-position: inside; 145 | } 146 | 147 | /*** Passing Styles */ 148 | 149 | #qunit-tests li li.pass { 150 | color: #5E740B; 151 | background-color: #fff; 152 | border-left: 26px solid #C6E746; 153 | } 154 | 155 | #qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; } 156 | #qunit-tests .pass .test-name { color: #366097; } 157 | 158 | #qunit-tests .pass .test-actual, 159 | #qunit-tests .pass .test-expected { color: #999999; } 160 | 161 | #qunit-banner.qunit-pass { background-color: #C6E746; } 162 | 163 | /*** Failing Styles */ 164 | 165 | #qunit-tests li li.fail { 166 | color: #710909; 167 | background-color: #fff; 168 | border-left: 26px solid #EE5757; 169 | } 170 | 171 | #qunit-tests > li:last-child { 172 | border-radius: 0 0 15px 15px; 173 | -moz-border-radius: 0 0 15px 15px; 174 | -webkit-border-bottom-right-radius: 15px; 175 | -webkit-border-bottom-left-radius: 15px; 176 | } 177 | 178 | #qunit-tests .fail { color: #000000; background-color: #EE5757; } 179 | #qunit-tests .fail .test-name, 180 | #qunit-tests .fail .module-name { color: #000000; } 181 | 182 | #qunit-tests .fail .test-actual { color: #EE5757; } 183 | #qunit-tests .fail .test-expected { color: green; } 184 | 185 | #qunit-banner.qunit-fail { background-color: #EE5757; } 186 | 187 | 188 | /** Result */ 189 | 190 | #qunit-testresult { 191 | padding: 0.5em 0.5em 0.5em 2.5em; 192 | 193 | color: #2b81af; 194 | background-color: #D2E0E6; 195 | 196 | border-bottom: 1px solid white; 197 | } 198 | 199 | /** Fixture */ 200 | 201 | #qunit-fixture { 202 | position: absolute; 203 | top: -10000px; 204 | left: -10000px; 205 | } 206 | -------------------------------------------------------------------------------- /vendor/qunit.js: -------------------------------------------------------------------------------- 1 | /* 2 | * QUnit - A JavaScript Unit Testing Framework 3 | * 4 | * http://docs.jquery.com/QUnit 5 | * 6 | * Copyright (c) 2011 John Resig, Jörn Zaefferer 7 | * Dual licensed under the MIT (MIT-LICENSE.txt) 8 | * or GPL (GPL-LICENSE.txt) licenses. 9 | */ 10 | 11 | (function(window) { 12 | 13 | var defined = { 14 | setTimeout: typeof window.setTimeout !== "undefined", 15 | sessionStorage: (function() { 16 | try { 17 | return !!sessionStorage.getItem; 18 | } catch(e){ 19 | return false; 20 | } 21 | })() 22 | } 23 | 24 | var testId = 0; 25 | 26 | var Test = function(name, testName, expected, testEnvironmentArg, async, callback) { 27 | this.name = name; 28 | this.testName = testName; 29 | this.expected = expected; 30 | this.testEnvironmentArg = testEnvironmentArg; 31 | this.async = async; 32 | this.callback = callback; 33 | this.assertions = []; 34 | }; 35 | Test.prototype = { 36 | init: function() { 37 | var tests = id("qunit-tests"); 38 | if (tests) { 39 | var b = document.createElement("strong"); 40 | b.innerHTML = "Running " + this.name; 41 | var li = document.createElement("li"); 42 | li.appendChild( b ); 43 | li.id = this.id = "test-output" + testId++; 44 | tests.appendChild( li ); 45 | } 46 | }, 47 | setup: function() { 48 | if (this.module != config.previousModule) { 49 | if ( config.previousModule ) { 50 | QUnit.moduleDone( { 51 | name: config.previousModule, 52 | failed: config.moduleStats.bad, 53 | passed: config.moduleStats.all - config.moduleStats.bad, 54 | total: config.moduleStats.all 55 | } ); 56 | } 57 | config.previousModule = this.module; 58 | config.moduleStats = { all: 0, bad: 0 }; 59 | QUnit.moduleStart( { 60 | name: this.module 61 | } ); 62 | } 63 | 64 | config.current = this; 65 | this.testEnvironment = extend({ 66 | setup: function() {}, 67 | teardown: function() {} 68 | }, this.moduleTestEnvironment); 69 | if (this.testEnvironmentArg) { 70 | extend(this.testEnvironment, this.testEnvironmentArg); 71 | } 72 | 73 | QUnit.testStart( { 74 | name: this.testName 75 | } ); 76 | 77 | // allow utility functions to access the current test environment 78 | // TODO why?? 79 | QUnit.current_testEnvironment = this.testEnvironment; 80 | 81 | try { 82 | if ( !config.pollution ) { 83 | saveGlobal(); 84 | } 85 | 86 | this.testEnvironment.setup.call(this.testEnvironment); 87 | } catch(e) { 88 | QUnit.ok( false, "Setup failed on " + this.testName + ": " + e.message ); 89 | } 90 | }, 91 | run: function() { 92 | if ( this.async ) { 93 | QUnit.stop(); 94 | } 95 | 96 | if ( config.notrycatch ) { 97 | this.callback.call(this.testEnvironment); 98 | return; 99 | } 100 | try { 101 | this.callback.call(this.testEnvironment); 102 | } catch(e) { 103 | fail("Test " + this.testName + " died, exception and test follows", e, this.callback); 104 | QUnit.ok( false, "Died on test #" + (this.assertions.length + 1) + ": " + e.message + " - " + QUnit.jsDump.parse(e) ); 105 | // else next test will carry the responsibility 106 | saveGlobal(); 107 | 108 | // Restart the tests if they're blocking 109 | if ( config.blocking ) { 110 | start(); 111 | } 112 | } 113 | }, 114 | teardown: function() { 115 | try { 116 | checkPollution(); 117 | this.testEnvironment.teardown.call(this.testEnvironment); 118 | } catch(e) { 119 | QUnit.ok( false, "Teardown failed on " + this.testName + ": " + e.message ); 120 | } 121 | }, 122 | finish: function() { 123 | if ( this.expected && this.expected != this.assertions.length ) { 124 | QUnit.ok( false, "Expected " + this.expected + " assertions, but " + this.assertions.length + " were run" ); 125 | } 126 | 127 | var good = 0, bad = 0, 128 | tests = id("qunit-tests"); 129 | 130 | config.stats.all += this.assertions.length; 131 | config.moduleStats.all += this.assertions.length; 132 | 133 | if ( tests ) { 134 | var ol = document.createElement("ol"); 135 | 136 | for ( var i = 0; i < this.assertions.length; i++ ) { 137 | var assertion = this.assertions[i]; 138 | 139 | var li = document.createElement("li"); 140 | li.className = assertion.result ? "pass" : "fail"; 141 | li.innerHTML = assertion.message || (assertion.result ? "okay" : "failed"); 142 | ol.appendChild( li ); 143 | 144 | if ( assertion.result ) { 145 | good++; 146 | } else { 147 | bad++; 148 | config.stats.bad++; 149 | config.moduleStats.bad++; 150 | } 151 | } 152 | 153 | // store result when possible 154 | defined.sessionStorage && sessionStorage.setItem("qunit-" + this.testName, bad); 155 | 156 | if (bad == 0) { 157 | ol.style.display = "none"; 158 | } 159 | 160 | var b = document.createElement("strong"); 161 | b.innerHTML = this.name + " (" + bad + ", " + good + ", " + this.assertions.length + ")"; 162 | 163 | addEvent(b, "click", function() { 164 | var next = b.nextSibling, display = next.style.display; 165 | next.style.display = display === "none" ? "block" : "none"; 166 | }); 167 | 168 | addEvent(b, "dblclick", function(e) { 169 | var target = e && e.target ? e.target : window.event.srcElement; 170 | if ( target.nodeName.toLowerCase() == "span" || target.nodeName.toLowerCase() == "b" ) { 171 | target = target.parentNode; 172 | } 173 | if ( window.location && target.nodeName.toLowerCase() === "strong" ) { 174 | window.location.search = "?" + encodeURIComponent(getText([target]).replace(/\(.+\)$/, "").replace(/(^\s*|\s*$)/g, "")); 175 | } 176 | }); 177 | 178 | var li = id(this.id); 179 | li.className = bad ? "fail" : "pass"; 180 | li.removeChild( li.firstChild ); 181 | li.appendChild( b ); 182 | li.appendChild( ol ); 183 | 184 | } else { 185 | for ( var i = 0; i < this.assertions.length; i++ ) { 186 | if ( !this.assertions[i].result ) { 187 | bad++; 188 | config.stats.bad++; 189 | config.moduleStats.bad++; 190 | } 191 | } 192 | } 193 | 194 | try { 195 | QUnit.reset(); 196 | } catch(e) { 197 | fail("reset() failed, following Test " + this.testName + ", exception and reset fn follows", e, QUnit.reset); 198 | } 199 | 200 | QUnit.testDone( { 201 | name: this.testName, 202 | failed: bad, 203 | passed: this.assertions.length - bad, 204 | total: this.assertions.length 205 | } ); 206 | }, 207 | 208 | queue: function() { 209 | var test = this; 210 | synchronize(function() { 211 | test.init(); 212 | }); 213 | function run() { 214 | // each of these can by async 215 | synchronize(function() { 216 | test.setup(); 217 | }); 218 | synchronize(function() { 219 | test.run(); 220 | }); 221 | synchronize(function() { 222 | test.teardown(); 223 | }); 224 | synchronize(function() { 225 | test.finish(); 226 | }); 227 | } 228 | // defer when previous test run passed, if storage is available 229 | var bad = defined.sessionStorage && +sessionStorage.getItem("qunit-" + this.testName); 230 | if (bad) { 231 | run(); 232 | } else { 233 | synchronize(run); 234 | }; 235 | } 236 | 237 | } 238 | 239 | var QUnit = { 240 | 241 | // call on start of module test to prepend name to all tests 242 | module: function(name, testEnvironment) { 243 | config.currentModule = name; 244 | config.currentModuleTestEnviroment = testEnvironment; 245 | }, 246 | 247 | asyncTest: function(testName, expected, callback) { 248 | if ( arguments.length === 2 ) { 249 | callback = expected; 250 | expected = 0; 251 | } 252 | 253 | QUnit.test(testName, expected, callback, true); 254 | }, 255 | 256 | test: function(testName, expected, callback, async) { 257 | var name = '' + testName + '', testEnvironmentArg; 258 | 259 | if ( arguments.length === 2 ) { 260 | callback = expected; 261 | expected = null; 262 | } 263 | // is 2nd argument a testEnvironment? 264 | if ( expected && typeof expected === 'object') { 265 | testEnvironmentArg = expected; 266 | expected = null; 267 | } 268 | 269 | if ( config.currentModule ) { 270 | name = '' + config.currentModule + ": " + name; 271 | } 272 | 273 | if ( !validTest(config.currentModule + ": " + testName) ) { 274 | return; 275 | } 276 | 277 | var test = new Test(name, testName, expected, testEnvironmentArg, async, callback); 278 | test.module = config.currentModule; 279 | test.moduleTestEnvironment = config.currentModuleTestEnviroment; 280 | test.queue(); 281 | }, 282 | 283 | /** 284 | * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. 285 | */ 286 | expect: function(asserts) { 287 | config.current.expected = asserts; 288 | }, 289 | 290 | /** 291 | * Asserts true. 292 | * @example ok( "asdfasdf".length > 5, "There must be at least 5 chars" ); 293 | */ 294 | ok: function(a, msg) { 295 | a = !!a; 296 | var details = { 297 | result: a, 298 | message: msg 299 | }; 300 | msg = escapeHtml(msg); 301 | QUnit.log(details); 302 | config.current.assertions.push({ 303 | result: a, 304 | message: msg 305 | }); 306 | }, 307 | 308 | /** 309 | * Checks that the first two arguments are equal, with an optional message. 310 | * Prints out both actual and expected values. 311 | * 312 | * Prefered to ok( actual == expected, message ) 313 | * 314 | * @example equal( format("Received {0} bytes.", 2), "Received 2 bytes." ); 315 | * 316 | * @param Object actual 317 | * @param Object expected 318 | * @param String message (optional) 319 | */ 320 | equal: function(actual, expected, message) { 321 | QUnit.push(expected == actual, actual, expected, message); 322 | }, 323 | 324 | notEqual: function(actual, expected, message) { 325 | QUnit.push(expected != actual, actual, expected, message); 326 | }, 327 | 328 | deepEqual: function(actual, expected, message) { 329 | QUnit.push(QUnit.equiv(actual, expected), actual, expected, message); 330 | }, 331 | 332 | notDeepEqual: function(actual, expected, message) { 333 | QUnit.push(!QUnit.equiv(actual, expected), actual, expected, message); 334 | }, 335 | 336 | strictEqual: function(actual, expected, message) { 337 | QUnit.push(expected === actual, actual, expected, message); 338 | }, 339 | 340 | notStrictEqual: function(actual, expected, message) { 341 | QUnit.push(expected !== actual, actual, expected, message); 342 | }, 343 | 344 | raises: function(block, expected, message) { 345 | var actual, ok = false; 346 | 347 | if (typeof expected === 'string') { 348 | message = expected; 349 | expected = null; 350 | } 351 | 352 | try { 353 | block(); 354 | } catch (e) { 355 | actual = e; 356 | } 357 | 358 | if (actual) { 359 | // we don't want to validate thrown error 360 | if (!expected) { 361 | ok = true; 362 | // expected is a regexp 363 | } else if (QUnit.objectType(expected) === "regexp") { 364 | ok = expected.test(actual); 365 | // expected is a constructor 366 | } else if (actual instanceof expected) { 367 | ok = true; 368 | // expected is a validation function which returns true is validation passed 369 | } else if (expected.call({}, actual) === true) { 370 | ok = true; 371 | } 372 | } 373 | 374 | QUnit.ok(ok, message); 375 | }, 376 | 377 | start: function() { 378 | config.semaphore--; 379 | if (config.semaphore > 0) { 380 | // don't start until equal number of stop-calls 381 | return; 382 | } 383 | if (config.semaphore < 0) { 384 | // ignore if start is called more often then stop 385 | config.semaphore = 0; 386 | } 387 | // A slight delay, to avoid any current callbacks 388 | if ( defined.setTimeout ) { 389 | window.setTimeout(function() { 390 | if ( config.timeout ) { 391 | clearTimeout(config.timeout); 392 | } 393 | 394 | config.blocking = false; 395 | process(); 396 | }, 13); 397 | } else { 398 | config.blocking = false; 399 | process(); 400 | } 401 | }, 402 | 403 | stop: function(timeout) { 404 | config.semaphore++; 405 | config.blocking = true; 406 | 407 | if ( timeout && defined.setTimeout ) { 408 | clearTimeout(config.timeout); 409 | config.timeout = window.setTimeout(function() { 410 | QUnit.ok( false, "Test timed out" ); 411 | QUnit.start(); 412 | }, timeout); 413 | } 414 | } 415 | 416 | }; 417 | 418 | // Backwards compatibility, deprecated 419 | QUnit.equals = QUnit.equal; 420 | QUnit.same = QUnit.deepEqual; 421 | 422 | // Maintain internal state 423 | var config = { 424 | // The queue of tests to run 425 | queue: [], 426 | 427 | // block until document ready 428 | blocking: true 429 | }; 430 | 431 | // Load paramaters 432 | (function() { 433 | var location = window.location || { search: "", protocol: "file:" }, 434 | GETParams = location.search.slice(1).split('&'); 435 | 436 | for ( var i = 0; i < GETParams.length; i++ ) { 437 | GETParams[i] = decodeURIComponent( GETParams[i] ); 438 | if ( GETParams[i] === "noglobals" ) { 439 | GETParams.splice( i, 1 ); 440 | i--; 441 | config.noglobals = true; 442 | } else if ( GETParams[i] === "notrycatch" ) { 443 | GETParams.splice( i, 1 ); 444 | i--; 445 | config.notrycatch = true; 446 | } else if ( GETParams[i].search('=') > -1 ) { 447 | GETParams.splice( i, 1 ); 448 | i--; 449 | } 450 | } 451 | 452 | // restrict modules/tests by get parameters 453 | config.filters = GETParams; 454 | 455 | // Figure out if we're running the tests from a server or not 456 | QUnit.isLocal = !!(location.protocol === 'file:'); 457 | })(); 458 | 459 | // Expose the API as global variables, unless an 'exports' 460 | // object exists, in that case we assume we're in CommonJS 461 | if ( typeof exports === "undefined" || typeof require === "undefined" ) { 462 | extend(window, QUnit); 463 | window.QUnit = QUnit; 464 | } else { 465 | extend(exports, QUnit); 466 | exports.QUnit = QUnit; 467 | } 468 | 469 | // define these after exposing globals to keep them in these QUnit namespace only 470 | extend(QUnit, { 471 | config: config, 472 | 473 | // Initialize the configuration options 474 | init: function() { 475 | extend(config, { 476 | stats: { all: 0, bad: 0 }, 477 | moduleStats: { all: 0, bad: 0 }, 478 | started: +new Date, 479 | updateRate: 1000, 480 | blocking: false, 481 | autostart: true, 482 | autorun: false, 483 | filters: [], 484 | queue: [], 485 | semaphore: 0 486 | }); 487 | 488 | var tests = id( "qunit-tests" ), 489 | banner = id( "qunit-banner" ), 490 | result = id( "qunit-testresult" ); 491 | 492 | if ( tests ) { 493 | tests.innerHTML = ""; 494 | } 495 | 496 | if ( banner ) { 497 | banner.className = ""; 498 | } 499 | 500 | if ( result ) { 501 | result.parentNode.removeChild( result ); 502 | } 503 | 504 | if ( tests ) { 505 | result = document.createElement( "p" ); 506 | result.id = "qunit-testresult"; 507 | result.className = "result"; 508 | tests.parentNode.insertBefore( result, tests ); 509 | result.innerHTML = 'Running...
     '; 510 | } 511 | }, 512 | 513 | /** 514 | * Resets the test setup. Useful for tests that modify the DOM. 515 | * 516 | * If jQuery is available, uses jQuery's html(), otherwise just innerHTML. 517 | */ 518 | reset: function() { 519 | if ( window.jQuery ) { 520 | jQuery( "#main, #qunit-fixture" ).html( config.fixture ); 521 | } else { 522 | var main = id( 'main' ) || id( 'qunit-fixture' ); 523 | if ( main ) { 524 | main.innerHTML = config.fixture; 525 | } 526 | } 527 | }, 528 | 529 | /** 530 | * Trigger an event on an element. 531 | * 532 | * @example triggerEvent( document.body, "click" ); 533 | * 534 | * @param DOMElement elem 535 | * @param String type 536 | */ 537 | triggerEvent: function( elem, type, event ) { 538 | if ( document.createEvent ) { 539 | event = document.createEvent("MouseEvents"); 540 | event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView, 541 | 0, 0, 0, 0, 0, false, false, false, false, 0, null); 542 | elem.dispatchEvent( event ); 543 | 544 | } else if ( elem.fireEvent ) { 545 | elem.fireEvent("on"+type); 546 | } 547 | }, 548 | 549 | // Safe object type checking 550 | is: function( type, obj ) { 551 | return QUnit.objectType( obj ) == type; 552 | }, 553 | 554 | objectType: function( obj ) { 555 | if (typeof obj === "undefined") { 556 | return "undefined"; 557 | 558 | // consider: typeof null === object 559 | } 560 | if (obj === null) { 561 | return "null"; 562 | } 563 | 564 | var type = Object.prototype.toString.call( obj ) 565 | .match(/^\[object\s(.*)\]$/)[1] || ''; 566 | 567 | switch (type) { 568 | case 'Number': 569 | if (isNaN(obj)) { 570 | return "nan"; 571 | } else { 572 | return "number"; 573 | } 574 | case 'String': 575 | case 'Boolean': 576 | case 'Array': 577 | case 'Date': 578 | case 'RegExp': 579 | case 'Function': 580 | return type.toLowerCase(); 581 | } 582 | if (typeof obj === "object") { 583 | return "object"; 584 | } 585 | return undefined; 586 | }, 587 | 588 | push: function(result, actual, expected, message) { 589 | var details = { 590 | result: result, 591 | message: message, 592 | actual: actual, 593 | expected: expected 594 | }; 595 | 596 | message = escapeHtml(message) || (result ? "okay" : "failed"); 597 | message = '' + message + ""; 598 | expected = escapeHtml(QUnit.jsDump.parse(expected)); 599 | actual = escapeHtml(QUnit.jsDump.parse(actual)); 600 | var output = message + ''; 601 | if (actual != expected) { 602 | output += ''; 603 | output += ''; 604 | } 605 | if (!result) { 606 | var source = sourceFromStacktrace(); 607 | if (source) { 608 | details.source = source; 609 | output += ''; 610 | } 611 | } 612 | output += "
    Expected:
    ' + expected + '
    Result:
    ' + actual + '
    Diff:
    ' + QUnit.diff(expected, actual) +'
    Source:
    ' + source +'
    "; 613 | 614 | QUnit.log(details); 615 | 616 | config.current.assertions.push({ 617 | result: !!result, 618 | message: output 619 | }); 620 | }, 621 | 622 | // Logging callbacks; all receive a single argument with the listed properties 623 | // run test/logs.html for any related changes 624 | begin: function() {}, 625 | // done: { failed, passed, total, runtime } 626 | done: function() {}, 627 | // log: { result, actual, expected, message } 628 | log: function() {}, 629 | // testStart: { name } 630 | testStart: function() {}, 631 | // testDone: { name, failed, passed, total } 632 | testDone: function() {}, 633 | // moduleStart: { name } 634 | moduleStart: function() {}, 635 | // moduleDone: { name, failed, passed, total } 636 | moduleDone: function() {} 637 | }); 638 | 639 | if ( typeof document === "undefined" || document.readyState === "complete" ) { 640 | config.autorun = true; 641 | } 642 | 643 | addEvent(window, "load", function() { 644 | QUnit.begin({}); 645 | 646 | // Initialize the config, saving the execution queue 647 | var oldconfig = extend({}, config); 648 | QUnit.init(); 649 | extend(config, oldconfig); 650 | 651 | config.blocking = false; 652 | 653 | var userAgent = id("qunit-userAgent"); 654 | if ( userAgent ) { 655 | userAgent.innerHTML = navigator.userAgent; 656 | } 657 | var banner = id("qunit-header"); 658 | if ( banner ) { 659 | var paramsIndex = location.href.lastIndexOf(location.search); 660 | if ( paramsIndex > -1 ) { 661 | var mainPageLocation = location.href.slice(0, paramsIndex); 662 | if ( mainPageLocation == location.href ) { 663 | banner.innerHTML = ' ' + banner.innerHTML + ' '; 664 | } else { 665 | var testName = decodeURIComponent(location.search.slice(1)); 666 | banner.innerHTML = '' + banner.innerHTML + '' + testName + ''; 667 | } 668 | } 669 | } 670 | 671 | var toolbar = id("qunit-testrunner-toolbar"); 672 | if ( toolbar ) { 673 | var filter = document.createElement("input"); 674 | filter.type = "checkbox"; 675 | filter.id = "qunit-filter-pass"; 676 | addEvent( filter, "click", function() { 677 | var ol = document.getElementById("qunit-tests"); 678 | if ( filter.checked ) { 679 | ol.className = ol.className + " hidepass"; 680 | } else { 681 | var tmp = " " + ol.className.replace( /[\n\t\r]/g, " " ) + " "; 682 | ol.className = tmp.replace(/ hidepass /, " "); 683 | } 684 | if ( defined.sessionStorage ) { 685 | sessionStorage.setItem("qunit-filter-passed-tests", filter.checked ? "true" : ""); 686 | } 687 | }); 688 | if ( defined.sessionStorage && sessionStorage.getItem("qunit-filter-passed-tests") ) { 689 | filter.checked = true; 690 | var ol = document.getElementById("qunit-tests"); 691 | ol.className = ol.className + " hidepass"; 692 | } 693 | toolbar.appendChild( filter ); 694 | 695 | var label = document.createElement("label"); 696 | label.setAttribute("for", "qunit-filter-pass"); 697 | label.innerHTML = "Hide passed tests"; 698 | toolbar.appendChild( label ); 699 | } 700 | 701 | var main = id('main') || id('qunit-fixture'); 702 | if ( main ) { 703 | config.fixture = main.innerHTML; 704 | } 705 | 706 | if (config.autostart) { 707 | QUnit.start(); 708 | } 709 | }); 710 | 711 | function done() { 712 | config.autorun = true; 713 | 714 | // Log the last module results 715 | if ( config.currentModule ) { 716 | QUnit.moduleDone( { 717 | name: config.currentModule, 718 | failed: config.moduleStats.bad, 719 | passed: config.moduleStats.all - config.moduleStats.bad, 720 | total: config.moduleStats.all 721 | } ); 722 | } 723 | 724 | var banner = id("qunit-banner"), 725 | tests = id("qunit-tests"), 726 | runtime = +new Date - config.started, 727 | passed = config.stats.all - config.stats.bad, 728 | html = [ 729 | 'Tests completed in ', 730 | runtime, 731 | ' milliseconds.
    ', 732 | '', 733 | passed, 734 | ' tests of ', 735 | config.stats.all, 736 | ' passed, ', 737 | config.stats.bad, 738 | ' failed.' 739 | ].join(''); 740 | 741 | if ( banner ) { 742 | banner.className = (config.stats.bad ? "qunit-fail" : "qunit-pass"); 743 | } 744 | 745 | if ( tests ) { 746 | id( "qunit-testresult" ).innerHTML = html; 747 | } 748 | 749 | QUnit.done( { 750 | failed: config.stats.bad, 751 | passed: passed, 752 | total: config.stats.all, 753 | runtime: runtime 754 | } ); 755 | } 756 | 757 | function validTest( name ) { 758 | var i = config.filters.length, 759 | run = false; 760 | 761 | if ( !i ) { 762 | return true; 763 | } 764 | 765 | while ( i-- ) { 766 | var filter = config.filters[i], 767 | not = filter.charAt(0) == '!'; 768 | 769 | if ( not ) { 770 | filter = filter.slice(1); 771 | } 772 | 773 | if ( name.indexOf(filter) !== -1 ) { 774 | return !not; 775 | } 776 | 777 | if ( not ) { 778 | run = true; 779 | } 780 | } 781 | 782 | return run; 783 | } 784 | 785 | // so far supports only Firefox, Chrome and Opera (buggy) 786 | // could be extended in the future to use something like https://github.com/csnover/TraceKit 787 | function sourceFromStacktrace() { 788 | try { 789 | throw new Error(); 790 | } catch ( e ) { 791 | if (e.stacktrace) { 792 | // Opera 793 | return e.stacktrace.split("\n")[6]; 794 | } else if (e.stack) { 795 | // Firefox, Chrome 796 | return e.stack.split("\n")[4]; 797 | } 798 | } 799 | } 800 | 801 | function escapeHtml(s) { 802 | if (!s) { 803 | return ""; 804 | } 805 | s = s + ""; 806 | return s.replace(/[\&"<>\\]/g, function(s) { 807 | switch(s) { 808 | case "&": return "&"; 809 | case "\\": return "\\\\"; 810 | case '"': return '\"'; 811 | case "<": return "<"; 812 | case ">": return ">"; 813 | default: return s; 814 | } 815 | }); 816 | } 817 | 818 | function synchronize( callback ) { 819 | config.queue.push( callback ); 820 | 821 | if ( config.autorun && !config.blocking ) { 822 | process(); 823 | } 824 | } 825 | 826 | function process() { 827 | var start = (new Date()).getTime(); 828 | 829 | while ( config.queue.length && !config.blocking ) { 830 | if ( config.updateRate <= 0 || (((new Date()).getTime() - start) < config.updateRate) ) { 831 | config.queue.shift()(); 832 | } else { 833 | window.setTimeout( process, 13 ); 834 | break; 835 | } 836 | } 837 | if (!config.blocking && !config.queue.length) { 838 | done(); 839 | } 840 | } 841 | 842 | function saveGlobal() { 843 | config.pollution = []; 844 | 845 | if ( config.noglobals ) { 846 | for ( var key in window ) { 847 | config.pollution.push( key ); 848 | } 849 | } 850 | } 851 | 852 | function checkPollution( name ) { 853 | var old = config.pollution; 854 | saveGlobal(); 855 | 856 | var newGlobals = diff( old, config.pollution ); 857 | if ( newGlobals.length > 0 ) { 858 | ok( false, "Introduced global variable(s): " + newGlobals.join(", ") ); 859 | config.current.expected++; 860 | } 861 | 862 | var deletedGlobals = diff( config.pollution, old ); 863 | if ( deletedGlobals.length > 0 ) { 864 | ok( false, "Deleted global variable(s): " + deletedGlobals.join(", ") ); 865 | config.current.expected++; 866 | } 867 | } 868 | 869 | // returns a new Array with the elements that are in a but not in b 870 | function diff( a, b ) { 871 | var result = a.slice(); 872 | for ( var i = 0; i < result.length; i++ ) { 873 | for ( var j = 0; j < b.length; j++ ) { 874 | if ( result[i] === b[j] ) { 875 | result.splice(i, 1); 876 | i--; 877 | break; 878 | } 879 | } 880 | } 881 | return result; 882 | } 883 | 884 | function fail(message, exception, callback) { 885 | if ( typeof console !== "undefined" && console.error && console.warn ) { 886 | console.error(message); 887 | console.error(exception); 888 | console.warn(callback.toString()); 889 | 890 | } else if ( window.opera && opera.postError ) { 891 | opera.postError(message, exception, callback.toString); 892 | } 893 | } 894 | 895 | function extend(a, b) { 896 | for ( var prop in b ) { 897 | a[prop] = b[prop]; 898 | } 899 | 900 | return a; 901 | } 902 | 903 | function addEvent(elem, type, fn) { 904 | if ( elem.addEventListener ) { 905 | elem.addEventListener( type, fn, false ); 906 | } else if ( elem.attachEvent ) { 907 | elem.attachEvent( "on" + type, fn ); 908 | } else { 909 | fn(); 910 | } 911 | } 912 | 913 | function id(name) { 914 | return !!(typeof document !== "undefined" && document && document.getElementById) && 915 | document.getElementById( name ); 916 | } 917 | 918 | // Test for equality any JavaScript type. 919 | // Discussions and reference: http://philrathe.com/articles/equiv 920 | // Test suites: http://philrathe.com/tests/equiv 921 | // Author: Philippe Rathé 922 | QUnit.equiv = function () { 923 | 924 | var innerEquiv; // the real equiv function 925 | var callers = []; // stack to decide between skip/abort functions 926 | var parents = []; // stack to avoiding loops from circular referencing 927 | 928 | // Call the o related callback with the given arguments. 929 | function bindCallbacks(o, callbacks, args) { 930 | var prop = QUnit.objectType(o); 931 | if (prop) { 932 | if (QUnit.objectType(callbacks[prop]) === "function") { 933 | return callbacks[prop].apply(callbacks, args); 934 | } else { 935 | return callbacks[prop]; // or undefined 936 | } 937 | } 938 | } 939 | 940 | var callbacks = function () { 941 | 942 | // for string, boolean, number and null 943 | function useStrictEquality(b, a) { 944 | if (b instanceof a.constructor || a instanceof b.constructor) { 945 | // to catch short annotaion VS 'new' annotation of a declaration 946 | // e.g. var i = 1; 947 | // var j = new Number(1); 948 | return a == b; 949 | } else { 950 | return a === b; 951 | } 952 | } 953 | 954 | return { 955 | "string": useStrictEquality, 956 | "boolean": useStrictEquality, 957 | "number": useStrictEquality, 958 | "null": useStrictEquality, 959 | "undefined": useStrictEquality, 960 | 961 | "nan": function (b) { 962 | return isNaN(b); 963 | }, 964 | 965 | "date": function (b, a) { 966 | return QUnit.objectType(b) === "date" && a.valueOf() === b.valueOf(); 967 | }, 968 | 969 | "regexp": function (b, a) { 970 | return QUnit.objectType(b) === "regexp" && 971 | a.source === b.source && // the regex itself 972 | a.global === b.global && // and its modifers (gmi) ... 973 | a.ignoreCase === b.ignoreCase && 974 | a.multiline === b.multiline; 975 | }, 976 | 977 | // - skip when the property is a method of an instance (OOP) 978 | // - abort otherwise, 979 | // initial === would have catch identical references anyway 980 | "function": function () { 981 | var caller = callers[callers.length - 1]; 982 | return caller !== Object && 983 | typeof caller !== "undefined"; 984 | }, 985 | 986 | "array": function (b, a) { 987 | var i, j, loop; 988 | var len; 989 | 990 | // b could be an object literal here 991 | if ( ! (QUnit.objectType(b) === "array")) { 992 | return false; 993 | } 994 | 995 | len = a.length; 996 | if (len !== b.length) { // safe and faster 997 | return false; 998 | } 999 | 1000 | //track reference to avoid circular references 1001 | parents.push(a); 1002 | for (i = 0; i < len; i++) { 1003 | loop = false; 1004 | for(j=0;j= 0) { 1149 | type = "array"; 1150 | } else { 1151 | type = typeof obj; 1152 | } 1153 | return type; 1154 | }, 1155 | separator:function() { 1156 | return this.multiline ? this.HTML ? '
    ' : '\n' : this.HTML ? ' ' : ' '; 1157 | }, 1158 | indent:function( extra ) {// extra can be a number, shortcut for increasing-calling-decreasing 1159 | if ( !this.multiline ) 1160 | return ''; 1161 | var chr = this.indentChar; 1162 | if ( this.HTML ) 1163 | chr = chr.replace(/\t/g,' ').replace(/ /g,' '); 1164 | return Array( this._depth_ + (extra||0) ).join(chr); 1165 | }, 1166 | up:function( a ) { 1167 | this._depth_ += a || 1; 1168 | }, 1169 | down:function( a ) { 1170 | this._depth_ -= a || 1; 1171 | }, 1172 | setParser:function( name, parser ) { 1173 | this.parsers[name] = parser; 1174 | }, 1175 | // The next 3 are exposed so you can use them 1176 | quote:quote, 1177 | literal:literal, 1178 | join:join, 1179 | // 1180 | _depth_: 1, 1181 | // This is the list of parsers, to modify them, use jsDump.setParser 1182 | parsers:{ 1183 | window: '[Window]', 1184 | document: '[Document]', 1185 | error:'[ERROR]', //when no parser is found, shouldn't happen 1186 | unknown: '[Unknown]', 1187 | 'null':'null', 1188 | undefined:'undefined', 1189 | 'function':function( fn ) { 1190 | var ret = 'function', 1191 | name = 'name' in fn ? fn.name : (reName.exec(fn)||[])[1];//functions never have name in IE 1192 | if ( name ) 1193 | ret += ' ' + name; 1194 | ret += '('; 1195 | 1196 | ret = [ ret, QUnit.jsDump.parse( fn, 'functionArgs' ), '){'].join(''); 1197 | return join( ret, QUnit.jsDump.parse(fn,'functionCode'), '}' ); 1198 | }, 1199 | array: array, 1200 | nodelist: array, 1201 | arguments: array, 1202 | object:function( map ) { 1203 | var ret = [ ]; 1204 | QUnit.jsDump.up(); 1205 | for ( var key in map ) 1206 | ret.push( QUnit.jsDump.parse(key,'key') + ': ' + QUnit.jsDump.parse(map[key]) ); 1207 | QUnit.jsDump.down(); 1208 | return join( '{', ret, '}' ); 1209 | }, 1210 | node:function( node ) { 1211 | var open = QUnit.jsDump.HTML ? '<' : '<', 1212 | close = QUnit.jsDump.HTML ? '>' : '>'; 1213 | 1214 | var tag = node.nodeName.toLowerCase(), 1215 | ret = open + tag; 1216 | 1217 | for ( var a in QUnit.jsDump.DOMAttrs ) { 1218 | var val = node[QUnit.jsDump.DOMAttrs[a]]; 1219 | if ( val ) 1220 | ret += ' ' + a + '=' + QUnit.jsDump.parse( val, 'attribute' ); 1221 | } 1222 | return ret + close + open + '/' + tag + close; 1223 | }, 1224 | functionArgs:function( fn ) {//function calls it internally, it's the arguments part of the function 1225 | var l = fn.length; 1226 | if ( !l ) return ''; 1227 | 1228 | var args = Array(l); 1229 | while ( l-- ) 1230 | args[l] = String.fromCharCode(97+l);//97 is 'a' 1231 | return ' ' + args.join(', ') + ' '; 1232 | }, 1233 | key:quote, //object calls it internally, the key part of an item in a map 1234 | functionCode:'[code]', //function calls it internally, it's the content of the function 1235 | attribute:quote, //node calls it internally, it's an html attribute value 1236 | string:quote, 1237 | date:quote, 1238 | regexp:literal, //regex 1239 | number:literal, 1240 | 'boolean':literal 1241 | }, 1242 | DOMAttrs:{//attributes to dump from nodes, name=>realName 1243 | id:'id', 1244 | name:'name', 1245 | 'class':'className' 1246 | }, 1247 | HTML:false,//if true, entities are escaped ( <, >, \t, space and \n ) 1248 | indentChar:' ',//indentation unit 1249 | multiline:true //if true, items in a collection, are separated by a \n, else just a space. 1250 | }; 1251 | 1252 | return jsDump; 1253 | })(); 1254 | 1255 | // from Sizzle.js 1256 | function getText( elems ) { 1257 | var ret = "", elem; 1258 | 1259 | for ( var i = 0; elems[i]; i++ ) { 1260 | elem = elems[i]; 1261 | 1262 | // Get the text from text nodes and CDATA nodes 1263 | if ( elem.nodeType === 3 || elem.nodeType === 4 ) { 1264 | ret += elem.nodeValue; 1265 | 1266 | // Traverse everything else, except comment nodes 1267 | } else if ( elem.nodeType !== 8 ) { 1268 | ret += getText( elem.childNodes ); 1269 | } 1270 | } 1271 | 1272 | return ret; 1273 | }; 1274 | 1275 | /* 1276 | * Javascript Diff Algorithm 1277 | * By John Resig (http://ejohn.org/) 1278 | * Modified by Chu Alan "sprite" 1279 | * 1280 | * Released under the MIT license. 1281 | * 1282 | * More Info: 1283 | * http://ejohn.org/projects/javascript-diff-algorithm/ 1284 | * 1285 | * Usage: QUnit.diff(expected, actual) 1286 | * 1287 | * QUnit.diff("the quick brown fox jumped over", "the quick fox jumps over") == "the quick brown fox jumped jumps over" 1288 | */ 1289 | QUnit.diff = (function() { 1290 | function diff(o, n){ 1291 | var ns = new Object(); 1292 | var os = new Object(); 1293 | 1294 | for (var i = 0; i < n.length; i++) { 1295 | if (ns[n[i]] == null) 1296 | ns[n[i]] = { 1297 | rows: new Array(), 1298 | o: null 1299 | }; 1300 | ns[n[i]].rows.push(i); 1301 | } 1302 | 1303 | for (var i = 0; i < o.length; i++) { 1304 | if (os[o[i]] == null) 1305 | os[o[i]] = { 1306 | rows: new Array(), 1307 | n: null 1308 | }; 1309 | os[o[i]].rows.push(i); 1310 | } 1311 | 1312 | for (var i in ns) { 1313 | if (ns[i].rows.length == 1 && typeof(os[i]) != "undefined" && os[i].rows.length == 1) { 1314 | n[ns[i].rows[0]] = { 1315 | text: n[ns[i].rows[0]], 1316 | row: os[i].rows[0] 1317 | }; 1318 | o[os[i].rows[0]] = { 1319 | text: o[os[i].rows[0]], 1320 | row: ns[i].rows[0] 1321 | }; 1322 | } 1323 | } 1324 | 1325 | for (var i = 0; i < n.length - 1; i++) { 1326 | if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null && 1327 | n[i + 1] == o[n[i].row + 1]) { 1328 | n[i + 1] = { 1329 | text: n[i + 1], 1330 | row: n[i].row + 1 1331 | }; 1332 | o[n[i].row + 1] = { 1333 | text: o[n[i].row + 1], 1334 | row: i + 1 1335 | }; 1336 | } 1337 | } 1338 | 1339 | for (var i = n.length - 1; i > 0; i--) { 1340 | if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null && 1341 | n[i - 1] == o[n[i].row - 1]) { 1342 | n[i - 1] = { 1343 | text: n[i - 1], 1344 | row: n[i].row - 1 1345 | }; 1346 | o[n[i].row - 1] = { 1347 | text: o[n[i].row - 1], 1348 | row: i - 1 1349 | }; 1350 | } 1351 | } 1352 | 1353 | return { 1354 | o: o, 1355 | n: n 1356 | }; 1357 | } 1358 | 1359 | return function(o, n){ 1360 | o = o.replace(/\s+$/, ''); 1361 | n = n.replace(/\s+$/, ''); 1362 | var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/)); 1363 | 1364 | var str = ""; 1365 | 1366 | var oSpace = o.match(/\s+/g); 1367 | if (oSpace == null) { 1368 | oSpace = [" "]; 1369 | } 1370 | else { 1371 | oSpace.push(" "); 1372 | } 1373 | var nSpace = n.match(/\s+/g); 1374 | if (nSpace == null) { 1375 | nSpace = [" "]; 1376 | } 1377 | else { 1378 | nSpace.push(" "); 1379 | } 1380 | 1381 | if (out.n.length == 0) { 1382 | for (var i = 0; i < out.o.length; i++) { 1383 | str += '' + out.o[i] + oSpace[i] + ""; 1384 | } 1385 | } 1386 | else { 1387 | if (out.n[0].text == null) { 1388 | for (n = 0; n < out.o.length && out.o[n].text == null; n++) { 1389 | str += '' + out.o[n] + oSpace[n] + ""; 1390 | } 1391 | } 1392 | 1393 | for (var i = 0; i < out.n.length; i++) { 1394 | if (out.n[i].text == null) { 1395 | str += '' + out.n[i] + nSpace[i] + ""; 1396 | } 1397 | else { 1398 | var pre = ""; 1399 | 1400 | for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) { 1401 | pre += '' + out.o[n] + oSpace[n] + ""; 1402 | } 1403 | str += " " + out.n[i].text + nSpace[i] + pre; 1404 | } 1405 | } 1406 | } 1407 | 1408 | return str; 1409 | }; 1410 | })(); 1411 | 1412 | })(this); 1413 | --------------------------------------------------------------------------------