Skip to content

Commit d40083c

Browse files
committed
Disabled the passthrough .attr(method_name) functionality. You can now use it if you do: .attr({method_name: value}, true) OR as an easy initialization method: jQuery('<div/>', {html: '...', id: 'test'}).
1 parent 148fb7b commit d40083c

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

src/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ jQuery.extend({
232232
offset: true
233233
},
234234

235-
attr: function( elem, name, value ) {
235+
attr: function( elem, name, value, pass ) {
236236
// don't set attributes on text and comment nodes
237237
if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
238238
return undefined;
239239
}
240240

241-
if ( name in jQuery.attrFn && value !== undefined ) {
241+
if ( pass && name in jQuery.attrFn ) {
242242
return jQuery(elem)[name](value);
243243
}
244244

src/core.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ jQuery.fn = jQuery.prototype = {
8484
ret = rsingleTag.exec( selector );
8585

8686
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+
}
8894

8995
} else {
9096
ret = buildFragment( [ match[1] ], [ doc ] );
@@ -687,13 +693,13 @@ function evalScript( i, elem ) {
687693

688694
// Mutifunctional method to get and set values to a collection
689695
// 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 ) {
691697
var length = elems.length;
692698

693699
// Setting many attributes
694700
if ( typeof key === "object" ) {
695701
for ( var k in key ) {
696-
access( elems, k, key[k], exec, fn );
702+
access( elems, k, key[k], exec, fn, value );
697703
}
698704
return elems;
699705
}
@@ -704,7 +710,7 @@ function access( elems, key, value, exec, fn ) {
704710
exec = exec && jQuery.isFunction(value);
705711

706712
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 );
708714
}
709715

710716
return elems;

test/unit/attributes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var functionReturningObj = function(value) { return (function() { return value;
55

66
test("attr(String)", function() {
77
expect(28);
8-
8+
99
// This one sometimes fails randomly ?!
1010
equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
1111

@@ -190,24 +190,24 @@ test("attr(jquery_method)", function(){
190190
elem = $elem[0];
191191

192192
// one at a time
193-
$elem.attr('html', 'foo');
193+
$elem.attr({'html': 'foo'}, true);
194194
equals( elem.innerHTML, 'foo', 'attr(html)');
195195

196-
$elem.attr('text', 'bar');
196+
$elem.attr({'text': 'bar'}, true);
197197
equals( elem.innerHTML, 'bar', 'attr(text)');
198198

199-
$elem.attr('css', {color:'red'});
199+
$elem.attr({'css': {color:'red'}}, true);
200200
ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
201201

202-
$elem.attr('height', 10);
202+
$elem.attr({'height': 10}, true);
203203
equals( elem.style.height, '10px', 'attr(height)');
204204

205205
// Multiple attributes
206206

207207
$elem.attr({
208208
width:10,
209209
css:{ paddingLeft:1, paddingRight:1 }
210-
});
210+
}, true);
211211

212212
equals( elem.style.width, '10px', 'attr({...})');
213213
equals( elem.style.paddingLeft, '1px', 'attr({...})');

test/unit/core.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test("Basic requirements", function() {
1212
});
1313

1414
test("jQuery()", function() {
15-
expect(15);
15+
expect(22);
1616

1717
// Basic constructor's behavior
1818

@@ -62,6 +62,22 @@ test("jQuery()", function() {
6262
equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
6363

6464
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');
6581
});
6682

6783
test("selector state", function() {

0 commit comments

Comments
 (0)