Bug Tracker

Changeset 927 for trunk/jquery/src

Show
Ignore:
Timestamp:
01/08/07 02:39:10 (2 years ago)
Author:
john
Message:

Pruned a lot of extra code out and improved how .html() works.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/jquery/jquery.js

    r926 r927  
    431431     */ 
    432432    attr: function( key, value, type ) { 
     433        var obj = key; 
     434         
     435        // Look for the case where we're accessing a style value 
     436        if ( key.constructor == String ) 
     437            if ( value == undefined ) 
     438                return jQuery[ type || "attr" ]( this[0], key ); 
     439            else { 
     440                obj = {}; 
     441                obj[ key ] = value; 
     442            } 
     443         
    433444        // Check to see if we're setting style values 
    434         return typeof key != "string" || value != undefined ? 
    435             this.each(function(){ 
    436                 // See if we're setting a hash of styles 
    437                 if ( value == undefined ) 
    438                     // Set all the styles 
    439                     for ( var prop in key ) 
    440                         jQuery.attr( 
    441                             type ? this.style : this, 
    442                             prop, jQuery.prop(this, prop, key[prop], type) 
    443                         ); 
    444  
    445                 // See if we're setting a single key/value style 
    446                 else 
    447                     jQuery.attr( 
    448                         type ? this.style : this, 
    449                         key, jQuery.prop(this, key, value, type) 
    450                     ); 
    451             }) : 
    452  
    453             // Look for the case where we're accessing a style value 
    454             jQuery[ type || "attr" ]( this[0], key ); 
     445        return this.each(function(){ 
     446            // Set all the styles 
     447            for ( var prop in obj ) 
     448                jQuery.attr( 
     449                    type ? this.style : this, 
     450                    prop, jQuery.prop(this, prop, obj[prop], type) 
     451                ); 
     452        }); 
    455453    }, 
    456454 
     
    10461044        return val == undefined ? 
    10471045            ( this.length ? this[0].innerHTML : null ) : 
    1048             this.attr( "innerHTML", val ); 
     1046            this.empty().append( val ); 
    10491047    }, 
    10501048     
     
    13141312            prop = jQuery.browser.msie ? "styleFloat" : "cssFloat"; 
    13151313 
    1316         if (!force && elem.style[prop]) { 
    1317  
     1314        if (!force && elem.style[prop]) 
    13181315            ret = elem.style[prop]; 
    13191316 
    1320         } else if (document.defaultView && document.defaultView.getComputedStyle) { 
     1317        else if (document.defaultView && document.defaultView.getComputedStyle) { 
    13211318 
    13221319            if (prop == "cssFloat" || prop == "styleFloat") 
     
    13481345    clean: function(a) { 
    13491346        var r = []; 
     1347         
    13501348        for ( var i = 0, al = a.length; i < al; i++ ) { 
    13511349            var arg = a[i]; 
    1352             if ( typeof arg == "string" ) { // Convert html string into DOM nodes 
     1350             
     1351             // Convert html string into DOM nodes 
     1352            if ( typeof arg == "string" ) { 
    13531353                // Trim whitespace, otherwise indexOf won't work as expected 
    1354                 var s = jQuery.trim(arg), s3 = s.substring(0,3), s6 = s.substring(0,6), 
    1355                     div = document.createElement("div"), wrap = [0,"",""]; 
    1356  
    1357                 if ( s.substring(0,4) == "<opt" ) // option or optgroup 
    1358                     wrap = [1, "<select>", "</select>"]; 
    1359                 else if ( s6 == "<thead" || s6 == "<tbody" || s6 == "<tfoot" ) 
    1360                     wrap = [1, "<table>", "</table>"]; 
    1361                 else if ( s3 == "<tr" ) 
    1362                     wrap = [2, "<table><tbody>", "</tbody></table>"]; 
    1363                 else if ( s3 == "<td" || s3 == "<th" ) // <thead> matched above 
    1364                     wrap = [3, "<table><tbody><tr>", "</tr></tbody></table>"]; 
     1354                var s = jQuery.trim(arg), div = document.createElement("div"), tb = []; 
     1355 
     1356                var wrap = 
     1357                     // option or optgroup 
     1358                    !s.indexOf("<opt") && [1, "<select>", "</select>"] || 
     1359                     
     1360                    !s.indexOf("<thead") || !s.indexOf("<tbody") || !s.indexOf("<tfoot") && 
     1361                    [1, "<table>", "</table>"] || 
     1362                     
     1363                    !s.indexOf("<tr") && 
     1364                    [2, "<table><tbody>", "</tbody></table>"] || 
     1365                     
     1366                    // <thead> matched above 
     1367                    !s.indexOf("<td") || !s.indexOf("<th") && 
     1368                    [3, "<table><tbody><tr>", "</tr></tbody></table>"] || 
     1369                     
     1370                    [0,"",""]; 
    13651371 
    13661372                // Go to html and back, then peel off extra wrappers 
    13671373                div.innerHTML = wrap[1] + s + wrap[2]; 
    1368                 while ( wrap[0]-- ) div = div.firstChild; 
     1374                 
     1375                // Move to the right depth 
     1376                while ( wrap[0]-- ) 
     1377                    div = div.firstChild; 
    13691378                 
    13701379                // Remove IE's autoinserted <tbody> from table fragments 
    13711380                if ( jQuery.browser.msie ) { 
    1372                     var tb = null; 
     1381                     
    13731382                    // String was a <table>, *may* have spurious <tbody> 
    1374                     if ( s6 == "<table" && s.indexOf("<tbody") < 0 )  
     1383                    if ( !s.indexOf("<table") && s.indexOf("<tbody") < 0 )  
    13751384                        tb = div.firstChild && div.firstChild.childNodes; 
     1385                         
    13761386                    // String was a bare <thead> or <tfoot> 
    13771387                    else if ( wrap[1] == "<table>" && s.indexOf("<tbody") < 0 ) 
    13781388                        tb = div.childNodes; 
    1379                     if ( tb ) { 
    1380                         for ( var n = tb.length-1; n >= 0 ; --n ) 
    1381                             if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length ) 
    1382                                 tb[n].parentNode.removeChild(tb[n]); 
    1383                     } 
     1389 
     1390                    for ( var n = tb.length-1; n >= 0 ; --n ) 
     1391                        if ( tb[n].nodeName.toUpperCase() == "TBODY" && !tb[n].childNodes.length ) 
     1392                            tb[n].parentNode.removeChild(tb[n]); 
     1393                     
    13841394                } 
    13851395                 
    13861396                arg = div.childNodes; 
    1387             }  
     1397            } 
    13881398             
    1389              
    1390             if ( arg.length != undefined && ( (jQuery.browser.safari && typeof arg == 'function') || !arg.nodeType ) ) // Safari reports typeof on a DOM NodeList to be a function 
    1391                 for ( var n = 0, argl = arg.length; n < argl; n++ ) // Handles Array, jQuery, DOM NodeList collections 
    1392                     r.push(arg[n]); 
     1399            if ( arg.nodeType ) 
     1400                r.push( arg ); 
    13931401            else 
    1394                 r.push( arg.nodeType ? arg : document.createTextNode(arg.toString()) ); 
     1402                r = jQuery.merge( r, arg ); 
    13951403        } 
    13961404 
     
    14231431                ( value == 1 ? "" : "alpha(opacity=" + value * 100 + ")" ); 
    14241432 
    1425         } else if ( name == "opacity" && jQuery.browser.msie ) { 
     1433        } else if ( name == "opacity" && jQuery.browser.msie ) 
    14261434            return elem.filter ?  
    14271435                parseFloat( elem.filter.match(/alpha\(opacity=(.*)\)/)[1] ) / 100 : 1; 
    1428         } 
    14291436         
    14301437        // Mozilla doesn't play well with opacity 1 
     
    14371444            return elem[fix[name]]; 
    14381445 
    1439         } else if ( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') ) { 
     1446        } else if ( value == undefined && jQuery.browser.msie && elem.nodeName && elem.nodeName.toUpperCase() == 'FORM' && (name == 'action' || name == 'method') ) 
    14401447            return elem.getAttributeNode(name).nodeValue; 
    14411448 
    14421449        // IE elem.getAttribute passes even for style 
    1443         } else if ( elem.tagName ) { 
     1450        else if ( elem.tagName ) { 
    14441451            if ( value != undefined ) elem.setAttribute( name, value ); 
    14451452            return elem.getAttribute( name ); 
     
    14701477        var r = []; 
    14711478 
    1472         if ( a.constructor != Array ) { 
     1479        if ( a.constructor != Array ) 
    14731480            for ( var i = 0, al = a.length; i < al; i++ ) 
    14741481                r.push( a[i] ); 
    1475         } else 
     1482        else 
    14761483            r = a.slice( 0 ); 
    14771484 
     
    15111518        // Now check for duplicates between the two arrays 
    15121519        // and only add the unique items 
    1513         for ( var i = 0, sl = second.length; i < sl; i++ ) { 
     1520        for ( var i = 0, sl = second.length; i < sl; i++ ) 
    15141521            // Check for duplicates 
    15151522            if ( jQuery.inArray( second[i], r ) == -1 ) 
    15161523                // The item is unique, add it 
    15171524                first.push( second[i] ); 
    1518         } 
    15191525 
    15201526        return first;