Skip to content

Commit ceba855

Browse files
committed
Fixes #9239. If the body is already present in the DOM, use a div within it to perform boxModel-related support tests. Unit test added.
1 parent b60c856 commit ceba855

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/support.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ jQuery.support = (function() {
1313
support,
1414
fragment,
1515
body,
16-
bodyStyle,
16+
testElementParent,
17+
testElement,
18+
testElementStyle,
1719
tds,
1820
events,
1921
eventName,
@@ -136,9 +138,11 @@ jQuery.support = (function() {
136138
// Figure out if the W3C box model works as expected
137139
div.style.width = div.style.paddingLeft = "1px";
138140

139-
// We use our own, invisible, body
140-
body = document.createElement( "body" );
141-
bodyStyle = {
141+
body = document.getElementsByTagName( "body" )[ 0 ];
142+
// We use our own, invisible, body unless the body is already present
143+
// in which case we use a div (#9239)
144+
testElement = document.createElement( body ? "div" : "body" );
145+
testElementStyle = {
142146
visibility: "hidden",
143147
width: 0,
144148
height: 0,
@@ -147,11 +151,19 @@ jQuery.support = (function() {
147151
// Set background to avoid IE crashes when removing (#9028)
148152
background: "none"
149153
};
150-
for ( i in bodyStyle ) {
151-
body.style[ i ] = bodyStyle[ i ];
154+
if ( body ) {
155+
jQuery.extend( testElementStyle, {
156+
position: "absolute",
157+
left: -1000,
158+
top: -1000
159+
});
160+
}
161+
for ( i in testElementStyle ) {
162+
testElement.style[ i ] = testElementStyle[ i ];
152163
}
153-
body.appendChild( div );
154-
documentElement.insertBefore( body, documentElement.firstChild );
164+
testElement.appendChild( div );
165+
testElementParent = body || documentElement;
166+
testElementParent.insertBefore( testElement, testElementParent.firstChild );
155167

156168
// Check if a disconnected checkbox will retain its checked
157169
// value of true after appended to the DOM (IE6/7)
@@ -210,8 +222,8 @@ jQuery.support = (function() {
210222
}
211223

212224
// Remove the body element we added
213-
body.innerHTML = "";
214-
documentElement.removeChild( body );
225+
testElement.innerHTML = "";
226+
testElementParent.removeChild( testElement );
215227

216228
// Technique from Juriy Zaytsev
217229
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/

test/unit/support.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,26 @@ function supportIFrameTest( title, url, noDisplay, func ) {
3030
supportIFrameTest( "proper boxModel in compatMode CSS1Compat (IE6 and IE7)", "boxModelIE", function( compatMode, boxModel ) {
3131
ok( compatMode !== "CSS1Compat" || boxModel, "boxModel properly detected" );
3232
});
33+
34+
supportIFrameTest( "body background is not lost if set prior to loading jQuery (#9238)", "bodyBackground", function( color, support ) {
35+
expect( 2 );
36+
var okValue = {
37+
"#000000": true,
38+
"rgb(0, 0, 0)": true
39+
};
40+
ok( okValue[ color ], "color was not reset (" + color + ")" );
41+
var i, passed = true;
42+
for ( i in jQuery.support ) {
43+
if ( jQuery.support[ i ] !== support[ i ] ) {
44+
passed = false;
45+
strictEquals( jQuery.support[ i ], support[ i ], "Support property " + i + " is different" );
46+
}
47+
}
48+
for ( i in support ) {
49+
if ( !( i in jQuery.support ) ) {
50+
ok = false;
51+
strictEquals( src[ i ], dest[ i ], "Unexpected property: " + i );
52+
}
53+
}
54+
ok( passed, "Same support properties" );
55+
});

0 commit comments

Comments
 (0)