Replace spaces in link fragments before searching for topics/comments

MediaWiki should never generate links like this, but users or 3rd
party tools probably create them accidentally quite regularly, and
we do a similar thing with Title normalisation.

Change-Id: Ia467f422578bcb9b4d3fb8feab0b85f4b6436f7e
This commit is contained in:
Ed Sanders 2024-01-30 13:19:45 +00:00 committed by Bartosz Dziewoński
parent 8069585489
commit 16e308119f

View file

@ -571,6 +571,20 @@ function init( $container, state ) {
var findCommentQuery;
var isHeading = false;
var highlightResult = highlighter.highlightTargetComment( pageThreads );
// Hash contains a non-replaced space (should be underscore), maybe due to
// manual creation or a broken third party tool. Just replace the spaces
// and navigate to the new URL.
// Ideally we'd use history.replaceState but that wouldn't scroll the page.
// Only do the replacement if the original hash doesn't correspond to a target
// element, but the fixed hash does, to avoid affects on other apps which
// may use fragments with spaces.
if ( location.hash && !mw.util.getTargetFromFragment() && location.hash.indexOf( '%20' ) !== -1 ) {
var fixedHash = location.hash.slice( 1 ).replace( /%20/g, '_' );
if ( mw.util.getTargetFromFragment( fixedHash ) ) {
location.hash = fixedHash;
}
}
if (
// Fragment doesn't correspond to an element on the page
location.hash &&