ApiSandbox: Better handling of parsed messages
authorBrad Jorsch <bjorsch@wikimedia.org>
Mon, 28 Nov 2016 17:01:27 +0000 (12:01 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Mon, 28 Nov 2016 17:05:25 +0000 (12:05 -0500)
There are several places where we're parsing a message to an HTML string
and passing it into ooui in a way that it'll interpret it as text. There
are more where we're handling it properly, but by parsing HTML strings
outselves instead of letting mw.Message do it.

So, let's add a Util function that will parse a message using
.parseDom() and apply our fixups.

Change-Id: I1f71916ac2fb3567c2fa2dffc64c4c8c91050ee9

resources/src/mediawiki.special/mediawiki.special.apisandbox.js

index d2015d8..60155fd 100644 (file)
@@ -51,9 +51,7 @@
                tokenWidget: {
                        alertTokenError: function ( code, error ) {
                                windowManager.openWindow( 'errorAlert', {
-                                       title: mw.message(
-                                               'apisandbox-results-fixtoken-fail', this.paramInfo.tokentype
-                                       ).parse(),
+                                       title: Util.parseMsg( 'apisandbox-results-fixtoken-fail', this.paramInfo.tokentype ),
                                        message: error,
                                        actions: [
                                                {
        };
 
        /**
-        * @class mw.special.ApiSandbox.Utils
+        * @class mw.special.ApiSandbox.Util
         * @private
         */
        Util = {
                },
 
                /**
-                * Parse an HTML string, adding target="_blank" to any links
+                * Parse an HTML string and call Util.fixupHTML()
                 *
                 * @param {string} html HTML to parse
                 * @return {jQuery}
                 */
                parseHTML: function ( html ) {
                        var $ret = $( $.parseHTML( html ) );
-                       $ret.filter( 'a' ).add( $ret.find( 'a' ) )
+                       return Util.fixupHTML( $ret );
+               },
+
+               /**
+                * Parse an i18n message and call Util.fixupHTML()
+                *
+                * @param {string} key Key of message to get
+                * @param {...Mixed} parameters Values for $N replacements
+                * @return {jQuery}
+                */
+               parseMsg: function () {
+                       var $ret = mw.message.apply( mw.message, arguments ).parseDom();
+                       return Util.fixupHTML( $ret );
+               },
+
+               /**
+                * Fix HTML for ApiSandbox display
+                *
+                * Fixes are:
+                * - Add target="_blank" to any links
+                *
+                * @param {jQuery} $html DOM to process
+                * @return {jQuery}
+                */
+               fixupHTML: function ( $html ) {
+                       $html.filter( 'a' ).add( $html.find( 'a' ) )
                                .filter( '[href]:not([target])' )
                                .attr( 'target', '_blank' );
-                       return $ret;
+                       return $html;
                }
        };
 
 
                        $content
                                .empty()
-                               .append( $( '<p>' ).append( mw.message( 'apisandbox-intro' ).parse() ) )
+                               .append( $( '<p>' ).append( Util.parseMsg( 'apisandbox-intro' ) ) )
                                .append(
                                        $( '<div>', { id: 'mw-apisandbox-ui' } )
                                                .append( $toolbar )
                        $.when.apply( $, deferreds ).done( function () {
                                if ( $.inArray( false, arguments ) !== -1 ) {
                                        windowManager.openWindow( 'errorAlert', {
-                                               title: mw.message( 'apisandbox-submit-invalid-fields-title' ).parse(),
-                                               message: mw.message( 'apisandbox-submit-invalid-fields-message' ).parse(),
+                                               title: Util.parseMsg( 'apisandbox-submit-invalid-fields-title' ),
+                                               message: Util.parseMsg( 'apisandbox-submit-invalid-fields-message' ),
                                                actions: [
                                                        {
                                                                action: 'accept',
                                                                readOnly: true,
                                                                value: mw.util.wikiScript( 'api' ) + '?' + query
                                                        } ), {
-                                                               label: mw.message( 'apisandbox-request-url-label' ).parse()
+                                                               label: Util.parseMsg( 'apisandbox-request-url-label' )
                                                        }
                                                ).$element,
                                                $result
                                                        if ( data.status && data.status !== 200 ) {
                                                                $( '<div>' )
                                                                        .addClass( 'api-pretty-header api-pretty-status' )
-                                                                       .append(
-                                                                               mw.message( 'api-format-prettyprint-status', data.status, data.statustext ).parse()
-                                                                       )
+                                                                       .append( Util.parseMsg( 'api-format-prettyprint-status', data.status, data.statustext ) )
                                                                        .appendTo( $result );
                                                        }
                                                        $result.append( Util.parseHTML( data.html ) );
                                                                                framed: false,
                                                                                icon: 'info',
                                                                                popup: {
-                                                                                       $content: $( '<div>' ).append( mw.message( 'apisandbox-continue-help' ).parse() ),
+                                                                                       $content: $( '<div>' ).append( Util.parseMsg( 'apisandbox-continue-help' ) ),
                                                                                        padded: true
                                                                                }
                                                                        } ).$element
 
                                if ( that.widgets[ name ] !== undefined ) {
                                        windowManager.openWindow( 'errorAlert', {
-                                               title: mw.message(
-                                                       'apisandbox-dynamic-error-exists', name
-                                               ).parse(),
+                                               title: Util.parseMsg( 'apisandbox-dynamic-error-exists', name ),
                                                actions: [
                                                        {
                                                                action: 'accept',
                                                                        dl.append( $( '<dd>', {
                                                                                addClass: 'info',
                                                                                append: [
-                                                                                       Util.parseHTML( mw.message(
+                                                                                       Util.parseMsg(
                                                                                                'api-help-param-limit2', pi.parameters[ i ].max, pi.parameters[ i ].highmax
-                                                                                       ).parse() ),
+                                                                                       ),
                                                                                        ' ',
-                                                                                       Util.parseHTML( mw.message( 'apisandbox-param-limit' ).parse() )
+                                                                                       Util.parseMsg( 'apisandbox-param-limit' )
                                                                                ]
                                                                        } ) );
                                                                } else {
                                                                        dl.append( $( '<dd>', {
                                                                                addClass: 'info',
                                                                                append: [
-                                                                                       Util.parseHTML( mw.message(
-                                                                                               'api-help-param-limit', pi.parameters[ i ].max
-                                                                                       ).parse() ),
+                                                                                       Util.parseMsg( 'api-help-param-limit', pi.parameters[ i ].max ),
                                                                                        ' ',
-                                                                                       Util.parseHTML( mw.message( 'apisandbox-param-limit' ).parse() )
+                                                                                       Util.parseMsg( 'apisandbox-param-limit' )
                                                                                ]
                                                                        } ) );
                                                                }
                                                                if ( tmp !== '' ) {
                                                                        dl.append( $( '<dd>', {
                                                                                addClass: 'info',
-                                                                               append: Util.parseHTML( mw.message(
+                                                                               append: Util.parseMsg(
                                                                                        'api-help-param-integer-' + tmp,
                                                                                        Util.apiBool( pi.parameters[ i ].multi ) ? 2 : 1,
                                                                                        pi.parameters[ i ].min, pi.parameters[ i ].max
-                                                                               ).parse() )
+                                                                               )
                                                                        } ) );
                                                                }
                                                                break;
                                        items.push( new OO.ui.FieldLayout(
                                                new OO.ui.Widget( {} ).toggle( false ), {
                                                        align: 'top',
-                                                       label: Util.parseHTML( mw.message( 'apisandbox-no-parameters' ).parse() )
+                                                       label: Util.parseMsg( 'apisandbox-no-parameters' )
                                                }
                                        ) );
                                }