Skip to content

CSS: Ignore the CSS cascade in show()/hide()/etc. #2180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
64 changes: 2 additions & 62 deletions src/css.js
Original file line number Diff line number Diff line change
@@ -10,17 +10,16 @@ define([
"./css/var/getStyles",
"./css/curCSS",
"./css/adjustCSS",
"./css/defaultDisplay",
"./css/addGetHookIf",
"./css/support",
"./data/var/dataPriv",
"./css/showHide",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pull .show() and .hide() into this file and remove the dependency inside css.js completely? That way it becomes a separate module and can be removed in a custom build if you use classes for showing and hiding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, introduce a new showHide module (.show, .hide, and .toggle) depending on data and depended upon by effects? That sounds reasonable, but out of scope for this ticket.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it can go in a different ticket and pull request, I'll create an issue. Now that showHide is broken out it seems pretty clear we could do that, and in fact the new module only helps with size reductions if it can be excluded. As it is right now, it can't ever be excluded because of the dependency here.


"./core/init",
"./css/swap",
"./core/ready",
"./selector" // contains
], function( jQuery, pnum, access, rmargin, rcssNum, rnumnonpx, cssExpand, isHidden,
getStyles, curCSS, adjustCSS, defaultDisplay, addGetHookIf, support, dataPriv ) {
getStyles, curCSS, adjustCSS, addGetHookIf, support, showHide ) {

var
// Swappable if display is none or starts with table
@@ -151,65 +150,6 @@ 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
71 changes: 0 additions & 71 deletions src/css/defaultDisplay.js

This file was deleted.

47 changes: 47 additions & 0 deletions src/css/showHide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
define([
"../data/var/dataPriv"
], function( dataPriv ) {

function showHide( elements, show ) {
var display, elem,
values = [],
index = 0,
length = elements.length;

// Determine new display value for elements that need to change
for ( ; index < length; index++ ) {
elem = elements[ index ];
if ( !elem.style ) {
continue;
}

display = elem.style.display;
if ( show ) {
if ( display === "none" ) {
// Restore a pre-hide() value if we have one
values[ index ] = dataPriv.get( elem, "display" ) || "";
}
} else {
if ( display !== "none" ) {
values[ index ] = "none";

// Remember the value we're replacing
dataPriv.set( elem, "display", display );
}
}
}

// Set the display of the elements in a second loop
// to avoid the constant reflow
for ( index = 0; index < length; index++ ) {
if ( values[ index ] != null ) {
elements[ index ].style.display = values[ index ];
}
}

return elements;
}

return showHide;

});
Loading
Oops, something went wrong.