Merge "inputs.less: Change focus state"
[lhc/web/wiklou.git] / resources / src / mediawiki.action / mediawiki.action.view.redirect.js
1 /*!
2 * JavaScript to update page URL when a redirect is viewed, ensuring that the
3 * page is scrolled to the id when it's a redirect with fragment.
4 *
5 * This is loaded in the top queue, so avoid unnecessary dependencies
6 * like mediawiki.Title or mediawiki.Uri.
7 */
8 ( function ( mw, $ ) {
9 var profile = $.client.profile(),
10 canonical = mw.config.get( 'wgInternalRedirectTargetUrl' ),
11 fragment = null,
12 shouldChangeFragment, index;
13
14 index = canonical.indexOf( '#' );
15 if ( index !== -1 ) {
16 fragment = canonical.slice( index );
17 }
18
19 // Never override the fragment if the user intended to look at a different section
20 shouldChangeFragment = fragment && !location.hash;
21
22 // Replace the whole URL if possible, otherwise just change the fragment
23 if ( canonical && history.replaceState ) {
24 if ( !shouldChangeFragment ) {
25 // If the current page view has a fragment already, don't override it
26 canonical = canonical.replace( /#.*$/, '' );
27 canonical += location.hash;
28 }
29
30 // This will also cause the browser to scroll to given fragment
31 history.replaceState( /*data=*/ history.state, /*title=*/ document.title, /*url=*/ canonical );
32
33 // …except for IE 10 and 11. Prod it with a location.hash change.
34 if ( shouldChangeFragment && profile.name === 'msie' && profile.versionNumber >= 10 ) {
35 location.hash = fragment;
36 }
37
38 } else if ( shouldChangeFragment ) {
39 if ( profile.layout === 'webkit' && profile.layoutVersion < 420 ) {
40 // Released Safari w/ WebKit 418.9.1 messes up horribly
41 // Nightlies of 420+ are ok
42 return;
43 }
44
45 location.hash = fragment;
46 }
47
48 if ( shouldChangeFragment && profile.layout === 'gecko' ) {
49 // Mozilla needs to wait until after load, otherwise the window doesn't
50 // scroll. See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
51 // There's no obvious way to detect this programmatically, so we use
52 // version-testing. If Firefox fixes the bug, they'll jump twice, but
53 // better twice than not at all, so make the fix hit future versions as
54 // well.
55 $( function () {
56 if ( location.hash === fragment ) {
57 location.hash = fragment;
58 }
59 } );
60 }
61
62 }( mediaWiki, jQuery ) );