Merge "EditPage::newSectionSummary should return a value in all code paths"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.htmlform.js
index f7aa7f8..5027f7a 100644 (file)
         * ending in array keys matching the given name (e.g. "baz" matches
         * "foo[bar][baz]").
         *
+        * @private
         * @param {jQuery} element
         * @param {string} name
         * @return {jQuery|null}
         */
        function hideIfGetField( $el, name ) {
-               var sel, $found, $p;
+               var $found, $p,
+                       suffix = name.replace( /^([^\[]+)/, '[$1]' );
+
+               function nameFilter() {
+                       return this.name === name ||
+                               ( this.name === ( 'wp' + name ) ) ||
+                               this.name.slice( -suffix.length ) === suffix;
+               }
 
-               sel = '[name="' + name + '"],' +
-                       '[name="wp' + name + '"],' +
-                       '[name$="' + name.replace( /^([^\[]+)/, '[$1]' ) + '"]';
                for ( $p = $el.parent(); $p.length > 0; $p = $p.parent() ) {
-                       $found = $p.find( sel );
-                       if ( $found.length > 0 ) {
+                       $found = $p.find( '[name]' ).filter( nameFilter );
+                       if ( $found.length ) {
                                return $found;
                        }
                }
         * Helper function for hide-if to return a test function and list of
         * dependent fields for a hide-if specification.
         *
+        * @private
         * @param {jQuery} element
         * @param {Array} hide-if spec
-        * @return {Array} 2 elements: jQuery of dependent fields, and test function
+        * @return {Array}
+        * @return {jQuery} return.0 Dependent fields
+        * @return {Function} return.1 Test function
         */
        function hideIfParse( $el, spec ) {
-               var op, i, l, v, $field, $fields, func, funcs, getVal;
+               var op, i, l, v, $field, $fields, fields, func, funcs, getVal;
 
                op = spec[0];
                l = spec.length;
                        case 'NAND':
                        case 'NOR':
                                funcs = [];
-                               $fields = $();
+                               fields = [];
                                for ( i = 1; i < l; i++ ) {
                                        if ( !$.isArray( spec[i] ) ) {
                                                throw new Error( op + ' parameters must be arrays' );
                                        }
                                        v = hideIfParse( $el, spec[i] );
-                                       $fields = $fields.add( v[0] );
+                                       fields = fields.concat( v[0].toArray() );
                                        funcs.push( v[1] );
                                }
+                               $fields = $( fields );
 
                                l = funcs.length;
                                switch ( op ) {
                                }
                                v = spec[2];
 
-                               if ( $field.first().attr( 'type' ) === 'radio' ||
-                                       $field.first().attr( 'type' ) === 'checkbox'
+                               if ( $field.first().prop( 'type' ) === 'radio' ||
+                                       $field.first().prop( 'type' ) === 'checkbox'
                                ) {
                                        getVal = function () {
                                                var $selected = $field.filter( ':checked' );
-                                               return $selected.length > 0 ? $selected.val() : '';
+                                               return $selected.length ? $selected.val() : '';
                                        };
                                } else {
                                        getVal = function () {
         */
        $.fn.goIn = function ( instantToggle ) {
                if ( instantToggle === true ) {
-                       return $( this ).show();
+                       return this.show();
                }
-               return $( this ).stop( true, true ).fadeIn();
+               return this.stop( true, true ).fadeIn();
        };
 
        /**
         */
        $.fn.goOut = function ( instantToggle ) {
                if ( instantToggle === true ) {
-                       return $( this ).hide();
+                       return this.hide();
                }
-               return $( this ).stop( true, true ).fadeOut();
+               return this.stop( true, true ).fadeOut();
        };
 
        /**
         * Bind a function to the jQuery object via live(), and also immediately trigger
         * the function on the objects with an 'instant' parameter set to true.
+        *
+        * @method liveAndTestAtStart
+        * @deprecated since 1.24 Use .on() and .each() directly.
         * @param {Function} callback
         * @param {boolean|jQuery.Event} callback.immediate True when the event is called immediately,
         *  an event object when triggered from an event.
+        * @return jQuery
+        * @chainable
         */
-       $.fn.liveAndTestAtStart = function ( callback ) {
-               $( this )
+       mw.log.deprecate( $.fn, 'liveAndTestAtStart', function ( callback ) {
+               this
+                       // Can't really migrate to .on() generically, needs knowledge of
+                       // calling code to know the correct selector. Fix callers and
+                       // get rid of this .liveAndTestAtStart() hack.
                        .live( 'change', callback )
                        .each( function () {
                                callback.call( this, true );
                        } );
-       };
+       } );
 
        function enhance( $root ) {
 
-               // Animate the SelectOrOther fields, to only show the text field when
-               // 'other' is selected.
-               $root.find( '.mw-htmlform-select-or-other' ).liveAndTestAtStart( function ( instant ) {
+               /**
+                * @ignore
+                * @param {boolean|jQuery.Event} instant
+                */
+               function handleSelectOrOther( instant ) {
                        var $other = $root.find( '#' + $( this ).attr( 'id' ) + '-other' );
                        $other = $other.add( $other.siblings( 'br' ) );
                        if ( $( this ).val() === 'other' ) {
                        } else {
                                $other.goOut( instant );
                        }
-               } );
+               }
+
+               // Animate the SelectOrOther fields, to only show the text field when
+               // 'other' is selected.
+               $root
+                       .on( 'change', '.mw-htmlform-select-or-other', handleSelectOrOther )
+                       .each( function () {
+                               handleSelectOrOther.call( this, true );
+                       } );
 
                // Set up hide-if elements
                $root.find( '.mw-htmlform-hide-if' ).each( function () {
-                       var $el = $( this ),
-                               spec = $el.data( 'hideIf' ),
-                               v, $fields, test, func;
+                       var v, $fields, test, func,
+                               $el = $( this ),
+                               spec = $el.data( 'hideIf' );
 
                        if ( !spec ) {
                                return;
                                        $el.show();
                                }
                        };
-                       $fields.change( func );
+                       $fields.on( 'change', func );
                        func();
                } );
 
                        $ul = $( this ).prev( 'ul.mw-htmlform-cloner-ul' );
 
                        html = $ul.data( 'template' ).replace(
-                               $ul.data( 'uniqueId' ), 'clone' + ( ++cloneCounter ), 'g'
+                               new RegExp( $.escapeRE( $ul.data( 'uniqueId' ) ), 'g' ),
+                               'clone' + ( ++cloneCounter )
                        );
 
                        $li = $( '<li>' )