Path: blob/main/docs/_static/sphinx_highlight.js
469 views
/* Highlighting utilities for Sphinx HTML documentation. */1"use strict";2const SPHINX_HIGHLIGHT_ENABLED = true3/**4* highlight a given string on a node by wrapping it in5* span elements with the given class name.6*/7const _highlight = (node, addItems, text, className) => {8if (node.nodeType === Node.TEXT_NODE) {9const val = node.nodeValue;10const parent = node.parentNode;11const pos = val.toLowerCase().indexOf(text);12if (13pos >= 0 &&14!parent.classList.contains(className) &&15!parent.classList.contains("nohighlight")16) {17let span;18const closestNode = parent.closest("body, svg, foreignObject");19const isInSVG = closestNode && closestNode.matches("svg");20if (isInSVG) {21span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");22} else {23span = document.createElement("span");24span.classList.add(className);25}26span.appendChild(document.createTextNode(val.substr(pos, text.length)));27const rest = document.createTextNode(val.substr(pos + text.length));28parent.insertBefore(29span,30parent.insertBefore(31rest,32node.nextSibling33)34);35node.nodeValue = val.substr(0, pos);36/* There may be more occurrences of search term in this node. So call this37* function recursively on the remaining fragment.38*/39_highlight(rest, addItems, text, className);40if (isInSVG) {41const rect = document.createElementNS(42"http://www.w3.org/2000/svg",43"rect"44);45const bbox = parent.getBBox();46rect.x.baseVal.value = bbox.x;47rect.y.baseVal.value = bbox.y;48rect.width.baseVal.value = bbox.width;49rect.height.baseVal.value = bbox.height;50rect.setAttribute("class", className);51addItems.push({ parent: parent, target: rect });52}53}54} else if (node.matches && !node.matches("button, select, textarea")) {55node.childNodes.forEach((el) => _highlight(el, addItems, text, className));56}57};58const _highlightText = (thisNode, text, className) => {59let addItems = [];60_highlight(thisNode, addItems, text, className);61addItems.forEach((obj) =>62obj.parent.insertAdjacentElement("beforebegin", obj.target)63);64};65/**66* Small JavaScript module for the documentation.67*/68const SphinxHighlight = {69/**70* highlight the search words provided in localstorage in the text71*/72highlightSearchWords: () => {73if (!SPHINX_HIGHLIGHT_ENABLED) return; // bail if no highlight74// get and clear terms from localstorage75const url = new URL(window.location);76const highlight =77localStorage.getItem("sphinx_highlight_terms")78|| url.searchParams.get("highlight")79|| "";80localStorage.removeItem("sphinx_highlight_terms")81url.searchParams.delete("highlight");82window.history.replaceState({}, "", url);83// get individual terms from highlight string84const terms = highlight.toLowerCase().split(/\s+/).filter(x => x);85if (terms.length === 0) return; // nothing to do86// There should never be more than one element matching "div.body"87const divBody = document.querySelectorAll("div.body");88const body = divBody.length ? divBody[0] : document.querySelector("body");89window.setTimeout(() => {90terms.forEach((term) => _highlightText(body, term, "highlighted"));91}, 10);92const searchBox = document.getElementById("searchbox");93if (searchBox === null) return;94searchBox.appendChild(95document96.createRange()97.createContextualFragment(98'<p class="highlight-link">' +99'<a href="javascript:SphinxHighlight.hideSearchWords()">' +100_("Hide Search Matches") +101"</a></p>"102)103);104},105/**106* helper function to hide the search marks again107*/108hideSearchWords: () => {109document110.querySelectorAll("#searchbox .highlight-link")111.forEach((el) => el.remove());112document113.querySelectorAll("span.highlighted")114.forEach((el) => el.classList.remove("highlighted"));115localStorage.removeItem("sphinx_highlight_terms")116},117initEscapeListener: () => {118// only install a listener if it is really needed119if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) return;120document.addEventListener("keydown", (event) => {121// bail for input elements122if (BLACKLISTED_KEY_CONTROL_ELEMENTS.has(document.activeElement.tagName)) return;123// bail with special keys124if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return;125if (DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS && (event.key === "Escape")) {126SphinxHighlight.hideSearchWords();127event.preventDefault();128}129});130},131};132_ready(() => {133/* Do not call highlightSearchWords() when we are on the search page.134* It will highlight words from the *previous* search query.135*/136if (typeof Search === "undefined") SphinxHighlight.highlightSearchWords();137SphinxHighlight.initEscapeListener();138});139140141