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