From 11e6b3d7efad7acdf63b594ea46fab303bc91a48 Mon Sep 17 00:00:00 2001 From: Matthew Flaschen Date: Thu, 10 Mar 2016 19:04:15 -0500 Subject: [PATCH] mediawiki.api.parse: Allow parsing pages Bug: T46925 Change-Id: Ic0d762d02c96e0b39b4983d3b66ebdfb6f88d3a2 --- resources/src/mediawiki/api/parse.js | 24 ++++++++++++++----- .../mediawiki.api/mediawiki.api.parse.test.js | 12 +++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/resources/src/mediawiki/api/parse.js b/resources/src/mediawiki/api/parse.js index 49219d782c..02528dcfff 100644 --- a/resources/src/mediawiki/api/parse.js +++ b/resources/src/mediawiki/api/parse.js @@ -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 ) { diff --git a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js index f2865ebb63..4fab90d659 100644 --- a/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js +++ b/tests/qunit/suites/resources/mediawiki.api/mediawiki.api.parse.test.js @@ -6,7 +6,7 @@ } ) ); QUnit.test( 'Hello world', function ( assert ) { - QUnit.expect( 1 ); + QUnit.expect( 2 ); var api = new mw.Api(); @@ -20,6 +20,16 @@ ); } ); + api.parse( new mw.Title( 'Earth' ) ).done( function ( html ) { + assert.equal( html, '

Earth is a planet.

' ); + } ); + + this.server.respondWith( /action=parse.*&page=Earth/, function ( request ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{ "parse": { "text": "

Earth is a planet.

" } }' + ); + } ); + this.server.respond(); } ); }( mediaWiki ) ); -- 2.20.1