From 6cad1988a9e14026c3d765659a92c5a46c6bc552 Mon Sep 17 00:00:00 2001 From: Thomas Ip Date: Thu, 6 Jun 2019 11:50:36 +0800 Subject: [PATCH] jQuery: Use positional methods on single selectors. For selectors like `$('.element:first')`, we can simply write `$('.element').first()`. --- frontend_tests/node_tests/templates.js | 50 +++++++++++++------------- static/js/archive.js | 2 +- static/js/copy_and_paste.js | 2 +- static/js/drafts.js | 4 +-- static/js/message_list_view.js | 2 +- static/js/message_viewport.js | 4 +-- static/js/portico/signup.js | 4 +-- static/js/rows.js | 10 +++--- static/js/subs.js | 2 +- 9 files changed, 40 insertions(+), 40 deletions(-) diff --git a/frontend_tests/node_tests/templates.js b/frontend_tests/node_tests/templates.js index 9697a9fec8..5522105283 100644 --- a/frontend_tests/node_tests/templates.js +++ b/frontend_tests/node_tests/templates.js @@ -136,7 +136,7 @@ run_test('admin_default_streams_list', () => { html += render('admin_default_streams_list', args); }); html += ""; - var span = $(html).find(".default_stream_name:first"); + var span = $(html).find(".default_stream_name").first(); assert.equal(span.text(), "devel"); // When the logged in user is not admin @@ -149,7 +149,7 @@ run_test('admin_default_streams_list', () => { html += render('admin_default_streams_list', args); }); html += ""; - span = $(html).find(".default_stream_name:first"); + span = $(html).find(".default_stream_name").first(); assert.equal(span.text(), "devel"); }); @@ -292,7 +292,7 @@ run_test('admin_invites_list', () => { assert($(buttons[3]).hasClass("resend")); assert.equal($(buttons[3]).attr("data-invite-id"), 1); - var span = $(html).find(".email:first"); + var span = $(html).find(".email").first(); assert.equal(span.text(), "alice@zulip.com"); }); @@ -323,7 +323,7 @@ run_test('admin_user_group_list', () => { html += render('admin_user_group_list', args); html += ''; - var group_id = $(html).find('.user-group:first').prop('id'); + var group_id = $(html).find('.user-group').first().prop('id'); var group_pills_id = $(html).find('.user-group:first .pill-container').attr('data-group-pills'); var group_name_display = $(html).find('.user-group:first .name').text().trim().replace(/\s+/g, ' '); var group_description = $(html).find('.user-group:first .description').text().trim().replace(/\s+/g, ' '); @@ -399,7 +399,7 @@ run_test('alert_word_settings_item', () => { html += render('alert_word_settings_item', args); html += ""; - var li = $(html).find("li.alert-word-item:first"); + var li = $(html).find("li.alert-word-item").first(); var value = li.find('.value'); var button = li.find('button'); assert.equal(li.attr('data-word'), 'lunch'); @@ -504,7 +504,7 @@ run_test('bot_owner_select', () => { ], }; var html = render('bot_owner_select', args); - var option = $(html).find("option:last"); + var option = $(html).find("option").last(); assert.equal(option.val(), "hamlet@zulip.com"); assert.equal(option.text(), "Hamlet"); }); @@ -517,12 +517,12 @@ run_test('compose_invite_users', () => { can_subscribe_other_users: true, }; var html = render('compose-invite-users', args); - var button = $(html).find("button:first"); + var button = $(html).find("button").first(); assert.equal(button.text(), "translated: Subscribe"); args.can_subscribe_other_users = false; html = render('compose-invite-users', args); - button = $(html).find("button:first"); + button = $(html).find("button").first(); assert.equal(button.length, 0); }); @@ -532,7 +532,7 @@ run_test('compose_all_everyone', () => { name: 'all', }; var html = render('compose_all_everyone', args); - var button = $(html).find("button:first"); + var button = $(html).find("button").first(); assert.equal(button.text(), "translated: Yes, send"); var error_msg = $(html).find('span.compose-all-everyone-msg').text().trim(); assert.equal(error_msg, "translated: Are you sure you want to mention all 101 people in this stream?"); @@ -543,7 +543,7 @@ run_test('compose_announce', () => { count: '101', }; var html = render('compose_announce', args); - var button = $(html).find("button:first"); + var button = $(html).find("button").first(); assert.equal(button.text(), "translated: Yes, send"); var error_msg = $(html).find('span.compose-announce-msg').text().trim(); assert.equal(error_msg, "translated: This stream is reserved for announcements.\n \n Are you sure you want to message all 101 people in this stream?"); @@ -551,10 +551,10 @@ run_test('compose_announce', () => { run_test('compose_not_subscribed', () => { var html = render('compose_not_subscribed', {should_display_sub_button: true}); - var button = $(html).find("button:first"); + var button = $(html).find("button").first(); assert.equal(button.text(), "translated: Subscribe"); html = render('compose_not_subscribed', {should_display_sub_button: false}); - button = $(html).find("button:first"); + button = $(html).find("button").first(); assert.equal(button.length, 0); }); @@ -652,7 +652,7 @@ run_test('draft_table_body', () => { run_test('email_address_hint', () => { var html = render('email_address_hint'); - var li = $(html).find("li:first"); + var li = $(html).find("li").first(); assert.equal(li.text(), 'translated: The email will be forwarded to this stream'); }); @@ -769,7 +769,7 @@ run_test('group_pms', () => { }; var html = render('group_pms', args); - var a = $(html).find("a:first"); + var a = $(html).find("a").first(); assert.equal(a.text(), 'Alice and Bob'); }); @@ -820,7 +820,7 @@ run_test('invite_subscription', () => { }; var html = render('invite_subscription', args); - var input = $(html).find("label:first"); + var input = $(html).find("label").first(); assert.equal(input.text().trim(), "devel"); }); @@ -840,7 +840,7 @@ run_test('single_message', () => { var html = render('single_message', message); html = '
' + html + '
'; - var first_message = $(html).find("div.messagebox:first"); + var first_message = $(html).find("div.messagebox").first(); var first_message_text = first_message.find(".message_content").text().trim(); assert.equal(first_message_text, "This is message one."); @@ -1023,7 +1023,7 @@ run_test('new_stream_users', () => { var html = render('new_stream_users', args); - var label = $(html).find("label:first"); + var label = $(html).find("label").first(); assert.equal(label.text().trim(), 'King Lear (lear@zulip.com)'); }); @@ -1041,7 +1041,7 @@ run_test('non_editable_user_group', () => { html += render('non_editable_user_group', args); html += ''; - var group_id = $(html).find('.user-group:first').prop('id'); + var group_id = $(html).find('.user-group').first().prop('id'); var group_pills_id = $(html).find('.user-group:first .pill-container').attr('data-group-pills'); var group_name_display = $(html).find('.user-group:first .name').text().trim().replace(/\s+/g, ' '); var group_description = $(html).find('.user-group:first .description').text().trim().replace(/\s+/g, ' '); @@ -1214,7 +1214,7 @@ run_test('stream_sidebar_actions', () => { var html = render('stream_sidebar_actions', args); - var li = $(html).find("li:first"); + var li = $(html).find("li").first(); assert.equal(li.text().trim(), 'translated: Stream settings'); }); @@ -1266,7 +1266,7 @@ run_test('subscription_settings', () => { var div = $(html).find(".subscription-type"); assert(div.text().indexOf('private stream') > 0); - var anchor = $(html).find(".change-stream-privacy:first"); + var anchor = $(html).find(".change-stream-privacy").first(); assert.equal(anchor.text(), "[translated: Change]"); }); @@ -1329,7 +1329,7 @@ run_test('subscriptions', () => { html += render('subscriptions', args); html += ''; - var span = $(html).find(".stream-name:first"); + var span = $(html).find(".stream-name").first(); assert.equal(span.text(), 'devel'); }); @@ -1354,14 +1354,14 @@ run_test('tab_bar', () => { var html = render('tab_bar', args); - var a = $(html).find("li:first"); + var a = $(html).find("li").first(); assert.equal(a.text().trim(), 'Home'); }); run_test('topic_edit_form', () => { var html = render('topic_edit_form'); - var button = $(html).find("button:first"); + var button = $(html).find("button").first(); assert.equal(button.find("i").attr("class"), 'fa fa-check'); }); @@ -1443,7 +1443,7 @@ run_test('typing_notifications', () => { html += render('typing_notifications', args); html += ''; - var li = $(html).find('li:first'); + var li = $(html).find('li').first(); assert.equal(li.text(), 'Hamlet is typing...'); }); @@ -1588,7 +1588,7 @@ run_test('user_presence_rows', () => { html += render('user_presence_rows', args); html += ''; - var a = $(html).find("a:first"); + var a = $(html).find("a").first(); assert.equal(a.text().trim(), 'King Lear'); }); diff --git a/static/js/archive.js b/static/js/archive.js index 67502e4a31..83378e27e8 100644 --- a/static/js/archive.js +++ b/static/js/archive.js @@ -110,7 +110,7 @@ var rows = { return $(message_row).parent('.recipient_row'); }, first_message_in_group: function (message_group) { - return $('div.message_row:first', message_group); + return $('div.message_row', message_group).first(); }, id: function (message_row) { return parseFloat(message_row.attr('zid')); diff --git a/static/js/copy_and_paste.js b/static/js/copy_and_paste.js index f586ef48a3..e3adfb1ea5 100644 --- a/static/js/copy_and_paste.js +++ b/static/js/copy_and_paste.js @@ -228,7 +228,7 @@ exports.analyze_selection = function (selection) { // the compose_close button when you go off the end of the // last message if (endc.attr('id') === "bottom_whitespace" || endc.attr('id') === "compose_close") { - initial_end_tr = $(".message_row:last"); + initial_end_tr = $(".message_row").last(); // The selection goes off the end of the message feed, so // this is a multi-message selection. skip_same_td_check = true; diff --git a/static/js/drafts.js b/static/js/drafts.js index 45be444fad..65155ac054 100644 --- a/static/js/drafts.js +++ b/static/js/drafts.js @@ -393,12 +393,12 @@ function drafts_scroll(next_focus_draft_row) { activate_element(next_focus_draft_row[0].children[0]); // If focused draft is first draft, scroll to the top. - if ($(".draft-info-box:first")[0].parentElement === next_focus_draft_row[0]) { + if ($(".draft-info-box").first()[0].parentElement === next_focus_draft_row[0]) { $(".drafts-list")[0].scrollTop = 0; } // If focused draft is the last draft, scroll to the bottom. - if ($(".draft-info-box:last")[0].parentElement === next_focus_draft_row[0]) { + if ($(".draft-info-box").last()[0].parentElement === next_focus_draft_row[0]) { $(".drafts-list")[0].scrollTop = $('.drafts-list')[0].scrollHeight - $('.drafts-list').height(); } diff --git a/static/js/message_list_view.js b/static/js/message_list_view.js index 95aca8b6ae..0bfa7c8ed3 100644 --- a/static/js/message_list_view.js +++ b/static/js/message_list_view.js @@ -753,7 +753,7 @@ MessageListView.prototype = { // Insert new messages in to the last message group if (message_actions.append_messages.length > 0) { - last_message_row = table.find('.message_row:last').expectOne(); + last_message_row = table.find('.message_row').last().expectOne(); last_group_row = rows.get_message_recipient_row(last_message_row); dom_messages = $(_.map(message_actions.append_messages, function (message_container) { return self._get_message_template(message_container); diff --git a/static/js/message_viewport.js b/static/js/message_viewport.js index e1b28365eb..9543954d0a 100644 --- a/static/js/message_viewport.js +++ b/static/js/message_viewport.js @@ -177,8 +177,8 @@ function _visible_divs(selected_row, row_min_height, row_to_output, div_class, // We do this explicitly without merges and without recalculating // the feed bounds to keep this computation as cheap as possible. var visible = []; - var above_pointer = selected_row.prevAll("div." + div_class + ":lt(" + num_neighbors + ")"); - var below_pointer = selected_row.nextAll("div." + div_class + ":lt(" + num_neighbors + ")"); + var above_pointer = selected_row.prevAll("div." + div_class).slice(0, num_neighbors); + var below_pointer = selected_row.nextAll("div." + div_class).slice(0, num_neighbors); add_to_visible(selected_row, visible, top_of_feed.get(), bottom_of_feed.get(), require_fully_visible, row_to_output); add_to_visible(above_pointer, visible, top_of_feed.get(), bottom_of_feed.get(), diff --git a/static/js/portico/signup.js b/static/js/portico/signup.js index 42ba83b395..dd66f9122f 100644 --- a/static/js/portico/signup.js +++ b/static/js/portico/signup.js @@ -54,12 +54,12 @@ $(function () { if ($('.help-inline:not(:empty)').length === 0) { // Find the first input field present in the form that is // not hidden and disabled and store it in a variable. - var firstInputElement = $("input:not(:hidden, :disabled):first"); + var firstInputElement = $("input:not(:hidden, :disabled)").first(); // Focus on the first input field in the form. common.autofocus(firstInputElement); } else { // If input field with errors is present. // Find the input field having errors and stores it in a variable. - var inputElementWithError = $('.help-inline:not(:empty):first').parent().find('input'); + var inputElementWithError = $('.help-inline:not(:empty)').first().parent().find('input'); // Focus on the input field having errors. common.autofocus(inputElementWithError); } diff --git a/static/js/rows.js b/static/js/rows.js index 35bce15a39..6374620bf2 100644 --- a/static/js/rows.js +++ b/static/js/rows.js @@ -18,7 +18,7 @@ exports.next_visible = function (message_row) { if (next_recipient_rows.length === 0) { return $(); } - return $('.selectable_row:first', next_recipient_rows[0]); + return $('.selectable_row', next_recipient_rows[0]).first(); }; exports.prev_visible = function (message_row) { @@ -34,15 +34,15 @@ exports.prev_visible = function (message_row) { if (prev_recipient_rows.length === 0) { return $(); } - return $('.selectable_row:last', prev_recipient_rows[0]); + return $('.selectable_row', prev_recipient_rows[0]).last(); }; exports.first_visible = function () { - return $('.focused_table .selectable_row:first'); + return $('.focused_table .selectable_row').first(); }; exports.last_visible = function () { - return $('.focused_table .selectable_row:last'); + return $('.focused_table .selectable_row').last(); }; exports.id = function (message_row) { @@ -79,7 +79,7 @@ exports.get_closest_group = function (element) { }; exports.first_message_in_group = function (message_group) { - return $('div.message_row:first', message_group); + return $('div.message_row', message_group).first(); }; exports.get_message_recipient_row = function (message_row) { diff --git a/static/js/subs.js b/static/js/subs.js index c9baf74435..e8c0a34c16 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -655,7 +655,7 @@ exports.switch_rows = function (event) { // Prevent switching stream rows when creating a new stream return false; } else if (!active_data.id || active_data.row.hasClass('notdisplayed')) { - switch_row = $('div.stream-row:not(.notdisplayed):first'); + switch_row = $('div.stream-row:not(.notdisplayed)').first(); if ($('#search_stream_name').is(":focus")) { $('#search_stream_name').blur(); }