Merge "Remove use of implicitGroupby() in WikiPage"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.util.js
index 4844e21..fb34a89 100644 (file)
@@ -1,12 +1,59 @@
 ( function ( mw, $ ) {
        'use strict';
 
+       var util;
+
+       /**
+        * Encode the string like PHP's rawurlencode
+        * @ignore
+        *
+        * @param {string} str String to be encoded.
+        * @return {string} Encoded string
+        */
+       function rawurlencode( str ) {
+               str = String( str );
+               return encodeURIComponent( str )
+                       .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
+                       .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /~/g, '%7E' );
+       }
+
+       /**
+        * Private helper function used by util.escapeId*()
+        * @ignore
+        *
+        * @param {string} str String to be encoded
+        * @param {string} mode Encoding mode, see documentation for $wgFragmentMode
+        *     in DefaultSettings.php
+        * @return {string} Encoded string
+        */
+       function escapeIdInternal( str, mode ) {
+               str = String( str );
+
+               switch ( mode ) {
+                       case 'html5':
+                               return str.replace( / /g, '_' );
+                       case 'html5-legacy':
+                               str = str.replace( /[ \t\n\r\f_'"&#%]+/g, '_' )
+                                       .replace( /^_+|_+$/, '' );
+                               if ( str === '' ) {
+                                       str = '_';
+                               }
+                               return str;
+                       case 'legacy':
+                               return rawurlencode( str.replace( / /g, '_' ) )
+                                       .replace( /%3A/g, ':' )
+                                       .replace( /%/g, '.' );
+                       default:
+                               throw new Error( 'Unrecognized ID escaping mode ' + mode );
+               }
+       }
+
        /**
         * Utility library
         * @class mw.util
         * @singleton
         */
-       var util = {
+       util = {
 
                /* Main body */
 
                 * @param {string} str String to be encoded.
                 * @return {string} Encoded string
                 */
-               rawurlencode: function ( str ) {
-                       str = String( str );
-                       return encodeURIComponent( str )
-                               .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
-                               .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' ).replace( /~/g, '%7E' );
+               rawurlencode: rawurlencode,
+
+               /**
+                * Encode string into HTML id compatible form suitable for use in HTML
+                * Analog to PHP Sanitizer::escapeIdForAttribute()
+                *
+                * @since 1.30
+                *
+                * @param {string} str String to encode
+                * @return {string} Encoded string
+                */
+               escapeIdForAttribute: function ( str ) {
+                       var mode = mw.config.get( 'wgFragmentMode' )[ 0 ];
+
+                       return escapeIdInternal( str, mode );
                },
 
                /**
-                * Encode the string like Sanitizer::escapeId in PHP
+                * Encode string into HTML id compatible form suitable for use in links
+                * Analog to PHP Sanitizer::escapeIdForLink()
                 *
-                * @param {string} str String to be encoded.
+                * @since 1.30
+                *
+                * @param {string} str String to encode
                 * @return {string} Encoded string
                 */
-               escapeId: function ( str ) {
-                       str = String( str );
-                       return util.rawurlencode( str.replace( / /g, '_' ) )
-                               .replace( /%3A/g, ':' )
-                               .replace( /%/g, '.' );
+               escapeIdForLink: function ( str ) {
+                       var mode = mw.config.get( 'wgFragmentMode' )[ 0 ];
+
+                       return escapeIdInternal( str, mode );
                },
 
                /**
 
                        // Append the encoded fragment
                        if ( fragment.length ) {
-                               url += '#' + util.escapeId( fragment );
+                               url += '#' + util.escapeIdForLink( fragment );
                        }
 
                        return url;
         * @inheritdoc #getUrl
         * @deprecated since 1.23 Use #getUrl instead.
         */
-       mw.log.deprecate( util, 'wikiGetlink', util.getUrl, 'Use mw.util.getUrl instead.' );
+       mw.log.deprecate( util, 'wikiGetlink', util.getUrl, 'Use mw.util.getUrl instead.', 'mw.util.wikiGetlink' );
 
        /**
         * Add the appropriate prefix to the accesskey shown in the tooltip.
                }
 
                $nodes.updateTooltipAccessKeys();
-       }, 'Use jquery.accessKeyLabel instead.' );
+       }, 'Use jquery.accessKeyLabel instead.', 'mw.util.updateTooltipAccessKeys' );
 
        /**
         * Add a little box at the top of the screen to inform the user of
                }
                mw.notify( message, { autoHide: true, tag: 'legacy' } );
                return true;
-       }, 'Use mw.notify instead.' );
+       }, 'Use mw.notify instead.', 'mw.util.jsMessage' );
+
+       /**
+        * Encode the string like Sanitizer::escapeId() in PHP
+        *
+        * @method escapeId
+        * @deprecated since 1.30 use escapeIdForAttribute() or escapeIdForLink()
+        * @param {string} str String to be encoded.
+        * @return {string} Encoded string
+        */
+       mw.log.deprecate( util, 'escapeId', function ( str ) {
+               return escapeIdInternal( str, 'legacy' );
+       }, 'Use mw.util.escapeIdForAttribute or mw.util.escapeIdForLink instead.', 'mw.util.escapeId' );
 
        /**
         * Initialisation of mw.util.$content