Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions sphinx_togglebutton/_static/togglebutton.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ let toggleChevron = `
<polyline points="9 6 15 12 9 18" />
</svg>`;

var togglebuttonLog = (...args) => {
if (typeof togglebuttonDebug !== "undefined" && togglebuttonDebug) {
console.log(...args);
}
};

var initToggleItems = () => {
var itemsToToggle = document.querySelectorAll(togglebuttonSelector);
console.log(
togglebuttonLog(
`[togglebutton]: Adding toggle buttons to ${itemsToToggle.length} items`
);
// Add the button to each admonition and hook up a callback to toggle visibility
Expand Down Expand Up @@ -163,7 +169,7 @@ var toggleClickHandler = (click) => {
// We've clicked the button itself and so don't need to do anything
button = click.target;
} else {
console.log(`[togglebutton]: Couldn't find button for ${click.target}`);
togglebuttonLog(`[togglebutton]: Couldn't find button for ${click.target}`);
}
target = document.getElementById(button.dataset["button"]);
toggleHidden(target);
Expand Down Expand Up @@ -234,7 +240,7 @@ if (toggleOpenOnPrint == "true") {
document
.querySelectorAll(".admonition.toggle.toggle-hidden")
.forEach((el) => {
console.log(el);
togglebuttonLog(el);
el.querySelector("button.toggle-button").click();
el.dataset["toggle_after_print"] = "true";
});
Expand Down
66 changes: 66 additions & 0 deletions tests/test_togglebutton_console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Regression for executablebooks/sphinx-togglebutton#56:
* initToggleItems must not write to the console unless a debug flag is on.
*/
const { test } = require("node:test");
const assert = require("node:assert/strict");
const fs = require("fs");
const path = require("path");
const vm = require("vm");

const JS_PATH = path.join(
__dirname,
"..",
"sphinx_togglebutton",
"_static",
"togglebutton.js"
);

function loadTogglebutton({ togglebuttonDebug } = {}) {
const logs = [];
const sandbox = {
console: {
log: (...args) => {
logs.push(args);
},
},
document: {
// Stay in "loading" so DOMContentLoaded hooks are not flushed on import.
readyState: "loading",
querySelectorAll: () => [],
addEventListener: () => {},
getElementById: () => null,
},
window: {
addEventListener: () => {},
},
CSS: { escape: (s) => s },
togglebuttonSelector: ".toggle",
toggleHintShow: "Click to show",
toggleHintHide: "Click to hide",
toggleOpenOnPrint: "false",
};
if (togglebuttonDebug !== undefined) {
sandbox.togglebuttonDebug = togglebuttonDebug;
}
vm.createContext(sandbox);
vm.runInContext(fs.readFileSync(JS_PATH, "utf8"), sandbox);
return { sandbox, logs };
}

test("initToggleItems does not log to the console by default", () => {
const { sandbox, logs } = loadTogglebutton();
sandbox.initToggleItems();
assert.equal(
logs.length,
0,
`unconditional console.log: ${JSON.stringify(logs)}`
);
});

test("initToggleItems logs when togglebuttonDebug is enabled", () => {
const { sandbox, logs } = loadTogglebutton({ togglebuttonDebug: true });
sandbox.initToggleItems();
assert.equal(logs.length, 1);
assert.match(String(logs[0]), /\[togglebutton\]: Adding toggle buttons to 0 items/);
});