Created
August 30, 2012 19:41
-
-
Save termi/3538932 to your computer and use it in GitHub Desktop.
Fix Safari bug with [radio input with no form].cloneNode()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<form> | |
<div id="test3"> | |
test1 : | |
<input id="test1" type="radio" name="test" checked /> | |
</div> | |
test 2: | |
<input type="radio" name="test" /> | |
</form> | |
<button id="test">test</button> | |
<input type="radio" name="test" checked /> | |
<script> | |
test.onclick = function() { | |
test3.cloneNode(true); | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @license Safari input[type=radio].cloneNode fix | MIT License | github.com/termi */ | |
// ==ClosureCompiler== | |
// @output_file_name safariRadioInputCloneNodeFix.min.js | |
// @compilation_level ADVANCED_OPTIMIZATIONS | |
// ==/ClosureCompiler== | |
;(function(){ | |
/** @type {(Node|Function)} */ | |
var test_and_bugfix; | |
test_and_bugfix = document.createElement("div"); | |
test_and_bugfix.innerHTML = "<input type=radio checked name=t_e>"; | |
test_and_bugfix.cloneNode(true); | |
if(test_and_bugfix.firstChild.checked === false) { | |
/** | |
* @param {(Array.<Node>|Node|NodeList)} inputs | |
* @param {boolean=} stop | |
*/ | |
test_and_bugfix = function(inputs, stop) { | |
if(!inputs || inputs.length === 0)return []; | |
var result = [] | |
, i = 0 | |
, input = | |
( | |
inputs["length"] !== void 0 ?//isArray | |
inputs[i] : | |
inputs | |
) | |
, originalChecked | |
, inputName | |
, _document | |
; | |
do { | |
inputName = input.name; | |
if(input.form !== null && !inputName || input["__originalChecked__"] !== void 0)continue; | |
if(originalChecked = input.checked) { | |
input["__originalChecked__"] = originalChecked; | |
result.push(input); | |
} | |
if(!stop) { | |
_document = input.ownerDocument; | |
if(_document) { | |
result = result.concat(test_and_bugfix(_document.querySelectorAll("input[type=radio][name=\"" + inputName + "\"]:checked"), true)); | |
} | |
} | |
} | |
while(input = inputs[++i]); | |
return result; | |
} | |
var _Node_cloneNode_ = Node.prototype.cloneNode; | |
Node.prototype.cloneNode = function(deep) { | |
var result | |
, inputs | |
, input | |
, tmp | |
; | |
if((this.nodeName || "").toLowerCase() == "input" && this.type === "radio" && (name = this.name)) { | |
inputs = test_and_bugfix(this); | |
} | |
else if(deep) { | |
inputs = test_and_bugfix(this.querySelectorAll("input[type=radio][name]")); | |
} | |
result = _Node_cloneNode_.call(this, deep); | |
if(inputs)while(input = inputs.pop()) { | |
if(tmp = input["__originalChecked__"]) { | |
input.checked = tmp; | |
delete input["__originalChecked__"]; | |
} | |
} | |
return result; | |
} | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Safari input[type=radio].cloneNode fix | MIT License | github.com/termi */ | |
;(function() {"use strict"; | |
var d;d=document.createElement("div");d.innerHTML="<input type=radio checked name=t_e>";d.cloneNode(!0); | |
if(!1===d.firstChild.checked){d=function(a,e){if(!a||0===a.length)return[];var c=[],f=0,b=void 0!==a.length?a[f]:a,h,g;do if(g=b.name,!(null!==b.form&&!g||void 0!==b.__originalChecked__)){if(h=b.checked)b.__originalChecked__=h,c.push(b);e||(b=b.ownerDocument)&&(c=c.concat(d(b.querySelectorAll('input[type=radio][name="'+g+'"]:checked'),!0)))}while(b=a[++f]);return c};var i=Node.prototype.cloneNode;Node.prototype.cloneNode=function(a){var e,c,f;"input"==(this.nodeName||"").toLowerCase()&&"radio"=== | |
this.type&&(name=this.name)?e=d(this):a&&(e=d(this.querySelectorAll("input[type=radio][name]")));a=i.call(this,a);if(e)for(;c=e.pop();)if(f=c.__originalChecked__)c.checked=f,delete c.__originalChecked__;return a}}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment