X-Git-Url: https://git.cyclocoop.org/%28%28?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki%2Fmediawiki.Title.js;h=2b76187359e5ab44d04780d74ec95c3dc8a20494;hb=f19c9021a466f414065db6f74257efc702325110;hp=53cbd6074e14038984930e64ba13b19196366e5d;hpb=5179c299bc112b292f51084ee80afea3878e7386;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki/mediawiki.Title.js b/resources/src/mediawiki/mediawiki.Title.js index 53cbd6074e..2b76187359 100644 --- a/resources/src/mediawiki/mediawiki.Title.js +++ b/resources/src/mediawiki/mediawiki.Title.js @@ -1,11 +1,9 @@ /*! * @author Neil Kandalgaonkar, 2010 - * @author Timo Tijhof, 2011-2013 + * @author Timo Tijhof * @since 1.18 */ -/* eslint-disable no-use-before-define */ - ( function ( mw, $ ) { /** * Parse titles into an object structure. Note that when using the constructor @@ -29,28 +27,13 @@ * mw.Title.makeTitle( NS_TEMPLATE, 'Template:Foo' ).getPrefixedText(); // => 'Template:Template:Foo' * * @class mw.Title - * @constructor - * @param {string} title Title of the page. If no second argument given, - * this will be searched for a namespace - * @param {number} [namespace=NS_MAIN] If given, will used as default namespace for the given title - * @throws {Error} When the title is invalid */ - function Title( title, namespace ) { - var parsed = parse( title, namespace ); - if ( !parsed ) { - throw new Error( 'Unable to parse title' ); - } - - this.namespace = parsed.namespace; - this.title = parsed.title; - this.ext = parsed.ext; - this.fragment = parsed.fragment; - } /* Private members */ - // eslint-disable-next-line vars-on-top var + mwString = require( 'mediawiki.String' ), + namespaceIds = mw.config.get( 'wgNamespaceIds' ), /** @@ -339,7 +322,7 @@ // Except for special pages, e.g. [[Special:Block/Long name]] // Note: The PHP implementation also asserts that even in NS_SPECIAL, the title should // be less than 512 bytes. - if ( namespace !== NS_SPECIAL && $.byteLength( title ) > TITLE_MAX_BYTES ) { + if ( namespace !== NS_SPECIAL && mwString.byteLength( title ) > TITLE_MAX_BYTES ) { return false; } @@ -426,21 +409,7 @@ * @return {string} */ trimToByteLength = function ( s, length ) { - var byteLength, chopOffChars, chopOffBytes; - - // bytelength is always greater or equal to the length in characters - s = s.substr( 0, length ); - while ( ( byteLength = $.byteLength( s ) ) > length ) { - // Calculate how many characters can be safely removed - // First, we need to know how many bytes the string exceeds the threshold - chopOffBytes = byteLength - length; - // A character in UTF-8 is at most 4 bytes - // One character must be removed in any case because the - // string is too long - chopOffChars = Math.max( 1, Math.floor( chopOffBytes / 4 ) ); - s = s.substr( 0, s.length - chopOffChars ); - } - return s; + return mwString.trimByteLength( '', s, length ).newVal; }, /** @@ -458,6 +427,25 @@ return trimToByteLength( name, FILENAME_MAX_BYTES - extension.length - 1 ) + '.' + extension; }; + /** + * @method constructor + * @param {string} title Title of the page. If no second argument given, + * this will be searched for a namespace + * @param {number} [namespace=NS_MAIN] If given, will used as default namespace for the given title + * @throws {Error} When the title is invalid + */ + function Title( title, namespace ) { + var parsed = parse( title, namespace ); + if ( !parsed ) { + throw new Error( 'Unable to parse title' ); + } + + this.namespace = parsed.namespace; + this.title = parsed.title; + this.ext = parsed.ext; + this.fragment = parsed.fragment; + } + /* Static members */ /** @@ -537,7 +525,7 @@ namespace = defaultNamespace === undefined ? NS_MAIN : defaultNamespace; // Normalise additional whitespace - title = $.trim( title.replace( /\s/g, ' ' ) ); + title = title.replace( /\s/g, ' ' ).trim(); // Process initial colon if ( title !== '' && title[ 0 ] === ':' ) { @@ -579,7 +567,7 @@ ext = parts.pop(); // Remove whitespace of the name part (that W/O extension) - title = $.trim( parts.join( '.' ) ); + title = parts.join( '.' ).trim(); // Cut, if too long and append file extension title = trimFileNameToByteLength( title, ext ); @@ -587,7 +575,7 @@ } else { // Missing file extension - title = $.trim( parts.join( '.' ) ); + title = parts.join( '.' ).trim(); // Name has no file extension and a fallback wasn't provided either return null;