From: Thiemo Kreuz Date: Mon, 13 May 2019 08:57:01 +0000 (+0200) Subject: mediawiki.Title: Remove dead code and streamline newFromUserInput() X-Git-Tag: 1.34.0-rc.0~1468^2 X-Git-Url: http://git.heureux-cyclage.org/?a=commitdiff_plain;h=8b13b98624722df300caa32c7e54b1315312706a;p=lhc%2Fweb%2Fwiklou.git mediawiki.Title: Remove dead code and streamline newFromUserInput() This patch also does: * Fix the behavior for filenames that have a dot, but no file extension after the dot. * Add missing test cases. It seems like this method is barely used anywhere: https://codesearch.wmflabs.org/search/?q=newFromUserInput&i=1 So we might as well remove it, and inline simplified versions that only do exactly the normalizations that are needed. For example, silently stripping leading colons is a somewhat suprising thing to do. Shouldn't such a title be invalid, instead of silently being converted to something else? But this is unrelated to what this patch does. Change-Id: I1596fb0d9d02ca230a37f3d70d65296d6ce4ddb4 --- diff --git a/resources/src/mediawiki.Title/Title.js b/resources/src/mediawiki.Title/Title.js index 900dab29ed..259febc9a7 100644 --- a/resources/src/mediawiki.Title/Title.js +++ b/resources/src/mediawiki.Title/Title.js @@ -507,7 +507,7 @@ Title.makeTitle = function ( namespace, title ) { * @return {mw.Title|null} A valid Title object or null if the input cannot be turned into a valid title */ Title.newFromUserInput = function ( title, defaultNamespaceOrOptions, options ) { - var namespace, m, id, ext, parts, + var namespace, m, id, ext, lastDot, defaultNamespace; // defaultNamespace is optional; check whether options moves up @@ -553,35 +553,27 @@ Title.newFromUserInput = function ( title, defaultNamespaceOrOptions, options ) namespace === NS_MEDIA || ( options.forUploading && ( namespace === NS_FILE ) ) ) { - title = sanitize( title, [ 'generalRule', 'fileRule' ] ); // Operate on the file extension // Although it is possible having spaces between the name and the ".ext" this isn't nice for // operating systems hiding file extensions -> strip them later on - parts = title.split( '.' ); - - if ( parts.length > 1 ) { - - // Get the last part, which is supposed to be the file extension - ext = parts.pop(); + lastDot = title.lastIndexOf( '.' ); - // Remove whitespace of the name part (that W/O extension) - title = parts.join( '.' ).trim(); - - // Cut, if too long and append file extension - title = trimFileNameToByteLength( title, ext ); + // No or empty file extension + if ( lastDot === -1 || lastDot >= title.length - 1 ) { + return null; + } - } else { + // Get the last part, which is supposed to be the file extension + ext = title.slice( lastDot + 1 ); - // Missing file extension - title = parts.join( '.' ).trim(); + // Remove whitespace of the name part (that without extension) + title = title.slice( 0, lastDot ).trim(); - // Name has no file extension and a fallback wasn't provided either - return null; - } + // Cut, if too long and append file extension + title = trimFileNameToByteLength( title, ext ); } else { - title = sanitize( title, [ 'generalRule' ] ); // Cut titles exceeding the TITLE_MAX_BYTES byte size limit diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js index fca1f7d016..a3f3cc89e5 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.Title.test.js @@ -652,6 +652,14 @@ title: 'File:Foo.JPEG ', expected: 'File:Foo.JPEG', description: 'Page in File-namespace with trailing whitespace' + }, + { + title: 'File:Foo', + description: 'File name without file extension' + }, + { + title: 'File:Foo.', + description: 'File name with empty file extension' } ];