aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2009-02-28 20:23:48 +0000
committerJohn Resig <jeresig@gmail.com>2009-02-28 20:23:48 +0000
commitf1f5cd9f6a80b0de18cc0402f28b440f48b95f83 (patch)
tree1c652483270eebb10441a2199d041aae360cb78f
parent1c8c3883a98d7d896efefe92eb0d22b1681e92f1 (diff)
downloadjquery-f1f5cd9f6a80b0de18cc0402f28b440f48b95f83.tar.gz
jquery-f1f5cd9f6a80b0de18cc0402f28b440f48b95f83.zip
Simplified the structure of the .css() and .attr() methods (reducing the number of calls). Need to optimize $.css/$.curCSS/$.attr still. Fixes #4269.
-rw-r--r--src/core.js56
-rw-r--r--test/unit/core.js7
2 files changed, 29 insertions, 34 deletions
diff --git a/src/core.js b/src/core.js
index 56fd0983e..c12b583db 100644
--- a/src/core.js
+++ b/src/core.js
@@ -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;
diff --git a/test/unit/core.js b/test/unit/core.js
index 9d91c4f75..a4e82eb6e 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -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];