939ad4fcb55a457fe926552232ba2201b6dbf97b
[jquery.git] / src / support.js
1 (function( jQuery ) {
2
3 (function() {
4
5         jQuery.support = {};
6
7         var div = document.createElement("div");
8
9         div.style.display = "none";
10         div.innerHTML = "   <link/><table></table><a href='/a' style='color:red;float:left;opacity:.55;'>a</a><input type='checkbox'/>";
11
12         var all = div.getElementsByTagName("*"),
13                 a = div.getElementsByTagName("a")[0],
14                 select = document.createElement("select"),
15                 opt = select.appendChild( document.createElement("option") ),
16                 input = div.getElementsByTagName("input")[0];
17
18         // Can't get basic test support
19         if ( !all || !all.length || !a ) {
20                 return;
21         }
22
23         jQuery.support = {
24                 // IE strips leading whitespace when .innerHTML is used
25                 leadingWhitespace: div.firstChild.nodeType === 3,
26
27                 // Make sure that tbody elements aren't automatically inserted
28                 // IE will insert them into empty tables
29                 tbody: !div.getElementsByTagName("tbody").length,
30
31                 // Make sure that link elements get serialized correctly by innerHTML
32                 // This requires a wrapper element in IE
33                 htmlSerialize: !!div.getElementsByTagName("link").length,
34
35                 // Get the style information from getAttribute
36                 // (IE uses .cssText insted)
37                 style: /red/.test( a.getAttribute("style") ),
38
39                 // Make sure that URLs aren't manipulated
40                 // (IE normalizes it by default)
41                 hrefNormalized: a.getAttribute("href") === "/a",
42
43                 // Make sure that element opacity exists
44                 // (IE uses filter instead)
45                 // Use a regex to work around a WebKit issue. See #5145
46                 opacity: /^0.55$/.test( a.style.opacity ),
47
48                 // Verify style float existence
49                 // (IE uses styleFloat instead of cssFloat)
50                 cssFloat: !!a.style.cssFloat,
51
52                 // Make sure that if no value is specified for a checkbox
53                 // that it defaults to "on".
54                 // (WebKit defaults to "" instead)
55                 checkOn: input.value === "on",
56
57                 // Make sure that a selected-by-default option has a working selected property.
58                 // (WebKit defaults to false instead of true, IE too, if it's in an optgroup)
59                 optSelected: opt.selected,
60
61                 // Will be defined later
62                 deleteExpando: true,
63                 optDisabled: false,
64                 checkClone: false,
65                 noCloneEvent: true,
66                 noCloneChecked: true,
67                 boxModel: null,
68                 inlineBlockNeedsLayout: false,
69                 shrinkWrapBlocks: false,
70                 reliableHiddenOffsets: true,
71                 reliableMarginRight: true
72         };
73
74         input.checked = true;
75         jQuery.support.noCloneChecked = input.cloneNode( true ).checked;
76
77         // Make sure that the options inside disabled selects aren't marked as disabled
78         // (WebKit marks them as diabled)
79         select.disabled = true;
80         jQuery.support.optDisabled = !opt.disabled;
81
82         var _scriptEval = null;
83         jQuery.support.scriptEval = function() {
84                 if ( _scriptEval === null ) {
85                         var root = document.documentElement,
86                                 script = document.createElement("script"),
87                                 id = "script" + jQuery.now();
88
89                         try {
90                                 script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
91                         } catch(e) {}
92
93                         root.insertBefore( script, root.firstChild );
94
95                         // Make sure that the execution of code works by injecting a script
96                         // tag with appendChild/createTextNode
97                         // (IE doesn't support this, fails, and uses .text instead)
98                         if ( window[ id ] ) {
99                                 _scriptEval = true;
100                                 delete window[ id ];
101                         } else {
102                                 _scriptEval = false;
103                         }
104
105                         root.removeChild( script );
106                         // release memory in IE
107                         root = script = id  = null;
108                 }
109
110                 return _scriptEval;
111         };
112
113         // Test to see if it's possible to delete an expando from an element
114         // Fails in Internet Explorer
115         try {
116                 delete div.test;
117
118         } catch(e) {
119                 jQuery.support.deleteExpando = false;
120         }
121
122         if ( !div.addEventListener && div.attachEvent && div.fireEvent ) {
123                 div.attachEvent("onclick", function click() {
124                         // Cloning a node shouldn't copy over any
125                         // bound event handlers (IE does this)
126                         jQuery.support.noCloneEvent = false;
127                         div.detachEvent("onclick", click);
128                 });
129                 div.cloneNode(true).fireEvent("onclick");
130         }
131
132         div = document.createElement("div");
133         div.innerHTML = "<input type='radio' name='radiotest' checked='checked'/>";
134
135         var fragment = document.createDocumentFragment();
136         fragment.appendChild( div.firstChild );
137
138         // WebKit doesn't clone checked state correctly in fragments
139         jQuery.support.checkClone = fragment.cloneNode(true).cloneNode(true).lastChild.checked;
140
141         // Figure out if the W3C box model works as expected
142         // document.body must exist before we can do this
143         jQuery(function() {
144                 var div = document.createElement("div"),
145                         body = document.getElementsByTagName("body")[0];
146
147                 // Frameset documents with no body should not run this code
148                 if ( !body ) {
149                         return;
150                 }
151
152                 div.style.width = div.style.paddingLeft = "1px";
153                 body.appendChild( div );
154                 jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
155
156                 if ( "zoom" in div.style ) {
157                         // Check if natively block-level elements act like inline-block
158                         // elements when setting their display to 'inline' and giving
159                         // them layout
160                         // (IE < 8 does this)
161                         div.style.display = "inline";
162                         div.style.zoom = 1;
163                         jQuery.support.inlineBlockNeedsLayout = div.offsetWidth === 2;
164
165                         // Check if elements with layout shrink-wrap their children
166                         // (IE 6 does this)
167                         div.style.display = "";
168                         div.innerHTML = "<div style='width:4px;'></div>";
169                         jQuery.support.shrinkWrapBlocks = div.offsetWidth !== 2;
170                 }
171
172                 div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
173                 var tds = div.getElementsByTagName("td");
174
175                 // Check if table cells still have offsetWidth/Height when they are set
176                 // to display:none and there are still other visible table cells in a
177                 // table row; if so, offsetWidth/Height are not reliable for use when
178                 // determining if an element has been hidden directly using
179                 // display:none (it is still safe to use offsets if a parent element is
180                 // hidden; don safety goggles and see bug #4512 for more information).
181                 // (only IE 8 fails this test)
182                 jQuery.support.reliableHiddenOffsets = tds[0].offsetHeight === 0;
183
184                 tds[0].style.display = "";
185                 tds[1].style.display = "none";
186
187                 // Check if empty table cells still have offsetWidth/Height
188                 // (IE < 8 fail this test)
189                 jQuery.support.reliableHiddenOffsets = jQuery.support.reliableHiddenOffsets && tds[0].offsetHeight === 0;
190                 div.innerHTML = "";
191
192                 // Check if div with explicit width and no margin-right incorrectly
193                 // gets computed margin-right based on width of container. For more
194                 // info see bug #3333
195                 // Fails in WebKit before Feb 2011 nightlies
196                 // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
197                 if ( document.defaultView && document.defaultView.getComputedStyle ) {
198                         div.style.width = "1px";
199                         div.style.marginRight = "0";
200                         jQuery.support.reliableMarginRight = ( parseInt(document.defaultView.getComputedStyle(div).marginRight, 10) || 0 ) === 0;
201                 }
202
203                 body.removeChild( div ).style.display = "none";
204                 div = tds = null;
205         });
206
207         // Technique from Juriy Zaytsev
208         // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
209         var eventSupported = function( eventName ) {
210                 var el = document.createElement("div");
211                 eventName = "on" + eventName;
212
213                 // We only care about the case where non-standard event systems
214                 // are used, namely in IE. Short-circuiting here helps us to
215                 // avoid an eval call (in setAttribute) which can cause CSP
216                 // to go haywire. See: https://developer.mozilla.org/en/Security/CSP
217                 if ( !el.attachEvent ) {
218                         return true;
219                 }
220
221                 var isSupported = (eventName in el);
222                 if ( !isSupported ) {
223                         el.setAttribute(eventName, "return;");
224                         isSupported = typeof el[eventName] === "function";
225                 }
226                 el = null;
227
228                 return isSupported;
229         };
230
231         jQuery.support.submitBubbles = eventSupported("submit");
232         jQuery.support.changeBubbles = eventSupported("change");
233
234         // release memory in IE
235         div = all = a = null;
236 })();
237 })( jQuery );