Debian Codegraph @

extensions/SyntaxHighlight_GeSHi/modules/pygments.linenumbers.js

raw symbols SCIP
1 $( () => {
2
3 /**
4 * Parse a line ID, e.g. "L-18"
5 *
6 * @param {string} id Line ID fragment
7 * @return {Object} Object with a string prefix and number
8 */
9 function parseId( id ) {
10 const matches = id.match( /(.*)-([0-9]+)/ );
11 return {
12 prefix: matches[ 1 ],
13 number: +matches[ 2 ]
14 };
15 }
16
17 /**
18 * Build a line ID from a parsed ID
19 *
20 * @param {Object} parsedId See #parseId
21 * @return {string} ID fragment
22 */
23 function buildId( parsedId ) {
24 return parsedId.prefix + '-' + parsedId.number;
25 }
26
27 /**
28 * Get a line element from an ID
29 *
30 * @param {string} id ID
31 * @return {HTMLElement|null} Line element, or null if not found (or the element is not a line)
32 */
33 function getLineElement( id ) {
34 const line = mw.util.getTargetFromFragment( id );
35 // Support IE 11, Edge 14, Safari 7: Can't use unprefixed Element.matches('… *') yet.
36 if ( !$( line ).closest( '.mw-highlight' ).length ) {
37 // Element not in a highlight block
38 return null;
39 }
40 return line;
41 }
42
43 let lastLines, lastAnchorLine;
44
45 /**
46 * Handle hash change events
47 *
48 * @param {boolean} scrollIntoView Scroll the selected lines into view
49 */
50 function onHashChange( scrollIntoView ) {
51 const hash = location.hash.slice( 1 );
52
53 const lines = [];
54 let anchorLine, focusLine;
55 const parts = hash.split( '--' );
56 if ( parts.length === 2 ) {
57 anchorLine = getLineElement( parts[ 0 ] );
58 focusLine = getLineElement( parts[ 1 ] );
59 if ( anchorLine && focusLine ) {
60 const anchorId = parseId( parts[ 0 ] );
61 const focusId = parseId( parts[ 1 ] );
62 if ( anchorId.prefix === focusId.prefix ) {
63 for ( let i = Math.min( anchorId.number, focusId.number ); i <= Math.max( anchorId.number, focusId.number ); i++ ) {
64 lines.push( mw.util.getTargetFromFragment( buildId( { prefix: anchorId.prefix, number: i } ) ) );
65 }
66 if ( scrollIntoView ) {
67 // A line range will not automatically scroll into view
68 lines[ 0 ].scrollIntoView();
69 }
70 }
71 }
72 } else {
73 anchorLine = getLineElement();
74 if ( anchorLine ) {
75 lines.push( anchorLine );
76 }
77 }
78
79 lastAnchorLine = anchorLine;
80
81 if ( lastLines ) {
82 lastLines.forEach( ( line ) => line.classList.remove( 'hll' ) );
83 }
84 lines.forEach( ( line ) => line.classList.add( 'hll' ) );
85
86 lastLines = lines;
87 }
88
89 window.addEventListener( 'hashchange', onHashChange );
90
91 $( document.body ).on( 'click', '.mw-highlight .linenos', ( e ) => {
92 e.preventDefault();
93
94 const targetUrl = new URL( e.target.parentNode.href );
95
96 if ( e.shiftKey && lastAnchorLine ) {
97 const anchorId = parseId( lastAnchorLine.getAttribute( 'id' ) );
98 const focusId = parseId( targetUrl.hash.slice( 1 ) );
99 if ( anchorId.prefix === focusId.prefix ) {
100 const hash = buildId( anchorId ) + '--' + buildId( focusId );
101 history.replaceState( null, '', '#' + hash );
102 } else {
103 history.replaceState( null, '', targetUrl );
104 }
105 } else {
106 history.replaceState( null, '', targetUrl );
107 }
108
109 onHashChange();
110 } );
111
112 // Check hash on load
113 onHashChange( true );
114
115 } );