Skip to content

Commit a253430

Browse files
committed
Fix #12723 and simplification and optmization of defaultDisplay helper
1 parent 642e9a4 commit a253430

2 files changed

Lines changed: 34 additions & 19 deletions

File tree

src/css/defaultDisplay.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@ define([
44
], function( jQuery ) {
55

66
var iframe,
7-
elemdisplay = { BODY: "block" };
7+
elemdisplay = {};
88

99
/**
1010
* Retrieve the actual display of a element
1111
* @param {String} name nodeName of the element
1212
* @param {Object} doc Document object
1313
*/
14+
// Called only from within defaultDisplay
1415
function actualDisplay( name, doc ) {
1516
var elem = jQuery( doc.createElement( name ) ).appendTo( doc.body ),
16-
display = jQuery.css( elem[0], "display" );
17-
elem.remove();
17+
18+
// getDefaultComputedStyle might be reliably used only on attached element
19+
display = window.getDefaultComputedStyle ?
20+
21+
// Use of this method is a temporary fix (more like optmization) until something better comes along,
22+
// since it was removed from specification and supported only in FF
23+
window.getDefaultComputedStyle( elem[ 0 ] ).display : jQuery.css( elem[ 0 ], "display" );
24+
25+
// We don't have any data stored on the element,
26+
// so use "detach" method as fast way to get rid of the element
27+
elem.detach();
28+
1829
return display;
1930
}
2031

@@ -31,15 +42,15 @@ function defaultDisplay( nodeName ) {
3142

3243
// If the simple way fails, read from inside an iframe
3344
if ( display === "none" || !display ) {
45+
3446
// Use the already-created iframe if possible
35-
iframe = ( iframe ||
36-
jQuery("<iframe frameborder='0' width='0' height='0'/>")
37-
.css( "cssText", "display:block !important" )
38-
).appendTo( doc.documentElement );
47+
iframe = (iframe || jQuery( "<iframe frameborder='0' width='0' height='0'/>" )).appendTo( doc.documentElement );
3948

4049
// Always write a new HTML skeleton so Webkit and Firefox don't choke on reuse
41-
doc = ( iframe[0].contentWindow || iframe[0].contentDocument ).document;
42-
doc.write("<!doctype html><html><body>");
50+
doc = iframe[ 0 ].contentDocument;
51+
52+
// Support: IE
53+
doc.write();
4354
doc.close();
4455

4556
display = actualDisplay( nodeName, doc );

test/unit/css.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -543,21 +543,25 @@ test( "show() resolves correct default display for detached nodes", function(){
543543
span.remove();
544544
});
545545

546-
test("show() resolves correct default display #10227", function() {
547-
expect(2);
546+
test("show() resolves correct default display #10227", 4, function() {
547+
var html = jQuery( document.documentElement ),
548+
body = jQuery( "body" );
548549

549-
var body = jQuery("body");
550-
body.append(
551-
"<p id='ddisplay'>a<style>body{display:none}</style></p>"
552-
);
550+
body.append( "<p class='ddisplay'>a<style>body{display:none}</style></p>" );
553551

554-
equal( body.css("display"), "none", "Initial display: none" );
552+
equal( body.css("display"), "none", "Initial display for body element: none" );
555553

556554
body.show();
557-
equal( body.css("display"), "block", "Correct display: block" );
555+
equal( body.css("display"), "block", "Correct display for body element: block" );
556+
557+
body.append( "<p class='ddisplay'>a<style>html{display:none}</style></p>" );
558+
559+
equal( html.css("display"), "none", "Initial display for html element: none" );
560+
561+
html.show();
562+
equal( html.css( "display" ), "block", "Correct display for html element: block" );
558563

559-
jQuery("#ddisplay").remove();
560-
QUnit.expectJqData( body[0], "olddisplay" );
564+
jQuery( ".ddisplay" ).remove();
561565
});
562566

563567
test("show() resolves correct default display when iframe display:none #12904", function() {

0 commit comments

Comments
 (0)