mediawiki.api.parse: Restore support for stringified objects
authorMatthew Flaschen <mflaschen@wikimedia.org>
Fri, 11 Mar 2016 00:04:15 +0000 (19:04 -0500)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 17 Mar 2016 00:41:17 +0000 (00:41 +0000)
Follows-up 11e6b3d. Don't assume non-string values are mw.Title objects.
mw.Title is the special case. Restore the previous default of assuming wikitext.

This was presumably avoided earlier because 'instanceof' throws if given undefined
as right-hand expression and don't want a needless dependency on mediawiki.Title.

Change-Id: I794ed4105d116e63ed505a17a237f289b80d3b15

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

index 02528dc..418fd23 100644 (file)
                                contentmodel: 'wikitext'
                        }, additionalParams );
 
-                       if ( typeof content === 'string' ) {
-                               // Wikitext
-                               config.text = content;
-                       } else {
-                               // mw.Title
+                       if ( mw.Title && content instanceof mw.Title ) {
+                               // Parse existing page
                                config.page = content.getPrefixedDb();
+                       } else {
+                               // Parse wikitext from input
+                               config.text = String( content );
                        }
 
                        apiPromise = this.get( config );
index 4fab90d..dc0cff4 100644 (file)
@@ -6,12 +6,20 @@
        } ) );
 
        QUnit.test( 'Hello world', function ( assert ) {
-               QUnit.expect( 2 );
+               QUnit.expect( 3 );
 
                var api = new mw.Api();
 
                api.parse( '\'\'\'Hello world\'\'\'' ).done( function ( html ) {
-                       assert.equal( html, '<p><b>Hello world</b></p>' );
+                       assert.equal( html, '<p><b>Hello world</b></p>', 'Parse wikitext by string' );
+               } );
+
+               api.parse( {
+                       toString: function () {
+                               return '\'\'\'Hello world\'\'\'';
+                       }
+               } ).done( function ( html ) {
+                       assert.equal( html, '<p><b>Hello world</b></p>', 'Parse wikitext by toString object' );
                } );
 
                this.server.respondWith( /action=parse.*&text='''Hello\+world'''/, function ( request ) {
@@ -21,7 +29,7 @@
                } );
 
                api.parse( new mw.Title( 'Earth' ) ).done( function ( html ) {
-                       assert.equal( html, '<p><b>Earth</b> is a planet.</p>' );
+                       assert.equal( html, '<p><b>Earth</b> is a planet.</p>', 'Parse page by Title object'  );
                } );
 
                this.server.respondWith( /action=parse.*&page=Earth/, function ( request ) {