X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=44eedf2525c80d8a0205f06c3b16396d5f36c000;hb=c119a80ea37a6940ec55dfb7b9f9b296800c7928;hp=6b05a06059f387ea41a45df96bb6cb9bc3b89678;hpb=c9511ef948ec3fd58bfd0af8898ef66abdc80255;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 6b05a06..44eedf2 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -28,10 +28,10 @@ window.undefined = window.undefined; * @name jQuery * @cat Core */ -function jQuery(a,c) { +var jQuery = function(a,c) { // Shortcut for document ready (because $(document).each() is silly) - if ( a && typeof a == "function" && jQuery.fn.ready ) + if ( a && typeof a == "function" && jQuery.fn.ready && !a.nodeType && a[0] == undefined ) // Safari reports typeof on DOM NodeLists as a function return jQuery(document).ready(a); // Make sure that a selection was provided @@ -56,7 +56,7 @@ function jQuery(a,c) { } // Watch for when an array is passed in - this.get( a.constructor == Array || a.length && !a.nodeType && a[0] != undefined && a[0].nodeType ? + this.get( a.constructor == Array || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType ? // Assume that it is an array of DOM Elements jQuery.merge( a, [] ) : @@ -71,7 +71,7 @@ function jQuery(a,c) { this.each(fn); return this; -} +}; // Map over the $ in case of overwrite if ( typeof $ != "undefined" ) @@ -449,6 +449,10 @@ jQuery.fn = jQuery.prototype = { * ok( document.getElementById('check2').checked == true, 'Set checked attribute' ); * $("#check2").attr('checked', false); * ok( document.getElementById('check2').checked == false, 'Set checked attribute' ); + * $("#text1").attr('readonly', true); + * ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' ); + * $("#text1").attr('readonly', false); + * ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' ); * * @test stop(); * $.get('data/dashboard.xml', function(xml) { @@ -534,6 +538,14 @@ jQuery.fn = jQuery.prototype = { * 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'); + * $('#floatTest').css({styleFloat: 'right'}); + * ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right'); + * $('#floatTest').css({cssFloat: 'left'}); + * ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left'); + * $('#floatTest').css({'float': 'right'}); + * ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right'); + * $('#floatTest').css({'font-size': '30px'}); + * ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px'); * * @name css * @type jQuery @@ -555,6 +567,14 @@ jQuery.fn = jQuery.prototype = { * 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'); + * $('#floatTest').css('styleFloat', 'left'); + * ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left'); + * $('#floatTest').css('cssFloat', 'right'); + * ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right'); + * $('#floatTest').css('float', 'left'); + * ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left'); + * $('#floatTest').css('font-size', '20px'); + * ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px'); * * @name css * @type jQuery @@ -685,6 +705,7 @@ jQuery.fn = jQuery.prototype = { * @test var defaultText = 'Try them out:' * var result = $('#first').append('buga'); * ok( result.text() == defaultText + 'buga', 'Check if text appending works' ); + * ok( $('#select3').append('').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element'); * * @name append * @type jQuery @@ -748,6 +769,7 @@ jQuery.fn = jQuery.prototype = { * @test var defaultText = 'Try them out:' * var result = $('#first').prepend('buga'); * ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' ); + * ok( $('#select3').prepend('').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element'); * * @name prepend * @type jQuery @@ -922,12 +944,15 @@ jQuery.fn = jQuery.prototype = { * @result $("p").find("span").end() == [

...

] * * @test ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' ); + * ok( $('#yahoo').end(), 'Check for end with nothing to end' ); * * @name end * @type jQuery * @cat DOM/Traversing */ end: function() { + if( !(this.stack && this.stack.length) ) + return this; return this.get( this.stack.pop() ); }, @@ -1447,7 +1472,7 @@ jQuery.extend({ if ( p == "height" || p == "width" ) { var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"]; - for ( var i in d ) { + for ( var i=0; i"; - } else if ( !a[i].indexOf(""; - } else if ( !a[i].indexOf(""; - } - - var div = document.createElement("div"); - div.innerHTML = a[i]; - - if ( table ) { - div = div.firstChild; - if ( table != "thead" ) div = div.firstChild; - if ( table == "td" ) div = div.firstChild; - } - - for ( var j = 0; j < div.childNodes.length; j++ ) - r.push( div.childNodes[j] ); - } else if ( a[i].jquery || a[i].length && !a[i].nodeType ) - for ( var k = 0; k < a[i].length; k++ ) - r.push( a[i][k] ); - else if ( a[i] !== null ) - r.push( a[i].nodeType ? a[i] : document.createTextNode(a[i].toString()) ); + var arg = a[i]; + if ( arg.constructor == String ) { // Convert html string into DOM nodes + // Trim whitespace, otherwise indexOf won't work as expected + var s = jQuery.trim(arg), div = document.createElement("div"), wrap = [0,"",""]; + + if ( !s.indexOf("", ""]; + else if ( !s.indexOf("", ""]; + else if ( !s.indexOf("", ""]; // tbody auto-inserted + else if ( !s.indexOf("", ""]; + + // Go to html and back, then peel off extra wrappers + div.innerHTML = wrap[1] + s + wrap[2]; + while ( wrap[0]-- ) div = div.firstChild; + arg = div.childNodes; + } + + + if ( arg.length != undefined && ( (jQuery.browser.safari && typeof arg == 'function') || !arg.nodeType ) ) // Safari reports typeof on a DOM NodeList to be a function + for ( var n = 0; n < arg.length; n++ ) // Handles Array, jQuery, DOM NodeList collections + r.push(arg[n]); + else + r.push( arg.nodeType ? arg : document.createTextNode(arg.toString()) ); } + return r; }, @@ -1624,12 +1648,8 @@ jQuery.extend({ ">|/", "jQuery.sibling(a.firstChild)", "\\+", "jQuery.sibling(a).next", "~", function(a){ - var r = []; var s = jQuery.sibling(a); - if ( s.n > 0 ) - for ( var i = s.n; i < s.length; i++ ) - r.push( s[i] ); - return r; + return s.n >= 0 ? s.slice(s.n+1) : []; } ], @@ -1672,10 +1692,6 @@ jQuery.extend({ * t( "Attribute Exists", "a[@title]", ["google"] ); * t( "Attribute Exists", "*[@title]", ["google"] ); * t( "Attribute Exists", "[@title]", ["google"] ); - * - * t( "Non-existing part of attribute", "[@name*=bla]", [] ); - * t( "Non-existing start of attribute", "[@name^=bla]", [] ); - * t( "Non-existing end of attribute", "[@name$=bla]", [] ); * * t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] ); * t( "Attribute Equals", 'a[@rel="bookmark"]', ["simon1"] ); @@ -1711,8 +1727,8 @@ jQuery.extend({ * t( "Attribute Exists", "//a[@title]", ["google"] ); * t( "Attribute Equals", "//a[@rel='bookmark']", ["simon1"] ); * t( "Parent Axis", "//p/..", ["main","foo"] ); - * t( "Sibling Axis", "//p/../", ["firstp","ap","foo","first","firstUL","empty","form","sndp","en","sap"] ); - * t( "Sibling Axis", "//p/../*", ["firstp","ap","foo","first","firstUL","empty","form","sndp","en","sap"] ); + * t( "Sibling Axis", "//p/../", ["firstp","ap","foo","first","firstUL","empty","form","floatTest","sndp","en","sap"] ); + * t( "Sibling Axis", "//p/../*", ["firstp","ap","foo","first","firstUL","empty","form","floatTest","sndp","en","sap"] ); * t( "Has Children", "//p[a]", ["firstp","ap","en","sap"] ); * * t( "nth Element", "p:nth(1)", ["ap"] ); @@ -1858,7 +1874,8 @@ jQuery.extend({ className: "className", value: "value", disabled: "disabled", - checked: "checked" + checked: "checked", + readonly: "readOnly" }; // IE actually uses filters for opacity ... elem is actually elem.style @@ -1882,7 +1899,7 @@ jQuery.extend({ return elem[fix[name]]; } else if( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') ) { return elem.getAttributeNode(name).nodeValue; - } else if ( elem.getAttribute != undefined && elem.tagName ) { // IE elem.getAttribute passes even for style + } else if ( elem.tagName ) { // IE elem.getAttribute passes even for style if ( value != undefined ) elem.setAttribute( name, value ); return elem.getAttribute( name ); } else { @@ -2224,8 +2241,8 @@ jQuery.extend({ }, trigger: function(type,data,element) { - // Touch up the incoming data - data = data || []; + // Clone the incoming data, if any + data = $.merge([], data || []); // Handle a global trigger if ( !element ) { @@ -2247,7 +2264,7 @@ jQuery.extend({ handle: function(event) { if ( typeof jQuery == "undefined" ) return false; - event = event || jQuery.event.fix( window.event ); + event = jQuery.event.fix( event || window.event || {} ); // Empty object is for triggered events with no data // If no correct event was found, fail if ( !event ) return false; @@ -2267,23 +2284,39 @@ jQuery.extend({ } } + // Clean up added properties in IE to prevent memory leak + if (jQuery.browser.msie) event.target = event.preventDefault = event.stopPropagation = null; + return returnValue; }, fix: function(event) { - if ( event ) { + // check IE + if(jQuery.browser.msie) { + // fix target property + event.target = event.srcElement; + + // check safari and if target is a textnode + } else if(jQuery.browser.safari && event.target.nodeType == 3) { + // target is readonly, clone the event object + event = jQuery.extend({}, event); + // get parentnode from textnode + event.target = event.target.parentNode; + } + + // fix preventDefault and stopPropagation + if (!event.preventDefault) event.preventDefault = function() { this.returnValue = false; }; - + + if (!event.stopPropagation) event.stopPropagation = function() { this.cancelBubble = true; }; - } - + return event; } - } }); @@ -3062,7 +3095,7 @@ jQuery.macros = { * * It only returns the immediately previous sibling, not all previous siblings. * - * @example $("p").previous(".selected") + * @example $("p").prev(".selected") * @before
Hello

Hello Again

And Again

* @result [
Hello
] * @@ -3281,8 +3314,8 @@ jQuery.macros = { }, /** - * Adds the specified class if it is present, removes it if it is - * not present. + * Adds the specified class if it is not present, removes it if it is + * present. * * @example $("p").toggleClass("selected") * @before

Hello

Hello Again