From e45af21a3f2a97eefa3fa88ee7370c1b253825f1 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 12 Dec 2018 13:20:25 +0000 Subject: [PATCH] mw.Title.newFromUserInput: Rename vars, improve docs for double signature Also add tests to cover this usage. Change-Id: I61c7e7377ac7d75073dac025f5f7ff4e599756cb --- resources/src/mediawiki.Title/Title.js | 14 +++++++++----- .../resources/mediawiki/mediawiki.Title.test.js | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/resources/src/mediawiki.Title/Title.js b/resources/src/mediawiki.Title/Title.js index def9f68a1f..ff7a40f804 100644 --- a/resources/src/mediawiki.Title/Title.js +++ b/resources/src/mediawiki.Title/Title.js @@ -495,8 +495,10 @@ * * @static * @param {string} title - * @param {number} [defaultNamespace=NS_MAIN] + * @param {number|Object} [defaultNamespaceOrOptions=NS_MAIN] * If given, will used as default namespace for the given title. + * This method can also be called with two arguments, in which case + * this becomes options (see below). * @param {Object} [options] additional options * @param {boolean} [options.forUploading=true] * Makes sure that a file is uploadable under the title returned. @@ -504,13 +506,15 @@ * Automatically assumed if the title is created in the Media namespace. * @return {mw.Title|null} A valid Title object or null if the input cannot be turned into a valid title */ - Title.newFromUserInput = function ( title, defaultNamespace, options ) { - var namespace, m, id, ext, parts; + Title.newFromUserInput = function ( title, defaultNamespaceOrOptions, options ) { + var namespace, m, id, ext, parts, + defaultNamespace; // defaultNamespace is optional; check whether options moves up if ( arguments.length < 3 && typeof defaultNamespace === 'object' ) { - options = defaultNamespace; - defaultNamespace = undefined; + options = defaultNamespaceOrOptions; + } else { + defaultNamespace = defaultNamespaceOrOptions; } // merge options into defaults diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js index 4a32157721..84e1d4eb5a 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js @@ -669,6 +669,10 @@ assert.notStrictEqual( title, null, prefix + 'Parses successfully' ); assert.strictEqual( title.toText(), thisCase.expected, prefix + 'Title as expected' ); + if ( thisCase.defaultNamespace === undefined ) { + title = mw.Title.newFromUserInput( thisCase.title, thisCase.options ); + assert.strictEqual( title.toText(), thisCase.expected, prefix + 'Skipping namespace argument' ); + } } else { assert.strictEqual( title, null, thisCase.description + ', should not produce an mw.Title object' ); } -- 2.20.1