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