mediawiki-extensions-Cite/modules/ext.cite.highlighting.js

115 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
/**
* Dynamic highlighting while reading an article
*
* @author Thiemo Kreuz
*/
( function () {
/**
* Checks if the ID uses a composite format that does not only consist of a sequential number.
*
* @param {string} id
* @return {boolean}
*/
function isNamedReference( id ) {
// Note: This assumes IDs start with the prefix; this is guaranteed by the parser function
Drop unused cite_reference(s)_link_prefix messages Same as Icfa8215 where we removed the …_suffix messages. This patch is not blocked on anything according to CodeSearch: https://codesearch.wmcloud.org/search/?q=cite_references%3F_link_prefix According to GlobalSearch there are 2 usages we need to talk about: https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.references%3F.link.prefix.* zh.wiktionary replaces "cite_ref-" with "_ref-", and "cite_note-" with "_note-", i.e. they did nothing but remove the word "cite". This happened in 2006, with no explanation. ka.wikibooks and ka.wikiquote replace "cite_note-" with "_შენიშვნა-", which translates back to "_note-". One user did this in 2007, 16 seconds apart. It appears like both are attempts to localize what can be localized, no matter if it's really necessary or not. https://zh.wiktionary.org/wiki/Special:Contributions/Shibo77?offset=20060510 https://ka.wikiquote.org/wiki/Special:Contributions/Trulala?offset=20070219 Note how one user experimented with an "a" in some of the edits to see what effect the change might have, to imediatelly revert it. The modifications don't really have an effect on anything, except on the anchors in the resulting <a href="#_ref-5"> and <sup id="_ref-5"> HTML. It might also be briefly visible in the browser's address bar when such a link is clicked. We can only assume the two users did this to make the URL appear shorter (?). A discussion apparently never happened. Bot users are inactive. Both pieces of HTML are generated in the Cite code. Removing the messages will change all places the same time. All links will continue to work. The only possible effect is that hard-coded weblinks to an individual reference will link to the top of the article instead. But: a) This is extremely unlikely to happen. There is no reason to link to a reference from outside of the article. b) Such links are not guaranteed to work anyway as they can break for a multitude of other reasons, e.g. the <ref> being renamed, removed, or replaced. c) Even if such a link breaks, it still links to the correct article. There is also no on-wiki code on zh.wiktionary that would do anything with the shortened prefix: https://zh.wiktionary.org/w/index.php?search=insource%3A%2F_%28ref%7Cnote%29-%2F&title=Special%3A%E6%90%9C%E7%B4%A2&profile=advanced&fulltext=1&ns2=1&ns4=1&ns8=1&ns10=1&ns12=1&ns828=1&ns2300=1 I argue this is safe to remove, even without contacting the mentioned communities first. Bug: T321217 Change-Id: I160a119710dc35679dbdc2f39ddf453dbd5a5dfa
2024-01-04 11:26:07 +00:00
return /^cite_ref-\D/.test( id );
}
/**
* @param {string} id
* @param {jQuery} $content
* @return {boolean}
*/
function isReusedNamedReference( id, $content ) {
if ( !isNamedReference( id ) ) {
return false;
}
// Either the ID is already a reuse, or at least one reuse exists somewhere else on the page
return id.slice( -2 ) !== '-0' ||
$content.find( '.references a[href="#' + $.escapeSelector( id.slice( 0, -1 ) ) + '1"]' ).length;
}
/**
* @param {jQuery} $backlinkWrapper
* @return {jQuery}
*/
function makeUpArrowLink( $backlinkWrapper ) {
let textNode = $backlinkWrapper[ 0 ].firstChild;
const accessibilityLabel = mw.msg( 'cite_references_link_accessibility_back_label' );
const $upArrowLink = $( '<a>' )
.addClass( 'mw-cite-up-arrow-backlink' )
.attr( 'aria-label', accessibilityLabel )
.attr( 'title', accessibilityLabel );
if ( !textNode ) {
return $upArrowLink;
}
// Skip additional, custom HTML wrappers, if any.
while ( textNode.firstChild ) {
textNode = textNode.firstChild;
}
if ( textNode.nodeType !== Node.TEXT_NODE || textNode.data.trim() === '' ) {
return $upArrowLink;
}
const 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.
$backlinkWrapper.prepend(
$( '<span>' )
.addClass( 'mw-cite-up-arrow' )
.text( upArrow ),
$upArrowLink
.text( upArrow )
);
return $upArrowLink;
}
/**
* @param {jQuery} $backlink
*/
function updateUpArrowLink( $backlink ) {
// It's convenient to stop at the class name, but it's not guaranteed to be there.
const $backlinkWrapper = $backlink.closest( '.mw-cite-backlink, li' );
let $upArrowLink = $backlinkWrapper.find( '.mw-cite-up-arrow-backlink' );
if ( !$upArrowLink.length && $backlinkWrapper.length ) {
$upArrowLink = makeUpArrowLink( $backlinkWrapper );
}
$upArrowLink.attr( 'href', $backlink.attr( 'href' ) );
}
mw.hook( 'wikipage.content' ).add( ( $content ) => {
// We are going to use the ID in the code below, so better be sure one is there.
$content.find( '.reference[id] > a' ).on( 'click', function () {
const id = $( this ).parent().attr( 'id' );
$content.find( '.mw-cite-targeted-backlink' ).removeClass( 'mw-cite-targeted-backlink' );
// Bail out if there is not at least a second backlink ("cite_references_link_many").
if ( !isReusedNamedReference( id, $content ) ) {
return;
}
// The :not() skips the duplicate link created below. Relevant when double clicking.
const $backlink = $content.find( '.references a[href="#' + $.escapeSelector( id ) + '"]:not(.mw-cite-up-arrow-backlink)' )
.first()
.addClass( 'mw-cite-targeted-backlink' );
if ( $backlink.length ) {
updateUpArrowLink( $backlink );
}
} );
} );
}() );