Merge "Stop using deprecated mediawiki.Uri module"

This commit is contained in:
jenkins-bot 2024-11-22 22:28:34 +00:00 committed by Gerrit Code Review
commit 9277babd64
5 changed files with 26 additions and 67 deletions

Binary file not shown.

Binary file not shown.

View file

@ -26,27 +26,37 @@ function isOwnPageAnchorLink( el ) {
* @return {string|undefined} * @return {string|undefined}
*/ */
export function getTitle( href, config ) { export function getTitle( href, config ) {
// Skip every URI that mw.Uri cannot parse // Skip every URL that cannot be parsed
let linkHref; let linkHref;
try { try {
linkHref = new mw.Uri( href ); linkHref = new URL( href );
} catch ( e ) { } catch ( e ) {
return undefined; // treat as relative URI
try {
linkHref = new URL( href, location.origin );
} catch ( errRelative ) {
// could not be parsed.
return undefined;
}
} }
// External links // External links
if ( linkHref.host !== location.hostname ) { if ( linkHref.hostname !== location.hostname ) {
return undefined; return undefined;
} }
const queryLength = Object.keys( linkHref.query ).length; const searchParams = linkHref.searchParams;
// Note that we use Array.from and length rather than `size` as Selenium browser tests fail
// with `size` being undefined. `size` property is relatively new (April 2023)
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/size
const queryLength = Array.from( searchParams ).length;
let title; let title;
// No query params (pretty URL) // No query params (pretty URL)
if ( !queryLength ) { if ( !queryLength ) {
const pattern = mw.util.escapeRegExp( config.get( 'wgArticlePath' ) ).replace( '\\$1', '([^?#]+)' ), const pattern = mw.util.escapeRegExp( config.get( 'wgArticlePath' ) ).replace( '\\$1', '([^?#]+)' ),
// eslint-disable-next-line security/detect-non-literal-regexp // eslint-disable-next-line security/detect-non-literal-regexp
matches = new RegExp( pattern ).exec( linkHref.path ); matches = new RegExp( pattern ).exec( linkHref.pathname );
// We can't be sure decodeURIComponent() is able to parse every possible match // We can't be sure decodeURIComponent() is able to parse every possible match
try { try {
@ -54,12 +64,12 @@ export function getTitle( href, config ) {
} catch ( e ) { } catch ( e ) {
// Will return undefined below // Will return undefined below
} }
} else if ( queryLength === 1 && 'title' in linkHref.query ) { } else if ( queryLength === 1 && searchParams.has( 'title' ) ) {
// URL is not pretty, but only has a `title` parameter // URL is not pretty, but only has a `title` parameter
title = linkHref.query.title; title = searchParams.get( 'title' );
} }
return title ? `${ title }${ linkHref.fragment ? `#${ linkHref.fragment }` : '' }` : undefined; return title ? `${ title }${ linkHref.hash ? linkHref.hash : '' }` : undefined;
} }
/** /**

View file

@ -5,31 +5,24 @@ QUnit.module( 'title#getTitle', {
this.config = new Map(); this.config = new Map();
this.config.set( 'wgArticlePath', '/wiki/$1' ); this.config.set( 'wgArticlePath', '/wiki/$1' );
this.location = global.location = { hostname: 'en.wikipedia.org' }; this.location = global.location = {
origin: 'https://en.wikipedia.org',
hostname: 'en.wikipedia.org'
};
mw.util = { mw.util = {
escapeRegExp: this.sandbox.spy( ( str ) => { escapeRegExp: this.sandbox.spy( ( str ) => {
return str.replace( /([\\{}()|.?*+\-^$[\]])/g, '\\$1' ); return str.replace( /([\\{}()|.?*+\-^$[\]])/g, '\\$1' );
} ) } )
}; };
mw.Uri = this.sandbox.stub().throws( 'UNIMPLEMENTED' );
}, },
afterEach() { afterEach() {
global.location = null; global.location = null;
mw.util = null; mw.util = null;
mw.Uri = null;
} }
} ); } );
QUnit.test( 'it should return the title of a url with a title query param', function ( assert ) { QUnit.test( 'it should return the title of a url with a title query param', function ( assert ) {
const href = '/w/index.php?title=Foo'; const href = '/w/index.php?title=Foo';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
query: {
title: 'Foo'
}
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -40,11 +33,6 @@ QUnit.test( 'it should return the title of a url with a title query param', func
QUnit.test( 'it should return the title of a pretty url if it conforms wgArticlePath', function ( assert ) { QUnit.test( 'it should return the title of a pretty url if it conforms wgArticlePath', function ( assert ) {
const href = '/wiki/Foo'; const href = '/wiki/Foo';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
path: href,
query: {}
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -55,11 +43,6 @@ QUnit.test( 'it should return the title of a pretty url if it conforms wgArticle
QUnit.test( 'it should return the title of a pretty url properly decoded', function ( assert ) { QUnit.test( 'it should return the title of a pretty url properly decoded', function ( assert ) {
const href = '/wiki/%E6%B8%AC%E8%A9%A6'; const href = '/wiki/%E6%B8%AC%E8%A9%A6';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
path: href,
query: {}
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -70,12 +53,6 @@ QUnit.test( 'it should return the title of a pretty url properly decoded', funct
QUnit.test( 'it should accept urls with fragments', function ( assert ) { QUnit.test( 'it should accept urls with fragments', function ( assert ) {
let href = '/wiki/Example_1#footnote_1'; let href = '/wiki/Example_1#footnote_1';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
path: href,
query: {},
fragment: 'footnote_1'
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -84,11 +61,6 @@ QUnit.test( 'it should accept urls with fragments', function ( assert ) {
); );
href = '/w/index.php?title=Example_2#footnote_2'; href = '/w/index.php?title=Example_2#footnote_2';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
query: { title: 'Example_2' },
fragment: 'footnote_2'
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -99,20 +71,12 @@ QUnit.test( 'it should accept urls with fragments', function ( assert ) {
QUnit.test( 'it should skip pretty urls with invalid % encoded characters', function ( assert ) { QUnit.test( 'it should skip pretty urls with invalid % encoded characters', function ( assert ) {
const href = '/wiki/100%'; const href = '/wiki/100%';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
path: href,
query: {}
} );
assert.strictEqual( getTitle( href, this.config ), undefined ); assert.strictEqual( getTitle( href, this.config ), undefined );
} ); } );
QUnit.test( 'it should skip urls that mw.Uri cannot parse', function ( assert ) { QUnit.test( 'it should skip urls that URL cannot parse', function ( assert ) {
const href = 'javascript:void(0);'; // eslint-disable-line no-script-url const href = 'javascript:void(0);'; // eslint-disable-line no-script-url
mw.Uri.withArgs( href ).throws(
new Error( 'Cannot parse' )
);
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -123,11 +87,6 @@ QUnit.test( 'it should skip urls that mw.Uri cannot parse', function ( assert )
QUnit.test( 'it should skip urls that are external', function ( assert ) { QUnit.test( 'it should skip urls that are external', function ( assert ) {
const href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'; const href = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
mw.Uri.withArgs( href ).returns( {
host: 'www.youtube.com',
path: '/watch',
query: { v: 'dQw4w9WgXcQ' }
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -139,11 +98,6 @@ QUnit.test( 'it should skip urls that are external', function ( assert ) {
QUnit.test( 'it should skip urls not on article path without one title query param', function ( assert ) { QUnit.test( 'it should skip urls not on article path without one title query param', function ( assert ) {
// No params // No params
let href = '/Foo'; let href = '/Foo';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
path: '/Foo',
query: {}
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),
@ -153,11 +107,6 @@ QUnit.test( 'it should skip urls not on article path without one title query par
// Multiple query params // Multiple query params
href = '/Foo?a=1&title=Foo'; href = '/Foo?a=1&title=Foo';
mw.Uri.withArgs( href ).returns( {
host: this.location.hostname,
path: '/Foo',
query: { a: 1, title: 'Foo' }
} );
assert.strictEqual( assert.strictEqual(
getTitle( href, this.config ), getTitle( href, this.config ),

View file

@ -121,8 +121,8 @@ module.exports = ( env, argv ) => ( {
// Minified uncompressed size limits for chunks / assets and entrypoints. Keep these numbers // Minified uncompressed size limits for chunks / assets and entrypoints. Keep these numbers
// up-to-date and rounded to the nearest 10th of a kibibyte so that code sizing costs are // up-to-date and rounded to the nearest 10th of a kibibyte so that code sizing costs are
// well understood. Related to bundlesize minified, gzipped compressed file size tests. // well understood. Related to bundlesize minified, gzipped compressed file size tests.
maxAssetSize: 40.0 * 1024, maxAssetSize: 40.1 * 1024,
maxEntrypointSize: 40.0 * 1024, maxEntrypointSize: 40.1 * 1024,
// The default filter excludes map files but we rename ours. // The default filter excludes map files but we rename ours.
assetFilter: ( filename ) => !filename.endsWith( srcMapExt ) assetFilter: ( filename ) => !filename.endsWith( srcMapExt )