-function runTests(files) {
- runTest( files, 0 );
+var queue = [];
+var blocking = false;
+
+function reset() {
+ synchronize(function() {
+ blocking = true;
+ $.get('index.html', function(content) {
+ var div = $(document.createElement('div')).html(content)
+ // search for main div
+ .find('[@id=main]').html();
+ $('#main').html(div);
+ blocking = false;
+ process();
+ });
+ });
}
-function runTest( files, num ) {
- $.get(files[num],function(js){
- js = js.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
+function synchronize(callback) {
+ queue[queue.length] = callback;
+ if(!blocking) {
+ process();
+ }
+}
- try {
- eval(js);
- } catch(e) {
- Test.push( [ false, "Died on test #" + Test.length + ": " + e ] );
- }
+function process() {
+ while(queue.length && !blocking) {
+ var call = queue[0];
+ queue = queue.slice(1);
+ call();
+ }
+}
- var good = 0, bad = 0;
- var ol = document.createElement("ol");
+function runTests(files) {
+ var startTime = new Date();
+ for( var i=0; i < files.length; i++) {
+ runTest( files, i );
+ reset();
+ }
+ synchronize(function() {
+ var runTime = new Date() - startTime;
+ $('body').append('<br/>Tests completed in ' + runTime + ' milliseconds.');
+ });
+}
- var li = "", state = "pass";
- for ( var i = 0; i < Test.length; i++ ) {
+function runTest( files, num ) {
+ synchronize(function() {
+ blocking = true;
+ $.get(files[num],function(js){
+ js = js.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
+
+ try {
+ eval(js);
+ } catch(e) {
+ Test.push( [ false, "Died on test #" + (Test.length+1) + ": " + e ] );
+ }
+
+ var good = 0, bad = 0;
+ var ol = document.createElement("ol");
+
+ var li = "", state = "pass";
+ for ( var i = 0; i < Test.length; i++ ) {
+ var li = document.createElement("li");
+ li.className = Test[i][0] ? "pass" : "fail";
+ li.innerHTML = Test[i][1];
+ ol.appendChild( li );
+
+ if ( !Test[i][0] ) {
+ state = "fail";
+ bad++;
+ } else good++;
+ }
+
var li = document.createElement("li");
- li.className = Test[i][0] ? "pass" : "fail";
- li.innerHTML = Test[i][1];
- ol.appendChild( li );
-
- if ( !Test[i][0] ) {
- state = "fail";
- bad++;
- } else good++;
- }
-
- var li = document.createElement("li");
- li.className = state;
-
- var b = document.createElement("b");
- b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
- b.onclick = function(){
- var n = this.nextSibling;
- if ( jQuery.css( n, "display" ) == "none" )
- n.style.display = "block";
- else
- n.style.display = "none";
- };
- li.appendChild( b );
-
- li.appendChild( ol );
-
- document.getElementById("tests").appendChild( li );
-
- Test = [];
- if ( ++num < files.length ) runTest( files, num );
+ li.className = state;
+
+ var b = document.createElement("b");
+ b.innerHTML = files[num] + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + Test.length + ")</b>";
+ b.onclick = function(){
+ var n = this.nextSibling;
+ if ( jQuery.css( n, "display" ) == "none" )
+ n.style.display = "block";
+ else
+ n.style.display = "none";
+ };
+ li.appendChild( b );
+
+ li.appendChild( ol );
+
+ document.getElementById("tests").appendChild( li );
+
+ Test = [];
+ blocking = false;
+ process();
+ });
});
}
return jQuery.each( this, fn, args );
},
+ /**
+ * Searches every matched element for the object and returns
+ * the index of the element, if found, starting with zero.
+ * Returns -1 if the object wasn't found.
+ *
+ * @example $("*").index(document.getElementById('foobar'))
+ * @before <div id="foobar"></div><b></b><span id="foo"></span>
+ * @result 0
+ *
+ * @example $("*").index(document.getElementById('foo'))
+ * @before <div id="foobar"></div><b></b><span id="foo"></span>
+ * @result 2
+ *
+ * @example $("*").index(document.getElementById('bar'))
+ * @before <div id="foobar"></div><b></b><span id="foo"></span>
+ * @result -1
+ *
+ * @test ok( $([window, document]).index(window) == 0, "Check for index of elements" );
+ * ok( $([window, document]).index(document) == 1, "Check for index of elements" );
+ * var inputElements = $('#radio1,#radio2,#check1,#check2');
+ * ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
+ * ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
+ * ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
+ * ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
+ * ok( inputElements.index(window) == -1, "Check for not found index" );
+ * ok( inputElements.index(document) == -1, "Check for not found index" );
+ *
+ * @name index
+ * @type Number
+ * @param Object obj Object to search for
+ * @cat Core
+ */
index: function( obj ) {
var pos = -1;
this.each(function(i){
* @before <img src="test.jpg"/>
* @result test.jpg
*
+ * @test ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
+ * @test ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
+ * @test ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
+ * @test ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
+ *
* @name attr
* @type Object
* @param String name The name of the property to access.
* @before <p style="color:red;">Test Paragraph.</p>
* @result red
*
+ * @test ok( $('#foo').css("display") == 'block', 'Check for css property "display"');
+ *
* @name css
* @type Object
* @param String name The name of the property to access.
* @before <p>Test Paragraph.</p>
* @result <p style="color:red; background:blue;">Test Paragraph.</p>
*
+ * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
+ * $('#foo').css({display: 'none'});
+ * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
+ * $('#foo').css({display: 'block'});
+ * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
+ *
* @name css
* @type jQuery
* @param Hash prop A set of key/value pairs to set as style properties.
* @before <p>Test Paragraph.</p>
* @result <p style="color:red;">Test Paragraph.</p>
*
+ * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
+ * $('#foo').css('display', 'none');
+ * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
+ * $('#foo').css('display', 'block');
+ * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
+ *
* @name css
* @type jQuery
* @param String key The name of the property to set.
* @before <p>Test Paragraph.</p>
* @result Test Paragraph.
*
+ * @test var expected = "This link has class=\"blog\": Simon Willison's Weblog";
+ * ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
+ *
* @name text
* @type String
* @cat DOM
* @before <p>Test Paragraph.</p>
* @result <div class='wrap'><p>Test Paragraph.</p></div>
*
+ * @test var defaultText = 'Try them out:'
+ * var result = $('#first').wrap('<div class="red">xx<span></span>yy</div>').text()
+ * ok( 'xx' + defaultText + 'yy' == result, 'Check for wrapping' );
+ * ok( $('#first').parent().parent().is('.red'), 'Check if wrapper div has class "red"' );
+ *
* @name wrap
* @type jQuery
* @param String html A string of HTML, that will be created on the fly and wrapped around the target.
* @before <p>I would like to say: </p>
* @result <p>I would like to say: <b>Hello</b></p>
*
+ * @test var defaultText = 'Try them out:'
+ * var result = $('#first').append('<b>buga</b>');
+ * ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
+ *
* @name append
* @type jQuery
* @param String html A string of HTML, that will be created on the fly and appended to the target.
* @before <p>I would like to say: </p><b id="foo">Hello</b>
* @result <p>I would like to say: <b id="foo">Hello</b></p>
*
+ * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
+ * $('#sap').append(document.getElementById('first'));
+ * ok( expected == $('#sap').text(), "Check for appending of element" );
+ *
* @name append
* @type jQuery
* @param Element elem A DOM element that will be appended.
* @before <p>I would like to say: </p><b>Hello</b>
* @result <p>I would like to say: <b>Hello</b></p>
*
+ * @test var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
+ * $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
+ * ok( expected == $('#sap').text(), "Check for appending of array of elements" );
+ *
* @name append
* @type jQuery
* @param Array<Element> elems An array of elements, all of which will be appended.
* @before <p>I would like to say: </p>
* @result <p><b>Hello</b>I would like to say: </p>
*
+ * @test var defaultText = 'Try them out:'
+ * var result = $('#first').prepend('<b>buga</b>');
+ * ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
+ *
* @name prepend
* @type jQuery
* @param String html A string of HTML, that will be created on the fly and appended to the target.
*/
/**
- * Append an element to the inside of all matched elements.
+ * Prepend an element to the inside of all matched elements.
* This operation is the best way to insert an element inside, at the
* beginning, of all the matched element.
*
* @example $("p").prepend( $("#foo")[0] );
* @before <p>I would like to say: </p><b id="foo">Hello</b>
* @result <p><b id="foo">Hello</b>I would like to say: </p>
+ *
+ * @test var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
+ * $('#sap').prepend(document.getElementById('first'));
+ * ok( expected == $('#sap').text(), "Check for prepending of element" );
*
* @name prepend
* @type jQuery
*/
/**
- * Append any number of elements to the inside of all matched elements.
+ * Prepend any number of elements to the inside of all matched elements.
* This operation is the best way to insert a set of elements inside, at the
* beginning, of all the matched element.
*
* @before <p>I would like to say: </p><b>Hello</b>
* @result <p><b>Hello</b>I would like to say: </p>
*
+ * @test var expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
+ * $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
+ * ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
+ *
* @name prepend
* @type jQuery
* @param Array<Element> elems An array of elements, all of which will be appended.
* @before <p>I would like to say: </p>
* @result <b>Hello</b><p>I would like to say: </p>
*
+ * @test var expected = 'This is a normal link: bugaYahoo';
+ * $('#yahoo').before('<b>buga</b>');
+ * ok( expected == $('#en').text(), 'Insert String before' );
+ *
* @name before
* @type jQuery
* @param String html A string of HTML, that will be created on the fly and appended to the target.
* @before <p>I would like to say: </p><b id="foo">Hello</b>
* @result <b id="foo">Hello</b><p>I would like to say: </p>
*
+ * @test var expected = "This is a normal link: Try them out:Yahoo";
+ * $('#yahoo').before(document.getElementById('first'));
+ * ok( expected == $('#en').text(), "Insert element before" );
+ *
* @name before
* @type jQuery
* @param Element elem A DOM element that will be appended.
* @before <p>I would like to say: </p><b>Hello</b>
* @result <b>Hello</b><p>I would like to say: </p>
*
+ * @test var expected = "This is a normal link: Try them out:diveintomarkYahoo";
+ * $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
+ * ok( expected == $('#en').text(), "Insert array of elements before" );
+ *
* @name before
* @type jQuery
* @param Array<Element> elems An array of elements, all of which will be appended.
* @before <p>I would like to say: </p>
* @result <p>I would like to say: </p><b>Hello</b>
*
+ * @test var expected = 'This is a normal link: Yahoobuga';
+ * $('#yahoo').after('<b>buga</b>');
+ * ok( expected == $('#en').text(), 'Insert String after' );
+ *
* @name after
* @type jQuery
* @param String html A string of HTML, that will be created on the fly and appended to the target.
* @before <b id="foo">Hello</b><p>I would like to say: </p>
* @result <p>I would like to say: </p><b id="foo">Hello</b>
*
+ * @test var expected = "This is a normal link: YahooTry them out:";
+ * $('#yahoo').after(document.getElementById('first'));
+ * ok( expected == $('#en').text(), "Insert element after" );
+ *
* @name after
* @type jQuery
* @param Element elem A DOM element that will be appended.
* @before <b>Hello</b><p>I would like to say: </p>
* @result <p>I would like to say: </p><b>Hello</b>
*
+ * @test var expected = "This is a normal link: YahooTry them out:diveintomark";
+ * $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
+ * ok( expected == $('#en').text(), "Insert array of elements after" );
+ *
* @name after
* @type jQuery
* @param Array<Element> elems An array of elements, all of which will be appended.
* @before <p><span>Hello</span>, how are you?</p>
* @result $("p").find("span").end() == [ <p>...</p> ]
*
+ * @test ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
+ *
* @name end
* @type jQuery
* @cat DOM/Traversing
* @before <p><span>Hello</span>, how are you?</p>
* @result $("p").find("span") == [ <span>Hello</span> ]
*
+ * @test ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
+ *
* @name find
* @type jQuery
* @param String expr An expression to search with.
* @before <b>Hello</b><p>, how are you?</p>
* @result <b>Hello</b><p><b>Hello</b>, how are you?</p>
*
+ * @test ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
+ * var clone = $('#yahoo').clone();
+ * ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
+ * ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
+ *
* @name clone
* @type jQuery
* @cat DOM/Manipulation
},
/**
- * A wrapper function for each() to be used by append and prepend.
- * Handles cases where you're trying to modify the inner contents of
- * a table, when you actually need to work with the tbody.
- *
- * @member jQuery
- * @param {String} expr The expression with which to filter
+ * Checks the current selection against an expression and returns true,
+ * if the selection fits the given expression. Does return false, if the
+ * selection does not fit or the expression is not valid.
+ *
+ * @example $("input[@type='checkbox']").parent().is("form")
+ * @before <form><input type="checkbox" /></form>
+ * @result true
+ * @desc Returns true, because the parent of the input is a form element
+ *
+ * @example $("input[@type='checkbox']").parent().is("form")
+ * @before <form><p><input type="checkbox" /></p></form>
+ * @result false
+ * @desc Returns false, because the parent of the input is a p element
+ *
+ * @example $("form").is(null)
+ * @before <form></form>
+ * @result false
+ * @desc An invalid expression always returns false.
+ *
+ * @test ok( $('#form').is('form'), 'Check for element: A form must be a form' );
+ * @test ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
+ * @test ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
+ * @test ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
+ * @test ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
+ * @test ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
+ * @test ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
+ * @test ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
+ * @test ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
+ * @test ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
+ * @test ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
+ * @test ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
+ * @test ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
+ * @test ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
+ * @test ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
+ * @test ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
+ * @test ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
+ * @test ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
+ * @test ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
+ * @test ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
+ * @test ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
+ * @test ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
+ *
+ * @name is
* @type Boolean
+ * @param String expr The expression with which to filter
* @cat DOM/Traversing
*/
is: function(expr) {
- return expr ? jQuery.filter(expr,this).r.length > 0 : this.length > 0;
+ return expr ? jQuery.filter(expr,this).r.length > 0 : false;
},
/**
/**
* Extend one object with another, returning the original,
* modified, object. This is a great utility for simple inheritance.
+ *
+ * @example var settings = { validate: false, limit: 5, name: "foo" };
+ * var options = { validate: true, name: "bar" };
+ * jQuery.extend(settings, options);
+ * @result settings == { validate: true, limit: 5, name: "bar" }
+ *
+ * @test var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" };
+ * var options = { xnumber2: 1, xstring2: "x", xxx: "newstring" };
+ * var optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" };
+ * var merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
+ * jQuery.extend(settings, options);
+ * isSet( settings, merged, "Check if extended: settings must be extended" );
+ * isSet ( options, optionsCopy, "Check if not modified: options must not be modified" );
*
* @name $.extend
* @param Object obj The object to extend
* @test t( "Enabled UI Element", "input:enabled", ["text1","radio1","radio2","check1","check2","hidden1","hidden2"] );
* @test t( "Disabled UI Element", "input:disabled", ["text2"] );
* @test t( "Checked UI Element", "input:checked", ["radio2","check1"] );
+ * @test t( "Selected Option Element", "option:selected", ["option1a","option2d","option3b","option3c"] );
* @test t( "Text Contains", "a:contains('Google')", ["google","groups"] );
* @test t( "Text Contains", "a:contains('Google Groups')", ["groups"] );
* @test t( "Element Preceded By", "p ~ div", ["foo"] );
* @before <input type="text" value="some text"/>
* @result "some text"
*
+ * @test ok( $("#text1").val() == "Test", "Check for value of input element" );
+ * @test ok( !$("#text1").val() == "", "Check for value of input element" );
+ *
* @name val
* @type String
* @cat DOM/Attributes
* @before <input type="text" value="some text"/>
* @result <input type="text" value="test"/>
*
+ * @test document.getElementById('text1').value = "bla";
+ * ok( $("#text1").val() == "bla", "Check for modified value of input element" );
+ * $("#text1").val('test');
+ * ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
+ *
* @name val
* @type jQuery
* @param String val Set the property to the specified value.