7be28fdaf43d9f124788a36c7fc1bbd2b8474cc9
[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                 _scriptEval: null,
65                 noCloneEvent: true,
66                 boxModel: null,
67                 inlineBlockNeedsLayout: false,
68                 shrinkWrapBlocks: false,
69                 reliableHiddenOffsets: true
70         };
71
72         // Make sure that the options inside disabled selects aren't marked as disabled
73         // (WebKit marks them as diabled)
74         select.disabled = true;
75         jQuery.support.optDisabled = !opt.disabled;
76
77         jQuery.support.scriptEval = function() {
78                 if ( jQuery.support._scriptEval === null ) {
79                         var root = document.documentElement,
80                                 script = document.createElement("script"),
81                                 id = "script" + jQuery.now();
82
83                         script.type = "text/javascript";
84                         try {
85                                 script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
86                         } catch(e) {}
87
88                         root.insertBefore( script, root.firstChild );
89
90                         // Make sure that the execution of code works by injecting a script
91                         // tag with appendChild/createTextNode
92                         // (IE doesn't support this, fails, and uses .text instead)
93                         if ( window[ id ] ) {
94                                 jQuery.support._scriptEval = true;
95                                 delete window[ id ];
96                         } else {
97                                 jQuery.support._scriptEval = false;
98                         }
99
100                         root.removeChild( script );
101                         // release memory in IE
102                         root = script = id  = null;
103                 }
104
105                 return jQuery.support._scriptEval;
106         };
107
108         // Test to see if it's possible to delete an expando from an element
109         // Fails in Internet Explorer
110         try {
111                 delete div.test;
112
113         } catch(e) {
114                 jQuery.support.deleteExpando = false;
115         }
116
117         if ( div.attachEvent && div.fireEvent ) {
118                 div.attachEvent("onclick", function click() {
119                         // Cloning a node shouldn't copy over any
120                         // bound event handlers (IE does this)
121                         jQuery.support.noCloneEvent = false;
122                         div.detachEvent("onclick", click);
123                 });
124                 div.cloneNode(true).fireEvent("onclick");
125         }
126
127         div = document.createElement("div");
128         div.innerHTML = "<input type='radio' name='radiotest' checked='checked'/>";
129
130         var fragment = document.createDocumentFragment();
131         fragment.appendChild( div.firstChild );
132
133         // WebKit doesn't clone checked state correctly in fragments
134         jQuery.support.checkClone = fragment.cloneNode(true).cloneNode(true).lastChild.checked;
135
136         // Figure out if the W3C box model works as expected
137         // document.body must exist before we can do this
138         jQuery(function() {
139                 var div = document.createElement("div");
140                 div.style.width = div.style.paddingLeft = "1px";
141
142                 document.body.appendChild( div );
143                 jQuery.boxModel = jQuery.support.boxModel = div.offsetWidth === 2;
144
145                 if ( "zoom" in div.style ) {
146                         // Check if natively block-level elements act like inline-block
147                         // elements when setting their display to 'inline' and giving
148                         // them layout
149                         // (IE < 8 does this)
150                         div.style.display = "inline";
151                         div.style.zoom = 1;
152                         jQuery.support.inlineBlockNeedsLayout = div.offsetWidth === 2;
153
154                         // Check if elements with layout shrink-wrap their children
155                         // (IE 6 does this)
156                         div.style.display = "";
157                         div.innerHTML = "<div style='width:4px;'></div>";
158                         jQuery.support.shrinkWrapBlocks = div.offsetWidth !== 2;
159                 }
160
161                 div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
162                 var tds = div.getElementsByTagName("td");
163
164                 // Check if table cells still have offsetWidth/Height when they are set
165                 // to display:none and there are still other visible table cells in a
166                 // table row; if so, offsetWidth/Height are not reliable for use when
167                 // determining if an element has been hidden directly using
168                 // display:none (it is still safe to use offsets if a parent element is
169                 // hidden; don safety goggles and see bug #4512 for more information).
170                 // (only IE 8 fails this test)
171                 jQuery.support.reliableHiddenOffsets = tds[0].offsetHeight === 0;
172
173                 tds[0].style.display = "";
174                 tds[1].style.display = "none";
175
176                 // Check if empty table cells still have offsetWidth/Height
177                 // (IE < 8 fail this test)
178                 jQuery.support.reliableHiddenOffsets = jQuery.support.reliableHiddenOffsets && tds[0].offsetHeight === 0;
179                 div.innerHTML = "";
180
181                 document.body.removeChild( div ).style.display = "none";
182                 div = tds = null;
183         });
184
185         // Technique from Juriy Zaytsev
186         // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
187         var eventSupported = function( eventName ) {
188                 var el = document.createElement("div");
189                 eventName = "on" + eventName;
190
191                 // We only care about the case where non-standard event systems
192                 // are used, namely in IE. Short-circuiting here helps us to
193                 // avoid an eval call (in setAttribute) which can cause CSP
194                 // to go haywire. See: https://developer.mozilla.org/en/Security/CSP
195                 if ( !el.attachEvent ) {
196                         return true;
197                 }
198
199                 var isSupported = (eventName in el);
200                 if ( !isSupported ) {
201                         el.setAttribute(eventName, "return;");
202                         isSupported = typeof el[eventName] === "function";
203                 }
204                 el = null;
205
206                 return isSupported;
207         };
208
209         jQuery.support.submitBubbles = eventSupported("submit");
210         jQuery.support.changeBubbles = eventSupported("change");
211
212         // release memory in IE
213         div = all = a = null;
214 })();
215 })( jQuery );