Skip to content

Commit 3d7b267

Browse files
fix: only add page navigation panel to compatible templates
Only add right-hand page navigation panel to #content elements that have the ietf-auto-nav class and test that the base template's DOM is compatible with the panel.
1 parent 3906bbd commit 3d7b267

3 files changed

Lines changed: 61 additions & 27 deletions

File tree

ietf/static/js/ietf.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,31 +104,37 @@ $(document)
104104
});
105105
});
106106

107-
// Automatically add a navigation pane to long pages
108-
$(document)
109-
.ready(function () {
110-
var headings = $("#content")
107+
// Automatically add a navigation pane to long pages if #content element has the ietf-auto-nav class.
108+
// The parent of #content must have the row class or the navigation pane will render incorrectly.
109+
$(function () {
110+
const contentElement = $('#content.ietf-auto-nav');
111+
if (contentElement.length > 0) {
112+
const headings = contentElement
111113
.find("h1:visible, h2:visible, h3:visible, h4:visible, h5:visible, h6:visible")
112114
.not(".navskip");
113115

114-
var contents = $(headings)
115-
.html()
116-
.split("<")
117-
.shift()
118-
.trim();
116+
const contents = (headings.length > 0) &&
117+
($(headings)
118+
.html()
119+
.split("<")
120+
.shift()
121+
.trim());
119122

120-
if (contents
121-
.length > 0 && $(headings)
122-
.last()
123-
.offset()
124-
.top > $(window)
125-
.height()) {
123+
if (
124+
contents &&
125+
(contents.length > 0) &&
126+
($(headings)
127+
.last()
128+
.offset()
129+
.top > $(window)
130+
.height())
131+
) {
126132
// console.log("Enabling nav.");
127-
var n = 0;
128-
var last_level;
129-
var nav;
133+
let n = 0;
134+
let last_level;
135+
let nav;
130136

131-
$("#content")
137+
contentElement
132138
.attr("data-bs-offset", 0)
133139
.attr("tabindex", 0)
134140
.after($(`
@@ -141,7 +147,7 @@ $(document)
141147
.not(".navskip")
142148
.each(function () {
143149
// Some headings have complex HTML in them - only use first part in that case.
144-
var text = $(this)
150+
const text = $(this)
145151
.html()
146152
.split("<")
147153
.shift()
@@ -151,7 +157,7 @@ $(document)
151157
// Nothing to do for empty headings.
152158
return;
153159
}
154-
var id = $(this)
160+
let id = $(this)
155161
.attr("id");
156162

157163
if (id === undefined) {
@@ -168,8 +174,8 @@ $(document)
168174
.last();
169175
}
170176

171-
var level = parseInt(this.nodeName.substring(1)) - 1;
172-
if (last_level == undefined) {
177+
const level = parseInt(this.nodeName.substring(1)) - 1;
178+
if (!last_level) {
173179
last_level = level;
174180
}
175181

@@ -186,7 +192,7 @@ $(document)
186192

187193
$(window)
188194
.on("scroll", function () {
189-
var item = $('#righthand-nav')
195+
const item = $('#righthand-nav')
190196
.find(".active")
191197
.last();
192198
if (item.length) {
@@ -200,7 +206,8 @@ $(document)
200206
.scrollspy("refresh");
201207

202208
}
203-
});
209+
}
210+
});
204211

205212
// Replace track/untrack functionality with js.
206213
$(document)

ietf/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</ul>
8484
</div>
8585
{% endif %}
86-
<div class="col overflow-hidden mx-3" id="content">
86+
<div class="col overflow-hidden mx-3 ietf-auto-nav" id="content">
8787
<noscript>
8888
<div class="alert alert-danger alert-ignore my-3">
8989
<b>Javascript disabled?</b> Like other modern websites, the IETF Datatracker relies on Javascript.

ietf/utils/tests.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import types
1010

11+
from pyquery import PyQuery
1112
from typing import Dict, List # pyflakes:ignore
1213

1314
from email.mime.image import MIMEImage
@@ -27,7 +28,7 @@
2728
from django.template import Context
2829
from django.template import Template # pyflakes:ignore
2930
from django.template.defaulttags import URLNode
30-
from django.template.loader import get_template
31+
from django.template.loader import get_template, render_to_string
3132
from django.templatetags.static import StaticNode
3233
from django.urls import reverse as urlreverse
3334

@@ -291,6 +292,32 @@ def test_500_page(self):
291292
r = self.client.get(url)
292293
self.assertTemplateUsed(r, '500.html')
293294

295+
296+
class BaseTemplateTests(TestCase):
297+
base_template = 'base.html'
298+
299+
def test_base_template_includes_ietf_js(self):
300+
content = render_to_string(self.base_template, {})
301+
pq = PyQuery(content)
302+
self.assertTrue(
303+
pq('head > script[src$="ietf/js/ietf.js"]'),
304+
'base template should include ietf.js',
305+
)
306+
307+
def test_base_template_righthand_nav(self):
308+
"""The base template provides an automatic righthand navigation panel
309+
310+
This is provided by ietf.js and requires the ietf-auto-nav class and a parent with the row class
311+
or the nav widget will not render properly.
312+
"""
313+
content = render_to_string(self.base_template, {})
314+
pq = PyQuery(content)
315+
self.assertTrue(
316+
pq('.row > #content.ietf-auto-nav'),
317+
'base template should have a #content element with .ietf-auto-nav class and .row parent',
318+
)
319+
320+
294321
@skipIf(skip_version_trac, skip_message_trac)
295322
@skipIf(skip_wiki_glue_testing, skip_message_svn)
296323
class TestWikiGlueManagementCommand(TestCase):

0 commit comments

Comments
 (0)