X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=70c3c605c41f3c47e4b7eda44ce53f9c74208dfb;hb=cc5625067a6bb75d357181cf6483ae0ab367c73f;hp=c025c816802027ea4328f0cbe822a04714881d3c;hpb=519b7d33e2e40c9d6f5defa67500979af2123d99;p=jquery.git
diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js
index c025c81..70c3c60 100644
--- a/src/jquery/jquery.js
+++ b/src/jquery/jquery.js
@@ -16,22 +16,22 @@ 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
* @name jQuery
* @cat Core
*/
-function jQuery(a,c) {
+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,9 +65,9 @@ function jQuery(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);
-}
+};
// Map over the $ in case of overwrite
if ( typeof $ != "undefined" )
@@ -127,9 +127,8 @@ if ( typeof $ != "undefined" )
* @before
one
two
three
* @result [
two
]
*
- * @example $(document).ready( loaded );
- * @desc Executes the "loaded" function when the DOM is ready to
- * be manipulated.
+ * @example $(document.body).background( "black" );
+ * @desc Sets the background color of the page to black.
*
* @name $
* @param Element elem A DOM element to be encapsulated by a jQuery object.
@@ -151,10 +150,15 @@ if ( typeof $ != "undefined" )
/**
* A shorthand for $(document).ready(), allowing you to bind a function
- * to be executed when the DOM document has finished loading.
+ * to be executed when the DOM document has finished loading. This function
+ * behaves just like $(document).ready(), in that it should be used to wrap
+ * all of the other $() operations on your page. While this function is,
+ * technically, chainable - there really isn't much use for chaining against it.
*
- * @example $( loaded )
- * @desc Executes the function "loaded" when the DOM is ready to be used.
+ * @example $(function(){
+ * // Document is ready
+ * });
+ * @desc Executes the function when the DOM is ready to be used.
*
* @name $
* @param Function fn The function to execute when the DOM is ready.
@@ -163,11 +167,13 @@ if ( typeof $ != "undefined" )
*/
/**
- * A means of creating a duplicate copy of a jQuery object.
+ * A means of creating a cloned copy of a jQuery object. This function
+ * copies the set of matched elements from one jQuery object and creates
+ * another, new, jQuery object containing the same elements.
*
* @example var div = $("div");
- * $( div ).find("p")
- * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div'.
+ * $( div ).find("p");
+ * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div' (as would normally be the case if a simple div.find("p") was done).
*
* @name $
* @param jQuery obj The jQuery object to be cloned.
@@ -197,7 +203,7 @@ jQuery.fn = jQuery.prototype = {
* @before
* @result 2
*
- * @test cmpOK( $("div").length, "==", 2, "Get Number of Elements Found" );
+ * @test ok( $("div").length == 2, "Get Number of Elements Found" );
*
* @property
* @name length
@@ -212,7 +218,7 @@ jQuery.fn = jQuery.prototype = {
* @before
* @result 2
*
- * @test cmpOK( $("div").size(), "==", 2, "Get Number of Elements Found" );
+ * @test ok( $("div").size() == 2, "Get Number of Elements Found" );
*
* @name size
* @type Number
@@ -246,7 +252,7 @@ jQuery.fn = jQuery.prototype = {
* @before
* @result [ ]
*
- * @test cmpOK( $("div").get(0), "==", document.getElementById("main"), "Get A Single Element" );
+ * @test ok( $("div").get(0) == document.getElementById("main"), "Get A Single Element" );
*
* @name get
* @type Element
@@ -325,6 +331,38 @@ jQuery.fn = jQuery.prototype = {
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
+ * @result 0
+ *
+ * @example $("*").index(document.getElementById('foo'))
+ * @before
+ * @result 2
+ *
+ * @example $("*").index(document.getElementById('bar'))
+ * @before
+ * @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){
@@ -342,6 +380,19 @@ jQuery.fn = jQuery.prototype = {
* @before
* @result test.jpg
*
+ * @test ok( $('#text1').attr('value') == "Test", 'Check for value 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
* @param String name The name of the property to access.
@@ -384,6 +435,13 @@ jQuery.fn = jQuery.prototype = {
* }
* ok( pass, "Set Attribute" );
*
+ * $("#name").attr('name', 'something');
+ * ok( $("#name").name() == 'something', 'Set name attribute' );
+ * $("#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' );
+ *
* @name attr
* @type jQuery
* @param String key The name of the property to set.
@@ -423,6 +481,19 @@ jQuery.fn = jQuery.prototype = {
* @example $("p").css("color");
* @before
Test Paragraph.
* @result red
+ * @desc Retrieves the color style of the first paragraph
+ *
+ * @example $("p").css("fontWeight");
+ * @before
Test Paragraph.
+ * @result bold
+ * @desc Retrieves the font-weight style of the first paragraph.
+ * Note that for all style properties with a dash (like 'font-weight'), you have to
+ * write it in camelCase. In other words: Every time you have a '-' in a
+ * property, remove it and replace the next character with an uppercase
+ * representation of itself. Eg. fontWeight, fontSize, fontFamily, borderWidth,
+ * borderStyle, borderBottomWidth etc.
+ *
+ * @test ok( $('#foo').css("display") == 'block', 'Check for css property "display"');
*
* @name css
* @type Object
@@ -439,6 +510,12 @@ jQuery.fn = jQuery.prototype = {
* @before
Test Paragraph.
* @result
Test Paragraph.
*
+ * @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.
@@ -451,6 +528,14 @@ jQuery.fn = jQuery.prototype = {
* @example $("p").css("color","red");
* @before
Test Paragraph.
* @result
Test Paragraph.
+ * @desc Changes the color of all paragraphs to red
+ *
+ *
+ * @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
@@ -471,6 +556,9 @@ jQuery.fn = jQuery.prototype = {
* @before
Test Paragraph.
* @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
@@ -499,9 +587,17 @@ jQuery.fn = jQuery.prototype = {
* and finds the deepest ancestor element within its
* structure - it is that element that will en-wrap everything else.
*
+ * This does not work with elements that contain text. Any necessary text
+ * must be added after the wrapping is done.
+ *
* @example $("p").wrap("");
* @before
Test Paragraph.
* @result
Test Paragraph.
+ *
+ * @test var defaultText = 'Try them out:'
+ * var result = $('#first').wrap('
').text();
+ * ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
+ * ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
*
* @name wrap
* @type jQuery
@@ -519,9 +615,17 @@ jQuery.fn = jQuery.prototype = {
* provided and finding the deepest ancestor element within its
* structure - it is that element that will en-wrap everything else.
*
- * @example $("p").wrap("");
- * @before
Test Paragraph.
- * @result
Test Paragraph.
+ * This does not work with elements that contain text. Any necessary text
+ * must be added after the wrapping is done.
+ *
+ * @example $("p").wrap( document.getElementById('content') );
+ * @before
Test Paragraph.
+ * @result
Test Paragraph.
+ *
+ * @test var defaultText = 'Try them out:'
+ * var result = $('#first').wrap(document.getElementById('empty')).parent();
+ * ok( result.is('ol'), 'Check for element wrapping' );
+ * ok( result.text() == defaultText, 'Check for element wrapping' );
*
* @name wrap
* @type jQuery
@@ -559,6 +663,16 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
* @result
I would like to say: Hello
*
+ * @test var defaultText = 'Try them out:'
+ * 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.
@@ -574,6 +688,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
Hello
* @result
I would like to say: Hello
*
+ * @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.
@@ -589,6 +707,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
Hello
* @result
I would like to say: Hello
*
+ * @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 elems An array of elements, all of which will be appended.
@@ -610,6 +732,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
* @result
HelloI would like to say:
*
+ * @test var defaultText = 'Try them out:'
+ * var result = $('#first').prepend('buga');
+ * 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.
@@ -617,13 +743,17 @@ jQuery.fn = jQuery.prototype = {
*/
/**
- * 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
I would like to say:
Hello
* @result
HelloI would like to say:
+ *
+ * @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
@@ -632,7 +762,7 @@ jQuery.fn = jQuery.prototype = {
*/
/**
- * 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.
*
@@ -640,6 +770,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
Hello
* @result
HelloI would like to say:
*
+ * @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 elems An array of elements, all of which will be appended.
@@ -659,6 +793,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
* @result Hello
I would like to say:
*
+ * @test var expected = 'This is a normal link: bugaYahoo';
+ * $('#yahoo').before('buga');
+ * 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.
@@ -672,6 +810,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
Hello
* @result Hello
I would like to say:
*
+ * @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.
@@ -685,6 +827,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
Hello
* @result Hello
I would like to say:
*
+ * @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 elems An array of elements, all of which will be appended.
@@ -704,6 +850,10 @@ jQuery.fn = jQuery.prototype = {
* @before
I would like to say:
* @result
I would like to say:
Hello
*
+ * @test var expected = 'This is a normal link: Yahoobuga';
+ * $('#yahoo').after('buga');
+ * 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.
@@ -717,6 +867,10 @@ jQuery.fn = jQuery.prototype = {
* @before Hello
I would like to say:
* @result
I would like to say:
Hello
*
+ * @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.
@@ -730,6 +884,10 @@ jQuery.fn = jQuery.prototype = {
* @before Hello
I would like to say:
* @result
I would like to say:
Hello
*
+ * @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 elems An array of elements, all of which will be appended.
@@ -750,6 +908,8 @@ jQuery.fn = jQuery.prototype = {
* @before
]
*
+ * @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
@@ -2826,6 +3211,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
@@ -2902,24 +3297,31 @@ jQuery.macros = {
},
/**
- * Binds a particular event (like click) to a each of a set of match elements.
- *
- * @example $("p").bind( "click", function() { alert("Hello"); } )
+ * Binds a handler to a particular event (like click) for each matched element.
+ * The event handler is passed an event object that you can use to prevent
+ * default behaviour. To stop both default action and event bubbling, your handler
+ * has to return false.
+ *
+ * @example $("p").bind( "click", function() {
+ * alert( $(this).text() );
+ * } )
* @before
Hello
- * @result [
Hello
]
- *
- * Cancel a default action and prevent it from bubbling by returning false
- * from your function.
+ * @result alert("Hello")
*
* @example $("form").bind( "submit", function() { return false; } )
+ * @desc Cancel a default action and prevent it from bubbling by returning false
+ * from your function.
*
- * Cancel a default action by using the preventDefault method.
- *
- * @example $("form").bind( "submit", function() { e.preventDefault(); } )
+ * @example $("form").bind( "submit", function(event) {
+ * event.preventDefault();
+ * } );
+ * @desc Cancel only the default action by using the preventDefault method.
*
- * Stop an event from bubbling by using the stopPropogation method.
*
- * @example $("form").bind( "submit", function() { e.stopPropogation(); } )
+ * @example $("form").bind( "submit", function(event) {
+ * event.stopPropagation();
+ * } )
+ * @desc Stop only an event from bubbling by using the stopPropagation method.
*
* @name bind
* @type jQuery