Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
js: Fix various sloppy uses of String#replace.
Anders Kaseorg513207523cd5
diff --git a/frontend_tests/node_tests/markdown.js b/frontend_tests/node_tests/markdown.jsindex 916a6827b6e..68599faf98e 100644--- a/frontend_tests/node_tests/markdown.js+++ b/frontend_tests/node_tests/markdown.js@@ -773,39 +773,39 @@ test("translate_emoticons_to_names", () => {// Extensive tests.// The following code loops over the test cases and each emoticon conversion// to generate multiple test cases.- const testcases = [- {name: "only emoticon", original: "<original>", expected: "<converted>"},- {name: "space at start", original: " <original>", expected: " <converted>"},- {name: "space at end", original: "<original> ", expected: "<converted> "},- {name: "symbol at end", original: "<original>!", expected: "<converted>!"},- {name: "symbol at start", original: "Hello,<original>", expected: "Hello,<converted>"},- {name: "after a word", original: "Hello<original>", expected: "Hello<original>"},- {name: "between words", original: "Hello<original>World", expected: "Hello<original>World"},- {- name: "end of sentence",- original: "End of sentence. <original>",- expected: "End of sentence. <converted>",- },- {- name: "between symbols",- original: "Hello.<original>! World.",- expected: "Hello.<original>! World.",- },- {- name: "before end of sentence",- original: "Hello <original>!",- expected: "Hello <converted>!",- },- ];for (const [shortcut, full_name] of Object.entries(emoji_codes.emoticon_conversions)) {- for (const t of testcases) {- const converted_value = full_name;- let original = t.original;- let expected = t.expected;- original = original.replace(/(<original>)/g, shortcut);- expected = expected- .replace(/(<original>)/g, shortcut)- .replace(/(<converted>)/g, converted_value);+ for (const {original, expected} of [+ {name: `only emoticon`, original: shortcut, expected: full_name},+ {name: `space at start`, original: ` ${shortcut}`, expected: ` ${full_name}`},+ {name: `space at end`, original: `${shortcut} `, expected: `${full_name} `},+ {name: `symbol at end`, original: `${shortcut}!`, expected: `${full_name}!`},+ {+ name: `symbol at start`,+ original: `Hello,${shortcut}`,+ expected: `Hello,${full_name}`,+ },+ {name: `after a word`, original: `Hello${shortcut}`, expected: `Hello${shortcut}`},+ {+ name: `between words`,+ original: `Hello${shortcut}World`,+ expected: `Hello${shortcut}World`,+ },+ {+ name: `end of sentence`,+ original: `End of sentence. ${shortcut}`,+ expected: `End of sentence. ${full_name}`,+ },+ {+ name: `between symbols`,+ original: `Hello.${shortcut}! World.`,+ expected: `Hello.${shortcut}! World.`,+ },+ {+ name: `before end of sentence`,+ original: `Hello ${shortcut}!`,+ expected: `Hello ${full_name}!`,+ },+ ]) {const result = markdown.translate_emoticons_to_names(original);assert.equal(result, expected);}diff --git a/static/js/click_handlers.js b/static/js/click_handlers.jsindex 3c14bb8e3a6..2d14a29f64f 100644--- a/static/js/click_handlers.js+++ b/static/js/click_handlers.js@@ -784,11 +784,9 @@ export function initialize() {hotspots.close_hotspot_icon(this);// show popover- const hotspot_name = $(e.target)- .closest(".hotspot-icon")- .attr("id")- .replace("hotspot_", "")- .replace("_icon", "");+ const [, hotspot_name] = /^hotspot_(.*)_icon$/.exec(+ $(e.target).closest(".hotspot-icon").attr("id"),+ );const overlay_name = "hotspot_" + hotspot_name + "_overlay";overlays.open_overlay({@@ -817,7 +815,7 @@ export function initialize() {const overlay_name = $(this).closest(".hotspot.overlay").attr("id");- const hotspot_name = overlay_name.replace("hotspot_", "").replace("_overlay", "");+ const [, hotspot_name] = /^hotspot_(.*)_overlay$/.exec(overlay_name);// Comment below to disable marking hotspots as read in productionhotspots.post_hotspot_as_read(hotspot_name);diff --git a/static/js/lightbox.js b/static/js/lightbox.jsindex a9045a06760..2c4c503d1e1 100644--- a/static/js/lightbox.js+++ b/static/js/lightbox.js@@ -283,7 +283,7 @@ export function build_open_image_function(on_close) {// the lightbox. A better fix will be to check a// `data-thumbnail-src` attribute that we add to the// canvas elements.- $preview_src = $preview_src.replace(/.{4}$/, "thumbnail");+ $preview_src = $preview_src.slice(0, -"full".length) + "thumbnail";payload = asset_map.get($preview_src);}if (payload === undefined) {diff --git a/static/js/markdown.js b/static/js/markdown.jsindex f51b90f8b67..67e46a36350 100644--- a/static/js/markdown.js+++ b/static/js/markdown.js@@ -218,21 +218,16 @@ export function apply_markdown(message) {},silencedMentionHandler(quote) {// Silence quoted mentions.- const user_mention_re = /<span[^>]*user-mention[^>]*data-user-id="(\d+|\*)"[^>]*>@/gm;- quote = quote.replace(user_mention_re, (match) => {- match = match.replace(/"user-mention"/g, '"user-mention silent"');- match = match.replace(/>@/g, ">");- return match;- });+ quote = quote.replace(+ /(<span class="user-mention)(" data-user-id="(\d+|\*)">)@/g,+ "$1 silent$2",+ );// Silence quoted user group mentions.- const user_group_re =- /<span[^>]*user-group-mention[^>]*data-user-group-id="\d+"[^>]*>@/gm;- quote = quote.replace(user_group_re, (match) => {- match = match.replace(/"user-group-mention"/g, '"user-group-mention silent"');- match = match.replace(/>@/g, ">");- return match;- });+ quote = quote.replace(+ /(<span class="user-group-mention)(" data-user-group-id="\d+">)@/g,+ "$1 silent$2",+ );// In most cases, if you are being mentioned in the message you're quoting, you wouldn't// mention yourself outside of the blockquote (and, above it). If that you do that, thediff --git a/static/js/notifications.js b/static/js/notifications.jsindex 35b6692cb93..cfc199bc16d 100644--- a/static/js/notifications.js+++ b/static/js/notifications.js@@ -256,10 +256,10 @@ export function process_notification(notification) {content = "New private message from " + message.sender_full_name;}key = message.display_reply_to;- other_recipients = message.display_reply_to;// Remove the sender from the list of other recipients- other_recipients = other_recipients.replace(", " + message.sender_full_name, "");- other_recipients = other_recipients.replace(message.sender_full_name + ", ", "");+ other_recipients = `, ${message.display_reply_to}, `+ .replace(`, ${message.sender_full_name}, `, ", ")+ .slice(", ".length, -", ".length);notification_source = "pm";} else {key = message.sender_full_name + " to " + message.stream + " > " + topic;@@ -736,7 +736,7 @@ export function handle_global_notification_updates(notification_name, setting) {}if (settings_config.stream_notification_settings.includes(notification_name)) {- notification_name = notification_name.replace("enable_stream_", "");+ [, notification_name] = /^enable_stream_(.*)$/.exec(notification_name);stream_ui_updates.update_notification_setting_checkbox(notification_name);}diff --git a/static/js/portico/landing-page.js b/static/js/portico/landing-page.jsindex 17cedbf921c..774909552e6 100644--- a/static/js/portico/landing-page.js+++ b/static/js/portico/landing-page.js@@ -130,7 +130,7 @@ const events = function () {// get the location url like `zulip.com/features/`, cut off the trailing// `/` and then split by `/` to get ["zulip.com", "features"], then// pop the last element to get the current section (eg. `features`).- const location = window.location.pathname.replace(/\/#*$/, "").split(/\//).pop();+ const location = window.location.pathname.replace(/\/$/, "").split(/\//).pop();$(`[data-on-page='${CSS.escape(location)}']`).addClass("active");diff --git a/static/js/reload.js b/static/js/reload.jsindex df7d1b1784e..5a8e5355994 100644--- a/static/js/reload.js+++ b/static/js/reload.js@@ -107,13 +107,11 @@ function preserve_state(send_after_reload, save_pointer, save_narrow, save_compo// Check if we're doing a compose-preserving reload. This must be// done before the first call to get_eventsexport function initialize() {- const location = window.location.toString();- const hash_fragment = location.slice(location.indexOf("#") + 1);-- // hash_fragment should be e.g. `reload:12345123412312`- if (hash_fragment.search("reload:") !== 0) {+ // location.hash should be e.g. `#reload:12345123412312`+ if (!location.hash.startsWith("#reload:")) {return;}+ const hash_fragment = location.hash.slice("#".length);// Using the token, recover the saved pre-reload data from local// storage. Afterwards, we clear the reload entry from local@@ -131,7 +129,7 @@ export function initialize() {}ls.remove(hash_fragment);- fragment = fragment.replace(/^reload:/, "");+ [, fragment] = /^#reload:(.*)/.exec(fragment);const keyvals = fragment.split("+");const vars = {};diff --git a/static/js/rendered_markdown.js b/static/js/rendered_markdown.jsindex bc70e06acb7..df253a843ea 100644--- a/static/js/rendered_markdown.js+++ b/static/js/rendered_markdown.js@@ -176,7 +176,7 @@ export const update_elements = (content) => {});content.find("span.timestamp-error").each(function () {- const time_str = $(this).text().replace("Invalid time format: ", "");+ const [, time_str] = /^Invalid time format: (.*)$/.exec($(this).text());const text = $t({defaultMessage: "Invalid time format: {timestamp}"},{timestamp: time_str},diff --git a/static/js/settings_org.js b/static/js/settings_org.jsindex 60e6258a1eb..a35b78f2ecd 100644--- a/static/js/settings_org.js+++ b/static/js/settings_org.js@@ -210,7 +210,7 @@ export function extract_property_name(elem, for_realm_default_settings) {// ID approach.return elem.attr("name");}- return elem.attr("id").split("-").join("_").replace("id_", "");+ return /^id_(.*)$/.exec(elem.attr("id").split("-").join("_"))[1];}function get_subsection_property_elements(element) {@@ -989,7 +989,7 @@ export function register_save_discard_widget_handlers(// ID approach.property_name = input_elem.attr("name");} else {- property_name = input_elem.attr("id").replace("id_realm_", "");+ [, property_name] = /^id_realm_(.*)$/.exec(input_elem.attr("id"));}data[property_name] = input_value;}@@ -1010,7 +1010,7 @@ export function register_save_discard_widget_handlers(// The organization settings system has some coupled// fields that must be submitted together, which is// managed by the get_complete_data_for_subsection function.- const subsection_id = save_button.attr("id").replace("org-submit-", "");+ const [, subsection_id] = /^org-submit-(.*)$/.exec(save_button.attr("id"));const subsection = subsection_id.split("-").join("_");extra_data = get_complete_data_for_subsection(subsection);}diff --git a/static/js/settings_users.js b/static/js/settings_users.jsindex 3f6701f0046..09485974d69 100644--- a/static/js/settings_users.js+++ b/static/js/settings_users.js@@ -103,11 +103,7 @@ export function update_view_on_deactivate(user_id) {if (user_role) {const user_id = row.data("user-id");- user_role.text(- "%state (%role)"- .replace("%state", $t({defaultMessage: "Deactivated"}))- .replace("%role", people.get_user_type(user_id)),- );+ user_role.text(`${$t({defaultMessage: "Deactivated"})} (${people.get_user_type(user_id)})`);}}diff --git a/static/js/stats/stats.js b/static/js/stats/stats.jsindex 3337b80f446..aeea3482b53 100644--- a/static/js/stats/stats.js+++ b/static/js/stats/stats.js@@ -80,7 +80,10 @@ function update_last_full_update(end_times) {month: "long",day: "numeric",});- const locale_time = update_time.toLocaleTimeString().replace(":00 ", " ");+ const locale_time = update_time.toLocaleTimeString(undefined, {+ hour: "numeric",+ minute: "numeric",+ });$("#id_last_full_update").text(locale_time + " on " + locale_date);$("#id_last_full_update").closest(".last-update").show();diff --git a/static/js/topic_list_data.js b/static/js/topic_list_data.jsindex e6f39736a39..eee8402f34f 100644--- a/static/js/topic_list_data.js+++ b/static/js/topic_list_data.js@@ -36,7 +36,9 @@ export function get_list_info(stream_id, zoomed) {let topic_display_name = topic_name;if (resolved) {- topic_display_name = topic_display_name.replace(message_edit.RESOLVED_TOPIC_PREFIX, "");+ topic_display_name = topic_display_name.slice(+ message_edit.RESOLVED_TOPIC_PREFIX.length,+ );}if (!zoomed) {