Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Disabled the passthrough .attr(method_name) functionality. You can no…

…w use it if you do: .attr({method_name: value}, true) OR as an easy initialization method: jQuery('<div/>', {html: '...', id: 'test'}).
  • Loading branch information...
commit d40083c866738727aa7ffd7f13d2955bc9575d5e 1 parent 148fb7b
John Resig authored December 18, 2009
4  src/attributes.js
@@ -232,13 +232,13 @@ jQuery.extend({
232 232
 		offset: true
233 233
 	},
234 234
 		
235  
-	attr: function( elem, name, value ) {
  235
+	attr: function( elem, name, value, pass ) {
236 236
 		// don't set attributes on text and comment nodes
237 237
 		if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
238 238
 			return undefined;
239 239
 		}
240 240
 
241  
-		if ( name in jQuery.attrFn && value !== undefined ) {
  241
+		if ( pass && name in jQuery.attrFn ) {
242 242
 			return jQuery(elem)[name](value);
243 243
 		}
244 244
 
14  src/core.js
@@ -84,7 +84,13 @@ jQuery.fn = jQuery.prototype = {
84 84
 					ret = rsingleTag.exec( selector );
85 85
 
86 86
 					if ( ret ) {
87  
-						selector = [ doc.createElement( ret[1] ) ];
  87
+						if ( jQuery.isPlainObject( context ) ) {
  88
+							selector = [ document.createElement( ret[1] ) ];
  89
+							jQuery.fn.attr.call( selector, context, true );
  90
+
  91
+						} else {
  92
+							selector = [ doc.createElement( ret[1] ) ];
  93
+						}
88 94
 
89 95
 					} else {
90 96
 						ret = buildFragment( [ match[1] ], [ doc ] );
@@ -687,13 +693,13 @@ function evalScript( i, elem ) {
687 693
 
688 694
 // Mutifunctional method to get and set values to a collection
689 695
 // The value/s can be optionally by executed if its a function
690  
-function access( elems, key, value, exec, fn ) {
  696
+function access( elems, key, value, exec, fn, pass ) {
691 697
 	var length = elems.length;
692 698
 	
693 699
 	// Setting many attributes
694 700
 	if ( typeof key === "object" ) {
695 701
 		for ( var k in key ) {
696  
-			access( elems, k, key[k], exec, fn );
  702
+			access( elems, k, key[k], exec, fn, value );
697 703
 		}
698 704
 		return elems;
699 705
 	}
@@ -704,7 +710,7 @@ function access( elems, key, value, exec, fn ) {
704 710
 		exec = exec && jQuery.isFunction(value);
705 711
 		
706 712
 		for ( var i = 0; i < length; i++ ) {
707  
-			fn( elems[i], key, exec ? value.call( elems[i], i ) : value );
  713
+			fn( elems[i], key, exec ? value.call( elems[i], i ) : value, pass );
708 714
 		}
709 715
 		
710 716
 		return elems;
12  test/unit/attributes.js
@@ -5,7 +5,7 @@ var functionReturningObj = function(value) { return (function() { return value;
5 5
 
6 6
 test("attr(String)", function() {
7 7
 	expect(28);
8  
-	
  8
+
9 9
 	// This one sometimes fails randomly ?!
10 10
 	equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
11 11
 	
@@ -190,16 +190,16 @@ test("attr(jquery_method)", function(){
190 190
 		elem = $elem[0];
191 191
 	
192 192
 	// one at a time	
193  
-	$elem.attr('html', 'foo');
  193
+	$elem.attr({'html': 'foo'}, true);
194 194
 	equals( elem.innerHTML, 'foo', 'attr(html)');
195 195
 	
196  
-	$elem.attr('text', 'bar');
  196
+	$elem.attr({'text': 'bar'}, true);
197 197
 	equals( elem.innerHTML, 'bar', 'attr(text)');
198 198
 	
199  
-	$elem.attr('css', {color:'red'});
  199
+	$elem.attr({'css': {color:'red'}}, true);
200 200
 	ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
201 201
 	
202  
-	$elem.attr('height', 10);
  202
+	$elem.attr({'height': 10}, true);
203 203
 	equals( elem.style.height, '10px', 'attr(height)');
204 204
 	
205 205
 	// Multiple attributes
@@ -207,7 +207,7 @@ test("attr(jquery_method)", function(){
207 207
 	$elem.attr({
208 208
 		width:10,
209 209
 		css:{ paddingLeft:1, paddingRight:1 }
210  
-	});
  210
+	}, true);
211 211
 	
212 212
 	equals( elem.style.width, '10px', 'attr({...})');
213 213
 	equals( elem.style.paddingLeft, '1px', 'attr({...})');
18  test/unit/core.js
@@ -12,7 +12,7 @@ test("Basic requirements", function() {
12 12
 });
13 13
 
14 14
 test("jQuery()", function() {
15  
-	expect(15);
  15
+	expect(22);
16 16
 
17 17
 	// Basic constructor's behavior
18 18
 
@@ -62,6 +62,22 @@ test("jQuery()", function() {
62 62
 	equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
63 63
 
64 64
 	equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
  65
+
  66
+	var elem = jQuery("<div/>", {
  67
+                width: 10,
  68
+                css: { paddingLeft:1, paddingRight:1 },
  69
+		text: "test",
  70
+		"class": "test2",
  71
+		id: "test3"
  72
+        });
  73
+
  74
+        equals( elem[0].style.width, '10px', 'jQuery() quick setter width');
  75
+        equals( elem[0].style.paddingLeft, '1px', 'jQuery quick setter css');
  76
+        equals( elem[0].style.paddingRight, '1px', 'jQuery quick setter css');
  77
+        equals( elem[0].childNodes.length, 1, 'jQuery quick setter text');
  78
+        equals( elem[0].firstChild.nodeValue, "test", 'jQuery quick setter text');
  79
+        equals( elem[0].className, "test2", 'jQuery() quick setter class');
  80
+        equals( elem[0].id, "test3", 'jQuery() quick setter id');
65 81
 });
66 82
 
67 83
 test("selector state", function() {

0 notes on commit d40083c

Please sign in to comment.
Something went wrong with that request. Please try again.