Skip to content

Commit 3a54136

Browse files
committed
[DISC-18903] Remove jquery and hanging javascript assets.
Some notes on files I changed. * `scaladoc/src/dotty/tools/scaladoc/renderers/Resources.scala` This adds jquery to list of resources to load in a scaladoc page. I could remove it after fixing `ux.js`. If you search for jquery now it appears in `dagre-d3.min.js`. But this is only in doc comment examples. * `scaladoc/resources/dotty_res/scripts/ux.js` jQuery powered things like sidebar link behaviour (issuing GET) and item expansion (from mini blurb to full description). * `docs/_spec/public/scripts/toc.js` I tried to do a 1 to 1 port, included bugs like scroll listener being unhooked after clicking a link in the TOC. This table of contents ISN'T the same as what appears in normal docs. This is specific to the language spec. There might be an opportunity here to share code, see IntersectionObserver in `ux.js`. * `docs/_spec/public/scripts/main.js` Nothing exciting here. * `docs/_spec/_layouts/toc.yml` Loads jQuery for previously mentioned language spec TOC. * `docs/_spec/_layouts/default.yml` I think duplicate to above. * `docs/_assets/js/toolbar.js` Lots of unused jquery code. This is hanging source code, with references to code that was removed with commit ... ``` 0f78f73 ``` * `docs/_assets/css/toolbar.css` Same as above. * `docs/_assets/js/sidebar.js` `togglePath` call path removed in same commit above.
1 parent 64411b6 commit 3a54136

File tree

9 files changed

+81
-209
lines changed

9 files changed

+81
-209
lines changed

docs/_assets/css/toolbar.css

Lines changed: 0 additions & 98 deletions
This file was deleted.

docs/_assets/js/sidebar.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/_assets/js/toolbar.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/_spec/_layouts/default.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" integrity="sha384-K3vbOmF2BtaVai+Qk37uypf7VrgBubhQreNQe9aGsz9lB63dIFiQVlJbr92dw2Lx" crossorigin="anonymous"></script>
1111
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" integrity="sha384-kmZOZB5ObwgQnS/DuDg6TScgOiWWBiVt0plIRkZCmE6rDZGrEOQeHM5PcHi+nyqe" crossorigin="anonymous"></script>
1212

13-
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
1413
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.2/styles/default.min.css">
1514
<!-- need to use include to see value of page.chapter variable -->
1615
<style type="text/css">

docs/_spec/_layouts/toc.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<link rel="icon" type="image/png" href="public/favicon.ico">
77
<link rel="shortcut icon" type="image/png" href="public/favicon.ico">
88

9-
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
109
<title>{{ page.title }} | Scala {{ site.thisScalaVersion }}</title>
1110

1211
<link rel="stylesheet" type="text/css" href="public/stylesheets/screen.css">

docs/_spec/public/scripts/main.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function currentChapter() {
22
return parseInt(document.location.pathname.split('/').pop().substr(0, 2), 10);
33
}
44

5-
function heading(i, heading, $heading) {
5+
function heading(i, heading) {
66
const currentLevel = parseInt(heading.tagName.substring(1));
77

88
if (currentLevel === this.headerLevel) {
@@ -19,12 +19,12 @@ function heading(i, heading, $heading) {
1919
this.headerCounts[this.headerLevel] = 1;
2020
}
2121
}
22-
return `${this.headerCounts[this.headerLevel]} ${$heading.text()}`;
22+
return `${this.headerCounts[this.headerLevel]} ${heading.innerText}`;
2323
}
2424

2525
// ignore when using wkhtmltopdf, or it won't work...
2626
if (window.jekyllEnv !== 'spec-pdf') {
27-
$('#toc').toc(
27+
addTOC(document.getElementById('toc'),
2828
{
2929
'selectors': 'h1,h2,h3',
3030
'smoothScrolling': false,
@@ -53,12 +53,16 @@ document.addEventListener("DOMContentLoaded", function() {
5353
// syntax highlighting after KaTeX is loaded,
5454
// so that math can be used in code blocks
5555
hljs.initHighlighting();
56-
$("pre nobr").addClass("fixws");
56+
document.querySelectorAll("pre nobr").forEach((element) => {
57+
element.classList.add("fixws")
58+
});
59+
5760
// point when all necessary js is done, so PDF to be rendered
5861
window.status = "loaded";
5962
});
6063

61-
$("#chapters a").each(function (index) {
62-
const href = $(this).attr("href");
63-
$(this).toggleClass("chapter-active", document.location.pathname.endsWith(href));
64+
document.querySelectorAll("#chapters a").forEach(function (element) {
65+
const href = element.attributes.getNamedItem("href");
66+
const toggle = href != null && document.location.pathname.endsWith(href.value);
67+
element.classList.toggle("chapter-active", toggle);
6468
});

docs/_spec/public/scripts/toc.js

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,94 @@
55
* copyright Greg Allen 2014
66
* MIT License
77
*/
8-
(function($) {
9-
var verboseIdCache = {};
10-
$.fn.toc = function(options) {
11-
var self = this;
12-
var opts = $.extend({}, jQuery.fn.toc.defaults, options);
138

14-
var container = $(opts.container);
15-
var headings = $(opts.selectors, container);
9+
/* The following ports the jquery plugin to modern javascript. */
10+
11+
/***
12+
* Add table of contents links to the given TOC container.
13+
* @param {HTMLElement} domElement - The DOM element to manipulate
14+
* @param {Object} options - A list of options to change TOC_DEFAULTS.
15+
* @return {HTMLElement} - The same tocContainer for chaining.
16+
*/
17+
function addTOC(tocContainer, options) {
18+
var verboseIdCache = {};
19+
// Since options are only one level of nesting deep,
20+
// we use the spread syntax to merge.
21+
var opts = {...TOC_DEFAULTS, ...options};
22+
var container = document.querySelector(opts.container);
23+
var headings = container.querySelectorAll(opts.selectors);
1624
var headingOffsets = [];
1725
var activeClassName = opts.activeClass;
18-
1926
var scrollTo = function(e, callback) {
20-
$('li', self).removeClass(activeClassName);
21-
$(e.target).parent().addClass(activeClassName);
27+
tocContainer.querySelectorAll('li').forEach(function (element) {
28+
element.classList.remove(activeClassName);
29+
});
30+
e.parentNode.classList.add(activeClassName);
2231
};
2332

24-
//highlight on scroll
2533
var timeout;
2634
var highlightOnScroll = function(e) {
2735
if (timeout) {
2836
clearTimeout(timeout);
2937
}
3038
timeout = setTimeout(function() {
31-
var top = $(window).scrollTop(),
39+
var top = window.pageYOffset,
3240
highlighted, closest = Number.MAX_VALUE, index = 0;
33-
3441
for (var i = 0, c = headingOffsets.length; i < c; i++) {
3542
var currentClosest = Math.abs(headingOffsets[i] - top);
3643
if (currentClosest < closest) {
3744
index = i;
3845
closest = currentClosest;
3946
}
4047
}
41-
42-
$('li', self).removeClass(activeClassName);
43-
highlighted = $('li:eq('+ index +')', self).addClass(activeClassName);
44-
opts.onHighlight(highlighted);
48+
tocContainer.querySelectorAll('li').forEach(function (element) {
49+
element.classList.remove(activeClassName);
50+
});
51+
highlighted = tocContainer.querySelector('li:nth-child(' + index + ')')
52+
if (highlighted != null) { highlighted.classList.add(activeClassName); }
53+
opts.onHighlight(highlighted);
4554
}, 50);
4655
};
56+
4757
if (opts.highlightOnScroll) {
48-
$(window).on('scroll', highlightOnScroll);
58+
window.addEventListener('scroll', highlightOnScroll);
4959
highlightOnScroll();
5060
}
5161

52-
return this.each(function() {
53-
//build TOC
54-
var el = $(this);
55-
var ul = $(opts.listType);
56-
57-
headings.each(function(i, heading) {
58-
var $h = $(heading);
59-
headingOffsets.push($h.offset().top - opts.highlightOffset);
60-
61-
var anchorName = opts.anchorName(i, heading, opts.prefix);
62-
63-
//add anchor
64-
if(heading.id !== anchorName) {
65-
var anchor = $('<span/>').attr('id', anchorName).insertBefore($h);
66-
}
67-
68-
//build TOC item
69-
var a = $('<a/>')
70-
.text(opts.headerText(i, heading, $h))
71-
.attr('href', '#' + anchorName)
72-
.on('click', function(e) {
73-
$(window).off('scroll', highlightOnScroll);
74-
scrollTo(e, function() {
75-
$(window).on('scroll', highlightOnScroll);
76-
});
77-
el.trigger('selected', $(this).attr('href'));
78-
});
79-
80-
var li = $('<li/>')
81-
.addClass(opts.itemClass(i, heading, $h, opts.prefix))
82-
.append(a);
62+
const listTypeRegex = new RegExp("<(.*)/>");
63+
const listTypeToTagName = listTypeRegex.exec(opts.listType);
64+
if (listTypeToTagName == null) { return tocContainer; }
65+
var ul = document.createElement(listTypeToTagName[1]);
66+
headings.forEach(function(heading, i) {
67+
headingOffsets.push(heading.getBoundingClientRect().top - opts.highlightOffset);
68+
var anchorName = opts.anchorName(i, heading, opts.prefix);
69+
if(heading.id !== anchorName) {
70+
var anchor = document.createElement("span");
71+
anchor.setAttribute('id', anchorName);
72+
heading.parentNode.insertBefore(anchor, heading);
73+
}
8374

84-
ul.append(li);
75+
var a = document.createElement("a");
76+
a.textContent = opts.headerText(i, heading);
77+
a.setAttribute('href', '#' + anchorName);
78+
a.addEventListener('click', () => {
79+
window.removeEventListener('scroll', highlightOnScroll);
80+
scrollTo(a, function() {
81+
// Kept bug from jquery version: callback isn't called, so scroll event listener not re-attached.
82+
window.addEventListener('scroll', highlightOnScroll);
83+
});
84+
// Research suggests this is a now unused custom event. Won't translate.
85+
// el.trigger('selected', $(this).attr('href'))
8586
});
86-
el.html(ul);
87+
var li = document.createElement('li');
88+
li.classList.add(opts.itemClass(i, heading, opts.prefix));
89+
li.append(a);
90+
ul.append(li);
8791
});
88-
};
89-
92+
tocContainer.replaceChildren(ul);
93+
}
9094

91-
jQuery.fn.toc.defaults = {
95+
const TOC_DEFAULTS = {
9296
container: 'body',
9397
listType: '<ul/>',
9498
selectors: 'h1,h2,h3',
@@ -101,28 +105,22 @@ jQuery.fn.toc.defaults = {
101105
if(heading.id.length) {
102106
return heading.id;
103107
}
104-
105-
var candidateId = $(heading).text().replace(/[^a-z0-9]/ig, ' ').replace(/\s+/g, '-').toLowerCase();
108+
var candidateId = heading.innerText.replace(/[^a-z0-9]/ig, ' ').replace(/\s+/g, '-').toLowerCase();
106109
if (verboseIdCache[candidateId]) {
107110
var j = 2;
108-
109111
while(verboseIdCache[candidateId + j]) {
110112
j++;
111113
}
112114
candidateId = candidateId + '-' + j;
113-
114115
}
115116
verboseIdCache[candidateId] = true;
116-
117117
return prefix + '-' + candidateId;
118118
},
119-
headerText: function(i, heading, $heading) {
120-
return $heading.text();
119+
headerText: function(i, heading) {
120+
return heading.innerText();
121121
},
122-
itemClass: function(i, heading, $heading, prefix) {
123-
return prefix + '-' + $heading[0].tagName.toLowerCase();
122+
itemClass: function(i, heading, prefix) {
123+
return prefix + '-' + heading.tagName.toLowerCase();
124124
}
125125

126126
};
127-
128-
})(jQuery);

0 commit comments

Comments
 (0)