Migrate redirectToFragment() from wikibits.js to own module
authorumherirrender <umherirrender_de.wp@web.de>
Tue, 17 Dec 2013 18:41:33 +0000 (19:41 +0100)
committerumherirrender <umherirrender_de.wp@web.de>
Tue, 17 Dec 2013 18:41:33 +0000 (19:41 +0100)
This patch creates the new top module
'mediawiki.action.view.redirectToFragment'.
This module contains the same code, but was migrated to use
jQuery.profile to detect the layout of the browser.

Other changes as written on bug 35858 can be done in seperate
patch set(s). This is to have a start.

The old code has to stay a version, because the cache can contain
inline script calls to this function, which should not break.

Bug: 35858
Change-Id: Ic10c060c3a2cbab455348e57740fc8abdc66ff9a

includes/Article.php
resources/Resources.php
resources/mediawiki.action/mediawiki.action.view.redirectToFragment.js [new file with mode: 0644]
skins/common/wikibits.js

index 821c32e..e7a1188 100644 (file)
@@ -989,9 +989,8 @@ class Article implements Page {
 
                                // Set the fragment if one was specified in the redirect
                                if ( strval( $this->getTitle()->getFragment() ) != '' ) {
-                                       $outputPage->addInlineScript( Xml::encodeJsCall(
-                                               'redirectToFragment', array( $this->getTitle()->getFragmentForURL() )
-                                       ) );
+                                       $outputPage->addJsConfigVars( 'wgRedirectToFragment', $this->getTitle()->getFragmentForURL() );
+                                       $outputPage->addModules( 'mediawiki.action.view.redirectToFragment' );
                                }
 
                                // Add a <link rel="canonical"> tag
index d2a06b7..b389fef 100644 (file)
@@ -835,6 +835,13 @@ return array(
                        'postedit-confirmation',
                ),
        ),
+       'mediawiki.action.view.redirectToFragment' => array(
+               'scripts' => 'resources/mediawiki.action/mediawiki.action.view.redirectToFragment.js',
+               'dependencies' => array(
+                       'jquery.client',
+               ),
+               'position' => 'top',
+       ),
        'mediawiki.action.view.rightClickEdit' => array(
                'scripts' => 'resources/mediawiki.action/mediawiki.action.view.rightClickEdit.js',
        ),
diff --git a/resources/mediawiki.action/mediawiki.action.view.redirectToFragment.js b/resources/mediawiki.action/mediawiki.action.view.redirectToFragment.js
new file mode 100644 (file)
index 0000000..1e2d624
--- /dev/null
@@ -0,0 +1,35 @@
+/**
+ * JavaScript to scroll the page to an id, when a redirect with fragment is viewed.
+ */
+( function ( mw, $ ) {
+       var profile = $.client.profile(),
+               fragment = mw.config.get( 'wgRedirectToFragment' );
+
+       if ( fragment === null ) {
+               // nothing to do
+               return;
+       }
+
+       if ( profile.layout === 'webkit' && profile.layoutVersion < 420 ) {
+               // Released Safari w/ WebKit 418.9.1 messes up horribly
+               // Nightlies of 420+ are ok
+               return;
+       }
+       if ( !window.location.hash ) {
+               window.location.hash = fragment;
+
+               // Mozilla needs to wait until after load, otherwise the window doesn't
+               // scroll.  See <https://bugzilla.mozilla.org/show_bug.cgi?id=516293>.
+               // There's no obvious way to detect this programmatically, so we use
+               // version-testing.  If Firefox fixes the bug, they'll jump twice, but
+               // better twice than not at all, so make the fix hit future versions as
+               // well.
+               if ( profile.layout === 'gecko' ) {
+                       $( function () {
+                               if ( window.location.hash === fragment ) {
+                                       window.location.hash = fragment;
+                               }
+                       } );
+               }
+       }
+}( mediaWiki, jQuery ) );
index d28ca0a..35fc9f2 100644 (file)
@@ -18,7 +18,14 @@ if ( mw.config.get( 'wgBreakFrames' ) ) {
        }
 }
 
-win.redirectToFragment = function ( fragment ) {
+/**
+ * Legacy function to scroll to an id while viewing the page over a redirect.
+ * Superseeded by module 'mediawiki.action.view.redirectToFragment' in version 1.23.
+ * Kepted because cache can contain still inline script calls to this function.
+ * Should be removed in version 1.24.
+ * @deprecated since 1.23 Use mediawiki.action.view.redirectToFragment instead
+ */
+mw.log.deprecate( win, 'redirectToFragment', function ( fragment ) {
        var webKitVersion,
                match = navigator.userAgent.match( /AppleWebKit\/(\d+)/ );
        if ( match ) {
@@ -46,7 +53,7 @@ win.redirectToFragment = function ( fragment ) {
                        } );
                }
        }
-};
+}, 'Use the module mediawiki.action.view.redirectToFragment instead.' );
 
 /**
  * User-agent sniffing.