From 5f5071dd14208461425cf6d7888ad6b86126f68d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:01:01 +0200 Subject: [PATCH 1/7] Rewrite using vanilla JavaScript --- Doc/tools/static/changelog_search.js | 49 ++++++++++++++-------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index c881a9bd4c84a7..8e45ba8ee0e809 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -1,53 +1,52 @@ -$(document).ready(function() { +document.addEventListener('DOMContentLoaded', function() { // add the search form and bind the events - $('h1').after([ + document.querySelector('h1').insertAdjacentHTML('afterend', [ '

Filter entries by content:', '', '

' ].join('\n')); function dofilter() { + let query; try { - var query = new RegExp($('#searchbox').val(), 'i'); + query = new RegExp(document.querySelector('#searchbox').value, 'i'); } catch (e) { return; // not a valid regex (yet) } - // find headers for the versions (What's new in Python X.Y.Z?) - $('#changelog h2').each(function(index1, h2) { - var h2_parent = $(h2).parent(); - var sections_found = 0; + const h2s = document.querySelectorAll('#changelog h2'); + for(let h2 of h2s) { + let sections_found = 0; // find headers for the sections (Core, Library, etc.) - h2_parent.find('h3').each(function(index2, h3) { - var h3_parent = $(h3).parent(); - var entries_found = 0; - // find all the entries - h3_parent.find('li').each(function(index3, li) { - var li = $(li); + const h3s = h2.parentNode.querySelectorAll('h3'); + for(let h3 of h3s) { + let entries_found = 0; + const lis = h3.parentNode.querySelectorAll('li'); + for(let li of lis) { // check if the query matches the entry - if (query.test(li.text())) { - li.show(); + if (query.test(li.textContent)) { + li.style.display = 'block'; entries_found++; } else { - li.hide(); + li.style.display = 'none'; } - }); + } // if there are entries, show the section, otherwise hide it if (entries_found > 0) { - h3_parent.show(); + h3.parentNode.style.display = 'block'; sections_found++; } else { - h3_parent.hide(); + h3.parentNode.style.display = 'none'; } - }); + } if (sections_found > 0) - h2_parent.show(); + h2.parentNode.style.display = 'block'; else - h2_parent.hide(); - }); + h2.parentNode.style.display = 'none'; + } } - $('#searchbox').keyup(dofilter); - $('#searchbox-submit').click(dofilter); + document.querySelector('#searchbox').addEventListener('keyup', dofilter); + document.querySelector('#searchbox-submit').addEventListener('click', dofilter); }); From e8c055c4a1690acf933265e72bf36e9543d39277 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 12:55:58 +0200 Subject: [PATCH 2/7] Re-add comments --- Doc/tools/static/changelog_search.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index 8e45ba8ee0e809..39f03b314226b5 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -14,6 +14,7 @@ document.addEventListener('DOMContentLoaded', function() { catch (e) { return; // not a valid regex (yet) } + // find headers for the versions (What's new in Python X.Y.Z?) const h2s = document.querySelectorAll('#changelog h2'); for(let h2 of h2s) { let sections_found = 0; @@ -21,6 +22,7 @@ document.addEventListener('DOMContentLoaded', function() { const h3s = h2.parentNode.querySelectorAll('h3'); for(let h3 of h3s) { let entries_found = 0; + // find all the entries const lis = h3.parentNode.querySelectorAll('li'); for(let li of lis) { // check if the query matches the entry From b38d8401a247ea584f72c206ef72015d7111c9c4 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:37:08 +0200 Subject: [PATCH 3/7] Add braces --- Doc/tools/static/changelog_search.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index 39f03b314226b5..01684fbda2800a 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -43,10 +43,12 @@ document.addEventListener('DOMContentLoaded', function() { h3.parentNode.style.display = 'none'; } } - if (sections_found > 0) + if (sections_found > 0) { h2.parentNode.style.display = 'block'; - else + } + else { h2.parentNode.style.display = 'none'; + } } } document.querySelector('#searchbox').addEventListener('keyup', dofilter); From b96383aec4432cc37c1f31f7db1455c49df98797 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:27:33 +0200 Subject: [PATCH 4/7] Prefer const Co-authored-by: Tomas R --- Doc/tools/static/changelog_search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index 01684fbda2800a..3493a31be6fe89 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -16,7 +16,7 @@ document.addEventListener('DOMContentLoaded', function() { } // find headers for the versions (What's new in Python X.Y.Z?) const h2s = document.querySelectorAll('#changelog h2'); - for(let h2 of h2s) { + for(const h2 of h2s) { let sections_found = 0; // find headers for the sections (Core, Library, etc.) const h3s = h2.parentNode.querySelectorAll('h3'); From a92636429e056e79cbb6432c640ebffde7f40711 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:30:29 +0200 Subject: [PATCH 5/7] Format with Prettier --- .editorconfig | 4 +- Doc/tools/static/changelog_search.js | 105 ++++++++++++++------------- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/.editorconfig b/.editorconfig index 0169eed951cd3f..a6187d64f3ce46 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,6 @@ root = true -[*.{py,c,cpp,h,rst,md,yml}] +[*.{py,c,cpp,h,js,rst,md,yml}] trim_trailing_whitespace = true insert_final_newline = true indent_style = space @@ -11,5 +11,5 @@ indent_size = 4 [*.rst] indent_size = 3 -[*.yml] +[*.{js,yml}] indent_size = 2 diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index 3493a31be6fe89..f1941d142c9dc1 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -1,56 +1,59 @@ -document.addEventListener('DOMContentLoaded', function() { - // add the search form and bind the events - document.querySelector('h1').insertAdjacentHTML('afterend', [ - '

Filter entries by content:', - '', - '

' - ].join('\n')); +document.addEventListener("DOMContentLoaded", function () { + // add the search form and bind the events + document + .querySelector("h1") + .insertAdjacentHTML( + "afterend", + [ + "

Filter entries by content:", + '', + '

', + ].join("\n"), + ); - function dofilter() { - let query; - try { - query = new RegExp(document.querySelector('#searchbox').value, 'i'); - } - catch (e) { - return; // not a valid regex (yet) + function dofilter() { + let query; + try { + query = new RegExp(document.querySelector("#searchbox").value, "i"); + } catch (e) { + return; // not a valid regex (yet) + } + // find headers for the versions (What's new in Python X.Y.Z?) + const h2s = document.querySelectorAll("#changelog h2"); + for (const h2 of h2s) { + let sections_found = 0; + // find headers for the sections (Core, Library, etc.) + const h3s = h2.parentNode.querySelectorAll("h3"); + for (let h3 of h3s) { + let entries_found = 0; + // find all the entries + const lis = h3.parentNode.querySelectorAll("li"); + for (let li of lis) { + // check if the query matches the entry + if (query.test(li.textContent)) { + li.style.display = "block"; + entries_found++; + } else { + li.style.display = "none"; + } } - // find headers for the versions (What's new in Python X.Y.Z?) - const h2s = document.querySelectorAll('#changelog h2'); - for(const h2 of h2s) { - let sections_found = 0; - // find headers for the sections (Core, Library, etc.) - const h3s = h2.parentNode.querySelectorAll('h3'); - for(let h3 of h3s) { - let entries_found = 0; - // find all the entries - const lis = h3.parentNode.querySelectorAll('li'); - for(let li of lis) { - // check if the query matches the entry - if (query.test(li.textContent)) { - li.style.display = 'block'; - entries_found++; - } - else { - li.style.display = 'none'; - } - } - // if there are entries, show the section, otherwise hide it - if (entries_found > 0) { - h3.parentNode.style.display = 'block'; - sections_found++; - } - else { - h3.parentNode.style.display = 'none'; - } - } - if (sections_found > 0) { - h2.parentNode.style.display = 'block'; - } - else { - h2.parentNode.style.display = 'none'; - } + // if there are entries, show the section, otherwise hide it + if (entries_found > 0) { + h3.parentNode.style.display = "block"; + sections_found++; + } else { + h3.parentNode.style.display = "none"; } + } + if (sections_found > 0) { + h2.parentNode.style.display = "block"; + } else { + h2.parentNode.style.display = "none"; + } } - document.querySelector('#searchbox').addEventListener('keyup', dofilter); - document.querySelector('#searchbox-submit').addEventListener('click', dofilter); + } + document.querySelector("#searchbox").addEventListener("keyup", dofilter); + document + .querySelector("#searchbox-submit") + .addEventListener("click", dofilter); }); From dc1f94daac0f4da06e0ca6bc5d9697db29798915 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:33:30 +0200 Subject: [PATCH 6/7] Use camelCase for function name For example: https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Writing_style_guide/Code_style_guide/JavaScript#functions --- Doc/tools/static/changelog_search.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index f1941d142c9dc1..39e892f0622eda 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -11,7 +11,7 @@ document.addEventListener("DOMContentLoaded", function () { ].join("\n"), ); - function dofilter() { + function doFilter() { let query; try { query = new RegExp(document.querySelector("#searchbox").value, "i"); @@ -52,8 +52,8 @@ document.addEventListener("DOMContentLoaded", function () { } } } - document.querySelector("#searchbox").addEventListener("keyup", dofilter); + document.querySelector("#searchbox").addEventListener("keyup", doFilter); document .querySelector("#searchbox-submit") - .addEventListener("click", dofilter); + .addEventListener("click", doFilter); }); From cccf0a7d64419d3016758786e24c75ff9f9a6b28 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 12 Feb 2024 20:14:12 +0200 Subject: [PATCH 7/7] Prefer const Co-authored-by: Tomas R --- Doc/tools/static/changelog_search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tools/static/changelog_search.js b/Doc/tools/static/changelog_search.js index 39e892f0622eda..0a77c0d71ae937 100644 --- a/Doc/tools/static/changelog_search.js +++ b/Doc/tools/static/changelog_search.js @@ -24,7 +24,7 @@ document.addEventListener("DOMContentLoaded", function () { let sections_found = 0; // find headers for the sections (Core, Library, etc.) const h3s = h2.parentNode.querySelectorAll("h3"); - for (let h3 of h3s) { + for (const h3 of h3s) { let entries_found = 0; // find all the entries const lis = h3.parentNode.querySelectorAll("li");