Merge "Add CLDRPluralRuleError, added in parent commit, to the AutoLoader"
[lhc/web/wiklou.git] / resources / mediawiki.api / mediawiki.api.parse.js
index 1cc68f2..e784ef7 100644 (file)
@@ -1,31 +1,42 @@
 /**
- * Additional mw.Api methods to assist with API calls related to parsing wikitext.
+ * mw.Api methods for parsing wikitext.
  */
-
-( function( $, mw ) {
+( function ( mw, $ ) {
 
        $.extend( mw.Api.prototype, {
                /**
                 * Convinience method for 'action=parse'. Parses wikitext into HTML.
                 *
                 * @param wikiText {String}
-                * @param success {Function} callback to which to pass success HTML
-                * @param err {Function} callback if error (optional)
-                * @return {jqXHR}
+                * @param ok {Function} [optional] deprecated (success callback)
+                * @param err {Function} [optional] deprecated (error callback)
+                * @return {jQuery.Promise}
                 */
-               parse: function( wikiText, success, err ) {
-                       var params = {
-                                       text: wikiText,
-                                       action: 'parse'
-                               },
-                               ok = function( data ) {
+               parse: function( wikiText, ok, err ) {
+                       var apiDeferred = $.Deferred();
+
+                       // Backwards compatibility (< MW 1.20)
+                       if ( ok ) {
+                               apiDeferred.done( ok );
+                       }
+                       if ( err ) {
+                               apiDeferred.fail( err );
+                       }
+
+                       this.get( {
+                                       action: 'parse',
+                                       text: wikiText
+                               } )
+                               .done( function ( data ) {
                                        if ( data.parse && data.parse.text && data.parse.text['*'] ) {
-                                               success( data.parse.text['*'] );
+                                               apiDeferred.resolve( data.parse.text['*'] );
                                        }
-                               };
-                       return this.get( params, { ok: ok, err: err } );
-               }
+                               } )
+                               .fail( apiDeferred.reject );
 
+                       // Return the promise
+                       return apiDeferred.promise();
+               }
        } );
 
-} )( jQuery, mediaWiki );
+} )( mediaWiki, jQuery );