5 * Sphinx JavaScript utilities for all documentation.
7 * :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS.
8 * :license: BSD, see LICENSE for details.
13 * select a different prefix for underscore
18 * make the code below compatible with browsers without
19 * an installed firebug like debugger
20 if (!window.console || !console.firebug) {
21 var names = ["log", "debug", "info", "warn", "error", "assert", "dir",
22 "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace",
23 "profile", "profileEnd"];
25 for (var i = 0; i < names.length; ++i)
26 window.console[names[i]] = function() {};
31 * small helper function to urldecode strings
33 * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
35 jQuery.urldecode = function(x) {
39 return decodeURIComponent(x.replace(/\+/g, ' '));
43 * small helper function to urlencode strings
45 jQuery.urlencode = encodeURIComponent;
48 * This function returns the parsed url parameters of the
49 * current request. Multiple values per key are supported,
50 * it will always return arrays of strings for the value parts.
52 jQuery.getQueryParameters = function(s) {
53 if (typeof s === 'undefined')
54 s = document.location.search;
55 var parts = s.substr(s.indexOf('?') + 1).split('&');
57 for (var i = 0; i < parts.length; i++) {
58 var tmp = parts[i].split('=', 2);
59 var key = jQuery.urldecode(tmp[0]);
60 var value = jQuery.urldecode(tmp[1]);
62 result[key].push(value);
64 result[key] = [value];
70 * highlight a given string on a jquery object by wrapping it in
71 * span elements with the given class name.
73 jQuery.fn.highlightText = function(text, className) {
74 function highlight(node, addItems) {
75 if (node.nodeType === 3) {
76 var val = node.nodeValue;
77 var pos = val.toLowerCase().indexOf(text);
79 !jQuery(node.parentNode).hasClass(className) &&
80 !jQuery(node.parentNode).hasClass("nohighlight")) {
82 var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg");
84 span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
86 span = document.createElement("span");
87 span.className = className;
89 span.appendChild(document.createTextNode(val.substr(pos, text.length)));
90 node.parentNode.insertBefore(span, node.parentNode.insertBefore(
91 document.createTextNode(val.substr(pos + text.length)),
93 node.nodeValue = val.substr(0, pos);
95 var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
96 var bbox = node.parentElement.getBBox();
97 rect.x.baseVal.value = bbox.x;
98 rect.y.baseVal.value = bbox.y;
99 rect.width.baseVal.value = bbox.width;
100 rect.height.baseVal.value = bbox.height;
101 rect.setAttribute('class', className);
103 "parent": node.parentNode,
108 else if (!jQuery(node).is("button, select, textarea")) {
109 jQuery.each(node.childNodes, function() {
110 highlight(this, addItems);
115 var result = this.each(function() {
116 highlight(this, addItems);
118 for (var i = 0; i < addItems.length; ++i) {
119 jQuery(addItems[i].parent).before(addItems[i].target);
125 * backward compatibility for jQuery.browser
126 * This will be supported until firefox bug is fixed.
128 if (!jQuery.browser) {
129 jQuery.uaMatch = function(ua) {
130 ua = ua.toLowerCase();
132 var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
133 /(webkit)[ \/]([\w.]+)/.exec(ua) ||
134 /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
135 /(msie) ([\w.]+)/.exec(ua) ||
136 ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
140 browser: match[ 1 ] || "",
141 version: match[ 2 ] || "0"
145 jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;
149 * Small JavaScript module for the documentation.
151 var Documentation = {
154 this.fixFirefoxAnchorBug();
155 this.highlightSearchWords();
156 this.initIndexTable();
157 if (DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) {
158 this.initOnKeyListeners();
166 PLURAL_EXPR : function(n) { return n === 1 ? 0 : 1; },
169 // gettext and ngettext don't access this so that the functions
170 // can safely bound to a different name (_ = Documentation.gettext)
171 gettext : function(string) {
172 var translated = Documentation.TRANSLATIONS[string];
173 if (typeof translated === 'undefined')
175 return (typeof translated === 'string') ? translated : translated[0];
178 ngettext : function(singular, plural, n) {
179 var translated = Documentation.TRANSLATIONS[singular];
180 if (typeof translated === 'undefined')
181 return (n == 1) ? singular : plural;
182 return translated[Documentation.PLURALEXPR(n)];
185 addTranslations : function(catalog) {
186 for (var key in catalog.messages)
187 this.TRANSLATIONS[key] = catalog.messages[key];
188 this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')');
189 this.LOCALE = catalog.locale;
193 * add context elements like header anchor links
195 addContextElements : function() {
196 $('div[id] > :header:first').each(function() {
197 $('<a class="headerlink">\u00B6</a>').
198 attr('href', '#' + this.id).
199 attr('title', _('Permalink to this headline')).
202 $('dt[id]').each(function() {
203 $('<a class="headerlink">\u00B6</a>').
204 attr('href', '#' + this.id).
205 attr('title', _('Permalink to this definition')).
211 * workaround a firefox stupidity
212 * see: https://bugzilla.mozilla.org/show_bug.cgi?id=645075
214 fixFirefoxAnchorBug : function() {
215 if (document.location.hash && $.browser.mozilla)
216 window.setTimeout(function() {
217 document.location.href += '';
222 * highlight the search words provided in the url in the text
224 highlightSearchWords : function() {
225 var params = $.getQueryParameters();
226 var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
228 var body = $('div.body');
232 window.setTimeout(function() {
233 $.each(terms, function() {
234 body.highlightText(this.toLowerCase(), 'highlighted');
237 $('<p class="highlight-link"><a href="javascript:Documentation.' +
238 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>')
239 .appendTo($('#searchbox'));
244 * init the domain index toggle buttons
246 initIndexTable : function() {
247 var togglers = $('img.toggler').click(function() {
248 var src = $(this).attr('src');
249 var idnum = $(this).attr('id').substr(7);
250 $('tr.cg-' + idnum).toggle();
251 if (src.substr(-9) === 'minus.png')
252 $(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
254 $(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
255 }).css('display', '');
256 if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) {
262 * helper function to hide the search marks again
264 hideSearchWords : function() {
265 $('#searchbox .highlight-link').fadeOut(300);
266 $('span.highlighted').removeClass('highlighted');
270 * make the url absolute
272 makeURL : function(relativeURL) {
273 return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL;
277 * get the current relative url
279 getCurrentURL : function() {
280 var path = document.location.pathname;
281 var parts = path.split(/\//);
282 $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
286 var url = parts.join('/');
287 return path.substring(url.lastIndexOf('/') + 1, path.length - 1);
290 initOnKeyListeners: function() {
291 $(document).keydown(function(event) {
292 var activeElementType = document.activeElement.tagName;
293 // don't navigate when in search box, textarea, dropdown or button
294 if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
295 && activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey
296 && !event.shiftKey) {
297 switch (event.keyCode) {
299 var prevHref = $('link[rel="prev"]').prop('href');
301 window.location.href = prevHref;
306 var nextHref = $('link[rel="next"]').prop('href');
308 window.location.href = nextHref;
318 // quick alias for translations
319 _ = Documentation.gettext;
321 $(document).ready(function() {
322 Documentation.init();