44 *
55 * Sphinx JavaScript utilities for all documentation.
66 *
7- * :copyright: Copyright 2007-2020 by the Sphinx team, see AUTHORS.
7+ * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
88 * :license: BSD, see LICENSE for details.
99 *
1010 */
@@ -29,9 +29,14 @@ if (!window.console || !console.firebug) {
2929
3030/**
3131 * small helper function to urldecode strings
32+ *
33+ * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
3234 */
3335jQuery . urldecode = function ( x ) {
34- return decodeURIComponent ( x ) . replace ( / \+ / g, ' ' ) ;
36+ if ( ! x ) {
37+ return x
38+ }
39+ return decodeURIComponent ( x . replace ( / \+ / g, ' ' ) ) ;
3540} ;
3641
3742/**
@@ -149,9 +154,7 @@ var Documentation = {
149154 this . fixFirefoxAnchorBug ( ) ;
150155 this . highlightSearchWords ( ) ;
151156 this . initIndexTable ( ) ;
152- if ( DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS ) {
153- this . initOnKeyListeners ( ) ;
154- }
157+ this . initOnKeyListeners ( ) ;
155158 } ,
156159
157160 /**
@@ -259,6 +262,16 @@ var Documentation = {
259262 hideSearchWords : function ( ) {
260263 $ ( '#searchbox .highlight-link' ) . fadeOut ( 300 ) ;
261264 $ ( 'span.highlighted' ) . removeClass ( 'highlighted' ) ;
265+ var url = new URL ( window . location ) ;
266+ url . searchParams . delete ( 'highlight' ) ;
267+ window . history . replaceState ( { } , '' , url ) ;
268+ } ,
269+
270+ /**
271+ * helper function to focus on search bar
272+ */
273+ focusSearchBar : function ( ) {
274+ $ ( 'input[name=q]' ) . first ( ) . focus ( ) ;
262275 } ,
263276
264277 /**
@@ -283,25 +296,54 @@ var Documentation = {
283296 } ,
284297
285298 initOnKeyListeners : function ( ) {
299+ // only install a listener if it is really needed
300+ if ( ! DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS &&
301+ ! DOCUMENTATION_OPTIONS . ENABLE_SEARCH_SHORTCUTS )
302+ return ;
303+
286304 $ ( document ) . keydown ( function ( event ) {
287305 var activeElementType = document . activeElement . tagName ;
288306 // don't navigate when in search box, textarea, dropdown or button
289307 if ( activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT'
290- && activeElementType !== 'BUTTON' && ! event . altKey && ! event . ctrlKey && ! event . metaKey
291- && ! event . shiftKey ) {
292- switch ( event . keyCode ) {
293- case 37 : // left
294- var prevHref = $ ( 'link[rel="prev"]' ) . prop ( 'href' ) ;
295- if ( prevHref ) {
296- window . location . href = prevHref ;
297- return false ;
298- }
299- case 39 : // right
300- var nextHref = $ ( 'link[rel="next"]' ) . prop ( 'href' ) ;
301- if ( nextHref ) {
302- window . location . href = nextHref ;
303- return false ;
304- }
308+ && activeElementType !== 'BUTTON' ) {
309+ if ( event . altKey || event . ctrlKey || event . metaKey )
310+ return ;
311+
312+ if ( ! event . shiftKey ) {
313+ switch ( event . key ) {
314+ case 'ArrowLeft' :
315+ if ( ! DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS )
316+ break ;
317+ var prevHref = $ ( 'link[rel="prev"]' ) . prop ( 'href' ) ;
318+ if ( prevHref ) {
319+ window . location . href = prevHref ;
320+ return false ;
321+ }
322+ break ;
323+ case 'ArrowRight' :
324+ if ( ! DOCUMENTATION_OPTIONS . NAVIGATION_WITH_KEYS )
325+ break ;
326+ var nextHref = $ ( 'link[rel="next"]' ) . prop ( 'href' ) ;
327+ if ( nextHref ) {
328+ window . location . href = nextHref ;
329+ return false ;
330+ }
331+ break ;
332+ case 'Escape' :
333+ if ( ! DOCUMENTATION_OPTIONS . ENABLE_SEARCH_SHORTCUTS )
334+ break ;
335+ Documentation . hideSearchWords ( ) ;
336+ return false ;
337+ }
338+ }
339+
340+ // some keyboard layouts may need Shift to get /
341+ switch ( event . key ) {
342+ case '/' :
343+ if ( ! DOCUMENTATION_OPTIONS . ENABLE_SEARCH_SHORTCUTS )
344+ break ;
345+ Documentation . focusSearchBar ( ) ;
346+ return false ;
305347 }
306348 }
307349 } ) ;
0 commit comments