3 test("Basic requirements", function() {
5 ok( Array.prototype.push, "Array.push()" );
6 ok( Function.prototype.apply, "Function.apply()" );
7 ok( document.getElementById, "getElementById" );
8 ok( document.getElementsByTagName, "getElementsByTagName" );
9 ok( RegExp, "RegExp" );
10 ok( jQuery, "jQuery" );
14 test("$()", function() {
17 var main = $("#main");
18 isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
20 // make sure this is handled
22 ok( true, "Check for \\r and \\n in jQuery()" );
26 var f = document.getElementById("iframe").contentDocument;
28 f.write("<html><body></body></html>");
30 $("<div>Testing</div>").appendTo(f.body);
34 ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );
37 test("isFunction", function() {
40 // Make sure that false values return false
41 ok( !jQuery.isFunction(), "No Value" );
42 ok( !jQuery.isFunction( null ), "null Value" );
43 ok( !jQuery.isFunction( undefined ), "undefined Value" );
44 ok( !jQuery.isFunction( "" ), "Empty String Value" );
45 ok( !jQuery.isFunction( 0 ), "0 Value" );
48 // Safari uses "(Internal Function)"
49 ok( jQuery.isFunction(String), "String Function" );
50 ok( jQuery.isFunction(Array), "Array Function" );
51 ok( jQuery.isFunction(Object), "Object Function" );
52 ok( jQuery.isFunction(Function), "Function Function" );
54 // When stringified, this could be misinterpreted
55 var mystr = "function";
56 ok( !jQuery.isFunction(mystr), "Function String" );
58 // When stringified, this could be misinterpreted
59 var myarr = [ "function" ];
60 ok( !jQuery.isFunction(myarr), "Function Array" );
62 // When stringified, this could be misinterpreted
63 var myfunction = { "function": "test" };
64 ok( !jQuery.isFunction(myfunction), "Function Object" );
66 // Make sure normal functions still work
67 var fn = function(){};
68 ok( jQuery.isFunction(fn), "Normal Function" );
70 var obj = document.createElement("object");
72 // Firefox says this is a function
73 ok( !jQuery.isFunction(obj), "Object Element" );
75 // IE says this is an object
76 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
78 var nodes = document.body.childNodes;
80 // Safari says this is a function
81 ok( !jQuery.isFunction(nodes), "childNodes Property" );
83 var first = document.body.firstChild;
85 // Normal elements are reported ok everywhere
86 ok( !jQuery.isFunction(first), "A normal DOM Element" );
88 var input = document.createElement("input");
90 document.body.appendChild( input );
92 // IE says this is an object
93 ok( jQuery.isFunction(input.focus), "A default function property" );
95 document.body.removeChild( input );
97 var a = document.createElement("a");
98 a.href = "some-function";
99 document.body.appendChild( a );
101 // This serializes with the word 'function' in it
102 ok( !jQuery.isFunction(a), "Anchor Element" );
104 document.body.removeChild( a );
106 // Recursive function calls have lengths and array-like properties
107 function callme(callback){
108 function fn(response){
112 ok( jQuery.isFunction(fn), "Recursive Function Call" );
114 fn({ some: "data" });
118 callme(function(){});
122 test("length", function() {
124 ok( $("div").length == 2, "Get Number of Elements Found" );
127 test("size()", function() {
129 ok( $("div").size() == 2, "Get Number of Elements Found" );
132 test("get()", function() {
134 isSet( $("div").get(), q("main","foo"), "Get All Elements" );
137 test("get(Number)", function() {
139 ok( $("div").get(0) == document.getElementById("main"), "Get A Single Element" );
142 test("add(String|Element|Array)", function() {
144 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
145 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
146 ok( $([]).add($("#form")[0].elements).length > 13, "Check elements from array" );
148 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
149 ok( x[0].id == "x1", "Check on-the-fly element1" );
150 ok( x[1].id == "x2", "Check on-the-fly element2" );
152 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
153 ok( x[0].id == "x1", "Check on-the-fly element1" );
154 ok( x[1].id == "x2", "Check on-the-fly element2" );
157 test("each(Function)", function() {
160 div.each(function(){this.foo = 'zoo';});
162 for ( var i = 0; i < div.size(); i++ ) {
163 if ( div.get(i).foo != "zoo" ) pass = false;
165 ok( pass, "Execute a function, Relative" );
168 test("index(Object)", function() {
170 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
171 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
172 var inputElements = $('#radio1,#radio2,#check1,#check2');
173 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
174 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
175 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
176 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
177 ok( inputElements.index(window) == -1, "Check for not found index" );
178 ok( inputElements.index(document) == -1, "Check for not found index" );
181 test("attr(String)", function() {
183 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
184 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
185 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
186 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
187 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
188 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
189 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
190 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
191 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
192 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
193 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
194 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
196 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
197 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
200 if ( location.protocol != "file:" ) {
201 test("attr(String) in XML Files", function() {
204 $.get("data/dashboard.xml", function(xml) {
205 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
206 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
212 test("attr(String, Function)", function() {
214 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
215 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
218 test("attr(Hash)", function() {
221 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
222 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
224 ok( pass, "Set Multiple Attributes" );
227 test("attr(String, Object)", function() {
230 div.attr("foo", "bar");
232 for ( var i = 0; i < div.size(); i++ ) {
233 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
235 ok( pass, "Set Attribute" );
237 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
239 $("#name").attr('name', 'something');
240 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
241 $("#check2").attr('checked', true);
242 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
243 $("#check2").attr('checked', false);
244 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
245 $("#text1").attr('readonly', true);
246 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
247 $("#text1").attr('readonly', false);
248 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
251 if ( location.protocol != "file:" ) {
252 test("attr(String, Object)x", function() {
255 $.get('data/dashboard.xml', function(xml) {
257 $('tab', xml).each(function() {
258 titles.push($(this).attr('title'));
260 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
261 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
267 test("css(String|Hash)", function() {
270 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
272 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
273 $('#foo').css({display: 'none'});
274 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
275 $('#foo').css({display: 'block'});
276 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
278 $('#floatTest').css({styleFloat: 'right'});
279 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
280 $('#floatTest').css({cssFloat: 'left'});
281 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
282 $('#floatTest').css({'float': 'right'});
283 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
284 $('#floatTest').css({'font-size': '30px'});
285 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
287 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
288 $('#foo').css({opacity: n});
289 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
290 $('#foo').css({opacity: parseFloat(n)});
291 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
293 $('#foo').css({opacity: ''});
294 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
297 test("css(String, Object)", function() {
299 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
300 $('#foo').css('display', 'none');
301 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
302 $('#foo').css('display', 'block');
303 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
305 $('#floatTest').css('styleFloat', 'left');
306 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
307 $('#floatTest').css('cssFloat', 'right');
308 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
309 $('#floatTest').css('float', 'left');
310 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
311 $('#floatTest').css('font-size', '20px');
312 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
314 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
315 $('#foo').css('opacity', n);
316 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
317 $('#foo').css('opacity', parseFloat(n));
318 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
320 $('#foo').css('opacity', '');
321 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
324 test("text()", function() {
326 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
327 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
330 test("wrap(String|Element)", function() {
332 var defaultText = 'Try them out:'
333 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
334 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
335 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
338 var defaultText = 'Try them out:'
339 var result = $('#first').wrap(document.getElementById('empty')).parent();
340 ok( result.is('ol'), 'Check for element wrapping' );
341 ok( result.text() == defaultText, 'Check for element wrapping' );
345 $('#check1').click(function() {
347 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
348 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
349 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
350 // use a fade in to check state after this event handler has finished
351 setTimeout(function() {
352 ok( !checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
358 test("append(String|Element|Array<Element>|jQuery)", function() {
360 var defaultText = 'Try them out:'
361 var result = $('#first').append('<b>buga</b>');
362 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
363 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
366 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
367 $('#sap').append(document.getElementById('first'));
368 ok( expected == $('#sap').text(), "Check for appending of element" );
371 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
372 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
373 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
376 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
377 $('#sap').append($("#first, #yahoo"));
378 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
381 $("#sap").append( 5 );
382 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
385 $("#sap").append( " text with spaces " );
386 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
389 ok( $("#sap").append([]), "Check for appending an empty array." );
390 ok( $("#sap").append(""), "Check for appending an empty string." );
391 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
394 $("#sap").append(document.getElementById('form'));
395 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
400 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
405 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
408 $('<fieldset>').appendTo('#form').append('<legend id="legend">test</legend>');
409 t( 'Append legend', '#legend', ['legend'] );
412 $('#select1').append('<OPTION>Test</OPTION>');
413 ok( $('#select1 option:last').text() == "Test", "Appending <OPTION> (all caps)" );
415 $('#table').append('<colgroup></colgroup>');
416 ok( $('#table colgroup').length, "Append colgroup" );
418 $('#table colgroup').append('<col>');
419 ok( $('#table colgroup col').length, "Append col" );
422 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
424 var defaultText = 'Try them out:'
425 $('<b>buga</b>').appendTo('#first');
426 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
427 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
430 var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
431 $(document.getElementById('first')).appendTo('#sap');
432 ok( expected == $('#sap').text(), "Check for appending of element" );
435 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
436 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
437 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
440 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
441 $("#first, #yahoo").appendTo('#sap');
442 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
445 $('#select1').appendTo('#foo');
446 t( 'Append select', '#foo select', ['select1'] );
449 test("prepend(String|Element|Array<Element>|jQuery)", function() {
451 var defaultText = 'Try them out:'
452 var result = $('#first').prepend('<b>buga</b>');
453 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
454 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
457 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
458 $('#sap').prepend(document.getElementById('first'));
459 ok( expected == $('#sap').text(), "Check for prepending of element" );
462 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
463 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
464 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
467 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
468 $('#sap').prepend($("#first, #yahoo"));
469 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
472 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
474 var defaultText = 'Try them out:'
475 $('<b>buga</b>').prependTo('#first');
476 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
477 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
480 var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
481 $(document.getElementById('first')).prependTo('#sap');
482 ok( expected == $('#sap').text(), "Check for prepending of element" );
485 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
486 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
487 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
490 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
491 $("#yahoo, #first").prependTo('#sap');
492 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
495 test("before(String|Element|Array<Element>|jQuery)", function() {
497 var expected = 'This is a normal link: bugaYahoo';
498 $('#yahoo').before('<b>buga</b>');
499 ok( expected == $('#en').text(), 'Insert String before' );
502 expected = "This is a normal link: Try them out:Yahoo";
503 $('#yahoo').before(document.getElementById('first'));
504 ok( expected == $('#en').text(), "Insert element before" );
507 expected = "This is a normal link: Try them out:diveintomarkYahoo";
508 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
509 ok( expected == $('#en').text(), "Insert array of elements before" );
512 expected = "This is a normal link: Try them out:diveintomarkYahoo";
513 $('#yahoo').before($("#first, #mark"));
514 ok( expected == $('#en').text(), "Insert jQuery before" );
517 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
519 var expected = 'This is a normal link: bugaYahoo';
520 $('<b>buga</b>').insertBefore('#yahoo');
521 ok( expected == $('#en').text(), 'Insert String before' );
524 expected = "This is a normal link: Try them out:Yahoo";
525 $(document.getElementById('first')).insertBefore('#yahoo');
526 ok( expected == $('#en').text(), "Insert element before" );
529 expected = "This is a normal link: Try them out:diveintomarkYahoo";
530 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
531 ok( expected == $('#en').text(), "Insert array of elements before" );
534 expected = "This is a normal link: Try them out:diveintomarkYahoo";
535 $("#first, #mark").insertBefore('#yahoo');
536 ok( expected == $('#en').text(), "Insert jQuery before" );
539 test("after(String|Element|Array<Element>|jQuery)", function() {
541 var expected = 'This is a normal link: Yahoobuga';
542 $('#yahoo').after('<b>buga</b>');
543 ok( expected == $('#en').text(), 'Insert String after' );
546 expected = "This is a normal link: YahooTry them out:";
547 $('#yahoo').after(document.getElementById('first'));
548 ok( expected == $('#en').text(), "Insert element after" );
551 expected = "This is a normal link: YahooTry them out:diveintomark";
552 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
553 ok( expected == $('#en').text(), "Insert array of elements after" );
556 expected = "This is a normal link: YahooTry them out:diveintomark";
557 $('#yahoo').after($("#first, #mark"));
558 ok( expected == $('#en').text(), "Insert jQuery after" );
561 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
563 var expected = 'This is a normal link: Yahoobuga';
564 $('<b>buga</b>').insertAfter('#yahoo');
565 ok( expected == $('#en').text(), 'Insert String after' );
568 expected = "This is a normal link: YahooTry them out:";
569 $(document.getElementById('first')).insertAfter('#yahoo');
570 ok( expected == $('#en').text(), "Insert element after" );
573 expected = "This is a normal link: YahooTry them out:diveintomark";
574 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
575 ok( expected == $('#en').text(), "Insert array of elements after" );
578 expected = "This is a normal link: YahooTry them out:diveintomark";
579 $("#mark, #first").insertAfter('#yahoo');
580 ok( expected == $('#en').text(), "Insert jQuery after" );
583 test("end()", function() {
585 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
586 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
590 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
593 test("find(String)", function() {
595 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
598 test("clone()", function() {
600 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
601 var clone = $('#yahoo').clone();
602 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
603 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
606 test("is(String)", function() {
608 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
609 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
610 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
611 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
612 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
613 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
614 ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
615 ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
616 ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
617 ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
618 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
619 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
620 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
621 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
622 ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
623 ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
624 ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
625 ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
626 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
627 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
628 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
629 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
631 // test is() with comma-seperated expressions
632 ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
633 ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
634 ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
635 ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
638 test("$.extend(Object, Object)", function() {
640 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
641 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
642 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
643 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
644 jQuery.extend(settings, options);
645 isObj( settings, merged, "Check if extended: settings must be extended" );
646 isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
649 test("$.extend(Object, Object, Object, Object)", function() {
651 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
652 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
653 options1 = { xnumber2: 1, xstring2: "x" },
654 options1Copy = { xnumber2: 1, xstring2: "x" },
655 options2 = { xstring2: "xx", xxx: "newstringx" },
656 options2Copy = { xstring2: "xx", xxx: "newstringx" },
657 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
658 var settings = jQuery.extend({}, defaults, options1, options2);
659 isObj( settings, merged, "Check if extended: settings must be extended" );
660 isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
661 isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
662 isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
665 test("val()", function() {
667 ok( $("#text1").val() == "Test", "Check for value of input element" );
668 ok( !$("#text1").val() == "", "Check for value of input element" );
671 test("val(String)", function() {
673 document.getElementById('text1').value = "bla";
674 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
675 $("#text1").val('test');
676 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
679 test("html(String)", function() {
682 div.html("<b>test</b>");
684 for ( var i = 0; i < div.size(); i++ ) {
685 if ( div.get(i).childNodes.length == 0 ) pass = false;
687 ok( pass, "Set HTML" );
689 $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>').evalScripts();
692 test("filter()", function() {
694 isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
695 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
696 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
697 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
700 test("not()", function() {
702 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
703 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
704 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
707 test("siblings([String])", function() {
709 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
710 isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
711 isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
712 isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "floatTest"), "Check for multiple filters" );
715 test("children([String])", function() {
717 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
718 isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
719 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
722 test("parent[s]([String])", function() {
724 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
725 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
726 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
727 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
729 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
730 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
731 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
732 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
735 test("next/prev([String])", function() {
737 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
738 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
739 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
740 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
742 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
743 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
744 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
745 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
748 test("show()", function() {
750 var pass = true, div = $("div");
751 div.show().each(function(){
752 if ( this.style.display == "none" ) pass = false;
757 test("addClass(String)", function() {
760 div.addClass("test");
762 for ( var i = 0; i < div.size(); i++ ) {
763 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
765 ok( pass, "Add Class" );
768 test("removeClass(String) - simple", function() {
770 var div = $("div").addClass("test").removeClass("test"),
772 for ( var i = 0; i < div.size(); i++ ) {
773 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
775 ok( pass, "Remove Class" );
778 var div = $("div").addClass("test").addClass("foo").addClass("bar");
779 div.removeClass("test").removeClass("bar").removeClass("foo");
781 for ( var i = 0; i < div.size(); i++ ) {
782 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
784 ok( pass, "Remove multiple classes" );
787 test("toggleClass(String)", function() {
789 var e = $("#firstp");
790 ok( !e.is(".test"), "Assert class not present" );
791 e.toggleClass("test");
792 ok( e.is(".test"), "Assert class present" );
793 e.toggleClass("test");
794 ok( !e.is(".test"), "Assert class not present" );
797 test("removeAttr(String", function() {
799 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
802 test("text(String)", function() {
804 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
807 test("$.each(Object,Function)", function() {
809 $.each( [0,1,2], function(i, n){
810 ok( i == n, "Check array iteration" );
813 $.each( [5,6,7], function(i, n){
814 ok( i == n - 5, "Check array iteration" );
817 $.each( { name: "name", lang: "lang" }, function(i, n){
818 ok( i == n, "Check object iteration" );
822 test("$.prop", function() {
824 var handle = function() { return this.id };
825 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
826 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
829 test("$.className", function() {
831 var x = $("<p>Hi</p>")[0];
834 ok( x.className == "hi", "Check single added class" );
836 ok( x.className == "hi foo bar", "Check more added classes" );
838 ok( x.className == "", "Remove all classes" );
839 c.add(x, "hi foo bar");
841 ok( x.className == "hi bar", "Check removal of one class" );
842 ok( c.has(x, "hi"), "Check has1" );
843 ok( c.has(x, "bar"), "Check has2" );
846 test("remove()", function() {
848 $("#ap").children().remove();
849 ok( $("#ap").text().length > 10, "Check text is not removed" );
850 ok( $("#ap").children().length == 0, "Check remove" );
853 $("#ap").children().remove("a");
854 ok( $("#ap").text().length > 10, "Check text is not removed" );
855 ok( $("#ap").children().length == 1, "Check filtered remove" );
858 test("empty()", function() {
860 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
861 ok( $("#ap").children().length == 4, "Check elements are not removed" );
864 test("eq(), gt(), lt(), contains()", function() {
866 ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
867 isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
868 isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
869 isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );