Skip to content

Commit 60c2859

Browse files
committed
Workaround for Webkit's broken Node.cloneNode implementation where radio inputs checked attributes are not copied.
1 parent eb496f7 commit 60c2859

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/manipulation.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
55
rtagName = /<([\w:]+)/,
66
rtbody = /<tbody/i,
77
rhtml = /<|&\w+;/,
8+
rchecked = /checked\s*([^=]|=\s*("checked"|'checked'))/i, // checked="checked" or checked (html5)
89
fcloseTag = function( all, front, tag ) {
910
return rselfClosing.test( tag ) ?
1011
all :
@@ -324,7 +325,8 @@ function cloneCopyEvent(orig, ret) {
324325
function buildFragment( args, nodes, scripts ) {
325326
var fragment, cacheable, cached, cacheresults, doc;
326327

327-
if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
328+
// webkit does not clone 'checked' attribute of radio inputs on cloneNode, so don't cache if string has a checked
329+
if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 && !rchecked.test( args[0] ) ) {
328330
cacheable = true;
329331
cacheresults = jQuery.fragments[ args[0] ];
330332
if ( cacheresults ) {

test/unit/manipulation.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ test("unwrap()", function() {
197197
});
198198

199199
var testAppend = function(valueObj) {
200-
expect(22);
200+
expect(25);
201201
var defaultText = 'Try them out:'
202202
var result = jQuery('#first').append(valueObj('<b>buga</b>'));
203203
equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
@@ -230,6 +230,18 @@ var testAppend = function(valueObj) {
230230
ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
231231
ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
232232
ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
233+
234+
reset();
235+
jQuery('form:last').append('<input type="radio" id="checkedRadio" checked="checked" />');
236+
ok( jQuery('#checkedRadio').is(':checked'), "Append checked radio");
237+
238+
reset();
239+
jQuery('form:last').append('<input checked = "checked" id="checkedRadio" type= "radio" />');
240+
ok( jQuery('#checkedRadio').is(':checked'), "Append alternately formated checked radio");
241+
242+
reset();
243+
jQuery('form:last').append('<input checked id="checkedRadio" type= "radio" />');
244+
ok( jQuery('#checkedRadio').is(':checked'), "Append HTML5-formated checked radio");
233245

234246
reset();
235247
jQuery("#sap").append(valueObj( document.getElementById('form') ));

0 commit comments

Comments
 (0)