Ticket #8099 Conditional iframe approache, caches both iframe and iframedoc for reuse

This commit is contained in:
rwldrn 2011-04-13 13:43:52 -04:00
parent 81de96af74
commit 4fde550cb6
2 changed files with 45 additions and 26 deletions

47
src/effects.js vendored
View File

@ -551,41 +551,56 @@ function defaultDisplay( nodeName ) {
if ( !elemdisplay[ nodeName ] ) { if ( !elemdisplay[ nodeName ] ) {
var iframe, iframeDoc, iframeNode, display, elem; var elem = jQuery( "<" + nodeName + ">" ).appendTo( "body" ),
display = elem.css( "display" );
iframe = defaultDisplay.iframe.clone()[ 0 ]; elem.remove();
if ( display === "none" || display === "" ) {
var iframe = defaultDisplay.iframe,
iframeDoc = defaultDisplay.iframeDoc;
// No iframe to use yet, so create it
if ( !defaultDisplay.iframe ) {
iframe = document.createElement( "iframe" );
iframe.width = iframe.height = 0;
document.body.appendChild( iframe ); document.body.appendChild( iframe );
iframeDoc = ( iframe.contentWindow || iframe.contentDocument ).document; iframeDoc = ( iframe.contentWindow || iframe.contentDocument ).document;
iframeDoc.open();
iframeDoc.write("<!doctype><html><body></body></html>"); iframeDoc.write("<!doctype><html><body></body></html>");
// Cache iframe element
defaultDisplay.iframe = iframe;
defaultDisplay.iframeDoc = iframeDoc;
} else {
// Reuse previous iframe
document.body.appendChild( iframe );
}
elem = iframeDoc.createElement( nodeName ); elem = iframeDoc.createElement( nodeName );
iframeDoc.body.appendChild( elem ); iframeDoc.body.appendChild( elem );
iframeDoc.close();
display = jQuery( elem ).css( "display" ); display = jQuery( elem ).css( "display" );
if ( display === "none" || display === "" ) { iframe.parentNode.removeChild( iframe );
display = "block";
} }
// Store the correct default display // Store the correct default display
elemdisplay[ nodeName ] = display; elemdisplay[ nodeName ] = display;
iframe.parentNode.removeChild( iframe );
} }
return elemdisplay[ nodeName ]; return elemdisplay[ nodeName ];
} }
defaultDisplay.iframe = jQuery("<iframe/>", { defaultDisplay.iframe = null;
css: { defaultDisplay.iframeDoc = null;
width: 0,
height: 0,
border: 0
}
});
})( jQuery ); })( jQuery );

View File

@ -163,15 +163,19 @@ test("Persist correct display value", function() {
}); });
test("show() resolves correct default display #8099", function() { test("show() resolves correct default display #8099", function() {
expect(3); expect(5);
var bug8099 = jQuery("<tt/>").appendTo("#main"); var bug8099 = jQuery("<tt/>").appendTo("#main");
equals( bug8099.css("display"), "none", "default display override for all tt" ); equals( bug8099.css("display"), "none", "default display override for all tt" );
equals( bug8099.show().css("display"), "inline", "Correctly resolves display:inline" ); equals( bug8099.show().css("display"), "inline", "Correctly resolves display:inline" );
equals( jQuery("#foo").hide().show().css("display"), "block", "Correctly resolves display:block after hide/show" );
equals( bug8099.hide().css("display"), "none", "default display override for all tt" );
equals( bug8099.show().css("display"), "inline", "Correctly resolves display:inline" );
bug8099.remove(); bug8099.remove();
equals( jQuery("#foo").hide().show().css("display"), "block", "Correctly resolves display:block after hide/show" );
}); });