defaultDisplay.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. define([
  2. "../core",
  3. "../manipulation" // appendTo
  4. ], function( jQuery ) {
  5. var iframe,
  6. elemdisplay = {};
  7. /**
  8. * Retrieve the actual display of a element
  9. * @param {String} name nodeName of the element
  10. * @param {Object} doc Document object
  11. */
  12. // Called only from within defaultDisplay
  13. function actualDisplay( name, doc ) {
  14. var style,
  15. elem = jQuery( doc.createElement( name ) ).appendTo( doc.body ),
  16. // getDefaultComputedStyle might be reliably used only on attached element
  17. display = window.getDefaultComputedStyle && ( style = window.getDefaultComputedStyle( elem[ 0 ] ) ) ?
  18. // Use of this method is a temporary fix (more like optimization) until something better comes along,
  19. // since it was removed from specification and supported only in FF
  20. style.display : jQuery.css( elem[ 0 ], "display" );
  21. // We don't have any data stored on the element,
  22. // so use "detach" method as fast way to get rid of the element
  23. elem.detach();
  24. return display;
  25. }
  26. /**
  27. * Try to determine the default display value of an element
  28. * @param {String} nodeName
  29. */
  30. function defaultDisplay( nodeName ) {
  31. var doc = document,
  32. display = elemdisplay[ nodeName ];
  33. if ( !display ) {
  34. display = actualDisplay( nodeName, doc );
  35. // If the simple way fails, read from inside an iframe
  36. if ( display === "none" || !display ) {
  37. // Use the already-created iframe if possible
  38. iframe = (iframe || jQuery( "<iframe frameborder='0' width='0' height='0'/>" )).appendTo( doc.documentElement );
  39. // Always write a new HTML skeleton so Webkit and Firefox don't choke on reuse
  40. doc = iframe[ 0 ].contentDocument;
  41. // Support: IE
  42. doc.write();
  43. doc.close();
  44. display = actualDisplay( nodeName, doc );
  45. iframe.detach();
  46. }
  47. // Store the correct default display
  48. elemdisplay[ nodeName ] = display;
  49. }
  50. return display;
  51. }
  52. return defaultDisplay;
  53. });