Debian Codegraph @

resources/src/mediawiki.action/mediawiki.action.view.redirect.js

raw symbols SCIP
1 /*!
2 * In general, MediaWiki does not ask browsers to resolve wiki page
3 * redirects client-side over HTTP. Instead, wiki page redirects are
4 * resolved server-side and rendered directly in response to a
5 * navigation.
6 *
7 * This script is responsible for:
8 *
9 * - Update the address bar to reflect the rendered destination.
10 *
11 * Given [[Foo]] redirecting to [[Bar]], when viewing [[Foo]]
12 * the server renders Bar content, with "Bar" as doc title
13 * and with "Bar" in the address bar.
14 *
15 * - For internal redirect destination that specify a fragment, if
16 * the navigation does not set its own fragment, scroll to the
17 * specified section.
18 *
19 * Given [[Foo]] redirecting to [[Bar#Foo]], the browser should
20 * scroll to "Foo", and render address bar Bar#Foo (not Foo, Bar,
21 * or Foo#Foo).
22 *
23 * Given [[Foo]] redirecting to [[Bar#Foo]], when navigating to
24 * [[Foo#Quux]], the address bar should reflect Bar#Quux, and
25 * let the native scroll happen, don't override scroll to #Foo.
26 */
27 ( function () {
28 'use strict';
29
30 let canonical = mw.config.get( 'wgInternalRedirectTargetUrl' );
31 if ( !canonical ) {
32 return;
33 }
34
35 let fragment = null;
36 if ( location.hash ) {
37 // Ignore redirect's own fragment and preserve fragment override in address
38 canonical = canonical.replace( /#.*$/, '' ) + location.hash;
39 } else {
40 const index = canonical.indexOf( '#' );
41 fragment = ( index !== -1 ) ? canonical.slice( index ) : null;
42 }
43
44 // Update address bar, including browser history.
45 // Preserve correct "Back"-button behaviour by using replaceState instead of
46 // pushState (or location.hash assignment)
47 history.replaceState( history.state, '', canonical );
48
49 if ( fragment ) {
50 // Specification for history.replaceState() doesn't require browser to scroll,
51 // so scroll to be sure (see also T110501).
52 const node = document.getElementById( fragment.slice( 1 ) );
53 if ( node ) {
54 node.scrollIntoView();
55 }
56 }
57
58 }() );