mediawiki-extensions-Cite/modules/ext.cite.highlighting.js
Thiemo Kreuz (WMDE) 2b3a62c26b Make arrows/carets links whenever we know where the user came from
I tried to arrange the new code in a way that it is compact, but still
readable. I think it's possible to arrange it even better, but browser
tests should be added first, in my opinion.

Bug: T205271
Change-Id: I1d579ef9d2787fc43c0a8bbf61c583f602dca5d4
2018-11-19 16:27:59 +00:00

55 lines
1.7 KiB
JavaScript

/**
* @author Thiemo Kreuz
*/
( function () {
'use strict';
mw.hook( 'wikipage.content' ).add( function ( $content ) {
// We are going to use the ID in the code below, so better be sure one is there.
$content.find( '.reference[id] > a' ).click( function () {
var $backlink, $backlinkWrapper, $upArrowLink, textNode, upArrow,
id = $( this ).parent().attr( 'id' ),
className = 'mw-cite-targeted-backlink';
$content.find( '.' + className ).removeClass( className );
// The additional "*" avoids the "↑" (when there is only one backlink) becoming bold.
$backlink = $content.find( '.mw-cite-backlink * a[href="#' + id + '"]' )
.addClass( className );
if ( !$backlink.length ) {
return;
}
$backlinkWrapper = $backlink.closest( '.mw-cite-backlink' );
$upArrowLink = $backlinkWrapper.find( '.mw-cite-up-arrow-backlink' );
if ( !$upArrowLink.length ) {
textNode = $backlinkWrapper[ 0 ].firstChild;
if ( textNode &&
textNode.nodeType === Node.TEXT_NODE &&
textNode.data.trim() !== ''
) {
upArrow = textNode.data.trim();
// The text node typically contains "↑ ", and we need to keep the space.
textNode.data = textNode.data.replace( upArrow, '' );
// Create a plain text and a clickable "↑". CSS :target selectors make sure only
// one is visible at a time.
$upArrowLink = $( '<a>' )
.addClass( 'mw-cite-up-arrow-backlink' )
.text( upArrow );
$backlinkWrapper.prepend(
$( '<span>' )
.addClass( 'mw-cite-up-arrow' )
.text( upArrow ),
$upArrowLink
);
}
}
$upArrowLink.attr( 'href', $backlink.attr( 'href' ) );
} );
} );
}() );