Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 /**
4 * Dynamic highlighting while reading an article
5 *
6 * @author Thiemo Kreuz
7 */
8 ( function () {
9 /**
10 * Checks if the ID uses a composite format that does not only consist of a sequential number.
11 *
12 * @param {string} id
13 * @return {boolean}
14 */
15 function isNamedReference( id ) {
16 // Note: This assumes IDs start with the prefix; this is guaranteed by the parser function
17 return /^cite_ref-\D/.test( id );
18 }
19
20 /**
21 * @param {string} id
22 * @param {jQuery} $content
23 * @return {boolean}
24 */
25 function isReusedNamedReference( id, $content ) {
26 if ( !isNamedReference( id ) ) {
27 return false;
28 }
29
30 // Either the ID is already a reuse, or at least one reuse exists somewhere else on the page
31 return id.slice( -2 ) !== '-0' ||
32 $content.find( '.references a[href="#' + $.escapeSelector( id.slice( 0, -1 ) ) + '1"]' ).length;
33 }
34
35 /**
36 * @param {jQuery} $backlinkWrapper
37 * @return {jQuery}
38 */
39 function makeUpArrowLink( $backlinkWrapper ) {
40 let textNode = $backlinkWrapper[ 0 ].firstChild;
41 const accessibilityLabel = mw.msg( 'cite_references_link_accessibility_back_label' );
42 const $upArrowLink = $( '<a>' )
43 .addClass( 'mw-cite-up-arrow-backlink' )
44 .attr( 'aria-label', accessibilityLabel )
45 .attr( 'title', accessibilityLabel );
46
47 if ( !textNode ) {
48 return $upArrowLink;
49 }
50
51 // Skip additional, custom HTML wrappers, if any.
52 while ( textNode.firstChild ) {
53 textNode = textNode.firstChild;
54 }
55
56 if ( textNode.nodeType !== Node.TEXT_NODE || textNode.data.trim() === '' ) {
57 return $upArrowLink;
58 }
59
60 const upArrow = textNode.data.trim();
61 // The text node typically contains "↑ ", and we need to keep the space.
62 textNode.data = textNode.data.replace( upArrow, '' );
63
64 // Create a plain text and a clickable "↑". CSS :target selectors make sure only
65 // one is visible at a time.
66 $backlinkWrapper.prepend(
67 $( '<span>' )
68 .addClass( 'mw-cite-up-arrow' )
69 .text( upArrow ),
70 $upArrowLink
71 .text( upArrow )
72 );
73
74 return $upArrowLink;
75 }
76
77 /**
78 * @param {jQuery} $backlink
79 */
80 function updateUpArrowLink( $backlink ) {
81 // It's convenient to stop at the class name, but it's not guaranteed to be there.
82 const $backlinkWrapper = $backlink.closest( '.mw-cite-backlink, li' );
83 let $upArrowLink = $backlinkWrapper.find( '.mw-cite-up-arrow-backlink' );
84
85 if ( !$upArrowLink.length && $backlinkWrapper.length ) {
86 $upArrowLink = makeUpArrowLink( $backlinkWrapper );
87 }
88
89 $upArrowLink.attr( 'href', $backlink.attr( 'href' ) );
90 }
91
92 mw.hook( 'wikipage.content' ).add( ( $content ) => {
93 // We are going to use the ID in the code below, so better be sure one is there.
94 $content.find( '.reference[id] > a' ).on( 'click', function () {
95 const id = $( this ).parent().attr( 'id' );
96
97 $content.find( '.mw-cite-targeted-backlink' ).removeClass( 'mw-cite-targeted-backlink' );
98
99 // Bail out if there is not at least a second backlink ("cite_references_link_many").
100 if ( !isReusedNamedReference( id, $content ) ) {
101 return;
102 }
103
104 // The :not() skips the duplicate link created below. Relevant when double clicking.
105 const $backlink = $content.find( '.references a[href="#' + $.escapeSelector( id ) + '"]:not(.mw-cite-up-arrow-backlink)' )
106 .first()
107 .addClass( 'mw-cite-targeted-backlink' );
108
109 if ( $backlink.length ) {
110 updateUpArrowLink( $backlink );
111 }
112 } );
113 } );
114 }() );