mediawiki.api.parse: Allow parsing pages
authorMatthew Flaschen <mflaschen@wikimedia.org>
Fri, 11 Mar 2016 00:04:15 +0000 (19:04 -0500)
committerMatthew Flaschen <mflaschen@wikimedia.org>
Fri, 11 Mar 2016 03:01:21 +0000 (22:01 -0500)
Bug: T46925
Change-Id: Ic0d762d02c96e0b39b4983d3b66ebdfb6f88d3a2

resources/src/mediawiki/api/parse.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js

index 49219d7..02528dc 100644 (file)
@@ -7,18 +7,30 @@
                /**
                 * Convenience method for 'action=parse'.
                 *
-                * @param {string} wikitext
+                * @param {string|mw.Title} content Content to parse, either as a wikitext string or
+                *   a mw.Title.
+                * @param {Object} additionalParams Parameters object to set custom settings, e.g.
+                *   redirects, sectionpreview.  prop should not be overridden.
                 * @return {jQuery.Promise}
                 * @return {Function} return.done
                 * @return {string} return.done.data Parsed HTML of `wikitext`.
                 */
-               parse: function ( wikitext ) {
-                       var apiPromise = this.get( {
+               parse: function ( content, additionalParams ) {
+                       var apiPromise, config = $.extend( {
                                formatversion: 2,
                                action: 'parse',
-                               contentmodel: 'wikitext',
-                               text: wikitext
-                       } );
+                               contentmodel: 'wikitext'
+                       }, additionalParams );
+
+                       if ( typeof content === 'string' ) {
+                               // Wikitext
+                               config.text = content;
+                       } else {
+                               // mw.Title
+                               config.page = content.getPrefixedDb();
+                       }
+
+                       apiPromise = this.get( config );
 
                        return apiPromise
                                .then( function ( data ) {
index f2865eb..4fab90d 100644 (file)
@@ -6,7 +6,7 @@
        } ) );
 
        QUnit.test( 'Hello world', function ( assert ) {
-               QUnit.expect( 1 );
+               QUnit.expect( 2 );
 
                var api = new mw.Api();
 
                        );
                } );
 
+               api.parse( new mw.Title( 'Earth' ) ).done( function ( html ) {
+                       assert.equal( html, '<p><b>Earth</b> is a planet.</p>' );
+               } );
+
+               this.server.respondWith( /action=parse.*&page=Earth/, function ( request ) {
+                       request.respond( 200, { 'Content-Type': 'application/json' },
+                               '{ "parse": { "text": "<p><b>Earth</b> is a planet.</p>" } }'
+                       );
+               } );
+
                this.server.respond();
        } );
 }( mediaWiki ) );