mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-11-23 22:45:20 +00:00
Fix highlighting code destroying backlinks on unnamed <ref>s
There are multiple formats for these IDs: cite_ref-1, cite_ref-2, and so on for anonymous <ref>s without a name. cite_ref-name_1-0 for named references, where "name" is the custom name, and "_1" is the sequential number for the reference (same number as above). The final "-0" is counting the usages. If a named <ref> is only used once, there is no cite_ref-name_1-1 anywhere on the page. The later was already checked by the code. But we forgot about unnamed references! As a consequence IDs like the cite_ref-1 above got misdetected as reused references. This patch tries hard to extract code into named functions, so it becomes much more clear what they do, and why. Bug: T215317 Change-Id: Iedb5b0c3dffae19bad7df9a43ed2d4512b3921ec
This commit is contained in:
parent
e71dcc355d
commit
85ad4513ba
|
@ -87,6 +87,7 @@
|
|||
"ext.cite.highlighting.css"
|
||||
],
|
||||
"messages": [
|
||||
"cite_reference_link_prefix",
|
||||
"cite_references_link_accessibility_label",
|
||||
"cite_references_link_many_accessibility_label",
|
||||
"cite_references_link_accessibility_back_label"
|
||||
|
|
|
@ -4,6 +4,35 @@
|
|||
( function () {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Checks if the ID uses a composite format that does not only consist of a sequential number,
|
||||
* as specified in "cite_reference_link_key_with_num".
|
||||
*
|
||||
* @param {string} id
|
||||
* @return {boolean}
|
||||
*/
|
||||
function isNamedReference( id ) {
|
||||
var prefix = mw.msg( 'cite_reference_link_prefix' );
|
||||
|
||||
// Note: This assumes IDs start with the prefix; this is guaranteed by the parser function
|
||||
return /\D/.test( id.slice( prefix.length ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @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}
|
||||
|
@ -72,9 +101,7 @@
|
|||
$content.find( '.' + className ).removeClass( className );
|
||||
|
||||
// Bail out if there is not at least a second backlink ("cite_references_link_many").
|
||||
if ( id.slice( -2 ) === '-0' &&
|
||||
!$content.find( '.references a[href="#' + $.escapeSelector( id.slice( 0, -1 ) ) + '1"]' ).length
|
||||
) {
|
||||
if ( !isReusedNamedReference( id, $content ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue