Fix accidental disabling of forward-declared stylesheets (#1373)

PR for the issue flagged in https://github.com/just-the-docs/just-the-docs/discussions/1359#discussioncomment-7267686 (and related to #1358, #1350, etc.). This PR essentially just disables the `head-nav` stylesheet by ID instead of relying on the index; this makes the code more robust against stylesheets that are arbitrarily entered into the document (such as by JavaScript, which is the case of #1359).
This commit is contained in:
Matt Wang
2023-10-25 11:52:44 -07:00
committed by GitHub
parent 56e0f1c800
commit 80bd7bfc9f
3 changed files with 18 additions and 12 deletions

View File

@@ -38,7 +38,7 @@ function initNav() {
const siteNav = document.getElementById('site-nav');
const mainHeader = document.getElementById('main-header');
const menuButton = document.getElementById('menu-button');
disableHeadStyleSheets();
jtd.addEvent(menuButton, 'click', function(e){
@@ -69,15 +69,19 @@ function initNav() {
}
// The <head> element is assumed to include the following stylesheets:
// 0. a <link> to /assets/css/just-the-docs-default.css
// 1. a <link> to /assets/css/just-the-docs-head-nav.css
// 2. a <style> containing the result of _includes/css/activation.scss.liquid.
// It also includes any styles provided by users in _includes/head_custom.html.
// Stylesheet 2 may be missing (compression can remove empty <style> elements)
// so disableHeadStyleSheet() needs to access it by its id.
// - a <link> to /assets/css/just-the-docs-head-nav.css,
// with id 'jtd-head-nav-stylesheet'
// - a <style> containing the result of _includes/css/activation.scss.liquid.
// To avoid relying on the order of stylesheets (which can change with HTML
// compression, user-added JavaScript, and other side effects), stylesheets
// are only interacted with via ID
function disableHeadStyleSheets() {
document.styleSheets[1].disabled = true;
const headNav = document.getElementById('jtd-head-nav-stylesheet');
if (headNav) {
headNav.disabled = true;
}
const activation = document.getElementById('jtd-nav-activation');
if (activation) {
activation.disabled = true;