Fixed the issue with jQuery conflicting with windows/IFrames.
[jquery.git] / src / jquery / jquery.js
index d50274f..93ebe20 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jQuery - New Wave Javascript
+ * jQuery @VERSION - New Wave Javascript
  *
  * Copyright (c) 2006 John Resig (jquery.com)
  * Dual licensed under the MIT (MIT-LICENSE.txt)
@@ -28,10 +28,10 @@ window.undefined = window.undefined;
  * @name jQuery
  * @cat Core
  */
-jQuery = function(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
@@ -50,23 +50,27 @@ jQuery = function(a,c) {
                return new jQuery(a,c);
 
        // Handle HTML strings
-       var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
-       if ( m ) a = jQuery.clean( [ m[1] ] );
+       if ( a.constructor == String ) {
+               var m = /^[^<]*(<.+>)[^>]*$/.exec(a);
+               if ( m ) a = jQuery.clean( [ m[1] ] );
+       }
 
        // 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, [] ) :
 
                // Find the matching elements and save them for later
                jQuery.find( a, c ) );
 
-  // See if an extra function was provided
+       // See if an extra function was provided
        var fn = arguments[ arguments.length - 1 ];
 
        // If so, execute it in context
        if ( fn && typeof fn == "function" )
                this.each(fn);
+
+       return this;
 };
 
 // Map over the $ in case of overwrite
@@ -95,7 +99,10 @@ if ( typeof $ != "undefined" )
  * @result [ <p>two</p> ]
  *
  * @example $("<div><p>Hello</p></div>").appendTo("#body")
- * @desc Creates a div element (and all of its contents) dynamically, and appends it to the element with the ID of body.
+ * @desc Creates a div element (and all of its contents) dynamically, 
+ * and appends it to the element with the ID of body. Internally, an
+ * element is created and it's innerHTML property set to the given markup.
+ * It is therefore both quite flexible and limited. 
  *
  * @name $
  * @param String expr An expression to search with, or a string of HTML to create on the fly.
@@ -187,7 +194,7 @@ var $ = jQuery;
 
 jQuery.fn = jQuery.prototype = {
        /**
-        * The current SVN version of jQuery.
+        * The current version of jQuery.
         *
         * @private
         * @property
@@ -195,7 +202,7 @@ jQuery.fn = jQuery.prototype = {
         * @type String
         * @cat Core
         */
-       jquery: "$Rev$",
+       jquery: "@VERSION",
 
        /**
         * The number of elements currently matched.
@@ -442,6 +449,21 @@ 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) { 
+        *   var titles = [];
+        *   $('tab', xml).each(function() {
+        *     titles.push($(this).attr('title'));
+        *   });
+        *   ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
+        *   ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
+        *   start();
+        * });
         *
         * @name attr
         * @type jQuery
@@ -516,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
@@ -537,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
@@ -645,7 +683,7 @@ jQuery.fn = jQuery.prototype = {
                        // Insert it before the element to be wrapped
                        this.parentNode.insertBefore( b, this );
 
-                       // Find he deepest point in the wrap structure
+                       // Find the deepest point in the wrap structure
                        while ( b.firstChild )
                                b = b.firstChild;
 
@@ -667,6 +705,7 @@ jQuery.fn = jQuery.prototype = {
         * @test var defaultText = 'Try them out:'
         * var result = $('#first').append('<b>buga</b>');
         * ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
+        * ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
         *
         * @name append
         * @type jQuery
@@ -730,6 +769,7 @@ jQuery.fn = jQuery.prototype = {
         * @test var defaultText = 'Try them out:'
         * var result = $('#first').prepend('<b>buga</b>');
         * ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
+        * ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
         *
         * @name prepend
         * @type jQuery
@@ -904,12 +944,15 @@ jQuery.fn = jQuery.prototype = {
         * @result $("p").find("span").end() == [ <p>...</p> ]
         *
         * @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() );
        },
 
@@ -1015,6 +1058,7 @@ jQuery.fn = jQuery.prototype = {
                                for ( var i = 0; i < t.length; i++ )
                                        if ( jQuery.filter(t[i],[a]).r.length )
                                                return a;
+                               return false;
                        }) ||
 
                        t.constructor == Boolean &&
@@ -1278,8 +1322,17 @@ jQuery.fn = jQuery.prototype = {
  * @cat Javascript
  */
 jQuery.extend = jQuery.fn.extend = function(obj,prop) {
+       // Watch for the case where null or undefined gets passed in by accident
+       if ( arguments.length > 1 && (prop === null || prop == undefined) )
+               return obj;
+
+       // If no property object was provided, then we're extending jQuery
        if ( !prop ) { prop = obj; obj = this; }
+
+       // Extend the base object
        for ( var i in prop ) obj[i] = prop[i];
+
+       // Return the modified object
        return obj;
 };
 
@@ -1371,7 +1424,7 @@ jQuery.extend({
                                fn.apply( obj[i], args || [i, obj[i]] );
                else
                        for ( var i = 0; i < obj.length; i++ )
-                               fn.apply( obj[i], args || [i, obj[i]] );
+                               if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break;
                return obj;
        },
 
@@ -1381,10 +1434,6 @@ jQuery.extend({
                        o.className += ( o.className ? " " : "" ) + c;
                },
                remove: function(o,c){
-                       /*
-                       o.className = !c ? "" :
-                               o.className.replace(
-                                       new RegExp("(^|\\s*\\b[^-])"+c+"($|\\b(?=[^-]))", "g"), "");*/
                        if( !c ) {
                                o.className = "";
                        } else {
@@ -1433,9 +1482,11 @@ jQuery.extend({
                                        oHeight = e.offsetHeight;
                                        oWidth = e.offsetWidth;
                                } else {
-                                       e = jQuery(e.cloneNode(true)).css({
-                                               visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
-                                       }).appendTo(e.parentNode)[0];
+                                       e = jQuery(e.cloneNode(true))
+                                               .find(":radio").removeAttr("checked").end()
+                                               .css({
+                                                       visibility: "hidden", position: "absolute", display: "block", right: "0", left: "0"
+                                               }).appendTo(e.parentNode)[0];
 
                                        var parPos = jQuery.css(e.parentNode,"position");
                                        if ( parPos == "" || parPos == "static" )
@@ -1452,14 +1503,19 @@ jQuery.extend({
                        });
 
                        return p == "height" ? oHeight : oWidth;
-               } else if ( p == "opacity" && jQuery.browser.msie )
-                       return parseFloat( jQuery.curCSS(e,"filter").replace(/[^0-9.]/,"") ) || 1;
+               }
 
                return jQuery.curCSS( e, p );
        },
 
        curCSS: function(elem, prop, force) {
                var ret;
+               
+               if (prop == 'opacity' && jQuery.browser.msie)
+                       return jQuery.attr(elem.style, 'opacity');
+                       
+               if (prop == "float" || prop == "cssFloat")
+                   prop = jQuery.browser.msie ? "styleFloat" : "cssFloat";
 
                if (!force && elem.style[prop]) {
 
@@ -1472,6 +1528,9 @@ jQuery.extend({
 
                } else if (document.defaultView && document.defaultView.getComputedStyle) {
 
+                       if (prop == "cssFloat" || prop == "styleFloat")
+                               prop = "float";
+
                        prop = prop.replace(/([A-Z])/g,"-$1").toLowerCase();
                        var cur = document.defaultView.getComputedStyle(elem, null);
 
@@ -1488,42 +1547,38 @@ jQuery.extend({
 
                return ret;
        },
-
+       
        clean: function(a) {
                var r = [];
                for ( var i = 0; i < a.length; i++ ) {
-                       if ( a[i].constructor == String ) {
-
-                               var table = "";
-
-                               if ( !a[i].indexOf("<thead") || !a[i].indexOf("<tbody") ) {
-                                       table = "thead";
-                                       a[i] = "<table>" + a[i] + "</table>";
-                               } else if ( !a[i].indexOf("<tr") ) {
-                                       table = "tr";
-                                       a[i] = "<table>" + a[i] + "</table>";
-                               } else if ( !a[i].indexOf("<td") || !a[i].indexOf("<th") ) {
-                                       table = "td";
-                                       a[i] = "<table><tbody><tr>" + a[i] + "</tr></tbody></table>";
-                               }
-
-                               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("<opt") ) // option or optgroup
+                                       wrap = [1, "<select>", "</select>"];
+                               else if ( !s.indexOf("<thead") || !s.indexOf("<tbody") )
+                                       wrap = [1, "<table>", "</table>"];
+                               else if ( !s.indexOf("<tr") )
+                                       wrap = [2, "<table>", "</table>"];      // tbody auto-inserted
+                               else if ( !s.indexOf("<td") || !s.indexOf("<th") )
+                                       wrap = [3, "<table><tbody><tr>", "</tr></tbody></table>"];
+
+                               // 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;
        },
 
@@ -1552,7 +1607,7 @@ jQuery.extend({
                        empty: "!a.childNodes.length",
 
                        // Text Check
-                       contains: "(a.innerText||a.innerHTML).indexOf(m[3])>=0",
+                       contains: "jQuery.fn.text.apply([a]).indexOf(m[3])>=0",
 
                        // Visibility
                        visible: "a.type!='hidden'&&jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
@@ -1562,7 +1617,7 @@ jQuery.extend({
                        enabled: "!a.disabled",
                        disabled: "a.disabled",
                        checked: "a.checked",
-                       selected: "a.selected",
+                       selected: "a.selected || jQuery.attr(a, 'selected')",
 
                        // Form elements
                        text: "a.type=='text'",
@@ -1641,10 +1696,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]", "[@name*=bla]", [] ); 
-        * t( "Non-existing start of attribute [@name^=bla]", "[@name^=bla]", [] ); 
-        * t( "Non-existing end of attribute [@name$=bla]", "[@name$=bla]", [] ); 
         *
         * t( "Attribute Equals", "a[@rel='bookmark']", ["simon1"] );
         * t( "Attribute Equals", 'a[@rel="bookmark"]', ["simon1"] );
@@ -1680,8 +1731,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"] );
@@ -1821,22 +1872,40 @@ jQuery.extend({
                var fix = {
                        "for": "htmlFor",
                        "class": "className",
-                       "float": "cssFloat",
+                       "float": jQuery.browser.msie ? "styleFloat" : "cssFloat",
+                       cssFloat: jQuery.browser.msie ? "styleFloat" : "cssFloat",
                        innerHTML: "innerHTML",
                        className: "className",
                        value: "value",
                        disabled: "disabled",
-                       checked: "checked"
+                       checked: "checked",
+                       readonly: "readOnly"
                };
+               
+               // IE actually uses filters for opacity ... elem is actually elem.style
+               if (name == "opacity" && jQuery.browser.msie && value != undefined) {
+                       // IE has trouble with opacity if it does not have layout
+                       // Would prefer to check element.hasLayout first but don't have access to the element here
+                       elem['zoom'] = 1; 
+                       if (value == 1) // Remove filter to avoid more IE weirdness
+                               return elem["filter"] = elem["filter"].replace(/alpha\([^\)]*\)/gi,"");
+                       else
+                               return elem["filter"] = elem["filter"].replace(/alpha\([^\)]*\)/gi,"") + "alpha(opacity=" + value * 100 + ")";
+               } else if (name == "opacity" && jQuery.browser.msie) {
+                       return elem["filter"] ? parseFloat( elem["filter"].match(/alpha\(opacity=(.*)\)/)[1] )/100 : 1;
+               }
+               
+               // Mozilla doesn't play well with opacity 1
+               if (name == "opacity" && jQuery.browser.mozilla && value == 1) value = 0.9999;
 
                if ( fix[name] ) {
                        if ( value != undefined ) elem[fix[name]] = value;
                        return elem[fix[name]];
-               } else if( value == undefined && $.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') ) {
+               } 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 ) {
+               } else if ( (jQuery.browser.msie || elem.getAttribute != undefined) && elem.tagName ) { // IE elem.getAttribute passes even for style; Do not "call" elem.getAttribute in IE <- weird crap
                        if ( value != undefined ) elem.setAttribute( name, value );
-                       return elem.getAttribute( name, 2 );
+                       return elem.getAttribute( name );
                } else {
                        name = name.replace(/-([a-z])/ig,function(z,b){return b.toUpperCase();});
                        if ( value != undefined ) elem[name] = value;
@@ -2197,12 +2266,12 @@ jQuery.extend({
                },
 
                handle: function(event) {
-                       if ( typeof jQuery == "undefined" ) return;
+                       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;
+                       if ( !event ) return false;
 
                        var returnValue = true;
 
@@ -2219,23 +2288,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;
                }
-
        }
 });
 
@@ -2658,7 +2743,7 @@ jQuery.macros = {
                /**
                 * Set the value of every matched element.
                 *
-                * @example $("input").value("test");
+                * @example $("input").val("test");
                 * @before <input type="text" value="some text"/>
                 * @result <input type="text" value="test"/>
                 *
@@ -3014,7 +3099,7 @@ jQuery.macros = {
                 *
                 * It only returns the immediately previous sibling, not all previous siblings.
                 *
-                * @example $("p").previous(".selected")
+                * @example $("p").prev(".selected")
                 * @before <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>
                 * @result [ <div><span>Hello</span></div> ]
                 *