From 0169459d5d896a87e4cc0026bfd9b823d5525220 Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Wed, 11 Nov 2015 20:50:37 +0300 Subject: [PATCH] Revert "CSS: Ignore the CSS cascade in show()/hide()/etc." This reverts commit 86419b10bfa5e3b71a7d416288ab806d47a31d1f. --- src/css.js | 81 ++++++++--- src/css/defaultDisplay.js | 72 ++++++++++ src/effects.js | 188 ++++++++++-------------- test/data/testsuite.css | 12 +- test/unit/css.js | 201 ++++++++++++++++---------- test/unit/effects.js | 296 ++++++++++++++------------------------ 6 files changed, 458 insertions(+), 392 deletions(-) create mode 100644 src/css/defaultDisplay.js diff --git a/src/css.js b/src/css.js index c0cee5baa..7f085560e 100644 --- a/src/css.js +++ b/src/css.js @@ -12,15 +12,16 @@ define( [ "./css/var/swap", "./css/curCSS", "./css/adjustCSS", + "./css/defaultDisplay", "./css/addGetHookIf", "./css/support", - "./css/showHide", + "./data/var/dataPriv", "./core/init", "./core/ready", "./selector" // contains -], function( jQuery, pnum, access, rmargin, document, rcssNum, rnumnonpx, cssExpand, - isHidden, getStyles, swap, curCSS, adjustCSS, addGetHookIf, support, showHide ) { +], function( jQuery, pnum, access, rmargin, document, rcssNum, rnumnonpx, cssExpand, isHidden, + getStyles, swap, curCSS, adjustCSS, defaultDisplay, addGetHookIf, support, dataPriv ) { var @@ -28,6 +29,7 @@ var // except "table", "table-cell", or "table-caption" // See here for display values: https://developer.mozilla.org/en-US/docs/CSS/display rdisplayswap = /^(none|table(?!-c[ea]).+)/, + cssShow = { position: "absolute", visibility: "hidden", display: "block" }, cssNormalTransform = { letterSpacing: "0", @@ -171,6 +173,66 @@ function getWidthOrHeight( elem, name, extra ) { ) + "px"; } +function showHide( elements, show ) { + var display, elem, hidden, + values = [], + index = 0, + length = elements.length; + + for ( ; index < length; index++ ) { + elem = elements[ index ]; + if ( !elem.style ) { + continue; + } + + values[ index ] = dataPriv.get( elem, "olddisplay" ); + display = elem.style.display; + if ( show ) { + + // Reset the inline display of this element to learn if it is + // being hidden by cascaded rules or not + if ( !values[ index ] && display === "none" ) { + elem.style.display = ""; + } + + // Set elements which have been overridden with display: none + // in a stylesheet to whatever the default browser style is + // for such an element + if ( elem.style.display === "" && isHidden( elem ) ) { + values[ index ] = dataPriv.access( + elem, + "olddisplay", + defaultDisplay( elem.nodeName ) + ); + } + } else { + hidden = isHidden( elem ); + + if ( display !== "none" || !hidden ) { + dataPriv.set( + elem, + "olddisplay", + hidden ? display : jQuery.css( elem, "display" ) + ); + } + } + } + + // Set the display of most of the elements in a second loop + // to avoid the constant reflow + for ( index = 0; index < length; index++ ) { + elem = elements[ index ]; + if ( !elem.style ) { + continue; + } + if ( !show || elem.style.display === "none" || elem.style.display === "" ) { + elem.style.display = show ? values[ index ] || "" : "none"; + } + } + + return elements; +} + jQuery.extend( { // Add in style property hooks for overriding the default @@ -353,19 +415,6 @@ jQuery.each( [ "height", "width" ], function( i, name ) { }; } ); -jQuery.cssHooks.marginLeft = addGetHookIf( support.reliableMarginLeft, - function( elem, computed ) { - if ( computed ) { - return ( parseFloat( curCSS( elem, "marginLeft" ) ) || - elem.getBoundingClientRect().left - - swap( elem, { marginLeft: 0 }, function() { - return elem.getBoundingClientRect().left; - } ) - ) + "px"; - } - } -); - // These hooks are used by animate to expand properties jQuery.each( { margin: "", diff --git a/src/css/defaultDisplay.js b/src/css/defaultDisplay.js new file mode 100644 index 000000000..b1fb5774d --- /dev/null +++ b/src/css/defaultDisplay.js @@ -0,0 +1,72 @@ +define( [ + "../core", + "../var/document", + "../manipulation" // appendTo +], function( jQuery, document ) { + +var iframe, + elemdisplay = { + + // Support: Firefox + // We have to pre-define these values for FF (#10227) + HTML: "block", + BODY: "block" + }; + +/** + * Retrieve the actual display of a element + * @param {String} name nodeName of the element + * @param {Object} doc Document object + */ + +// Called only from within defaultDisplay +function actualDisplay( name, doc ) { + var elem = jQuery( doc.createElement( name ) ).appendTo( doc.body ), + + display = jQuery.css( elem[ 0 ], "display" ); + + // We don't have any data stored on the element, + // so use "detach" method as fast way to get rid of the element + elem.detach(); + + return display; +} + +/** + * Try to determine the default display value of an element + * @param {String} nodeName + */ +function defaultDisplay( nodeName ) { + var doc = document, + display = elemdisplay[ nodeName ]; + + if ( !display ) { + display = actualDisplay( nodeName, doc ); + + // If the simple way fails, read from inside an iframe + if ( display === "none" || !display ) { + + // Use the already-created iframe if possible + iframe = ( iframe || jQuery( "