Fix #11767: modularize effects #786

Closed
wants to merge 3 commits into
from
View
@@ -50,7 +50,7 @@ module.exports = function( grunt ) {
"src/ajax/jsonp.js",
"src/ajax/script.js",
"src/ajax/xhr.js",
- "src/effects.js",
+ { flag: "effects", src: "src/effects.js" },
"src/offset.js",
"src/dimensions.js",
"src/exports.js",
@@ -104,7 +104,7 @@ module.exports = function( grunt ) {
});
// Default grunt.
- grunt.registerTask( "default", "selector build lint min compare_size" );
+ grunt.registerTask( "default", "selector build:*:* lint min compare_size" );
grunt.loadNpmTasks("grunt-compare-size");
@@ -159,29 +159,48 @@ module.exports = function( grunt ) {
// Special concat/build task to handle various jQuery build requirements
- grunt.registerMultiTask( "build", "Concatenate source, embed date/version", function() {
- // Concat specified files.
- var compiled = "",
- name = this.file.dest;
+ grunt.registerMultiTask(
+ "build",
+ "Concatenate source (include/exclude modules with +/- flags), embed date/version",
+ function() {
+ // Concat specified files.
+ var compiled = "",
+ modules = this.flags,
+ optIn = !modules["*"],
+ name = this.file.dest;
+
+ this.file.src.forEach(function( filepath ) {
+ // Include optional modules per build flags; exclusion trumps inclusion
+ var flag = filepath.flag;
+ if ( flag ) {
+ if ( modules[ "-" + flag ] ||
+ optIn && !modules[ flag ] && !modules[ "+" + flag ] ) {
+
+ log.writeln( "Excluding " + filepath.flag + ": '" + filepath.src + "'." );
+ return;
+ }
+ log.writeln( "Including " + filepath.flag + ": '" + filepath.src + "'." );
+ filepath = filepath.src;
+ }
+
+ // Unwrap redundant IIFEs
+ compiled += file.read( filepath ).replace( /^\(function\( jQuery \) \{|\}\)\( jQuery \);\s*$/g, "" );
+ });
+
+ // Embed Date
+ // Embed Version
+ compiled = compiled.replace( "@DATE", new Date() )
+ .replace( "@VERSION", config("pkg.version") );
+
+ // Write concatenated source to file
+ file.write( name, compiled );
+
+ // Fail task if errors were logged.
+ if ( this.errorCount ) {
+ return false;
+ }
- this.file.src.forEach(function( filepath ) {
- compiled += file.read( filepath ).replace( /.function..jQuery...\{/g, "" ).replace( /\}...jQuery..;/g, "" );
+ // Otherwise, print a success message.
+ log.writeln( "File '" + name + "' created." );
});
-
- // Embed Date
- // Embed Version
- compiled = compiled.replace( "@DATE", new Date() )
- .replace( "@VERSION", config("pkg.version") );
-
- // Write concatenated source to file
- file.write( name, compiled );
-
- // Fail task if errors were logged.
- if ( this.errorCount ) {
- return false;
- }
-
- // Otherwise, print a success message.
- log.writeln( "File '" + name + "' created." );
- });
};
View
@@ -2,22 +2,23 @@
jQuery.cssExpand = [ "Top", "Right", "Bottom", "Left" ];
-var ralpha = /alpha\([^)]*\)/i,
+var curCSS, iframe, iframeDoc,
+ ralpha = /alpha\([^)]*\)/i,
ropacity = /opacity=([^)]*)/,
// fixed for IE9, see #8346
rupper = /([A-Z]|^ms)/g,
rnumsplit = /^([\-+]?(?:\d*\.)?\d+)(.*)$/i,
rnumnonpx = /^-?(?:\d*\.)?\d+(?!px)[^\d\s]+$/i,
rrelNum = /^([\-+])=([\-+.\de]+)/,
rmargin = /^margin/,
-
+ elemdisplay = {},
cssShow = { position: "absolute", visibility: "hidden", display: "block" },
// order is important!
cssExpand = jQuery.cssExpand,
cssPrefixes = [ "Webkit", "O", "Moz", "ms" ],
- curCSS;
+ eventsToggle = jQuery.fn.toggle;
// return a css property mapped to a potentially vendor prefixed property
function vendorPropName( style, name ) {
@@ -42,13 +43,83 @@ function vendorPropName( style, name ) {
return origName;
}
-jQuery.fn.css = function( name, value ) {
- return jQuery.access( this, function( elem, name, value ) {
- return value !== undefined ?
- jQuery.style( elem, name, value ) :
- jQuery.css( elem, name );
- }, name, value, arguments.length > 1 );
-};
+function showHide( elements, show ) {
+ var elem, display,
+ values = [],
+ index = 0,
+ length = elements.length;
+
+ for ( ; index < length; index++ ) {
+ elem = elements[ index ];
+ if ( !elem.style ) {
+ continue;
+ }
+ values[ index ] = jQuery._data( elem, "olddisplay" );
+ if ( show ) {
+ // Reset the inline display of this element to learn if it is
+ // being hidden by cascaded rules or not
+ if ( !values[ index ] && elem.style.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 === "" && curCSS( elem, "display" ) === "none") ||
+ !jQuery.contains( elem.ownerDocument.documentElement, elem ) ) {
+ values[ index ] = jQuery._data( elem, "olddisplay", jQuery.defaultDisplay(elem.nodeName) );
+ }
+ } else {
+ display = curCSS( elem, "display" );
+
+ if ( !values[ index ] && display !== "none" ) {
+ jQuery._data( elem, "olddisplay", 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.fn.extend({
+ css: function( name, value ) {
+ return jQuery.access( this, function( elem, name, value ) {
+ return value !== undefined ?
+ jQuery.style( elem, name, value ) :
+ jQuery.css( elem, name );
+ }, name, value, arguments.length > 1 );
+ },
+ show: function() {
+ return showHide( this, true );
+ },
+ hide: function() {
+ return showHide( this );
+ },
+ toggle: function( fn, fn2 ) {
+ var bool = typeof fn === "boolean";
+
+ if ( jQuery.isFunction( fn ) && jQuery.isFunction( fn2 ) ) {
+ return eventsToggle.apply( this, arguments );
+ }
+
+ return this.each(function() {
+ var state = bool ? fn : jQuery( this ).is(":hidden");
+ showHide([ this ], state );
+ });
+ }
+});
jQuery.extend({
// Add in style property hooks for overriding the default
@@ -191,6 +262,49 @@ jQuery.extend({
}
return ret;
+ },
+
+ // Try to determine the default display value of an element
+ defaultDisplay: function( nodeName ) {
gibson042
gibson042 May 20, 2012 Member

Replying to #785 (comment) :
Until we have something like shared, I don't see any other way to achieve the necessary results for code like this that is accessed from multiple modules.

gibson042
gibson042 May 20, 2012 Member

But honestly, I think I prefer it this way... defaultDisplay is the kind of thing that might be conditionally replaced in client environments offering a better way.

jaubourg
jaubourg May 20, 2012 Member

In that case, if you think it could be temporary, let's call it _defaultDisplay to mark it as undocumented.

mikesherov
mikesherov May 20, 2012 Member

I agree with @rwldrn , no reason to expose this on the object. Let's make sure once Rick removes the IIFE's that we make this private. I can't imagine anyone droppping in a defaultDisplay replacement.

+ if ( elemdisplay[ nodeName ] ) {
+ return elemdisplay[ nodeName ];
+ }
+
+ var elem = jQuery( "<" + nodeName + ">" ).appendTo( document.body ),
+ display = elem.css("display");
+ elem.remove();
+
+ // If the simple way fails,
+ // get element's real default display by attaching it to a temp iframe
+ if ( display === "none" || display === "" ) {
+ // Use the already-created iframe if possible
+ iframe = document.body.appendChild(
+ iframe || jQuery.extend( document.createElement("iframe"), {
+ frameBorder: 0,
+ width: 0,
+ height: 0
+ })
+ );
+
+ // Create a cacheable copy of the iframe document on first call.
+ // IE and Opera will allow us to reuse the iframeDoc without re-writing the fake HTML
+ // document to it; WebKit & Firefox won't allow reusing the iframe document.
+ if ( !iframeDoc || !iframe.createElement ) {
+ iframeDoc = ( iframe.contentWindow || iframe.contentDocument ).document;
+ iframeDoc.write("<!doctype html><html><body>");
+ iframeDoc.close();
+ }
+
+ elem = iframeDoc.body.appendChild( iframeDoc.createElement(nodeName) );
+
+ display = curCSS( elem, "display" );
+ document.body.removeChild( iframe );
+ }
+
+ // Store the correct default display
+ elemdisplay[ nodeName ] = display;
+
+ return display;
}
});
Oops, something went wrong.