MediaWiki:Common.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 141:
document.body;
 
// Collect headings (H2–H5;H2–H6). h6Prefer isspans boldwith body.mw-headline text,(stable excludedanchor from the TOCids).
// Prefer spans with .mw-headline (stable anchor ids)
var items = [];
var headings = root.querySelectorAll('h2, h3, h4, h5, h6');
headings.forEach(function (h) {
var level = parseInt(h.tagName.slice(1), 10);
if (level < 2 || level > 56) return;
var headline = h.querySelector('.mw-headline') || h;
var id = headline.id || h.id;
Line 691 ⟶ 690:
 
 
/* ── Hide DEEP (nested) level-6 headings from the sidebar (Vector) TOC ─────────────────────────────
The doc pages carry repeated h6 sub-entries that clutter the TOC. MediaWiki's
MediaWiki's TOC classes (vector-toc-level-N) encode only a COMPACTED nesting depth, not
depth, NOT the original heading tag — when levels jump (h3->h6) it still only
incrementsthe oneoriginal level —tag, so CSS cannot reliablycan't target h6. Resolve— resolve each TOCentry to its body
heading instead. We hide ONLY NESTED h6: a TOP-LEVEL section that happens to
entry to its body heading and hide the ones that are actually <h6>. Safe: it
be h6 (e.g. Cover / Audit Information) is KEPT, because hiding a numbered
only ever hides h6 (never h2–h5). The mobile overlay excludes h6 separately
top-level entry leaves a gap in MediaWiki's static TOC numbers (0 -> 3). */
(above). */
(function () {
function headingFor(anchor) {
Line 717 ⟶ 716:
if (href.charAt(0) !== '#' || href.length < 2) { return; }
var h = headingFor(href.slice(1));
if (!h &&|| h.tagName =!== 'H6') { li.style.display = 'none'return; }
// Only hide NESTED h6 (deep repeated sub-entries). A top-level h6 section
// is kept so the top-level numbering stays contiguous.
var nested = li.parentElement && li.parentElement.closest &&
li.parentElement.closest('.vector-toc-list-item');
if (nested) { li.style.display = 'none'; }
});
}
// After hiding nested h6 entries, a parent whose children were ALL h6 keeps a
// dangling expand/collapse chevron with nothing left to reveal — hide those
// toggles (and restore any that still have a visible child, on a re-run).
function hideEmptyToggles() {
var items = document.querySelectorAll('.vector-toc .vector-toc-list-item');
Array.prototype.forEach.call(items, function (li) {
var toggle = li.querySelector(':scope > .vector-toc-toggle');
if (!toggle) { return; }
var childList = li.querySelector(':scope > .vector-toc-list');
var visible = false;
if (childList) {
Array.prototype.forEach.call(
childList.querySelectorAll(':scope > .vector-toc-list-item'),
function (k) { if (k.style.display !== 'none') { visible = true; } }
);
}
toggle.style.display = visible ? '' : 'none';
});
}
function run() { pruneTocH6(); hideEmptyToggles(); }
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', pruneTocH6run);
} else {
pruneTocH6run();
}
// Vector can lazily (re)build the pinned TOC; re-run on its content hook.
if (window.mw && mw.hook) { mw.hook('wikipage.content').add(pruneTocH6run); }
})();