3 test("Basic requirements", function() {
\r
5 ok( Array.prototype.push, "Array.push()" );
\r
6 ok( Function.prototype.apply, "Function.apply()" );
\r
7 ok( document.getElementById, "getElementById" );
\r
8 ok( document.getElementsByTagName, "getElementsByTagName" );
\r
9 ok( RegExp, "RegExp" );
\r
10 ok( jQuery, "jQuery" );
\r
14 test("$()", function() {
\r
15 var main = $("#main");
\r
16 isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
\r
18 // make sure this is handled
\r
22 test("isFunction", function() {
\r
25 // Make sure that false values return false
\r
26 ok( !jQuery.isFunction(), "No Value" );
\r
27 ok( !jQuery.isFunction( null ), "null Value" );
\r
28 ok( !jQuery.isFunction( undefined ), "undefined Value" );
\r
29 ok( !jQuery.isFunction( "" ), "Empty String Value" );
\r
30 ok( !jQuery.isFunction( 0 ), "0 Value" );
\r
33 // Safari uses "(Internal Function)"
\r
34 ok( jQuery.isFunction(String), "String Function" );
\r
35 ok( jQuery.isFunction(Array), "Array Function" );
\r
36 ok( jQuery.isFunction(Object), "Object Function" );
\r
37 ok( jQuery.isFunction(Function), "Function Function" );
\r
39 // When stringified, this could be misinterpreted
\r
40 var mystr = "function";
\r
41 ok( !jQuery.isFunction(mystr), "Function String" );
\r
43 // When stringified, this could be misinterpreted
\r
44 var myarr = [ "function" ];
\r
45 ok( !jQuery.isFunction(myarr), "Function Array" );
\r
47 // When stringified, this could be misinterpreted
\r
48 var myfunction = { "function": "test" };
\r
49 ok( !jQuery.isFunction(myfunction), "Function Object" );
\r
51 // Make sure normal functions still work
\r
52 var fn = function(){};
\r
53 ok( jQuery.isFunction(fn), "Normal Function" );
\r
55 var obj = document.createElement("object");
\r
57 // Firefox says this is a function
\r
58 ok( !jQuery.isFunction(obj), "Object Element" );
\r
60 // IE says this is an object
\r
61 ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
\r
63 var nodes = document.body.childNodes;
\r
65 // Safari says this is a function
\r
66 ok( !jQuery.isFunction(nodes), "childNodes Property" );
\r
68 var first = document.body.firstChild;
\r
70 // Normal elements are reported ok everywhere
\r
71 ok( !jQuery.isFunction(first), "A normal DOM Element" );
\r
73 var input = document.createElement("input");
\r
74 input.type = "text";
\r
75 document.body.appendChild( input );
\r
77 // IE says this is an object
\r
78 ok( jQuery.isFunction(input.focus), "A default function property" );
\r
80 document.body.removeChild( input );
\r
82 var a = document.createElement("a");
\r
83 a.href = "some-function";
\r
84 document.body.appendChild( a );
\r
86 // This serializes with the word 'function' in it
\r
87 ok( !jQuery.isFunction(a), "Anchor Element" );
\r
89 document.body.removeChild( a );
\r
91 // Recursive function calls have lengths and array-like properties
\r
92 function callme(callback){
\r
93 function fn(response){
\r
97 ok( jQuery.isFunction(fn), "Recursive Function Call" );
\r
99 fn({ some: "data" });
\r
103 callme(function(){});
\r
107 test("length", function() {
\r
108 ok( $("div").length == 2, "Get Number of Elements Found" );
\r
111 test("size()", function() {
\r
112 ok( $("div").size() == 2, "Get Number of Elements Found" );
\r
115 test("get()", function() {
\r
116 isSet( $("div").get(), q("main","foo"), "Get All Elements" );
\r
119 test("get(Number)", function() {
\r
120 ok( $("div").get(0) == document.getElementById("main"), "Get A Single Element" );
\r
123 test("add(String|Element|Array)", function() {
\r
124 isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
\r
125 isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
\r
126 ok( $([]).add($("#form")[0].elements).length > 13, "Check elements from array" );
\r
128 var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
\r
129 ok( x[0].id == "x1", "Check on-the-fly element1" );
\r
130 ok( x[1].id == "x2", "Check on-the-fly element2" );
\r
132 var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
\r
133 ok( x[0].id == "x1", "Check on-the-fly element1" );
\r
134 ok( x[1].id == "x2", "Check on-the-fly element2" );
\r
137 test("each(Function)", function() {
\r
139 var div = $("div");
\r
140 div.each(function(){this.foo = 'zoo';});
\r
142 for ( var i = 0; i < div.size(); i++ ) {
\r
143 if ( div.get(i).foo != "zoo" ) pass = false;
\r
145 ok( pass, "Execute a function, Relative" );
\r
148 test("index(Object)", function() {
\r
150 ok( $([window, document]).index(window) == 0, "Check for index of elements" );
\r
151 ok( $([window, document]).index(document) == 1, "Check for index of elements" );
\r
152 var inputElements = $('#radio1,#radio2,#check1,#check2');
\r
153 ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
\r
154 ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
\r
155 ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
\r
156 ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
\r
157 ok( inputElements.index(window) == -1, "Check for not found index" );
\r
158 ok( inputElements.index(document) == -1, "Check for not found index" );
\r
161 test("attr(String)", function() {
\r
163 ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
\r
164 ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
\r
165 ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
\r
166 ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
\r
167 ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
\r
168 ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
\r
169 ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
\r
170 ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
\r
171 ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
\r
172 ok( $('#name').attr('name') == "name", 'Check for name attribute' );
\r
173 ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
\r
174 ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
\r
176 $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
\r
177 ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
\r
180 if ( location.protocol != "file:" ) {
\r
181 test("attr(String) in XML Files", function() {
\r
184 $.get("data/dashboard.xml", function(xml) {
\r
185 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
\r
186 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
\r
192 test("attr(String, Function)", function() {
\r
194 ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
\r
195 ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
\r
198 test("attr(Hash)", function() {
\r
201 $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
\r
202 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
\r
204 ok( pass, "Set Multiple Attributes" );
\r
207 test("attr(String, Object)", function() {
\r
209 var div = $("div");
\r
210 div.attr("foo", "bar");
\r
212 for ( var i = 0; i < div.size(); i++ ) {
\r
213 if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
\r
215 ok( pass, "Set Attribute" );
\r
217 ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
\r
219 $("#name").attr('name', 'something');
\r
220 ok( $("#name").attr('name') == 'something', 'Set name attribute' );
\r
221 $("#check2").attr('checked', true);
\r
222 ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
\r
223 $("#check2").attr('checked', false);
\r
224 ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
\r
225 $("#text1").attr('readonly', true);
\r
226 ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
\r
227 $("#text1").attr('readonly', false);
\r
228 ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
\r
231 if ( location.protocol != "file:" ) {
\r
232 test("attr(String, Object)x", function() {
\r
235 $.get('data/dashboard.xml', function(xml) {
\r
237 $('tab', xml).each(function() {
\r
238 titles.push($(this).attr('title'));
\r
240 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
\r
241 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
\r
247 test("css(String|Hash)", function() {
\r
250 ok( $('#main').css("display") == 'none', 'Check for css property "display"');
\r
252 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
\r
253 $('#foo').css({display: 'none'});
\r
254 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
\r
255 $('#foo').css({display: 'block'});
\r
256 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
\r
258 $('#floatTest').css({styleFloat: 'right'});
\r
259 ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
\r
260 $('#floatTest').css({cssFloat: 'left'});
\r
261 ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
\r
262 $('#floatTest').css({'float': 'right'});
\r
263 ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
\r
264 $('#floatTest').css({'font-size': '30px'});
\r
265 ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
\r
267 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
\r
268 $('#foo').css({opacity: n});
\r
269 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
\r
270 $('#foo').css({opacity: parseFloat(n)});
\r
271 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
\r
273 $('#foo').css({opacity: ''});
\r
274 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
\r
277 test("css(String, Object)", function() {
\r
279 ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
\r
280 $('#foo').css('display', 'none');
\r
281 ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
\r
282 $('#foo').css('display', 'block');
\r
283 ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
\r
285 $('#floatTest').css('styleFloat', 'left');
\r
286 ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
\r
287 $('#floatTest').css('cssFloat', 'right');
\r
288 ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
\r
289 $('#floatTest').css('float', 'left');
\r
290 ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
\r
291 $('#floatTest').css('font-size', '20px');
\r
292 ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
\r
294 $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
\r
295 $('#foo').css('opacity', n);
\r
296 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
\r
297 $('#foo').css('opacity', parseFloat(n));
\r
298 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
\r
300 $('#foo').css('opacity', '');
\r
301 ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
\r
304 test("text()", function() {
\r
305 var expected = "This link has class=\"blog\": Simon Willison's Weblog";
\r
306 ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
\r
309 test("wrap(String|Element)", function() {
\r
311 var defaultText = 'Try them out:'
\r
312 var result = $('#first').wrap('<div class="red"><span></span></div>').text();
\r
313 ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
\r
314 ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
\r
317 var defaultText = 'Try them out:'
\r
318 var result = $('#first').wrap(document.getElementById('empty')).parent();
\r
319 ok( result.is('ol'), 'Check for element wrapping' );
\r
320 ok( result.text() == defaultText, 'Check for element wrapping' );
\r
323 test("append(String|Element|Array<Element>|jQuery)", function() {
\r
325 var defaultText = 'Try them out:'
\r
326 var result = $('#first').append('<b>buga</b>');
\r
327 ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
\r
328 ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
\r
331 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
\r
332 $('#sap').append(document.getElementById('first'));
\r
333 ok( expected == $('#sap').text(), "Check for appending of element" );
\r
336 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
337 $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
\r
338 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
\r
341 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
342 $('#sap').append($("#first, #yahoo"));
\r
343 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
\r
346 $("#sap").append( 5 );
\r
347 ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
\r
350 $("#sap").append( " text with spaces " );
\r
351 ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
\r
354 ok( $("#sap").append([]), "Check for appending an empty array." );
\r
355 ok( $("#sap").append(""), "Check for appending an empty string." );
\r
356 ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
\r
359 $("#sap").append(document.getElementById('form'));
\r
360 ok( $("#sap>form").size() == 1, "Check for appending a form" ); // Bug #910
\r
365 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
\r
370 ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
\r
374 test("appendTo(String|Element|Array<Element>|jQuery)", function() {
\r
376 var defaultText = 'Try them out:'
\r
377 $('<b>buga</b>').appendTo('#first');
\r
378 ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
\r
379 ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
\r
382 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
\r
383 $(document.getElementById('first')).appendTo('#sap');
\r
384 ok( expected == $('#sap').text(), "Check for appending of element" );
\r
387 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
388 $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
\r
389 ok( expected == $('#sap').text(), "Check for appending of array of elements" );
\r
392 expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
\r
393 $("#first, #yahoo").appendTo('#sap');
\r
394 ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
\r
397 test("prepend(String|Element|Array<Element>|jQuery)", function() {
\r
399 var defaultText = 'Try them out:'
\r
400 var result = $('#first').prepend('<b>buga</b>');
\r
401 ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
\r
402 ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
\r
405 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
\r
406 $('#sap').prepend(document.getElementById('first'));
\r
407 ok( expected == $('#sap').text(), "Check for prepending of element" );
\r
410 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
411 $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
\r
412 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
\r
415 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
416 $('#sap').prepend($("#first, #yahoo"));
\r
417 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
\r
420 test("prependTo(String|Element|Array<Element>|jQuery)", function() {
\r
422 var defaultText = 'Try them out:'
\r
423 $('<b>buga</b>').prependTo('#first');
\r
424 ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
\r
425 ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
\r
428 expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
\r
429 $(document.getElementById('first')).prependTo('#sap');
\r
430 ok( expected == $('#sap').text(), "Check for prepending of element" );
\r
433 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
434 $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
\r
435 ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
\r
438 expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
\r
439 $("#yahoo, #first").prependTo('#sap');
\r
440 ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
\r
443 test("before(String|Element|Array<Element>|jQuery)", function() {
\r
445 var expected = 'This is a normal link: bugaYahoo';
\r
446 $('#yahoo').before('<b>buga</b>');
\r
447 ok( expected == $('#en').text(), 'Insert String before' );
\r
450 expected = "This is a normal link: Try them out:Yahoo";
\r
451 $('#yahoo').before(document.getElementById('first'));
\r
452 ok( expected == $('#en').text(), "Insert element before" );
\r
455 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
456 $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
\r
457 ok( expected == $('#en').text(), "Insert array of elements before" );
\r
460 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
461 $('#yahoo').before($("#first, #mark"));
\r
462 ok( expected == $('#en').text(), "Insert jQuery before" );
\r
465 test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
\r
467 var expected = 'This is a normal link: bugaYahoo';
\r
468 $('<b>buga</b>').insertBefore('#yahoo');
\r
469 ok( expected == $('#en').text(), 'Insert String before' );
\r
472 expected = "This is a normal link: Try them out:Yahoo";
\r
473 $(document.getElementById('first')).insertBefore('#yahoo');
\r
474 ok( expected == $('#en').text(), "Insert element before" );
\r
477 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
478 $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
\r
479 ok( expected == $('#en').text(), "Insert array of elements before" );
\r
482 expected = "This is a normal link: Try them out:diveintomarkYahoo";
\r
483 $("#first, #mark").insertBefore('#yahoo');
\r
484 ok( expected == $('#en').text(), "Insert jQuery before" );
\r
487 test("after(String|Element|Array<Element>|jQuery)", function() {
\r
489 var expected = 'This is a normal link: Yahoobuga';
\r
490 $('#yahoo').after('<b>buga</b>');
\r
491 ok( expected == $('#en').text(), 'Insert String after' );
\r
494 expected = "This is a normal link: YahooTry them out:";
\r
495 $('#yahoo').after(document.getElementById('first'));
\r
496 ok( expected == $('#en').text(), "Insert element after" );
\r
499 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
500 $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
\r
501 ok( expected == $('#en').text(), "Insert array of elements after" );
\r
504 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
505 $('#yahoo').after($("#first, #mark"));
\r
506 ok( expected == $('#en').text(), "Insert jQuery after" );
\r
509 test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
\r
511 var expected = 'This is a normal link: Yahoobuga';
\r
512 $('<b>buga</b>').insertAfter('#yahoo');
\r
513 ok( expected == $('#en').text(), 'Insert String after' );
\r
516 expected = "This is a normal link: YahooTry them out:";
\r
517 $(document.getElementById('first')).insertAfter('#yahoo');
\r
518 ok( expected == $('#en').text(), "Insert element after" );
\r
521 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
522 $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
\r
523 ok( expected == $('#en').text(), "Insert array of elements after" );
\r
526 expected = "This is a normal link: YahooTry them out:diveintomark";
\r
527 $("#mark, #first").insertAfter('#yahoo');
\r
528 ok( expected == $('#en').text(), "Insert jQuery after" );
\r
531 test("end()", function() {
\r
533 ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
\r
534 ok( $('#yahoo').end(), 'Check for end with nothing to end' );
\r
536 var x = $('#yahoo');
\r
538 ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
\r
541 test("find(String)", function() {
\r
542 ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
\r
545 test("clone()", function() {
\r
547 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
\r
548 var clone = $('#yahoo').clone();
\r
549 ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
\r
550 ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
\r
553 test("is(String)", function() {
\r
555 ok( $('#form').is('form'), 'Check for element: A form must be a form' );
\r
556 ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
\r
557 ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
\r
558 ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
\r
559 ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
\r
560 ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
\r
561 ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
\r
562 ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
\r
563 ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
\r
564 ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
\r
565 ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
\r
566 ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
\r
567 ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
\r
568 ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
\r
569 ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
\r
570 ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
\r
571 ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
\r
572 ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
\r
573 ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
\r
574 ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
\r
575 ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
\r
576 ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
\r
578 // test is() with comma-seperated expressions
\r
579 ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
\r
580 ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
\r
581 ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
\r
582 ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
\r
585 test("$.extend(Object, Object)", function() {
\r
587 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
588 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
589 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
\r
590 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" };
\r
591 jQuery.extend(settings, options);
\r
592 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
593 isSet ( options, optionsCopy, "Check if not modified: options must not be modified" );
\r
596 test("$.extend(Object, Object, Object, Object)", function() {
\r
598 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
599 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
\r
600 options1 = { xnumber2: 1, xstring2: "x" },
\r
601 options1Copy = { xnumber2: 1, xstring2: "x" },
\r
602 options2 = { xstring2: "xx", xxx: "newstringx" },
\r
603 options2Copy = { xstring2: "xx", xxx: "newstringx" },
\r
604 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
\r
605 var settings = jQuery.extend({}, defaults, options1, options2);
\r
606 isSet( settings, merged, "Check if extended: settings must be extended" );
\r
607 isSet ( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
\r
608 isSet ( options1, options1Copy, "Check if not modified: options1 must not be modified" );
\r
609 isSet ( options2, options2Copy, "Check if not modified: options2 must not be modified" );
\r
612 test("val()", function() {
\r
614 ok( $("#text1").val() == "Test", "Check for value of input element" );
\r
615 ok( !$("#text1").val() == "", "Check for value of input element" );
\r
618 test("val(String)", function() {
\r
620 document.getElementById('text1').value = "bla";
\r
621 ok( $("#text1").val() == "bla", "Check for modified value of input element" );
\r
622 $("#text1").val('test');
\r
623 ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
\r
626 test("html(String)", function() {
\r
628 var div = $("div");
\r
629 div.html("<b>test</b>");
\r
631 for ( var i = 0; i < div.size(); i++ ) {
\r
632 if ( div.get(i).childNodes.length == 0 ) pass = false;
\r
634 ok( pass, "Set HTML" );
\r
637 test("filter()", function() {
\r
639 isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
\r
640 isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
\r
641 isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
\r
642 isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
\r
645 test("not()", function() {
\r
647 ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
\r
648 isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
\r
649 isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
\r
653 test("siblings([String])", function() {
\r
655 isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
\r
656 isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" );
\r
657 isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
\r
658 isSet( $("#foo").siblings("form, b").get(), q("form", "floatTest"), "Check for multiple filters" );
\r
661 test("children([String])", function() {
\r
663 isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
\r
664 isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
\r
665 isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
\r
668 test("parent[s]([String])", function() {
\r
670 ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
\r
671 ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
\r
672 ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
\r
673 ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
\r
675 ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
\r
676 ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
\r
677 ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
\r
678 isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
\r
681 test("next/prev([String])", function() {
\r
683 ok( $("#ap").next()[0].id == "foo", "Simple next check" );
\r
684 ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
\r
685 ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
\r
686 ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
\r
688 ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
\r
689 ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
\r
690 ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
\r
691 ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
\r
694 test("show()", function() {
\r
696 var pass = true, div = $("div");
\r
697 div.show().each(function(){
\r
698 if ( this.style.display == "none" ) pass = false;
\r
700 ok( pass, "Show" );
\r
703 test("addClass(String)", function() {
\r
704 var div = $("div");
\r
705 div.addClass("test");
\r
707 for ( var i = 0; i < div.size(); i++ ) {
\r
708 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
\r
710 ok( pass, "Add Class" );
\r
713 test("removeClass(String) - simple", function() {
\r
715 var div = $("div").addClass("test").removeClass("test"),
\r
717 for ( var i = 0; i < div.size(); i++ ) {
\r
718 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
\r
720 ok( pass, "Remove Class" );
\r
723 test("removeClass(String) - add three classes and remove again", function() {
\r
725 var div = $("div").addClass("test").addClass("foo").addClass("bar");
\r
726 div.removeClass("test").removeClass("bar").removeClass("foo");
\r
728 for ( var i = 0; i < div.size(); i++ ) {
\r
729 if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
\r
731 ok( pass, "Remove multiple classes" );
\r
734 test("toggleClass(String)", function() {
\r
736 var e = $("#firstp");
\r
737 ok( !e.is(".test"), "Assert class not present" );
\r
738 e.toggleClass("test");
\r
739 ok( e.is(".test"), "Assert class present" );
\r
740 e.toggleClass("test");
\r
741 ok( !e.is(".test"), "Assert class not present" );
\r
744 test("removeAttr(String", function() {
\r
745 ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
\r
748 test("text(String)", function() {
\r
750 ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
\r
753 test("$.each(Object,Function)", function() {
\r
755 $.each( [0,1,2], function(i, n){
\r
756 ok( i == n, "Check array iteration" );
\r
759 $.each( [5,6,7], function(i, n){
\r
760 ok( i == n - 5, "Check array iteration" );
\r
763 $.each( { name: "name", lang: "lang" }, function(i, n){
\r
764 ok( i == n, "Check object iteration" );
\r
768 test("$.prop", function() {
\r
770 var handle = function() { return this.id };
\r
771 ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
\r
772 ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
\r
775 test("$.className", function() {
\r
777 var x = $("<p>Hi</p>")[0];
\r
778 var c = $.className;
\r
780 ok( x.className == "hi", "Check single added class" );
\r
781 c.add(x, "foo bar");
\r
782 ok( x.className == "hi foo bar", "Check more added classes" );
\r
784 ok( x.className == "", "Remove all classes" );
\r
785 c.add(x, "hi foo bar");
\r
786 c.remove(x, "foo");
\r
787 ok( x.className == "hi bar", "Check removal of one class" );
\r
788 ok( c.has(x, "hi"), "Check has1" );
\r
789 ok( c.has(x, "bar"), "Check has2" );
\r
792 test("remove()", function() {
\r
793 $("#ap").children().remove();
\r
794 ok( $("#ap").text().length > 10, "Check text is not removed" );
\r
795 ok( $("#ap").children().length == 0, "Check remove" );
\r
798 $("#ap").children().remove("a");
\r
799 ok( $("#ap").text().length > 10, "Check text is not removed" );
\r
800 ok( $("#ap").children().length == 1, "Check filtered remove" );
\r
803 test("empty()", function() {
\r
804 ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
\r
805 ok( $("#ap").children().length == 4, "Check elements are not removed" );
\r
808 test("eq(), gt(), lt(), contains()", function() {
\r
809 ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
\r
810 isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
\r
811 isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
\r
812 isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );
\r
815 test("click() context", function() {
\r
816 $('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
\r
817 var close = $('spanx', this); // same with $(this).find('span');
\r
818 ok( close.length == 0, "Element does not exist, length must be zero" );
\r
819 ok( !close[0], "Element does not exist, direct access to element must return undefined" );
\r
820 //console.log( close[0]); // it's the <a> and not a <span> element
\r