mediawiki-extensions-Syntax.../modules/pygments.linenumbers.js
Timo Tijhof 7d9c268ffd pygments.linenumbers: Avoid deep jQuery loops on unrelated interactions
Follows-up 42c97fa (I82ed4ade), 63c5943a9 (Ic71346196).

This commit makes no functional changes, but it reduces overhead
from the SyntaxHighlight code when interacting with other features,
such as elaborate modals or other hashchange-heavy code paths that
a gadget might produce.

It uses vanilla DOM instead of jQuery where possible, and adds an
early return rather than fairly deep continuation no-ops.

Change-Id: I2264f6f398193802f05c738bad4c294da007fb27
2021-02-19 03:43:24 +00:00

32 lines
737 B
JavaScript

$( function () {
var lastLine;
function onHashChange() {
var id = location.hash.slice( 1 ),
// Don't assume location.hash will be parseable as a selector (T271572)
// and avoid warning when id is empty (T272844)
line = id ? document.getElementById( id ) : null;
if ( lastLine ) {
lastLine.classList.remove( 'hll' );
}
// Support IE 11, Edge 14, Safari 7: Can't use unprefixed Element.matches('… *') yet.
if ( !line || !$( line ).closest( '.mw-highlight' ).length ) {
// Matched ID wasn't in a highlight block
lastLine = null;
return;
}
line.classList.add( 'hll' );
lastLine = line;
}
window.addEventListener( 'hashchange', onHashChange );
// Check hash on load
onHashChange();
}() );