Skip to content
Snippets Groups Projects
qunit-1.12.0.js 57.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hakim El Hattab's avatar
    Hakim El Hattab committed
    
    					return ret + open + "/" + tag + close;
    				},
    				// function calls it internally, it's the arguments part of the function
    				functionArgs: function( fn ) {
    					var args,
    						l = fn.length;
    
    					if ( !l ) {
    						return "";
    					}
    
    					args = new Array(l);
    					while ( l-- ) {
    						// 97 is 'a'
    						args[l] = String.fromCharCode(97+l);
    					}
    					return " " + args.join( ", " ) + " ";
    				},
    				// object calls it internally, the key part of an item in a map
    				key: quote,
    				// function calls it internally, it's the content of the function
    				functionCode: "[code]",
    				// node calls it internally, it's an html attribute value
    				attribute: quote,
    				string: quote,
    				date: quote,
    				regexp: literal,
    				number: literal,
    				"boolean": literal
    			},
    			// if true, entities are escaped ( <, >, \t, space and \n )
    			HTML: false,
    			// indentation unit
    			indentChar: "  ",
    			// if true, items in a collection, are separated by a \n, else just a space.
    			multiline: true
    		};
    
    	return jsDump;
    }());
    
    // from jquery.js
    function inArray( elem, array ) {
    	if ( array.indexOf ) {
    		return array.indexOf( elem );
    	}
    
    	for ( var i = 0, length = array.length; i < length; i++ ) {
    		if ( array[ i ] === elem ) {
    			return i;
    		}
    	}
    
    	return -1;
    }
    
    /*
     * Javascript Diff Algorithm
     *  By John Resig (http://ejohn.org/)
     *  Modified by Chu Alan "sprite"
     *
     * Released under the MIT license.
     *
     * More Info:
     *  http://ejohn.org/projects/javascript-diff-algorithm/
     *
     * Usage: QUnit.diff(expected, actual)
     *
     * QUnit.diff( "the quick brown fox jumped over", "the quick fox jumps over" ) == "the  quick <del>brown </del> fox <del>jumped </del><ins>jumps </ins> over"
     */
    QUnit.diff = (function() {
    	/*jshint eqeqeq:false, eqnull:true */
    	function diff( o, n ) {
    		var i,
    			ns = {},
    			os = {};
    
    		for ( i = 0; i < n.length; i++ ) {
    			if ( !hasOwn.call( ns, n[i] ) ) {
    				ns[ n[i] ] = {
    					rows: [],
    					o: null
    				};
    			}
    			ns[ n[i] ].rows.push( i );
    		}
    
    		for ( i = 0; i < o.length; i++ ) {
    			if ( !hasOwn.call( os, o[i] ) ) {
    				os[ o[i] ] = {
    					rows: [],
    					n: null
    				};
    			}
    			os[ o[i] ].rows.push( i );
    		}
    
    		for ( i in ns ) {
    			if ( hasOwn.call( ns, i ) ) {
    				if ( ns[i].rows.length === 1 && hasOwn.call( os, i ) && os[i].rows.length === 1 ) {
    					n[ ns[i].rows[0] ] = {
    						text: n[ ns[i].rows[0] ],
    						row: os[i].rows[0]
    					};
    					o[ os[i].rows[0] ] = {
    						text: o[ os[i].rows[0] ],
    						row: ns[i].rows[0]
    					};
    				}
    			}
    		}
    
    		for ( i = 0; i < n.length - 1; i++ ) {
    			if ( n[i].text != null && n[ i + 1 ].text == null && n[i].row + 1 < o.length && o[ n[i].row + 1 ].text == null &&
    						n[ i + 1 ] == o[ n[i].row + 1 ] ) {
    
    				n[ i + 1 ] = {
    					text: n[ i + 1 ],
    					row: n[i].row + 1
    				};
    				o[ n[i].row + 1 ] = {
    					text: o[ n[i].row + 1 ],
    					row: i + 1
    				};
    			}
    		}
    
    		for ( i = n.length - 1; i > 0; i-- ) {
    			if ( n[i].text != null && n[ i - 1 ].text == null && n[i].row > 0 && o[ n[i].row - 1 ].text == null &&
    						n[ i - 1 ] == o[ n[i].row - 1 ]) {
    
    				n[ i - 1 ] = {
    					text: n[ i - 1 ],
    					row: n[i].row - 1
    				};
    				o[ n[i].row - 1 ] = {
    					text: o[ n[i].row - 1 ],
    					row: i - 1
    				};
    			}
    		}
    
    		return {
    			o: o,
    			n: n
    		};
    	}
    
    	return function( o, n ) {
    		o = o.replace( /\s+$/, "" );
    		n = n.replace( /\s+$/, "" );
    
    		var i, pre,
    			str = "",
    			out = diff( o === "" ? [] : o.split(/\s+/), n === "" ? [] : n.split(/\s+/) ),
    			oSpace = o.match(/\s+/g),
    			nSpace = n.match(/\s+/g);
    
    		if ( oSpace == null ) {
    			oSpace = [ " " ];
    		}
    		else {
    			oSpace.push( " " );
    		}
    
    		if ( nSpace == null ) {
    			nSpace = [ " " ];
    		}
    		else {
    			nSpace.push( " " );
    		}
    
    		if ( out.n.length === 0 ) {
    			for ( i = 0; i < out.o.length; i++ ) {
    				str += "<del>" + out.o[i] + oSpace[i] + "</del>";
    			}
    		}
    		else {
    			if ( out.n[0].text == null ) {
    				for ( n = 0; n < out.o.length && out.o[n].text == null; n++ ) {
    					str += "<del>" + out.o[n] + oSpace[n] + "</del>";
    				}
    			}
    
    			for ( i = 0; i < out.n.length; i++ ) {
    				if (out.n[i].text == null) {
    					str += "<ins>" + out.n[i] + nSpace[i] + "</ins>";
    				}
    				else {
    					// `pre` initialized at top of scope
    					pre = "";
    
    					for ( n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++ ) {
    						pre += "<del>" + out.o[n] + oSpace[n] + "</del>";
    					}
    					str += " " + out.n[i].text + nSpace[i] + pre;
    				}
    			}
    		}
    
    		return str;
    	};
    }());
    
    // for CommonJS environments, export everything
    if ( typeof exports !== "undefined" ) {
    	extend( exports, QUnit.constructor.prototype );
    }
    
    // get at whatever the global object is, like window in browsers
    }( (function() {return this;}.call()) ));