[3.11] gh-115317: Rewrite changelog filter to use vanilla JavaScript (GH-115324) (#115373)

gh-115317: Rewrite changelog filter to use vanilla JavaScript (GH-115324)
(cherry picked from commit 341d7874f0)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Tomas R <tomas.roun8@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-02-13 06:30:54 +01:00 committed by GitHub
parent 21edde17b3
commit a33ae3b63e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 52 deletions

View file

@ -1,6 +1,6 @@
root = true root = true
[*.{py,c,cpp,h,rst,md,yml}] [*.{py,c,cpp,h,js,rst,md,yml}]
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = true insert_final_newline = true
indent_style = space indent_style = space
@ -11,5 +11,5 @@ indent_size = 4
[*.rst] [*.rst]
indent_size = 3 indent_size = 3
[*.yml] [*.{js,yml}]
indent_size = 2 indent_size = 2

View file

@ -1,53 +1,59 @@
$(document).ready(function() { document.addEventListener("DOMContentLoaded", function () {
// add the search form and bind the events // add the search form and bind the events
$('h1').after([ document
'<p>Filter entries by content:', .querySelector("h1")
.insertAdjacentHTML(
"afterend",
[
"<p>Filter entries by content:",
'<input type="text" value="" id="searchbox" style="width: 50%">', '<input type="text" value="" id="searchbox" style="width: 50%">',
'<input type="submit" id="searchbox-submit" value="Filter"></p>' '<input type="submit" id="searchbox-submit" value="Filter"></p>',
].join('\n')); ].join("\n"),
);
function dofilter() { function doFilter() {
let query;
try { try {
var query = new RegExp($('#searchbox').val(), 'i'); query = new RegExp(document.querySelector("#searchbox").value, "i");
} } catch (e) {
catch (e) {
return; // not a valid regex (yet) return; // not a valid regex (yet)
} }
// find headers for the versions (What's new in Python X.Y.Z?) // find headers for the versions (What's new in Python X.Y.Z?)
$('#changelog h2').each(function(index1, h2) { const h2s = document.querySelectorAll("#changelog h2");
var h2_parent = $(h2).parent(); for (const h2 of h2s) {
var sections_found = 0; let sections_found = 0;
// find headers for the sections (Core, Library, etc.) // find headers for the sections (Core, Library, etc.)
h2_parent.find('h3').each(function(index2, h3) { const h3s = h2.parentNode.querySelectorAll("h3");
var h3_parent = $(h3).parent(); for (const h3 of h3s) {
var entries_found = 0; let entries_found = 0;
// find all the entries // find all the entries
h3_parent.find('li').each(function(index3, li) { const lis = h3.parentNode.querySelectorAll("li");
var li = $(li); for (let li of lis) {
// check if the query matches the entry // check if the query matches the entry
if (query.test(li.text())) { if (query.test(li.textContent)) {
li.show(); li.style.display = "block";
entries_found++; entries_found++;
} else {
li.style.display = "none";
} }
else {
li.hide();
} }
});
// if there are entries, show the section, otherwise hide it // if there are entries, show the section, otherwise hide it
if (entries_found > 0) { if (entries_found > 0) {
h3_parent.show(); h3.parentNode.style.display = "block";
sections_found++; sections_found++;
} else {
h3.parentNode.style.display = "none";
} }
else {
h3_parent.hide();
} }
}); if (sections_found > 0) {
if (sections_found > 0) h2.parentNode.style.display = "block";
h2_parent.show(); } else {
else h2.parentNode.style.display = "none";
h2_parent.hide();
});
} }
$('#searchbox').keyup(dofilter); }
$('#searchbox-submit').click(dofilter); }
document.querySelector("#searchbox").addEventListener("keyup", doFilter);
document
.querySelector("#searchbox-submit")
.addEventListener("click", doFilter);
}); });