From bd05f1c9f8212cf0c685fe2b197476f5ced05e2b Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 7 Oct 2020 01:13:26 -0700 Subject: [PATCH] eslint: Fix unicorn/prefer-query-selector. https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/prefer-query-selector.md Signed-off-by: Anders Kaseorg --- frontend_tests/node_tests/compose.js | 4 +- static/js/compose.js | 2 +- static/js/stats/stats.js | 58 ++++++++++++++-------------- static/js/util.js | 2 +- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js index b68e3c8545..760650540d 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -22,9 +22,6 @@ const _navigator = { }; const _document = { - getElementById() { - return $("#compose-textarea"); - }, execCommand() { return false; }, @@ -459,6 +456,7 @@ run_test("markdown_shortcuts", () => { ); }; + $("#compose-textarea")[0] = {}; $("#compose-textarea").range = function () { return { start: range_start, diff --git a/static/js/compose.js b/static/js/compose.js index be52cab0c9..2332b1f566 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -765,7 +765,7 @@ exports.handle_keydown = function (event, textarea) { // Ctrl + L: Insert a link to selected text wrap_text_with_markdown("[", "](url)"); const position = textarea.caret(); - const txt = document.getElementById(textarea[0].id); + const txt = textarea[0]; // Include selected text in between [] parentheses and insert '(url)' // where "url" should be automatically selected. diff --git a/static/js/stats/stats.js b/static/js/stats/stats.js index d2e1306d20..c9956daf57 100644 --- a/static/js/stats/stats.js +++ b/static/js/stats/stats.js @@ -178,24 +178,24 @@ function populate_messages_sent_over_time(data) { ); function add_hover_handler() { - document.getElementById("id_messages_sent_over_time").on("plotly_hover", (data) => { + document.querySelector("#id_messages_sent_over_time").on("plotly_hover", (data) => { $("#hoverinfo").show(); - document.getElementById("hover_date").innerText = + document.querySelector("#hover_date").innerText = data.points[0].data.text[data.points[0].pointNumber]; const values = [null, null, null]; data.points.forEach((trace) => { values[trace.curveNumber] = trace.y; }); - const hover_text_ids = ["hover_me", "hover_human", "hover_bot"]; - const hover_value_ids = ["hover_me_value", "hover_human_value", "hover_bot_value"]; + const hover_text_ids = ["#hover_me", "#hover_human", "#hover_bot"]; + const hover_value_ids = ["#hover_me_value", "#hover_human_value", "#hover_bot_value"]; for (let i = 0; i < values.length; i += 1) { if (values[i] !== null) { - document.getElementById(hover_text_ids[i]).style.display = "inline"; - document.getElementById(hover_value_ids[i]).style.display = "inline"; - document.getElementById(hover_value_ids[i]).innerText = values[i]; + document.querySelector(hover_text_ids[i]).style.display = "inline"; + document.querySelector(hover_value_ids[i]).style.display = "inline"; + document.querySelector(hover_value_ids[i]).innerText = values[i]; } else { - document.getElementById(hover_text_ids[i]).style.display = "none"; - document.getElementById(hover_value_ids[i]).style.display = "none"; + document.querySelector(hover_text_ids[i]).style.display = "none"; + document.querySelector(hover_value_ids[i]).style.display = "none"; } } }); @@ -302,7 +302,7 @@ function populate_messages_sent_over_time(data) { traces.bot.visible = "legendonly"; traces.me.visible = "legendonly"; } else { - const plotDiv = document.getElementById("id_messages_sent_over_time"); + const plotDiv = document.querySelector("#id_messages_sent_over_time"); traces.me.visible = plotDiv.data[0].visible; traces.human.visible = plotDiv.data[1].visible; traces.bot.visible = plotDiv.data[2].visible; @@ -636,7 +636,7 @@ function populate_messages_sent_by_message_type(data) { time_button = "cumulative"; $("#messages_by_type_cumulative_button").addClass("selected"); } - const totaldiv = document.getElementById("pie_messages_sent_by_type_total"); + const totaldiv = document.querySelector("#pie_messages_sent_by_type_total"); if (data.end_times.length < 365) { $("#pie_messages_sent_by_type button[data-time='year']").remove(); @@ -724,25 +724,25 @@ function populate_number_of_users(data) { } function add_hover_handler() { - document.getElementById("id_number_of_users").on("plotly_hover", (data) => { + document.querySelector("#id_number_of_users").on("plotly_hover", (data) => { $("#users_hover_info").show(); - document.getElementById("users_hover_date").innerText = + document.querySelector("#users_hover_date").innerText = data.points[0].data.text[data.points[0].pointNumber]; const values = [null, null, null]; data.points.forEach((trace) => { values[trace.curveNumber] = trace.y; }); const hover_value_ids = [ - "users_hover_1day_value", - "users_hover_15day_value", - "users_hover_all_time_value", + "#users_hover_1day_value", + "#users_hover_15day_value", + "#users_hover_all_time_value", ]; for (let i = 0; i < values.length; i += 1) { if (values[i] !== null) { - document.getElementById(hover_value_ids[i]).style.display = "inline"; - document.getElementById(hover_value_ids[i]).innerText = values[i]; + document.querySelector(hover_value_ids[i]).style.display = "inline"; + document.querySelector(hover_value_ids[i]).innerText = values[i]; } else { - document.getElementById(hover_value_ids[i]).style.display = "none"; + document.querySelector(hover_value_ids[i]).style.display = "none"; } } }); @@ -857,24 +857,24 @@ function populate_messages_read_over_time(data) { ); function add_hover_handler() { - document.getElementById("id_messages_read_over_time").on("plotly_hover", (data) => { + document.querySelector("#id_messages_read_over_time").on("plotly_hover", (data) => { $("#read_hover_info").show(); - document.getElementById("read_hover_date").innerText = + document.querySelector("#read_hover_date").innerText = data.points[0].data.text[data.points[0].pointNumber]; const values = [null, null]; data.points.forEach((trace) => { values[trace.curveNumber] = trace.y; }); - const read_hover_text_ids = ["read_hover_me", "read_hover_everyone"]; - const read_hover_value_ids = ["read_hover_me_value", "read_hover_everyone_value"]; + const read_hover_text_ids = ["#read_hover_me", "#read_hover_everyone"]; + const read_hover_value_ids = ["#read_hover_me_value", "#read_hover_everyone_value"]; for (let i = 0; i < values.length; i += 1) { if (values[i] !== null) { - document.getElementById(read_hover_text_ids[i]).style.display = "inline"; - document.getElementById(read_hover_value_ids[i]).style.display = "inline"; - document.getElementById(read_hover_value_ids[i]).innerText = values[i]; + document.querySelector(read_hover_text_ids[i]).style.display = "inline"; + document.querySelector(read_hover_value_ids[i]).style.display = "inline"; + document.querySelector(read_hover_value_ids[i]).innerText = values[i]; } else { - document.getElementById(read_hover_text_ids[i]).style.display = "none"; - document.getElementById(read_hover_value_ids[i]).style.display = "none"; + document.querySelector(read_hover_text_ids[i]).style.display = "none"; + document.querySelector(read_hover_value_ids[i]).style.display = "none"; } } }); @@ -971,7 +971,7 @@ function populate_messages_read_over_time(data) { traces.everyone.visible = true; traces.me.visible = "legendonly"; } else { - const plotDiv = document.getElementById("id_messages_read_over_time"); + const plotDiv = document.querySelector("#id_messages_read_over_time"); traces.me.visible = plotDiv.data[0].visible; traces.everyone.visible = plotDiv.data[1].visible; } diff --git a/static/js/util.js b/static/js/util.js index 099c3b18f5..6ed237d972 100644 --- a/static/js/util.js +++ b/static/js/util.js @@ -283,7 +283,7 @@ exports.convert_message_topic = function (message) { exports.clean_user_content_links = function (html) { const content = new DOMParser().parseFromString(html, "text/html").body; - for (const elt of content.getElementsByTagName("a")) { + for (const elt of content.querySelectorAll("a")) { // Ensure that all external links have target="_blank" // rel="opener noreferrer". This ensures that external links // never replace the Zulip webapp while also protecting