Drop JavaScript Frameworks (#10028)

* Delete jQuery and underscore.js

* Move underscores.js setup to searchtools.js

* Update jQuery.url(en|de)code

* Update jQuery.getQueryParameters

* Firefox bug is no longer present

xref https://bugzilla.mozilla.org/show_bug.cgi?id=645075#c49

* Update jQuery.fn.highlightText

* Use enum instead of magic number

* Update test descriptions to remove obsolete jQuery reference

* Update Documentation.getCurrentURL

* Revert accidental fix of Documentation.getCurrentURL

* Update Documentation.initOnKeyListeners

* Update Documentation.hideSearchWords

* Update Documentation.highlightSearchWords

* Update Documentation.initDomainIndexTable

* Use arrow functions and const

* Replace $(document).ready

* Strict mode

* Move Documentation.hideSearchWords next to Documentation.highlightSearchWords

* Update translation functions in Documentation

* Replace $(document).ready in searchtools.js

* Update Scorer

* Update Search.hasIndex, Search.deferQuery, Search.stopPulse

* Prefer window.location

* Update Search.init

* Update Search.loadIndex

* Update Search.setIndex

* Update Search.startPulse

* Add _escapeRegExp

* Update Search.makeSearchSummary

* Update Search.htmlToText

* Update Search.performSearch

* Factor out _displayNextItem

* Update Search.query

* Update Search.performObjectSearch

* Update Search.performTermsSearch

* Remove underscores.js setup

* Use Sets

* Update test configuration

* Fix test failures

* Drop unused make/get URL functions

* Strict mode in searchtools.js

* Remove outmoded check for jQuery and underscore.js

* Ran prettier

prettier --print-width 120 --no-semi --quote-props as-needed --no-bracket-spacing --arrow-parens avoid --write sphinx/themes/basic/static

* Remove more references to jQuery and underscore.js

* Remove jQuery and underscore.js licences

* Update classic theme for no jQuery

* Update all other themes for no jQuery

* Restore jQuery & underscores.js to Sphinx themes

Enables a more gradual deprecation

* Added deprecation note to CHANGES

* Run prettier with defaults

* Update deprecation message to include extensions, note that sources must be copied

* oops

* Address Pradyun's feedback

* Forgot this one

* `let` doesn't work, as it is scoped to the block...

* Remove missed jQuery in sphinx13 theme
This commit is contained in:
Adam Turner 2022-01-30 19:27:12 +00:00 committed by GitHub
parent 444dfc50aa
commit 3b01fbe2ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 2772 additions and 1502 deletions

View File

@ -6,14 +6,15 @@ jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
node-version: 10.7 node-version: 16
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Use Node.js ${{ env.node-version }} - name: Use Node.js ${{ env.node-version }}
uses: actions/setup-node@v1 uses: actions/setup-node@v2
with: with:
node-version: ${{ env.node-version }} node-version: ${{ env.node-version }}
cache: "npm"
- run: npm install - run: npm install
- name: Run headless test - name: Run headless test
uses: GabrielBB/xvfb-action@v1 uses: GabrielBB/xvfb-action@v1

View File

@ -18,6 +18,7 @@ Other co-maintainers:
Other contributors, listed alphabetically, are: Other contributors, listed alphabetically, are:
* Adam Turner -- JavaScript improvements
* Alastair Houghton -- Apple Help builder * Alastair Houghton -- Apple Help builder
* Alexander Todorov -- inheritance_diagram tests and improvements * Alexander Todorov -- inheritance_diagram tests and improvements
* Andi Albrecht -- agogo theme * Andi Albrecht -- agogo theme

20
CHANGES
View File

@ -27,6 +27,23 @@ Incompatible changes
Deprecated Deprecated
---------- ----------
* #10028: jQuery and underscore.js will no longer be automatically injected into
themes from Sphinx 6.0. If you develop a theme or extension that uses the
``jQuery``, ``$``, or ``$u`` global objects, you need to update your
JavaScript or use the mitigation below.
To re-add jQuery and underscore.js, you will need to copy ``jquery.js`` and
``underscore.js`` from `the Sphinx repository`_ to your ``static`` directory,
and add the following to your ``layout.html``:
.. _the Sphinx repository: https://github.com/sphinx-doc/sphinx/tree/v4.3.2/sphinx/themes/basic/static
.. code-block:: html+jinja
{%- block scripts %}
<script src="{{ pathto('_static/jquery.js', resource=True) }}"></script>
<script src="{{ pathto('_static/underscore.js', resource=True) }}"></script>
{{ super() }}
{%- endblock %}
* setuptools integration. The ``build_sphinx`` sub-command for setup.py is * setuptools integration. The ``build_sphinx`` sub-command for setup.py is
marked as deprecated to follow the policy of setuptools team. marked as deprecated to follow the policy of setuptools team.
* The ``locale`` argument of ``sphinx.util.i18n:babel_format_date()`` becomes * The ``locale`` argument of ``sphinx.util.i18n:babel_format_date()`` becomes
@ -41,6 +58,9 @@ Features added
* #9075: autodoc: The default value of :confval:`autodoc_typehints_format` is * #9075: autodoc: The default value of :confval:`autodoc_typehints_format` is
changed to ``'smart'``. It will suppress the leading module names of changed to ``'smart'``. It will suppress the leading module names of
typehints (ex. ``io.StringIO`` -> ``StringIO``). typehints (ex. ``io.StringIO`` -> ``StringIO``).
* #10028: Removed internal usages of JavaScript frameworks (jQuery and
underscore.js) and modernised ``doctools.js`` and ``searchtools.js`` to
EMCAScript 2018.
Bugs fixed Bugs fixed
---------- ----------

View File

@ -14,7 +14,7 @@
<form action="https://groups.google.com/group/sphinx-users/boxsubscribe" <form action="https://groups.google.com/group/sphinx-users/boxsubscribe"
class="subscribeform"> class="subscribeform">
<input type="text" name="email" value="your@email" <input type="text" name="email" value="your@email"
onfocus="$(this).val('');" /> onfocus="this.value = ''" />
<input type="submit" name="sub" value="Subscribe" /> <input type="submit" name="sub" value="Subscribe" />
</form> </form>
</div> </div>

View File

@ -27,31 +27,28 @@
</style> </style>
<script> <script>
// intelligent scrolling of the sidebar content // intelligent scrolling of the sidebar content
$(window).scroll(function() { window.onscroll = () => {
var sb = $('.sphinxsidebarwrapper'); const sb = document.getElementsByClassName('sphinxsidebarwrapper')[0]
var win = $(window); const sbh = sb.offsetHeight
var sbh = sb.height(); const offset = document.getElementsByClassName('sphinxsidebar')[0].offsetTop;
var offset = $('.sphinxsidebar').position()['top']; const wintop = window.scrollTop;
var wintop = win.scrollTop(); const winbot = wintop + window.offsetHeight
var winbot = wintop + win.innerHeight(); const curtop = sb.offsetTop;
var curtop = sb.position()['top']; const curbot = curtop + sbh;
var curbot = curtop + sbh;
// does sidebar fit in window? // does sidebar fit in window?
if (sbh < win.innerHeight()) { if (sbh < window.offsetHeight) {
// yes: easy case -- always keep at the top // yes: easy case -- always keep at the top
sb.css('top', $u.min([$u.max([0, wintop - offset - 10]), sb.style.top = Math.min(Math.max(0, wintop - offset - 10), window.innerHeight - sbh - 200)
$(document).height() - sbh - 200]));
} else { } else {
// no: only scroll if top/bottom edge of sidebar is at // no: only scroll if top/bottom edge of sidebar is at
// top/bottom edge of window // top/bottom edge of window
if (curtop > wintop && curbot > winbot) { if (curtop > wintop && curbot > winbot) {
sb.css('top', $u.max([wintop - offset - 10, 0])); sb.style.top = Math.max(wintop - offset - 10, 0)
} else if (curtop < wintop && curbot < winbot) { } else if (curtop < wintop && curbot < winbot) {
sb.css('top', $u.min([winbot - sbh - offset - 20, sb.style.top = Math.min(winbot - sbh - offset - 20, window.innerHeight - sbh - 200)
$(document).height() - sbh - 200])); }
} }
} }
});
</script> </script>
{%- endif %} {%- endif %}
{% endblock %} {% endblock %}

View File

@ -15,8 +15,6 @@ module.exports = function(config) {
// list of files / patterns to load in the browser // list of files / patterns to load in the browser
files: [ files: [
'sphinx/themes/basic/static/underscore.js',
'sphinx/themes/basic/static/jquery.js',
'sphinx/themes/basic/static/doctools.js', 'sphinx/themes/basic/static/doctools.js',
'sphinx/themes/basic/static/searchtools.js', 'sphinx/themes/basic/static/searchtools.js',
'tests/js/*.js' 'tests/js/*.js'
@ -59,7 +57,7 @@ module.exports = function(config) {
// start these browsers // start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'Firefox'], browsers: ["Firefox"],
// Continuous Integration mode // Continuous Integration mode

2486
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -12,9 +12,8 @@
}, },
"devDependencies": { "devDependencies": {
"jasmine-core": "^3.4.0", "jasmine-core": "^3.4.0",
"karma": "^4.0.0", "karma": "^6.3.9",
"karma-chrome-launcher": "^3.0.0", "karma-firefox-launcher": "^2.0.0",
"karma-firefox-launcher": "^1.1.0", "karma-jasmine": "^4.0.0"
"karma-jasmine": "^2.0.0"
} }
} }

View File

@ -315,8 +315,11 @@ class StandaloneHTMLBuilder(Builder):
self.script_files = [] self.script_files = []
self.add_js_file('documentation_options.js', id="documentation_options", self.add_js_file('documentation_options.js', id="documentation_options",
data_url_root='', priority=200) data_url_root='', priority=200)
# Remove frameworks and compatability module below in Sphinx 6.0
# xref RemovedInSphinx60Warning
self.add_js_file('jquery.js', priority=200) self.add_js_file('jquery.js', priority=200)
self.add_js_file('underscore.js', priority=200) self.add_js_file('underscore.js', priority=200)
self.add_js_file('_sphinx_javascript_frameworks_compat.js', priority=200)
self.add_js_file('doctools.js', priority=200) self.add_js_file('doctools.js', priority=200)
for filename, attrs in self.app.registry.js_files: for filename, attrs in self.app.registry.js_files:

View File

@ -81,6 +81,7 @@
{%- endblock %} {%- endblock %}
{%- endif %} {%- endif %}
</div> </div>
{%- block sidebarextra %}{%- endblock %}
</div> </div>
{%- endif %} {%- endif %}
{%- endmacro %} {%- endmacro %}

View File

@ -17,5 +17,5 @@
</form> </form>
</div> </div>
</div> </div>
<script>$('#searchbox').show(0);</script> <script>document.getElementById('searchbox').style.display = "block"</script>
{%- endif %} {%- endif %}

View File

@ -0,0 +1,134 @@
/*
* _sphinx_javascript_frameworks_compat.js
* ~~~~~~~~~~
*
* Compatability shim for jQuery and underscores.js.
*
* WILL BE REMOVED IN Sphinx 6.0
* xref RemovedInSphinx60Warning
*
*/
/**
* select a different prefix for underscore
*/
$u = _.noConflict();
/**
* small helper function to urldecode strings
*
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
*/
jQuery.urldecode = function(x) {
if (!x) {
return x
}
return decodeURIComponent(x.replace(/\+/g, ' '));
};
/**
* small helper function to urlencode strings
*/
jQuery.urlencode = encodeURIComponent;
/**
* This function returns the parsed url parameters of the
* current request. Multiple values per key are supported,
* it will always return arrays of strings for the value parts.
*/
jQuery.getQueryParameters = function(s) {
if (typeof s === 'undefined')
s = document.location.search;
var parts = s.substr(s.indexOf('?') + 1).split('&');
var result = {};
for (var i = 0; i < parts.length; i++) {
var tmp = parts[i].split('=', 2);
var key = jQuery.urldecode(tmp[0]);
var value = jQuery.urldecode(tmp[1]);
if (key in result)
result[key].push(value);
else
result[key] = [value];
}
return result;
};
/**
* highlight a given string on a jquery object by wrapping it in
* span elements with the given class name.
*/
jQuery.fn.highlightText = function(text, className) {
function highlight(node, addItems) {
if (node.nodeType === 3) {
var val = node.nodeValue;
var pos = val.toLowerCase().indexOf(text);
if (pos >= 0 &&
!jQuery(node.parentNode).hasClass(className) &&
!jQuery(node.parentNode).hasClass("nohighlight")) {
var span;
var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg");
if (isInSVG) {
span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
} else {
span = document.createElement("span");
span.className = className;
}
span.appendChild(document.createTextNode(val.substr(pos, text.length)));
node.parentNode.insertBefore(span, node.parentNode.insertBefore(
document.createTextNode(val.substr(pos + text.length)),
node.nextSibling));
node.nodeValue = val.substr(0, pos);
if (isInSVG) {
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
var bbox = node.parentElement.getBBox();
rect.x.baseVal.value = bbox.x;
rect.y.baseVal.value = bbox.y;
rect.width.baseVal.value = bbox.width;
rect.height.baseVal.value = bbox.height;
rect.setAttribute('class', className);
addItems.push({
"parent": node.parentNode,
"target": rect});
}
}
}
else if (!jQuery(node).is("button, select, textarea")) {
jQuery.each(node.childNodes, function() {
highlight(this, addItems);
});
}
}
var addItems = [];
var result = this.each(function() {
highlight(this, addItems);
});
for (var i = 0; i < addItems.length; ++i) {
jQuery(addItems[i].parent).before(addItems[i].target);
}
return result;
};
/*
* backward compatibility for jQuery.browser
* This will be supported until firefox bug is fixed.
*/
if (!jQuery.browser) {
jQuery.uaMatch = function(ua) {
ua = ua.toLowerCase();
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
[];
return {
browser: match[ 1 ] || "",
version: match[ 2 ] || "0"
};
};
jQuery.browser = {};
jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;
}

View File

@ -2,325 +2,224 @@
* doctools.js * doctools.js
* ~~~~~~~~~~~ * ~~~~~~~~~~~
* *
* Sphinx JavaScript utilities for all documentation. * Base JavaScript utilities for all Sphinx HTML documentation.
* *
* :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS. * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
* :license: BSD, see LICENSE for details. * :license: BSD, see LICENSE for details.
* *
*/ */
"use strict";
/** const _ready = (callback) => {
* select a different prefix for underscore if (document.readyState !== "loading") {
*/ callback();
$u = _.noConflict(); } else {
document.addEventListener("DOMContentLoaded", callback);
/**
* make the code below compatible with browsers without
* an installed firebug like debugger
if (!window.console || !console.firebug) {
var names = ["log", "debug", "info", "warn", "error", "assert", "dir",
"dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace",
"profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {};
}
*/
/**
* small helper function to urldecode strings
*
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
*/
jQuery.urldecode = function(x) {
if (!x) {
return x
} }
return decodeURIComponent(x.replace(/\+/g, ' '));
}; };
/** /**
* small helper function to urlencode strings * highlight a given string on a node by wrapping it in
*/
jQuery.urlencode = encodeURIComponent;
/**
* This function returns the parsed url parameters of the
* current request. Multiple values per key are supported,
* it will always return arrays of strings for the value parts.
*/
jQuery.getQueryParameters = function(s) {
if (typeof s === 'undefined')
s = document.location.search;
var parts = s.substr(s.indexOf('?') + 1).split('&');
var result = {};
for (var i = 0; i < parts.length; i++) {
var tmp = parts[i].split('=', 2);
var key = jQuery.urldecode(tmp[0]);
var value = jQuery.urldecode(tmp[1]);
if (key in result)
result[key].push(value);
else
result[key] = [value];
}
return result;
};
/**
* highlight a given string on a jquery object by wrapping it in
* span elements with the given class name. * span elements with the given class name.
*/ */
jQuery.fn.highlightText = function(text, className) { const _highlight = (node, addItems, text, className) => {
function highlight(node, addItems) { if (node.nodeType === Node.TEXT_NODE) {
if (node.nodeType === 3) { const val = node.nodeValue;
var val = node.nodeValue; const parent = node.parentNode;
var pos = val.toLowerCase().indexOf(text); const pos = val.toLowerCase().indexOf(text);
if (pos >= 0 && if (
!jQuery(node.parentNode).hasClass(className) && pos >= 0 &&
!jQuery(node.parentNode).hasClass("nohighlight")) { !parent.classList.contains(className) &&
var span; !parent.classList.contains("nohighlight")
var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg"); ) {
let span;
const closestNode = parent.closest("body, svg, foreignObject");
const isInSVG = closestNode && closestNode.matches("svg");
if (isInSVG) { if (isInSVG) {
span = document.createElementNS("http://www.w3.org/2000/svg", "tspan"); span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
} else { } else {
span = document.createElement("span"); span = document.createElement("span");
span.className = className; span.classList.add(className);
} }
span.appendChild(document.createTextNode(val.substr(pos, text.length))); span.appendChild(document.createTextNode(val.substr(pos, text.length)));
node.parentNode.insertBefore(span, node.parentNode.insertBefore( parent.insertBefore(
span,
parent.insertBefore(
document.createTextNode(val.substr(pos + text.length)), document.createTextNode(val.substr(pos + text.length)),
node.nextSibling)); node.nextSibling
)
);
node.nodeValue = val.substr(0, pos); node.nodeValue = val.substr(0, pos);
if (isInSVG) { if (isInSVG) {
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); const rect = document.createElementNS(
var bbox = node.parentElement.getBBox(); "http://www.w3.org/2000/svg",
"rect"
);
const bbox = parent.getBBox();
rect.x.baseVal.value = bbox.x; rect.x.baseVal.value = bbox.x;
rect.y.baseVal.value = bbox.y; rect.y.baseVal.value = bbox.y;
rect.width.baseVal.value = bbox.width; rect.width.baseVal.value = bbox.width;
rect.height.baseVal.value = bbox.height; rect.height.baseVal.value = bbox.height;
rect.setAttribute('class', className); rect.setAttribute("class", className);
addItems.push({ addItems.push({ parent: parent, target: rect });
"parent": node.parentNode,
"target": rect});
} }
} }
} else if (node.matches && !node.matches("button, select, textarea")) {
node.childNodes.forEach((el) => _highlight(el, addItems, text, className));
} }
else if (!jQuery(node).is("button, select, textarea")) {
jQuery.each(node.childNodes, function() {
highlight(this, addItems);
});
}
}
var addItems = [];
var result = this.each(function() {
highlight(this, addItems);
});
for (var i = 0; i < addItems.length; ++i) {
jQuery(addItems[i].parent).before(addItems[i].target);
}
return result;
}; };
const _highlightText = (thisNode, text, className) => {
/* let addItems = [];
* backward compatibility for jQuery.browser _highlight(thisNode, addItems, text, className);
* This will be supported until firefox bug is fixed. addItems.forEach((obj) =>
*/ obj.parent.insertAdjacentElement("beforebegin", obj.target)
if (!jQuery.browser) { );
jQuery.uaMatch = function(ua) { };
ua = ua.toLowerCase();
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
[];
return {
browser: match[ 1 ] || "",
version: match[ 2 ] || "0"
};
};
jQuery.browser = {};
jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;
}
/** /**
* Small JavaScript module for the documentation. * Small JavaScript module for the documentation.
*/ */
var Documentation = { const Documentation = {
init: () => {
init : function() { Documentation.highlightSearchWords();
this.fixFirefoxAnchorBug(); Documentation.initDomainIndexTable();
this.highlightSearchWords(); if (DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
this.initIndexTable(); Documentation.initOnKeyListeners();
if (DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) {
this.initOnKeyListeners();
}
}, },
/** /**
* i18n support * i18n support
*/ */
TRANSLATIONS : {}, TRANSLATIONS: {},
PLURAL_EXPR : function(n) { return n === 1 ? 0 : 1; }, PLURAL_EXPR: (n) => (n === 1 ? 0 : 1),
LOCALE : 'unknown', LOCALE: "unknown",
// gettext and ngettext don't access this so that the functions // gettext and ngettext don't access this so that the functions
// can safely bound to a different name (_ = Documentation.gettext) // can safely bound to a different name (_ = Documentation.gettext)
gettext : function(string) { gettext: (string) => {
var translated = Documentation.TRANSLATIONS[string]; const translated = Documentation.TRANSLATIONS[string];
if (typeof translated === 'undefined') switch (typeof translated) {
return string; case "undefined":
return (typeof translated === 'string') ? translated : translated[0]; return string; // no translation
case "string":
return translated; // translation exists
default:
return translated[0]; // (singular, plural) translation tuple exists
}
}, },
ngettext : function(singular, plural, n) { ngettext: (singular, plural, n) => {
var translated = Documentation.TRANSLATIONS[singular]; const translated = Documentation.TRANSLATIONS[singular];
if (typeof translated === 'undefined') if (typeof translated !== "undefined")
return (n == 1) ? singular : plural; return translated[Documentation.PLURAL_EXPR(n)];
return translated[Documentation.PLURALEXPR(n)]; return n === 1 ? singular : plural;
}, },
addTranslations : function(catalog) { addTranslations: (catalog) => {
for (var key in catalog.messages) Object.assign(Documentation.TRANSLATIONS, catalog.messages);
this.TRANSLATIONS[key] = catalog.messages[key]; Documentation.PLURAL_EXPR = new Function(
this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')'); "n",
this.LOCALE = catalog.locale; `return (${catalog.plural_expr})`
}, );
Documentation.LOCALE = catalog.locale;
/**
* add context elements like header anchor links
*/
addContextElements : function() {
$('div[id] > :header:first').each(function() {
$('<a class="headerlink">\u00B6</a>').
attr('href', '#' + this.id).
attr('title', _('Permalink to this heading')).
appendTo(this);
});
$('dt[id]').each(function() {
$('<a class="headerlink">\u00B6</a>').
attr('href', '#' + this.id).
attr('title', _('Permalink to this definition')).
appendTo(this);
});
},
/**
* workaround a firefox stupidity
* see: https://bugzilla.mozilla.org/show_bug.cgi?id=645075
*/
fixFirefoxAnchorBug : function() {
if (document.location.hash && $.browser.mozilla)
window.setTimeout(function() {
document.location.href += '';
}, 10);
}, },
/** /**
* highlight the search words provided in the url in the text * highlight the search words provided in the url in the text
*/ */
highlightSearchWords : function() { highlightSearchWords: () => {
var params = $.getQueryParameters(); const highlight = new URLSearchParams(window.location.search).get(
var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : []; "highlight"
if (terms.length) { );
var body = $('div.body'); const terms = highlight ? highlight.split(/\s+/) : [];
if (!body.length) { if (terms.length === 0) return; // nothing to do
body = $('body');
}
window.setTimeout(function() {
$.each(terms, function() {
body.highlightText(this.toLowerCase(), 'highlighted');
});
}, 10);
$('<p class="highlight-link"><a href="javascript:Documentation.' +
'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>')
.appendTo($('#searchbox'));
}
},
/** let body = document.querySelectorAll("div.body");
* init the domain index toggle buttons if (!body.length) body = document.querySelector("body");
*/ window.setTimeout(() => {
initIndexTable : function() { terms.forEach((term) =>
var togglers = $('img.toggler').click(function() { _highlightText(body, term.toLowerCase(), "highlighted")
var src = $(this).attr('src'); );
var idnum = $(this).attr('id').substr(7); }, 10);
$('tr.cg-' + idnum).toggle();
if (src.substr(-9) === 'minus.png') const searchBox = document.getElementById("searchbox");
$(this).attr('src', src.substr(0, src.length-9) + 'plus.png'); if (searchBox === null) return;
else searchBox.appendChild(
$(this).attr('src', src.substr(0, src.length-8) + 'minus.png'); document
}).css('display', ''); .createRange()
if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) { .createContextualFragment(
togglers.click(); '<p class="highlight-link">' +
} '<a href="javascript:Documentation.hideSearchWords()">' +
Documentation.gettext("Hide Search Matches") +
"</a></p>"
)
);
}, },
/** /**
* helper function to hide the search marks again * helper function to hide the search marks again
*/ */
hideSearchWords : function() { hideSearchWords: () => {
$('#searchbox .highlight-link').fadeOut(300); document
$('span.highlighted').removeClass('highlighted'); .querySelectorAll("#searchbox .highlight-link")
var url = new URL(window.location); .forEach((el) => el.remove());
url.searchParams.delete('highlight'); document
.querySelectorAll("span.highlighted")
.forEach((el) => el.classList.remove("highlighted"));
new URLSearchParams(window.location.search).delete('highlight')
window.history.replaceState({}, '', url); window.history.replaceState({}, '', url);
}, },
/** /**
* make the url absolute * Initialise the domain index toggle buttons
*/ */
makeURL : function(relativeURL) { initDomainIndexTable: () => {
return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL; const toggler = (el) => {
const idNumber = el.id.substr(7);
const toggledRows = document.querySelectorAll(`tr.cg-${idNumber}`);
if (el.src.substr(-9) === "minus.png") {
el.src = `${el.src.substr(0, el.src.length - 9)}plus.png`;
toggledRows.forEach((el) => (el.style.display = "none"));
} else {
el.src = `${el.src.substr(0, el.src.length - 8)}minus.png`;
toggledRows.forEach((el) => (el.style.display = ""));
}
};
const togglerElements = document.querySelectorAll("img.toggler");
togglerElements.forEach((el) =>
el.addEventListener("click", (event) => toggler(event.currentTarget))
);
togglerElements.forEach((el) => (el.style.display = ""));
if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) togglerElements.forEach(toggler);
}, },
/** initOnKeyListeners: () => {
* get the current relative url const blacklistedElements = new Set([
*/ "TEXTAREA",
getCurrentURL : function() { "INPUT",
var path = document.location.pathname; "SELECT",
var parts = path.split(/\//); "BUTTON",
$.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() { ]);
if (this === '..') document.addEventListener("keydown", (event) => {
parts.pop(); if (blacklistedElements.has(document.activeElement.tagName)) return; // bail for input elements
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)
return; // bail with special keys
if (event.key === "ArrowLeft") {
const prevLink = document.querySelector('link[rel="prev"]');
if (prevLink && prevLink.href) window.location.href = prevLink.href;
} else if (event.key === "ArrowRight") {
const nextLink = document.querySelector('link[rel="next"]').href;
if (nextLink && nextLink.href) window.location.href = nextLink.href;
}
}); });
var url = parts.join('/');
return path.substring(url.lastIndexOf('/') + 1, path.length - 1);
}, },
initOnKeyListeners: function() {
$(document).keydown(function(event) {
var activeElementType = document.activeElement.tagName;
// don't navigate when in search box, textarea, dropdown or button
if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
&& activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey
&& !event.shiftKey) {
switch (event.keyCode) {
case 37: // left
var prevHref = $('link[rel="prev"]').prop('href');
if (prevHref) {
window.location.href = prevHref;
return false;
}
break;
case 39: // right
var nextHref = $('link[rel="next"]').prop('href');
if (nextHref) {
window.location.href = nextHref;
return false;
}
break;
}
}
});
}
}; };
// quick alias for translations // quick alias for translations
_ = Documentation.gettext; const _ = Documentation.gettext;
$(document).ready(function() { _ready(Documentation.init);
Documentation.init();
});

View File

@ -8,18 +8,20 @@
* :license: BSD, see LICENSE for details. * :license: BSD, see LICENSE for details.
* *
*/ */
"use strict";
if (!Scorer) { /**
/**
* Simple result scoring code. * Simple result scoring code.
*/ */
if (!Scorer) {
var Scorer = { var Scorer = {
// Implement the following function to further tweak the score for each result // Implement the following function to further tweak the score for each result
// The function takes a result array [filename, title, anchor, descr, score] // The function takes a result array [docname, title, anchor, descr, score, filename]
// and returns the new score. // and returns the new score.
/* /*
score: function(result) { score: result => {
return result[4]; const [docname, title, anchor, descr, score, filename] = result
return score
}, },
*/ */
@ -28,9 +30,11 @@ if (!Scorer) {
// or matches in the last dotted part of the object name // or matches in the last dotted part of the object name
objPartialMatch: 6, objPartialMatch: 6,
// Additive scores depending on the priority of the object // Additive scores depending on the priority of the object
objPrio: {0: 15, // used to be importantResults objPrio: {
0: 15, // used to be importantResults
1: 5, // used to be objectResults 1: 5, // used to be objectResults
2: -5}, // used to be unimportantResults 2: -5, // used to be unimportantResults
},
// Used when the priority is not in the mapping. // Used when the priority is not in the mapping.
objPrioDefault: 0, objPrioDefault: 0,
@ -39,456 +43,431 @@ if (!Scorer) {
partialTitle: 7, partialTitle: 7,
// query found in terms // query found in terms
term: 5, term: 5,
partialTerm: 2 partialTerm: 2,
}; };
} }
if (!splitQuery) { const _removeChildren = (element) => {
function splitQuery(query) { while (element.lastChild) element.removeChild(element.lastChild);
return query.split(/\s+/); };
/**
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
*/
const _escapeRegExp = (string) =>
string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
const _displayItem = (item, highlightTerms, searchTerms) => {
const docBuilder = DOCUMENTATION_OPTIONS.BUILDER;
const docUrlRoot = DOCUMENTATION_OPTIONS.URL_ROOT;
const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX;
const docLinkSuffix = DOCUMENTATION_OPTIONS.LINK_SUFFIX;
const docHasSource = DOCUMENTATION_OPTIONS.HAS_SOURCE;
const [docName, title, anchor, descr] = item;
let listItem = document.createElement("li");
let requestUrl;
let linkUrl;
if (docBuilder === "dirhtml") {
// dirhtml builder
let dirname = docName + "/";
if (dirname.match(/\/index\/$/))
dirname = dirname.substring(0, dirname.length - 6);
else if (dirname === "index/") dirname = "";
requestUrl = docUrlRoot + dirname;
linkUrl = requestUrl;
} else {
// normal html builders
requestUrl = docUrlRoot + docName + docFileSuffix;
linkUrl = docName + docLinkSuffix;
} }
} const params = new URLSearchParams();
params.set("highlight", [...highlightTerms].join(" "));
let linkEl = listItem.appendChild(document.createElement("a"));
linkEl.href = linkUrl + "?" + params.toString() + anchor;
linkEl.innerHTML = title;
if (descr)
listItem.appendChild(document.createElement("span")).innerText =
" (" + descr + ")";
else if (docHasSource)
fetch(requestUrl)
.then((responseData) => responseData.text())
.then((data) => {
if (data)
listItem.appendChild(
Search.makeSearchSummary(data, searchTerms, highlightTerms)
);
});
Search.output.appendChild(listItem);
};
const _finishSearch = (resultCount) => {
Search.stopPulse();
Search.title.innerText = _("Search Results");
if (!resultCount)
Search.status.innerText = Documentation.gettext(
"Your search did not match any documents. Please make sure that all words are spelled correctly and that you've selected enough categories."
);
else
Search.status.innerText = _(
`Search finished, found ${resultCount} page(s) matching the search query.`
);
};
const _displayNextItem = (
results,
resultCount,
highlightTerms,
searchTerms
) => {
// results left, load the summary and display it
// this is intended to be dynamic (don't sub resultsCount)
if (results.length) {
_displayItem(results.pop(), highlightTerms, searchTerms);
setTimeout(
() => _displayNextItem(results, resultCount, highlightTerms, searchTerms),
5
);
}
// search finished, update title and status message
else _finishSearch(resultCount);
};
/** /**
* Search Module * Search Module
*/ */
var Search = { const Search = {
_index: null,
_queued_query: null,
_pulse_status: -1,
_index : null, htmlToText: (htmlString) => {
_queued_query : null, const htmlElement = document
_pulse_status : -1, .createRange()
.createContextualFragment(htmlString);
htmlToText : function(htmlString) { _removeChildren(htmlElement.querySelectorAll(".headerlink"));
var virtualDocument = document.implementation.createHTMLDocument('virtual'); const docContent = htmlElement.querySelector('[role="main"]');
var htmlElement = $(htmlString, virtualDocument); if (docContent !== undefined) return docContent.textContent;
htmlElement.find('.headerlink').remove(); console.warn(
docContent = htmlElement.find('[role=main]')[0]; "Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template."
if(docContent === undefined) { );
console.warn("Content block not found. Sphinx search tries to obtain it " +
"via '[role=main]'. Could you check your theme or template.");
return ""; return "";
}
return docContent.textContent || docContent.innerText;
}, },
init : function() { init: () => {
var params = $.getQueryParameters(); const query = new URLSearchParams(window.location.search).get("q");
if (params.q) { document
var query = params.q[0]; .querySelectorAll('input[name="q"]')
$('input[name="q"]')[0].value = query; .forEach((el) => (el.value = query));
this.performSearch(query); if (query) Search.performSearch(query);
},
loadIndex: (url) =>
(document.body.appendChild(document.createElement("script")).src = url),
setIndex: (index) => {
Search._index = index;
if (Search._queued_query !== null) {
Search._queued_query = null;
Search.query(Search._queued_query);
} }
}, },
loadIndex : function(url) { hasIndex: () => Search._index !== null,
$.ajax({type: "GET", url: url, data: null,
dataType: "script", cache: true,
complete: function(jqxhr, textstatus) {
if (textstatus != "success") {
document.getElementById("searchindexloader").src = url;
}
}});
},
setIndex : function(index) { deferQuery: (query) => (Search._queued_query = query),
var q;
this._index = index;
if ((q = this._queued_query) !== null) {
this._queued_query = null;
Search.query(q);
}
},
hasIndex : function() { stopPulse: () => (Search._pulse_status = -1),
return this._index !== null;
},
deferQuery : function(query) { startPulse: () => {
this._queued_query = query; if (Search._pulse_status >= 0) return;
},
stopPulse : function() { const pulse = () => {
this._pulse_status = 0;
},
startPulse : function() {
if (this._pulse_status >= 0)
return;
function pulse() {
var i;
Search._pulse_status = (Search._pulse_status + 1) % 4; Search._pulse_status = (Search._pulse_status + 1) % 4;
var dotString = ''; Search.dots.innerText = ".".repeat(Search._pulse_status);
for (i = 0; i < Search._pulse_status; i++) if (Search._pulse_status >= 0) window.setTimeout(pulse, 500);
dotString += '.'; };
Search.dots.text(dotString);
if (Search._pulse_status > -1)
window.setTimeout(pulse, 500);
}
pulse(); pulse();
}, },
/** /**
* perform a search for something (or wait until index is loaded) * perform a search for something (or wait until index is loaded)
*/ */
performSearch : function(query) { performSearch: (query) => {
// create the required interface elements // create the required interface elements
this.out = $('#search-results'); const searchText = document.createElement("h2");
this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out); searchText.textContent = _("Searching");
this.dots = $('<span></span>').appendTo(this.title); const searchSummary = document.createElement("p");
this.status = $('<p class="search-summary">&nbsp;</p>').appendTo(this.out); searchSummary.classList.add("search-summary");
this.output = $('<ul class="search"/>').appendTo(this.out); searchSummary.innerText = "";
const searchList = document.createElement("ul");
searchList.classList.add("search");
$('#search-progress').text(_('Preparing search...')); const out = document.getElementById("search-results");
this.startPulse(); Search.title = out.appendChild(searchText);
Search.dots = Search.title.appendChild(document.createElement("span"));
Search.status = out.appendChild(searchSummary);
Search.output = out.appendChild(searchList);
document.getElementById("search-progress").innerText = _(
"Preparing search..."
);
Search.startPulse();
// index already loaded, the browser was quick! // index already loaded, the browser was quick!
if (this.hasIndex()) if (Search.hasIndex()) Search.query(query);
this.query(query); else Search.deferQuery(query);
else
this.deferQuery(query);
}, },
/** /**
* execute search (requires search index to be loaded) * execute search (requires search index to be loaded)
*/ */
query : function(query) { query: (query) => {
var i; // stem the search terms and add them to the correct list
const stemmer = new Stemmer();
const searchTerms = new Set();
const excludedTerms = new Set();
const highlightTerms = new Set();
const objectTerms = new Set(query.toLowerCase().trim().split(/\s+/));
query
.trim()
.split(/\s+/)
.forEach((queryTerm) => {
const queryTermLower = queryTerm.toLowerCase();
// stem the searchterms and add them to the correct list // maybe skip this "word"
var stemmer = new Stemmer(); // stopwords array is from language_data.js
var searchterms = []; if (
var excluded = []; stopwords.indexOf(queryTermLower) !== -1 ||
var hlterms = []; queryTerm.match(/^\d+$/)
var tmp = splitQuery(query); )
var objectterms = []; return;
for (i = 0; i < tmp.length; i++) {
if (tmp[i] !== "") {
objectterms.push(tmp[i].toLowerCase());
}
if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i] === "") {
// skip this "word"
continue;
}
// stem the word // stem the word
var word = stemmer.stemWord(tmp[i].toLowerCase()); let word = stemmer.stemWord(queryTermLower);
// prevent stemmer from cutting word smaller than two chars // prevent stemmer from cutting word smaller than two chars
if(word.length < 3 && tmp[i].length >= 3) { if (word.length < 3 && queryTerm.length >= 3) {
word = tmp[i]; word = queryTerm;
} }
var toAppend;
// select the correct list // select the correct list
if (word[0] == '-') { if (word[0] === "-") excludedTerms.add(word.substr(1));
toAppend = excluded;
word = word.substr(1);
}
else { else {
toAppend = searchterms; searchTerms.add(word);
hlterms.push(tmp[i].toLowerCase()); highlightTerms.add(queryTermLower);
} }
// only add if not already in the list });
if (!$u.contains(toAppend, word))
toAppend.push(word);
}
var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
// console.debug('SEARCH: searching for:'); // console.debug("SEARCH: searching for:");
// console.info('required: ', searchterms); // console.info("required: ", [...searchTerms]);
// console.info('excluded: ', excluded); // console.info("excluded: ", [...excludedTerms]);
// prepare search // array of [docname, title, anchor, descr, score, filename]
var terms = this._index.terms; let results = [];
var titleterms = this._index.titleterms; _removeChildren(document.getElementById("search-progress"));
// array of [filename, title, anchor, descr, score]
var results = [];
$('#search-progress').empty();
// lookup as object // lookup as object
for (i = 0; i < objectterms.length; i++) { objectTerms.forEach((term) =>
var others = [].concat(objectterms.slice(0, i), results.push(...Search.performObjectSearch(term, objectTerms))
objectterms.slice(i+1, objectterms.length)); );
results = results.concat(this.performObjectSearch(objectterms[i], others));
}
// lookup as search terms in fulltext // lookup as search terms in fulltext
results = results.concat(this.performTermsSearch(searchterms, excluded, terms, titleterms)); results.push(...Search.performTermsSearch(searchTerms, excludedTerms));
// let the scorer override scores with a custom scoring function // let the scorer override scores with a custom scoring function
if (Scorer.score) { if (Scorer.score) results.forEach((item) => (item[4] = Scorer.score(item)));
for (i = 0; i < results.length; i++)
results[i][4] = Scorer.score(results[i]);
}
// now sort the results by score (in opposite order of appearance, since the // now sort the results by score (in opposite order of appearance, since the
// display function below uses pop() to retrieve items) and then // display function below uses pop() to retrieve items) and then
// alphabetically // alphabetically
results.sort(function(a, b) { results.sort((a, b) => {
var left = a[4]; const leftScore = a[4];
var right = b[4]; const rightScore = b[4];
if (left > right) { if (leftScore === rightScore) {
return 1;
} else if (left < right) {
return -1;
} else {
// same score: sort alphabetically // same score: sort alphabetically
left = a[1].toLowerCase(); const leftTitle = a[1].toLowerCase();
right = b[1].toLowerCase(); const rightTitle = b[1].toLowerCase();
return (left > right) ? -1 : ((left < right) ? 1 : 0); if (leftTitle === rightTitle) return 0;
return leftTitle > rightTitle ? -1 : 1; // inverted is intentional
} }
return leftScore > rightScore ? 1 : -1;
}); });
// for debugging // for debugging
//Search.lastresults = results.slice(); // a copy //Search.lastresults = results.slice(); // a copy
//console.info('search results:', Search.lastresults); // console.info("search results:", Search.lastresults);
// print the results // print the results
var resultCount = results.length; _displayNextItem(results, results.length, highlightTerms, searchTerms);
function displayNextItem() {
// results left, load the summary and display it
if (results.length) {
var item = results.pop();
var listItem = $('<li></li>');
var requestUrl = "";
var linkUrl = "";
if (DOCUMENTATION_OPTIONS.BUILDER === 'dirhtml') {
// dirhtml builder
var dirname = item[0] + '/';
if (dirname.match(/\/index\/$/)) {
dirname = dirname.substring(0, dirname.length-6);
} else if (dirname == 'index/') {
dirname = '';
}
requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + dirname;
linkUrl = requestUrl;
} else {
// normal html builders
requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX;
linkUrl = item[0] + DOCUMENTATION_OPTIONS.LINK_SUFFIX;
}
listItem.append($('<a/>').attr('href',
linkUrl +
highlightstring + item[2]).html(item[1]));
if (item[3]) {
listItem.append($('<span> (' + item[3] + ')</span>'));
Search.output.append(listItem);
setTimeout(function() {
displayNextItem();
}, 5);
} else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
$.ajax({url: requestUrl,
dataType: "text",
complete: function(jqxhr, textstatus) {
var data = jqxhr.responseText;
if (data !== '' && data !== undefined) {
var summary = Search.makeSearchSummary(data, searchterms, hlterms);
if (summary) {
listItem.append(summary);
}
}
Search.output.append(listItem);
setTimeout(function() {
displayNextItem();
}, 5);
}});
} else {
// no source available, just display title
Search.output.append(listItem);
setTimeout(function() {
displayNextItem();
}, 5);
}
}
// search finished, update title and status message
else {
Search.stopPulse();
Search.title.text(_('Search Results'));
if (!resultCount)
Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.'));
else
Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
Search.status.fadeIn(500);
}
}
displayNextItem();
}, },
/** /**
* search for object names * search for object names
*/ */
performObjectSearch : function(object, otherterms) { performObjectSearch: (object, objectTerms) => {
var filenames = this._index.filenames; const filenames = Search._index.filenames;
var docnames = this._index.docnames; const docNames = Search._index.docnames;
var objects = this._index.objects; const objects = Search._index.objects;
var objnames = this._index.objnames; const objNames = Search._index.objnames;
var titles = this._index.titles; const titles = Search._index.titles;
var i; const results = [];
var results = [];
const objectSearchCallback = (prefix, name) => {
const fullname = (prefix ? prefix + "." : "") + name;
const fullnameLower = fullname.toLowerCase();
if (fullnameLower.indexOf(object) < 0) return;
let score = 0;
const parts = fullnameLower.split(".");
for (var prefix in objects) {
for (var iMatch = 0; iMatch != objects[prefix].length; ++iMatch) {
var match = objects[prefix][iMatch];
var name = match[4];
var fullname = (prefix ? prefix + '.' : '') + name;
var fullnameLower = fullname.toLowerCase()
if (fullnameLower.indexOf(object) > -1) {
var score = 0;
var parts = fullnameLower.split('.');
// check for different match types: exact matches of full name or // check for different match types: exact matches of full name or
// "last name" (i.e. last dotted part) // "last name" (i.e. last dotted part)
if (fullnameLower == object || parts[parts.length - 1] == object) { if (fullnameLower === object || parts.slice(-1)[0] === object)
score += Scorer.objNameMatch; score += Scorer.objNameMatch;
// matches in last name else if (parts.slice(-1)[0].indexOf(object) > -1)
} else if (parts[parts.length - 1].indexOf(object) > -1) { score += Scorer.objPartialMatch; // matches in last name
score += Scorer.objPartialMatch;
} const match = objects[prefix][name];
var objname = objnames[match[1]][2]; const objName = objNames[match[1]][2];
var title = titles[match[0]]; const title = titles[match[0]];
// If more than one term searched for, we require other words to be // If more than one term searched for, we require other words to be
// found in the name/title/description // found in the name/title/description
if (otherterms.length > 0) { const otherTerms = new Set(objectTerms);
var haystack = (prefix + ' ' + name + ' ' + otherTerms.delete(object);
objname + ' ' + title).toLowerCase(); if (otherTerms.size > 0) {
var allfound = true; const haystack = `${prefix} ${name} ${objName} ${title}`.toLowerCase();
for (i = 0; i < otherterms.length; i++) { if (
if (haystack.indexOf(otherterms[i]) == -1) { [...otherTerms].some((otherTerm) => haystack.indexOf(otherTerm) < 0)
allfound = false; )
break; return;
} }
}
if (!allfound) {
continue;
}
}
var descr = objname + _(', in ') + title;
var anchor = match[3]; let anchor = match[3];
if (anchor === '') if (anchor === "") anchor = fullname;
anchor = fullname; else if (anchor === "-") anchor = objNames[match[1]][1] + "-" + fullname;
else if (anchor == '-')
anchor = objnames[match[1]][1] + '-' + fullname; const descr = objName + _(", in ") + title;
// add custom score for some objects according to scorer // add custom score for some objects according to scorer
if (Scorer.objPrio.hasOwnProperty(match[2])) { if (Scorer.objPrio.hasOwnProperty(match[2]))
score += Scorer.objPrio[match[2]]; score += Scorer.objPrio[match[2]];
} else { else score += Scorer.objPrioDefault;
score += Scorer.objPrioDefault;
}
results.push([docnames[match[0]], fullname, '#'+anchor, descr, score, filenames[match[0]]]);
}
}
}
results.push([
docNames[match[0]],
fullname,
"#" + anchor,
descr,
score,
filenames[match[0]],
]);
};
Object.keys(objects).forEach((prefix) =>
Object.keys(objects[prefix]).forEach((name) =>
objectSearchCallback(prefix, name)
)
);
return results; return results;
}, },
/**
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
*/
escapeRegExp : function(string) {
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
},
/** /**
* search for full-text terms in the index * search for full-text terms in the index
*/ */
performTermsSearch : function(searchterms, excluded, terms, titleterms) { performTermsSearch: (searchTerms, excludedTerms) => {
var docnames = this._index.docnames; // prepare search
var filenames = this._index.filenames; const terms = Search._index.terms;
var titles = this._index.titles; const titleTerms = Search._index.titleterms;
const docNames = Search._index.docnames;
const filenames = Search._index.filenames;
const titles = Search._index.titles;
var i, j, file; const scoreMap = new Map();
var fileMap = {}; const fileMap = new Map();
var scoreMap = {};
var results = [];
// perform the search on the required terms // perform the search on the required terms
for (i = 0; i < searchterms.length; i++) { searchTerms.forEach((word) => {
var word = searchterms[i]; const files = [];
var files = []; const arr = [
var _o = [ { files: terms[word], score: Scorer.term },
{files: terms[word], score: Scorer.term}, { files: titleTerms[word], score: Scorer.title },
{files: titleterms[word], score: Scorer.title}
]; ];
// add support for partial matches // add support for partial matches
if (word.length > 2) { if (word.length > 2) {
var word_regex = this.escapeRegExp(word); const escapedWord = _escapeRegExp(word);
for (var w in terms) { Object.keys(terms).forEach((term) => {
if (w.match(word_regex) && !terms[word]) { if (term.match(escapedWord) && !terms[word])
_o.push({files: terms[w], score: Scorer.partialTerm}) arr.push({ files: terms[term], score: Scorer.partialTerm });
} });
} Object.keys(titleTerms).forEach((term) => {
for (var w in titleterms) { if (term.match(escapedWord) && !titleTerms[word])
if (w.match(word_regex) && !titleterms[word]) { arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
_o.push({files: titleterms[w], score: Scorer.partialTitle}) });
}
}
} }
// no match but word was a required one // no match but word was a required one
if ($u.every(_o, function(o){return o.files === undefined;})) { if (arr.every((record) => record.files === undefined)) return;
break;
}
// found search word in contents // found search word in contents
$u.each(_o, function(o) { arr.forEach((record) => {
var _files = o.files; if (record.files === undefined) return;
if (_files === undefined)
return
if (_files.length === undefined) let recordFiles = record.files;
_files = [_files]; if (recordFiles.length === undefined) recordFiles = [recordFiles];
files = files.concat(_files); files.push(...recordFiles);
// set score for the word in each file to Scorer.term // set score for the word in each file
for (j = 0; j < _files.length; j++) { recordFiles.forEach((file) => {
file = _files[j]; if (!scoreMap.has(file)) scoreMap.set(file, {});
if (!(file in scoreMap)) scoreMap.get(file)[word] = record.score;
scoreMap[file] = {}; });
scoreMap[file][word] = o.score;
}
}); });
// create the mapping // create the mapping
for (j = 0; j < files.length; j++) { files.forEach((file) => {
file = files[j]; if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1)
if (file in fileMap && fileMap[file].indexOf(word) === -1) fileMap.get(file).push(word);
fileMap[file].push(word); else fileMap.set(file, [word]);
else });
fileMap[file] = [word]; });
}
}
// now check if the files don't contain excluded terms // now check if the files don't contain excluded terms
for (file in fileMap) { const results = [];
var valid = true; for (const [file, wordList] of fileMap) {
// check if all requirements are matched // check if all requirements are matched
var filteredTermCount = // as search terms with length < 3 are discarded: ignore
searchterms.filter(function(term){return term.length > 2}).length // as search terms with length < 3 are discarded
const filteredTermCount = [...searchTerms].filter(
(term) => term.length > 2
).length;
if ( if (
fileMap[file].length != searchterms.length && wordList.length !== searchTerms.size &&
fileMap[file].length != filteredTermCount wordList.length !== filteredTermCount
) continue; )
continue;
// ensure that none of the excluded terms is in the search result // ensure that none of the excluded terms is in the search result
for (i = 0; i < excluded.length; i++) { if (
if (terms[excluded[i]] == file || [...excludedTerms].some(
titleterms[excluded[i]] == file || (term) =>
$u.contains(terms[excluded[i]] || [], file) || terms[term] === file ||
$u.contains(titleterms[excluded[i]] || [], file)) { titleTerms[term] === file ||
valid = false; (terms[term] || []).includes(file) ||
(titleTerms[term] || []).includes(file)
)
)
break; break;
}
}
// if we have still a valid result we can add it to the result list
if (valid) {
// select one (max) score for the file. // select one (max) score for the file.
// for better ranking, we should calculate ranking by using words statistics like basic tf-idf... const score = Math.max(...wordList.map((w) => scoreMap.get(file)[w]));
var score = $u.max($u.map(fileMap[file], function(w){return scoreMap[file][w]})); // add result to the result list
results.push([docnames[file], titles[file], '', null, score, filenames[file]]); results.push([
} docNames[file],
titles[file],
"",
null,
score,
filenames[file],
]);
} }
return results; return results;
}, },
@ -496,34 +475,33 @@ var Search = {
/** /**
* helper function to return a node containing the * helper function to return a node containing the
* search summary for a given text. keywords is a list * search summary for a given text. keywords is a list
* of stemmed words, hlwords is the list of normal, unstemmed * of stemmed words, highlightWords is the list of normal, unstemmed
* words. the first one is used to find the occurrence, the * words. the first one is used to find the occurrence, the
* latter for highlighting it. * latter for highlighting it.
*/ */
makeSearchSummary : function(htmlText, keywords, hlwords) { makeSearchSummary: (htmlText, keywords, highlightWords) => {
var text = Search.htmlToText(htmlText); const text = Search.htmlToText(htmlText).toLowerCase();
if (text == "") { if (text === "") return null;
return null;
} const actualStartPosition = [...keywords]
var textLower = text.toLowerCase(); .map((k) => text.indexOf(k.toLowerCase()))
var start = 0; .filter((i) => i > -1)
$.each(keywords, function() { .slice(-1)[0];
var i = textLower.indexOf(this.toLowerCase()); const startWithContext = Math.max(actualStartPosition - 120, 0);
if (i > -1)
start = i; const top = startWithContext === 0 ? "" : "...";
}); const tail = startWithContext + 240 < text.length ? "..." : "";
start = Math.max(start - 120, 0);
var excerpt = ((start > 0) ? '...' : '') + let summary = document.createElement("div");
$.trim(text.substr(start, 240)) + summary.classList.add("context");
((start + 240 - text.length) ? '...' : ''); summary.innerText = top + text.substr(startWithContext, 240).trim() + tail;
var rv = $('<p class="context"></p>').text(excerpt);
$.each(hlwords, function() { highlightWords.forEach((highlightWord) =>
rv = rv.highlightText(this, 'highlighted'); _highlightText(summary, highlightWord, "highlighted")
}); );
return rv;
} return summary;
},
}; };
$(document).ready(function() { _ready(Search.init);
Search.init();
});

View File

@ -9,33 +9,22 @@
// :copyright: Copyright 2012-2014 by Sphinx team, see AUTHORS. // :copyright: Copyright 2012-2014 by Sphinx team, see AUTHORS.
// :license: BSD, see LICENSE for details. // :license: BSD, see LICENSE for details.
// //
$(document).ready(function(){ const initialiseBizStyle = () => {
if (navigator.userAgent.indexOf('iPhone') > 0 || if (navigator.userAgent.indexOf("iPhone") > 0 || navigator.userAgent.indexOf("Android") > 0) {
navigator.userAgent.indexOf('Android') > 0) { document.querySelector("li.nav-item-0 a").innerText = "Top"
$("li.nav-item-0 a").text("Top");
} }
const truncator = item => {if (item.textContent.length > 20) {
item.title = item.innerText
item.innerText = item.innerText.substr(0, 17) + "..."
}
}
document.querySelectorAll("div.related:first ul li:not(.right) a").slice(1).forEach(truncator);
document.querySelectorAll("div.related:last ul li:not(.right) a").slice(1).forEach(truncator);
}
$("div.related:first ul li:not(.right) a").slice(1).each(function(i, item){ window.addEventListener("resize",
if (item.text.length > 20) { () => (document.querySelector("li.nav-item-0 a").innerText = (window.innerWidth <= 776) ? "Top" : "{{ shorttitle|e }}")
var tmpstr = item.text )
$(item).attr("title", tmpstr);
$(item).text(tmpstr.substr(0, 17) + "...");
}
});
$("div.related:last ul li:not(.right) a").slice(1).each(function(i, item){
if (item.text.length > 20) {
var tmpstr = item.text
$(item).attr("title", tmpstr);
$(item).text(tmpstr.substr(0, 17) + "...");
}
});
});
$(window).resize(function(){ if (document.readyState !== "loading") initialiseBizStyle()
if ($(window).width() <= 776) { else document.addEventListener("DOMContentLoaded", initialiseBizStyle)
$("li.nav-item-0 a").text("Top");
}
else {
$("li.nav-item-0 a").text("{{ shorttitle|e }}");
}
});

View File

@ -15,3 +15,9 @@
<script src="{{ pathto('_static/sidebar.js', 1) }}"></script> <script src="{{ pathto('_static/sidebar.js', 1) }}"></script>
{% endif %} {% endif %}
{%- endblock %} {%- endblock %}
{%- block sidebarextra %}{% if theme_collapsiblesidebar|tobool %}
<div id="sidebarbutton" title="{{ _('Collapse sidebar') }}">
<span>{{ '»' if theme_rightsidebar|tobool else '«' }}</span>
</div>
{% endif %}{% endblock %}

View File

@ -28,6 +28,7 @@ body {
} }
div.document { div.document {
display: flex;
background-color: {{ theme_sidebarbgcolor }}; background-color: {{ theme_sidebarbgcolor }};
} }
@ -153,9 +154,39 @@ div.sphinxsidebar input {
} }
{% if theme_collapsiblesidebar|tobool %} {% if theme_collapsiblesidebar|tobool %}
{% if theme_rightsidebar|tobool %}
{% set side = 'right' %}
{% set opposite = 'left' %}
{% else %}
{% set side = 'left' %}
{% set opposite = 'right' %}
{% endif %}
/* for collapsible sidebar */ /* for collapsible sidebar */
div#sidebarbutton { #sidebarbutton {
height: 100%;
background-color: {{ theme_sidebarbtncolor }}; background-color: {{ theme_sidebarbtncolor }};
margin-{{side}}: 0;
color: #FFFFFF;
border-{{side}}: 1px solid {{ theme_relbarbgcolor }};
font-size: 1.2em;
cursor: pointer;
padding-top: 1px;
float: {{ 'left' if theme_rightsidebar|tobool else 'right' }};
display: table; /* for vertically centering the <span> */
}
#sidebarbutton:hover {
background-color: {{ theme_relbarbgcolor }};
}
#sidebarbutton span {
display: table-cell;
vertical-align: middle;
}
div.sphinxsidebarwrapper {
float: {{side}};
margin-{{opposite}}: 0;
} }
{% endif %} {% endif %}

View File

@ -21,145 +21,52 @@
* *
*/ */
$(function() { const initialiseSidebar = () => {
{% if theme_rightsidebar|tobool %} {% if theme_rightsidebar|tobool %}
{% set side = 'right' %} {% set side = 'Right' %}
{% set opposite = 'left' %}
{% set initial_label = '&raquo;' %}
{% set expand_label = '«' %}
{% set collapse_label = '»' %}
{% else %} {% else %}
{% set side = 'left' %} {% set side = 'Left' %}
{% set opposite = 'right' %}
{% set initial_label = '&laquo;' %}
{% set expand_label = '»' %}
{% set collapse_label = '«' %}
{% endif %} {% endif %}
// global elements used by the functions. // global elements used by the functions.
// the 'sidebarbutton' element is defined as global after its const bodyWrapper = document.getElementsByClassName("bodywrapper")[0]
// creation, in the add_sidebar_button function const sidebar = document.getElementsByClassName("sphinxsidebar")[0]
var bodywrapper = $('.bodywrapper'); const sidebarWrapper = document.getElementsByClassName('sphinxsidebarwrapper')[0]
var sidebar = $('.sphinxsidebar'); const sidebarButton = document.getElementById("sidebarbutton")
var sidebarwrapper = $('.sphinxsidebarwrapper'); const sidebarArrow = sidebarButton.querySelector('span')
// for some reason, the document has no sidebar; do not run into errors // for some reason, the document has no sidebar; do not run into errors
if (!sidebar.length) return; if (typeof sidebar === "undefined") return;
// original margin-left of the bodywrapper and width of the sidebar const flipArrow = element => element.innerText = (element.innerText === "»") ? "«" : "»"
// with the sidebar expanded
var bw_margin_expanded = bodywrapper.css('margin-{{side}}');
var ssb_width_expanded = sidebar.width();
// margin-left of the bodywrapper and width of the sidebar const collapse_sidebar = () => {
// with the sidebar collapsed bodyWrapper.style.margin{{side}} = ".8em";
var bw_margin_collapsed = '.8em'; sidebar.style.width = ".8em"
var ssb_width_collapsed = '.8em'; sidebarWrapper.style.display = "none"
flipArrow(sidebarArrow)
// colors used by the current theme sidebarButton.title = _('Expand sidebar')
var dark_color = $('.related').css('background-color'); window.localStorage.setItem("sidebar", "collapsed")
var light_color = $('.document').css('background-color');
function sidebar_is_collapsed() {
return sidebarwrapper.is(':not(:visible)');
} }
function toggle_sidebar() { const expand_sidebar = () => {
if (sidebar_is_collapsed()) bodyWrapper.style.margin{{side}} = ""
expand_sidebar(); sidebar.style.removeProperty("width")
else sidebarWrapper.style.display = ""
collapse_sidebar(); flipArrow(sidebarArrow)
sidebarButton.title = _('Collapse sidebar')
window.localStorage.setItem("sidebar", "expanded")
} }
function collapse_sidebar() { sidebarButton.addEventListener("click", () => {
sidebarwrapper.hide(); (sidebarWrapper.style.display === "none") ? expand_sidebar() : collapse_sidebar()
sidebar.css('width', ssb_width_collapsed); })
bodywrapper.css('margin-{{side}}', bw_margin_collapsed);
sidebarbutton.css({
'margin-{{side}}': '0',
'height': bodywrapper.height()
});
sidebarbutton.find('span').text('{{expand_label}}');
sidebarbutton.attr('title', _('Expand sidebar'));
document.cookie = 'sidebar=collapsed';
}
function expand_sidebar() { if (!window.localStorage.getItem("sidebar")) return
bodywrapper.css('margin-{{side}}', bw_margin_expanded); const value = window.localStorage.getItem("sidebar")
sidebar.css('width', ssb_width_expanded); if (value === "collapsed") collapse_sidebar();
sidebarwrapper.show(); else if (value === "expanded") expand_sidebar();
sidebarbutton.css({ }
'margin-{{side}}': ssb_width_expanded-12,
'height': bodywrapper.height()
});
sidebarbutton.find('span').text('{{collapse_label}}');
sidebarbutton.attr('title', _('Collapse sidebar'));
document.cookie = 'sidebar=expanded';
}
function add_sidebar_button() { if (document.readyState !== "loading") initialiseSidebar()
sidebarwrapper.css({ else document.addEventListener("DOMContentLoaded", initialiseSidebar)
'float': '{{side}}',
'margin-{{opposite}}': '0',
'width': ssb_width_expanded - 28
});
// create the button
sidebar.append(
'<div id="sidebarbutton"><span>{{initial_label}}</span></div>'
);
var sidebarbutton = $('#sidebarbutton');
light_color = sidebarbutton.css('background-color');
// find the height of the viewport to center the '<<' in the page
var viewport_height;
if (window.innerHeight)
viewport_height = window.innerHeight;
else
viewport_height = $(window).height();
sidebarbutton.find('span').css({
'display': 'block',
'margin-top': (viewport_height - sidebar.position().top - 20) / 2
});
sidebarbutton.click(toggle_sidebar);
sidebarbutton.attr('title', _('Collapse sidebar'));
sidebarbutton.css({
'color': '#FFFFFF',
'border-{{side}}': '1px solid ' + dark_color,
'font-size': '1.2em',
'cursor': 'pointer',
'height': bodywrapper.height(),
'padding-top': '1px',
'margin-{{side}}': ssb_width_expanded - 12
});
sidebarbutton.hover(
function () {
$(this).css('background-color', dark_color);
},
function () {
$(this).css('background-color', light_color);
}
);
}
function set_position_from_cookie() {
if (!document.cookie)
return;
var items = document.cookie.split(';');
for(var k=0; k<items.length; k++) {
var key_val = items[k].split('=');
var key = key_val[0].replace(/ /, ""); // strip leading spaces
if (key == 'sidebar') {
var value = key_val[1];
if ((value == 'collapsed') && (!sidebar_is_collapsed()))
collapse_sidebar();
else if ((value == 'expanded') && (sidebar_is_collapsed()))
expand_sidebar();
}
}
}
add_sidebar_button();
var sidebarbutton = $('#sidebarbutton');
set_position_from_cookie();
});

View File

@ -1,26 +1,12 @@
$(function() { const initialiseThemeExtras = () => {
const toc = document.getElementById("toc")
var toc.style.display = ""
toc = $('#toc').show(), const items = toc.getElementsByTagName("ul")[0]
items = $('#toc > ul').hide(); items.style.display = "none"
toc.getElementsByTagName("h3").addEventListener("click", () => {
$('#toc h3') if (items.style.display !== "none") toc.classList.remove("expandedtoc")
.click(function() { else toc.classList.add("expandedtoc");
if (items.is(':visible')) { })
items.animate({ }
height: 'hide', if (document.readyState !== "loading") initialiseThemeExtras()
opacity: 'hide' else document.addEventListener("DOMContentLoaded", initialiseThemeExtras)
}, 300, function() {
toc.removeClass('expandedtoc');
});
}
else {
items.animate({
height: 'show',
opacity: 'show'
}, 400);
toc.addClass('expandedtoc');
}
});
});

View File

@ -1,91 +1,41 @@
var DOCUMENTATION_OPTIONS = {}; const DOCUMENTATION_OPTIONS = {};
describe('highlightText', function() {
describe('jQuery extensions', function() { const cyrillicTerm = 'шеллы';
const umlautTerm = 'gänsefüßchen';
describe('urldecode', function() {
it('should correctly decode URLs and replace `+`s with a spaces', function() {
var test_encoded_string =
'%D1%88%D0%B5%D0%BB%D0%BB%D1%8B+%D1%88%D0%B5%D0%BB%D0%BB%D1%8B';
var test_decoded_string = 'шеллы шеллы';
expect(jQuery.urldecode(test_encoded_string)).toEqual(test_decoded_string);
});
it('+ should result in " "', function() {
expect(jQuery.urldecode('+')).toEqual(' ');
});
});
describe('getQueryParameters', function() {
var paramString = '?q=test+this&check_keywords=yes&area=default';
var queryParamObject = {
area: ['default'],
check_keywords: ['yes'],
q: ['test this']
};
it('should correctly create query parameter object from string', function() {
expect(jQuery.getQueryParameters(paramString)).toEqual(queryParamObject);
});
it('should correctly create query param object from URL params', function() {
history.pushState({}, '_', window.location + paramString);
expect(jQuery.getQueryParameters()).toEqual(queryParamObject);
});
});
describe('highlightText', function() {
var cyrillicTerm = 'шеллы';
var umlautTerm = 'gänsefüßchen';
it('should highlight text incl. special characters correctly in HTML', function() { it('should highlight text incl. special characters correctly in HTML', function() {
var highlightTestSpan = const highlightTestSpan = new DOMParser().parseFromString(
jQuery('<span>This is the шеллы and Gänsefüßchen test!</span>'); '<span>This is the шеллы and Gänsefüßchen test!</span>', 'text/html').body.firstChild
jQuery(document.body).append(highlightTestSpan); _highlightText(highlightTestSpan, cyrillicTerm, 'highlighted');
highlightTestSpan.highlightText(cyrillicTerm, 'highlighted'); _highlightText(highlightTestSpan, umlautTerm, 'highlighted');
highlightTestSpan.highlightText(umlautTerm, 'highlighted'); const expectedHtmlString =
var expectedHtmlString =
'This is the <span class=\"highlighted\">шеллы</span> and ' + 'This is the <span class=\"highlighted\">шеллы</span> and ' +
'<span class=\"highlighted\">Gänsefüßchen</span> test!'; '<span class=\"highlighted\">Gänsefüßchen</span> test!';
expect(highlightTestSpan.html()).toEqual(expectedHtmlString); expect(highlightTestSpan.innerHTML).toEqual(expectedHtmlString);
}); });
it('should highlight text incl. special characters correctly in SVG', function() { it('should highlight text incl. special characters correctly in SVG', function() {
var highlightTestSvg = jQuery( const highlightTestSvg = new DOMParser().parseFromString(
'<span id="svg-highlight-test">' + '<span id="svg-highlight-test">' +
'<svg xmlns="http://www.w3.org/2000/svg" height="50" width="500">' + '<svg xmlns="http://www.w3.org/2000/svg" height="50" width="500">' +
'<text x="0" y="15">' + '<text x="0" y="15">' +
'This is the шеллы and Gänsefüßchen test!' + 'This is the шеллы and Gänsefüßchen test!' +
'</text>' + '</text>' +
'</svg>' + '</svg>' +
'</span>'); '</span>', 'text/html').body.firstChild
jQuery(document.body).append(highlightTestSvg); _highlightText(highlightTestSvg, cyrillicTerm, 'highlighted');
highlightTestSvg.highlightText(cyrillicTerm, 'highlighted'); _highlightText(highlightTestSvg, umlautTerm, 'highlighted');
highlightTestSvg.highlightText(umlautTerm, 'highlighted');
/* Note wild cards and ``toMatch``; allowing for some variability /* Note wild cards and ``toMatch``; allowing for some variability
seems to be necessary, even between different FF versions */ seems to be necessary, even between different FF versions */
var expectedSvgString = const expectedSvgString =
'<svg xmlns="http://www.w3.org/2000/svg" height="50" width="500">' + '<svg xmlns="http://www.w3.org/2000/svg" height="50" width="500">'
'<rect x=".*" y=".*" width=".*" height=".*" class="highlighted">' + + '<rect x=".*" y=".*" width=".*" height=".*" class="highlighted"/>'
'</rect>' + + '<rect x=".*" y=".*" width=".*" height=".*" class="highlighted"/>'
'<rect x=".*" y=".*" width=".*" height=".*" class="highlighted">' + + '<text x=".*" y=".*">This is the <tspan>шеллы</tspan> and '
'</rect>' + + '<tspan>Gänsefüßchen</tspan> test!</text></svg>';
'<text x=".*" y=".*">' + expect(new XMLSerializer().serializeToString(highlightTestSvg.firstChild)).toMatch(new RegExp(expectedSvgString));
'This is the ' +
'<tspan>шеллы</tspan> and ' +
'<tspan>Gänsefüßchen</tspan> test!' +
'</text>' +
'</svg>';
expect(highlightTestSvg.html()).toMatch(new RegExp(expectedSvgString));
}); });
});
}); });