jquery.makeCollapsible: Use CSS :target when handling links to collapsed elements

This works correctly when the fragment identifier is percent-encoded,
and relies on the browser to handle the annoying work of decoding it.

Follow-up to 599c80d06e.

Bug: T283959
Change-Id: I6062912b496a85d5838580824dbfcbfc726f896c
This commit is contained in:
Bartosz Dziewoński 2021-05-30 02:33:45 +02:00
parent d95a7246dc
commit 5295ecafd3
2 changed files with 5 additions and 11 deletions

View file

@ -163,15 +163,9 @@
* @private
*/
function hashHandler() {
var fragmentId, fragment, $parents;
var fragment, $parents;
fragmentId = window.location.hash.slice( 1 );
if ( !fragmentId ) {
// The hash is empty
return;
}
fragment = document.getElementById( fragmentId );
fragment = document.querySelector( ':target' );
if ( !fragment ) {
// The fragment doesn't exist
return;

View file

@ -379,9 +379,9 @@
QUnit.test( 'reveal hash fragment', function ( assert ) {
var $collapsible = prepareCollapsible(
'<div class="mw-collapsible mw-collapsed">' + loremIpsum + '<div id="div,a:nth-child(even)">' + loremIpsum + '</div></div>'
'<div class="mw-collapsible mw-collapsed">' + loremIpsum + '<div id="español,a:nth-child(even)">' + loremIpsum + '</div></div>'
),
fragment = document.getElementById( 'div,a:nth-child(even)' ),
fragment = document.getElementById( 'español,a:nth-child(even)' ),
done = assert.async();
assert.assertTrue( fragment.offsetParent === null, 'initial: fragment is hidden' );
@ -392,7 +392,7 @@
window.location.hash = '';
} );
window.location.hash = 'div,a:nth-child(even)';
window.location.hash = 'espa%C3%B1ol,a:nth-child(even)';
} );
QUnit.test( 'T168689 - nested collapsible divs should keep independent state', function ( assert ) {