X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=829ef4a17b21474d3d4acd287e7ffb8df6aef212;hb=feabeb8572979f7888bd73e80f436bece1448e5a;hp=dca186744cd58d79818b63aab2a5f44bdd846be6;hpb=1293e80ff9afa96198c81fa79aa267a4ee44d9d1;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index dca1867..829ef4a 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -16,12 +16,12 @@ window.undefined = window.undefined; * Create a new jQuery Object * * @test ok( Array.prototype.push, "Array.push()" ); - * @test ok( Function.prototype.apply, "Function.apply()" ); - * @test ok( document.getElementById, "getElementById" ); - * @test ok( document.getElementsByTagName, "getElementsByTagName" ); - * @test ok( RegExp, "RegExp" ); - * @test ok( jQuery, "jQuery" ); - * @test ok( $, "$()" ); + * ok( Function.prototype.apply, "Function.apply()" ); + * ok( document.getElementById, "getElementById" ); + * ok( document.getElementsByTagName, "getElementsByTagName" ); + * ok( RegExp, "RegExp" ); + * ok( jQuery, "jQuery" ); + * ok( $, "$()" ); * * @constructor * @private @@ -31,7 +31,7 @@ window.undefined = window.undefined; jQuery = function(a,c) { // Shortcut for document ready (because $(document).each() is silly) - if ( a && a.constructor == Function && jQuery.fn.ready ) + if ( a && typeof a == "function" && jQuery.fn.ready ) return jQuery(document).ready(a); // Make sure that a selection was provided @@ -65,7 +65,7 @@ jQuery = function(a,c) { var fn = arguments[ arguments.length - 1 ]; // If so, execute it in context - if ( fn && fn.constructor == Function ) + if ( fn && typeof fn == "function" ) this.each(fn); }; @@ -349,14 +349,14 @@ jQuery.fn = jQuery.prototype = { * @result -1 * * @test ok( $([window, document]).index(window) == 0, "Check for index of elements" ); - * @test ok( $([window, document]).index(document) == 1, "Check for index of elements" ); - * @test var inputElements = $('#radio1,#radio2,#check1,#check2'); - * @test ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" ); - * @test ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" ); - * @test ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" ); - * @test ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" ); - * @test ok( inputElements.index(window) == -1, "Check for not found index" ); - * @test ok( inputElements.index(document) == -1, "Check for not found index" ); + * 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 @@ -381,17 +381,17 @@ jQuery.fn = jQuery.prototype = { * @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' ); - * @test ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' ); - * @test ok( $('#google').attr('title') == "Google!", 'Check for title attribute' ); - * @test ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' ); - * @test ok( $('#en').attr('lang') == "en", 'Check for lang attribute' ); - * @test ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' ); - * @test ok( $('#name').attr('name') == "name", 'Check for name attribute' ); - * @test ok( $('#text1').attr('name') == "action", 'Check for name attribute' ); - * @test ok( $('#form').attr('action') == "formaction", 'Check for action attribute' ); + * ok( $('#text1').attr('type') == "text", 'Check for type attribute' ); + * ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' ); + * ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' ); + * ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' ); + * ok( $('#google').attr('title') == "Google!", 'Check for title attribute' ); + * ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' ); + * ok( $('#en').attr('lang') == "en", 'Check for lang attribute' ); + * ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' ); + * ok( $('#name').attr('name') == "name", 'Check for name attribute' ); + * ok( $('#text1').attr('name') == "action", 'Check for name attribute' ); + * ok( $('#form').attr('action') == "formaction", 'Check for action attribute' ); * * @name attr * @type Object @@ -435,9 +435,9 @@ jQuery.fn = jQuery.prototype = { * } * ok( pass, "Set Attribute" ); * - * @test $("#name").attr('name', 'something'); + * $("#name").attr('name', 'something'); * ok( $("#name").name() == 'something', 'Set name attribute' ); - * @test $("#check2").attr('checked', true); + * $("#check2").attr('checked', true); * ok( document.getElementById('check2').checked == true, 'Set checked attribute' ); * $("#check2").attr('checked', false); * ok( document.getElementById('check2').checked == false, 'Set checked attribute' ); @@ -511,9 +511,9 @@ jQuery.fn = jQuery.prototype = { * @result
Test Paragraph.
* * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); - * @test $('#foo').css({display: 'none'}); + * $('#foo').css({display: 'none'}); * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); - * @test $('#foo').css({display: 'block'}); + * $('#foo').css({display: 'block'}); * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); * * @name css @@ -532,9 +532,9 @@ jQuery.fn = jQuery.prototype = { * * * @test ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible'); - * @test $('#foo').css('display', 'none'); + * $('#foo').css('display', 'none'); * ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden'); - * @test $('#foo').css('display', 'block'); + * $('#foo').css('display', 'block'); * ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible'); * * @name css @@ -667,6 +667,12 @@ jQuery.fn = jQuery.prototype = { * var result = $('#first').append('buga'); * ok( result.text() == defaultText + 'buga', 'Check if text appending works' ); * + * reset(); + * var expected = "Try them out: bla "; + * $('#first').append(" "); + * $('#first').append("bla "); + * ok( expected == $('#first').text(), "Check for appending of spaces" ); + * * @name append * @type jQuery * @param String html A string of HTML, that will be created on the fly and appended to the target. @@ -1013,7 +1019,7 @@ jQuery.fn = jQuery.prototype = { t.constructor == Boolean && ( t ? this.get() : [] ) || - t.constructor == Function && + typeof t == "function" && jQuery.grep( this, t ) || jQuery.filter(t,this).r, arguments ); @@ -1041,6 +1047,7 @@ jQuery.fn = jQuery.prototype = { * @example $("p").not("#selected") * @beforeHello
Hello Again
* @result [Hello
] + * * @test ok($("#main > p#ap > a").not("#google").length == 2, ".not") * * @name not @@ -1121,27 +1128,27 @@ jQuery.fn = jQuery.prototype = { * @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' ); + * ok( !$('#form').is('div'), 'Check for element: A form is not a div' ); + * ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' ); + * ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' ); + * ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' ); + * ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' ); + * ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' ); + * ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' ); + * ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' ); + * ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' ); + * ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' ); + * ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' ); + * ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' ); + * ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' ); + * ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' ); + * ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' ); + * ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' ); + * ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' ); + * ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' ); + * ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' ); + * ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' ); + * ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' ); * * @name is * @type Boolean @@ -1151,6 +1158,52 @@ jQuery.fn = jQuery.prototype = { is: function(expr) { return expr ? jQuery.filter(expr,this).r.length > 0 : false; }, + + /** + * Executes the first callback for every element that fits the expression + * and executes the second callback for every element that does not fit + * the expression. + * + * @example $('div').ifelse(':visible', + * function() { $(this).slideUp(); }, + function() { $(this).slideDown(); } + * ); + * @desc Slides down all visible div elements and slides down all others + * + * @test var checked = 0, notChecked = 0; + * var inputChecked = $(':input').ifelse(':checked', + * function() { checked++; }, + * function() { notChecked++ } + * ); + * ok( checked == 2, 'Check is if/else: Count checked elements' ); + * ok( notChecked == 12, 'Check is if/else: Count unchecked elements' ); + * + * $('#first, #foo, #ap').ifelse('p', + * function() { $(this).html('me == p') }, + * function() { $(this).html('me != p') } + * ); + * ok( $('#first').text() == 'me == p', 'Check filter-if-clause' ); + * ok( $('#foo').text() == 'me != p', 'Check else-clause' ); + * ok( $('#ap').text() == 'me == p', 'Check filter-if-clause' ); + * + * @name ifelse + * @type jQuery + * @param String expression The expression with which to filter + * @param Function ifCallback Called for elements that fit the expression + * @param Function elseCallback Called for elements that don't fit the expression + * @cat DOM/Traversing + */ + ifelse: function(expr, ifCallback, elseCallback) { + var ifCallback = ifCallback || function() {}; + var elseCalllback = elseCallback || function() {}; + return this.each(function() { + if($(this).is(expr)) { + ifCallback.apply(this); + } else { + elseCallback.apply(this); + } + }); + }, /** * @@ -1208,7 +1261,7 @@ jQuery.fn = jQuery.prototype = { } else { var old = this.get(); this.get( a ); - if ( fn.constructor == Function ) + if ( typeof fn == "function" ) this.each( fn ); this.get( old ); } @@ -1367,9 +1420,22 @@ jQuery.extend({ o.className += ( o.className ? " " : "" ) + c; }, remove: function(o,c){ + /* o.className = !c ? "" : o.className.replace( - new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), ""); + new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");*/ + if( !c ) { + o.className = ""; + } else { + var classes = o.className.split(" "); + for(var i=0; iHello
And Again
* @result [Hello Again
] * * @test isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" ); - * @test isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" ); + * isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" ); * * @name siblings * @type jQuery * @param String expr An expression to filter the sibling Elements with * @cat DOM/Traversing */ - siblings: jQuery.sibling, + siblings: "jQuery.sibling(a, null, true)", /** @@ -2993,6 +3115,8 @@ jQuery.macros = { * @beforeHello
And Again
* @result [ Hello Again ] * + * @test isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" ); + * * @name children * @type jQuery * @cat DOM/Traversing @@ -3006,6 +3130,8 @@ jQuery.macros = { * @beforeHello Again
And Again
Hello Again
] * + * @test isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" ); + * * @name children * @type jQuery * @param String expr An expression to filter the child Elements with @@ -3134,6 +3260,16 @@ jQuery.macros = { * if ( div.get(i).className.indexOf("test") != -1 ) pass = false; * } * ok( pass, "Remove Class" ); + * + * reset(); + * + * var div = $("div").addClass("test").addClass("foo").addClass("bar"); + * div.removeClass("test").removeClass("bar").removeClass("foo"); + * var pass = true; + * for ( var i = 0; i < div.size(); i++ ) { + * if ( div.get(i).className.match(/test|bar|foo/) ) pass = false; + * } + * ok( pass, "Remove multiple classes" ); * * @name removeClass * @type jQuery