Make sure that checked state is cloned properly. Based upon the patch by Michael...
[jquery.git] / src / support.js
1 (function() {
2
3         jQuery.support = {};
4
5         var root = document.documentElement,
6                 script = document.createElement("script"),
7                 div = document.createElement("div"),
8                 id = "script" + now();
9
10         div.style.display = "none";
11         div.innerHTML = "   <link/><table></table><a href='/a' style='color:red;float:left;opacity:.55;'>a</a><input type='checkbox'/>";
12
13         var all = div.getElementsByTagName("*"),
14                 a = div.getElementsByTagName("a")[0];
15
16         // Can't get basic test support
17         if ( !all || !all.length || !a ) {
18                 return;
19         }
20
21         jQuery.support = {
22                 // IE strips leading whitespace when .innerHTML is used
23                 leadingWhitespace: div.firstChild.nodeType === 3,
24
25                 // Make sure that tbody elements aren't automatically inserted
26                 // IE will insert them into empty tables
27                 tbody: !div.getElementsByTagName("tbody").length,
28
29                 // Make sure that link elements get serialized correctly by innerHTML
30                 // This requires a wrapper element in IE
31                 htmlSerialize: !!div.getElementsByTagName("link").length,
32
33                 // Get the style information from getAttribute
34                 // (IE uses .cssText insted)
35                 style: /red/.test( a.getAttribute("style") ),
36
37                 // Make sure that URLs aren't manipulated
38                 // (IE normalizes it by default)
39                 hrefNormalized: a.getAttribute("href") === "/a",
40
41                 // Make sure that element opacity exists
42                 // (IE uses filter instead)
43                 // Use a regex to work around a WebKit issue. See #5145
44                 opacity: /^0.55$/.test( a.style.opacity ),
45
46                 // Verify style float existence
47                 // (IE uses styleFloat instead of cssFloat)
48                 cssFloat: !!a.style.cssFloat,
49
50                 // Make sure that if no value is specified for a checkbox
51                 // that it defaults to "on".
52                 // (WebKit defaults to "" instead)
53                 checkOn: div.getElementsByTagName("input")[0].value === "on",
54
55                 // Make sure that a selected-by-default option has a working selected property.
56                 // (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
57                 optSelected: document.createElement("select").appendChild( document.createElement("option") ).selected,
58
59                 // Will be defined later
60                 checkClone: false,
61                 scriptEval: false,
62                 noCloneEvent: true,
63                 boxModel: null
64         };
65
66         script.type = "text/javascript";
67         try {
68                 script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
69         } catch(e) {}
70
71         root.insertBefore( script, root.firstChild );
72
73         // Make sure that the execution of code works by injecting a script
74         // tag with appendChild/createTextNode
75         // (IE doesn't support this, fails, and uses .text instead)
76         if ( window[ id ] ) {
77                 jQuery.support.scriptEval = true;
78                 delete window[ id ];
79         }
80
81         root.removeChild( script );
82
83         if ( div.attachEvent && div.fireEvent ) {
84                 div.attachEvent("onclick", function click() {
85                         // Cloning a node shouldn't copy over any
86                         // bound event handlers (IE does this)
87                         jQuery.support.noCloneEvent = false;
88                         div.detachEvent("onclick", click);
89                 });
90                 div.cloneNode(true).fireEvent("onclick");
91         }
92
93         div = document.createElement("div");
94         div.innerHTML = "<input type='radio' name='radiotest' checked='checked'/>";
95
96         var fragment = document.createDocumentFragment();
97         fragment.appendChild( div.firstChild );
98
99         // WebKit doesn't clone checked state correctly in fragments
100         jQuery.support.checkClone = fragment.cloneNode(true).cloneNode(true).lastChild.checked;
101
102         // Figure out if the W3C box model works as expected
103         // document.body must exist before we can do this
104         jQuery(function() {
105                 var div = document.createElement("div");
106                 div.style.width = div.style.paddingLeft = "1px";
107
108                 document.body.appendChild( div );
109                 jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
110                 document.body.removeChild( div ).style.display = 'none';
111                 div = null;
112         });
113
114         // Technique from Juriy Zaytsev
115         // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
116         var eventSupported = function( eventName ) { 
117                 var el = document.createElement("div"); 
118                 eventName = "on" + eventName; 
119
120                 var isSupported = (eventName in el); 
121                 if ( !isSupported ) { 
122                         el.setAttribute(eventName, "return;"); 
123                         isSupported = typeof el[eventName] === "function"; 
124                 } 
125                 el = null; 
126
127                 return isSupported; 
128         };
129         
130         jQuery.support.submitBubbles = eventSupported("submit");
131         jQuery.support.changeBubbles = eventSupported("change");
132
133         // release memory in IE
134         root = script = div = all = a = null;
135 })();
136
137 jQuery.props = {
138         "for": "htmlFor",
139         "class": "className",
140         readonly: "readOnly",
141         maxlength: "maxLength",
142         cellspacing: "cellSpacing",
143         rowspan: "rowSpan",
144         colspan: "colSpan",
145         tabindex: "tabIndex",
146         usemap: "useMap",
147         frameborder: "frameBorder"
148 };