Skip to contentSkip to conversation

docs/sphinx: Fix TOC scroll hack for the home page

PATCH In review Gustavo Sousa 2 participants 3 messages Jan 23, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 docs/sphinx: Fix TOC scroll hack for the home page

When on the documentation home page, there won't be any ".current" element since no entry from the TOC was selected yet. That results in a javascript error. Fix that by only trying to set the scrollTop if we have matches for current entries.

---
 Documentation/sphinx/templates/kernel-toc.html | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/sphinx/templates/kernel-toc.html +3 -1
index b58efa99df52..41f1efbe64bb 100644
@@ -12,5 +12,7 @@
 <script type="text/javascript"> <!--
   var sbar = document.getElementsByClassName("sphinxsidebar")[0];
   let currents = document.getElementsByClassName("current")
-  sbar.scrollTop = currents[currents.length - 1].offsetTop;
+  if (currents.length) {
+    sbar.scrollTop = currents[currents.length - 1].offsetTop;
+  }
   --> </script>
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 15 lines · Gustavo Sousa

When on the documentation home page, there won't be any ".current" element since no entry from the TOC was selected yet. That results in a javascript error. Fix that by only trying to set the scrollTop if we have matches for current entries.

---
 Documentation/sphinx/templates/kernel-toc.html | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/sphinx/templates/kernel-toc.html +0 -0
index b58efa99df52..41f1efbe64bb 100644

Documentation/sphinx/templates/kernel-toc.html line 18

Quoted 8 lines
Documentation/sphinx/templates/kernel-toc.html +3 -1
@@ -12,5 +12,7 @@
 <script type="text/javascript"> <!--
   var sbar = document.getElementsByClassName("sphinxsidebar")[0];
   let currents = document.getElementsByClassName("current")
-  sbar.scrollTop = currents[currents.length - 1].offsetTop;
+  if (currents.length) {
+    sbar.scrollTop = currents[currents.length - 1].offsetTop;
+  }
   --> </script>

Interesting, that's definitely a bug. Did you stumble across this in the console, or did it manifest in some other way for you?

Either way, I've applied this, thanks.

jon

#2 lore raw

Quoting Jonathan Corbet (2024-01-23 18:33:52-03:00)

Quoted 12 lines
Quoted 10 lines · Gustavo Sousa

When on the documentation home page, there won't be any ".current" element since no entry from the TOC was selected yet. That results in a javascript error. Fix that by only trying to set the scrollTop if we have matches for current entries.

---
 Documentation/sphinx/templates/kernel-toc.html | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 16 lines

Documentation/sphinx/templates/kernel-toc.html line 18

Quoted 13 lines
Documentation/sphinx/templates/kernel-toc.html +3 -1
index b58efa99df52..41f1efbe64bb 100644
@@ -12,5 +12,7 @@
 <script type="text/javascript"> <!--
   var sbar = document.getElementsByClassName("sphinxsidebar")[0];
   let currents = document.getElementsByClassName("current")
-  sbar.scrollTop = currents[currents.length - 1].offsetTop;
+  if (currents.length) {
+    sbar.scrollTop = currents[currents.length - 1].offsetTop;
+  }
   --> </script>

Interesting, that's definitely a bug. Did you stumble across this in the console, or did it manifest in some other way for you?

I just saw the error in the console :-)

Fortunately, the error is currently harmless, but it could become a problem in the future if we were to put more stuff after the offending line.

Quoted 2 lines

Either way, I've applied this, thanks.

Thanks!

--
Gustavo Sousa