Simplified the structure of the .css() and .attr() methods (reducing the number of...
authorJohn Resig <jeresig@gmail.com>
Sat, 28 Feb 2009 20:23:48 +0000 (20:23 +0000)
committerJohn Resig <jeresig@gmail.com>
Sat, 28 Feb 2009 20:23:48 +0000 (20:23 +0000)
src/core.js
test/unit/core.js

index 56fd098..c12b583 100644 (file)
@@ -166,29 +166,42 @@ jQuery.fn = jQuery.prototype = {
        },
 
        attr: function( name, value, type ) {
-               var options = name;
+               var options = name, isFunction = jQuery.isFunction( value );
 
                // Look for the case where we're accessing a style value
-               if ( typeof name === "string" )
-                       if ( value === undefined )
-                               return this[0] && jQuery[ type || "attr" ]( this[0], name );
+               if ( typeof name === "string" ) {
+                       if ( value === undefined ) {
+                               return this.length ?
+                                       jQuery[ type || "attr" ]( this[0], name ) :
+                                       null;
 
-                       else {
+                       } else {
                                options = {};
                                options[ name ] = value;
                        }
+               }
 
                // Check to see if we're setting style values
-               return this.each(function(i){
+               for ( var i = 0, l = this.length; i < l; i++ ) {
+                       var elem = this[i];
+
                        // Set all the styles
-                       for ( name in options )
-                               jQuery.attr(
-                                       type ?
-                                               this.style :
-                                               this,
-                                       name, jQuery.prop( this, options[ name ], type, i, name )
-                               );
-               });
+                       for ( var prop in options ) {
+                               value = options[prop];
+
+                               if ( isFunction ) {
+                                       value = value.call( elem, i );
+                               }
+
+                               if ( typeof value === "number" && type === "curCSS" && !exclude.test(prop) ) {
+                                       value = value + "px";
+                               }
+
+                               jQuery.attr( type ? elem.style : elem, prop, value );
+                       }
+               }
+
+               return this;
        },
 
        css: function( key, value ) {
@@ -644,7 +657,7 @@ jQuery.extend({
        // check if an element is in a (or is an) XML document
        isXMLDoc: function( elem ) {
                return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" ||
-                       !!elem.ownerDocument && jQuery.isXMLDoc( elem.ownerDocument );
+                       !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML";
        },
 
        // Evalulates a script in a global context
@@ -700,17 +713,6 @@ jQuery.extend({
                return object;
        },
 
-       prop: function( elem, value, type, i, name ) {
-               // Handle executable functions
-               if ( jQuery.isFunction( value ) )
-                       value = value.call( elem, i );
-
-               // Handle passing in a number to a CSS property
-               return typeof value === "number" && type == "curCSS" && !exclude.test( name ) ?
-                       value + "px" :
-                       value;
-       },
-
        className: {
                // internal only, use addClass("class")
                add: function( elem, classNames ) {
@@ -974,7 +976,7 @@ jQuery.extend({
                if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
                        return undefined;
 
-               var notxml = !jQuery.isXMLDoc( elem ),
+               var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
                        // Whether we are setting (or getting)
                        set = value !== undefined;
 
index 9d91c4f..a4e82eb 100644 (file)
@@ -1688,13 +1688,6 @@ test("jQuery.each(Object,Function)", function() {
         equals( total, 3, "Looping over an object, with break" );
 });
 
-test("jQuery.prop", function() {
-       expect(2);
-       var handle = function() { return this.id };
-       equals( jQuery.prop(jQuery("#ap")[0], handle), "ap", "Check with Function argument" );
-       equals( jQuery.prop(jQuery("#ap")[0], "value"), "value", "Check with value argument" );
-});
-
 test("jQuery.className", function() {
        expect(6);
        var x = jQuery("<p>Hi</p>")[0];