js: Avoid href="#" for buttons with a JavaScript click handler.

As seen in
https://chat.zulip.org/#narrow/stream/9-issues/topic/edit.20history.20bug/near/1320430,
clicking such a link takes you to the user's default view if the click
handler throws an exception before doing preventDefault().

There hrefs also have the negative effect of having your browser claim
that clicking the link will navigate you to the default view, which it
won't.

Comes with a linter rule to prevent future instances, since it seems
there are some recently added ones, though they are likely the result
of copy/paste.
This commit is contained in:
Tim Abbott
2022-02-08 17:38:12 -08:00
parent 03d3439784
commit 729c09074a
11 changed files with 32 additions and 27 deletions

View File

@@ -2,7 +2,7 @@
<ul class="nav nav-list actions_popover"> <ul class="nav nav-list actions_popover">
{{#if should_display_hide_option}} {{#if should_display_hide_option}}
<li> <li>
<a href="#" class="rehide_muted_user_message" data-message-id="{{message_id}}"> <a class="rehide_muted_user_message" data-message-id="{{message_id}}">
<i class="fa fa-eye-slash" aria-hidden="true"></i> <i class="fa fa-eye-slash" aria-hidden="true"></i>
{{t "Hide muted message again" }} {{t "Hide muted message again" }}
</a> </a>
@@ -11,7 +11,7 @@
{{#if should_display_edit_and_view_source}} {{#if should_display_edit_and_view_source}}
<li> <li>
<a href="#" class="popover_edit_message" data-message-id="{{message_id}}"> <a class="popover_edit_message" data-message-id="{{message_id}}">
<i class="{{#if use_edit_icon}}fa fa-pencil{{else}}fa fa-file-code-o{{/if}}" aria-hidden="true"></i> {{editability_menu_item}} <i class="{{#if use_edit_icon}}fa fa-pencil{{else}}fa fa-file-code-o{{/if}}" aria-hidden="true"></i> {{editability_menu_item}}
<span class="hotkey-hint">(e)</span> <span class="hotkey-hint">(e)</span>
</a> </a>
@@ -20,7 +20,7 @@
{{#if should_display_quote_and_reply}} {{#if should_display_quote_and_reply}}
<li> <li>
<a href="#" class="respond_button" data-message-id="{{message_id}}"> <a class="respond_button" data-message-id="{{message_id}}">
<i class="fa fa-reply" aria-hidden="true"></i> {{t "Quote and reply or forward" }} <i class="fa fa-reply" aria-hidden="true"></i> {{t "Quote and reply or forward" }}
<span class="hotkey-hint">(>)</span> <span class="hotkey-hint">(>)</span>
</a> </a>
@@ -29,7 +29,7 @@
{{#if should_display_reminder_option}} {{#if should_display_reminder_option}}
<li> <li>
<a href="#" class='reminder_button' data-message-id="{{message_id}}"> <a class='reminder_button' data-message-id="{{message_id}}">
<i class="fa fa-bell" aria-hidden="true"></i> {{t "Remind me about this" }} <i class="fa fa-bell" aria-hidden="true"></i> {{t "Remind me about this" }}
</a> </a>
</li> </li>
@@ -37,7 +37,7 @@
{{#if should_display_collapse}} {{#if should_display_collapse}}
<li> <li>
<a href="#" class="popover_toggle_collapse" data-message-id="{{message_id}}"> <a class="popover_toggle_collapse" data-message-id="{{message_id}}">
<i class="fa fa-minus" aria-hidden="true"></i> <i class="fa fa-minus" aria-hidden="true"></i>
{{t "Collapse" }} <span class="hotkey-hint">()</span> {{t "Collapse" }} <span class="hotkey-hint">()</span>
</a> </a>
@@ -46,7 +46,7 @@
{{#if should_display_uncollapse}} {{#if should_display_uncollapse}}
<li> <li>
<a href="#" class="popover_toggle_collapse" data-message-id="{{message_id}}"> <a class="popover_toggle_collapse" data-message-id="{{message_id}}">
<i class="fa fa-plus" aria-hidden="true"></i> <i class="fa fa-plus" aria-hidden="true"></i>
{{t "Un-collapse" }} <span class="hotkey-hint">()</span> {{t "Un-collapse" }} <span class="hotkey-hint">()</span>
</a> </a>
@@ -55,7 +55,7 @@
{{#if should_display_edit_history_option}} {{#if should_display_edit_history_option}}
<li> <li>
<a href="#" class="view_edit_history" data-message-id="{{message_id}}"> <a class="view_edit_history" data-message-id="{{message_id}}">
<i class="fa fa-clock-o" aria-hidden="true"></i> <i class="fa fa-clock-o" aria-hidden="true"></i>
{{t "View edit history" }} {{t "View edit history" }}
</a> </a>
@@ -63,7 +63,7 @@
{{/if}} {{/if}}
{{#if should_display_delete_option}} {{#if should_display_delete_option}}
<li> <li>
<a href="#" class="delete_message" data-message-id="{{message_id}}"> <a class="delete_message" data-message-id="{{message_id}}">
<i class="fa fa-times" aria-hidden="true"></i> <i class="fa fa-times" aria-hidden="true"></i>
{{t "Delete message" }} {{t "Delete message" }}
</a> </a>
@@ -71,7 +71,7 @@
{{/if}} {{/if}}
{{#if can_mute_topic}} {{#if can_mute_topic}}
<li> <li>
<a href="#" class="popover_mute_topic" data-msg-stream-id="{{stream_id}}" data-msg-topic="{{topic}}"> <a class="popover_mute_topic" data-msg-stream-id="{{stream_id}}" data-msg-topic="{{topic}}">
<i class="fa fa-bell-slash" aria-hidden="true"></i> <i class="fa fa-bell-slash" aria-hidden="true"></i>
{{#tr}}Mute the topic <b>{topic}</b>{{/tr}} <span class="hotkey-hint">(M)</span> {{#tr}}Mute the topic <b>{topic}</b>{{/tr}} <span class="hotkey-hint">(M)</span>
</a> </a>
@@ -80,7 +80,7 @@
{{#if can_unmute_topic}} {{#if can_unmute_topic}}
<li> <li>
<a href="#" class="popover_unmute_topic" data-msg-stream-id="{{stream_id}}" data-msg-topic="{{topic}}"> <a class="popover_unmute_topic" data-msg-stream-id="{{stream_id}}" data-msg-topic="{{topic}}">
<i class="fa fa-bell" aria-hidden="true"></i> <i class="fa fa-bell" aria-hidden="true"></i>
{{#tr}}Unmute the topic <b>{topic}</b>{{/tr}} {{#tr}}Unmute the topic <b>{topic}</b>{{/tr}}
</a> </a>
@@ -89,7 +89,7 @@
{{#if should_display_add_reaction_option}} {{#if should_display_add_reaction_option}}
<li> <li>
<a href="#" class="reaction_button" data-message-id="{{message_id}}"> <a class="reaction_button" data-message-id="{{message_id}}">
<i class="fa fa-smile-o" aria-hidden="true"></i> <i class="fa fa-smile-o" aria-hidden="true"></i>
{{#tr}}Add emoji reaction{{/tr}} {{#tr}}Add emoji reaction{{/tr}}
</a> </a>

View File

@@ -1,6 +1,6 @@
<ul class="nav" role="navigation"> <ul class="nav" role="navigation">
<li class="dropdown actual-dropdown-menu" id="gear-menu"> <li class="dropdown actual-dropdown-menu" id="gear-menu">
<a id="settings-dropdown" href="#" role="button" class="dropdown-toggle" data-target="nada" data-toggle="dropdown" title="{{t 'Menu' }} (g)"> <a id="settings-dropdown" role="button" class="dropdown-toggle" data-target="nada" data-toggle="dropdown" title="{{t 'Menu' }} (g)">
<i class="fa fa-cog settings-dropdown-cog" aria-hidden="true"></i> <i class="fa fa-cog settings-dropdown-cog" aria-hidden="true"></i>
</a> </a>
<ul class="dropdown-menu" role="menu" aria-labelledby="settings-dropdown"> <ul class="dropdown-menu" role="menu" aria-labelledby="settings-dropdown">

View File

@@ -83,7 +83,7 @@
</div> </div>
</div> </div>
<div id="topics_header"> <div id="topics_header">
<a href="" class="show-all-streams"> <i class="fa fa-chevron-left" aria-hidden="true"></i>{{t 'Back to streams' }}</a> <a class="show-all-streams"> <i class="fa fa-chevron-left" aria-hidden="true"></i>{{t 'Back to streams' }}</a>
</div> </div>
<div id="stream-filters-container" class="scrolling_list" data-simplebar> <div id="stream-filters-container" class="scrolling_list" data-simplebar>
<ul id="stream_filters" class="filters"></ul> <ul id="stream_filters" class="filters"></ul>

View File

@@ -1,6 +1,6 @@
<li class="topic-list-item show-more-topics bottom_left_row {{#unless more_topics_unreads}}zero-topic-unreads{{/unless}}"> <li class="topic-list-item show-more-topics bottom_left_row {{#unless more_topics_unreads}}zero-topic-unreads{{/unless}}">
<span class='topic-box'> <span class='topic-box'>
<a class="topic-name" href="#">{{t "more topics" }}</a> <a class="topic-name">{{t "more topics" }}</a>
<span class="unread_count {{#unless more_topics_unreads}}zero_count{{/unless}}"> <span class="unread_count {{#unless more_topics_unreads}}zero_count{{/unless}}">
{{more_topics_unreads}} {{more_topics_unreads}}
</span> </span>

View File

@@ -8,7 +8,7 @@
<div class="column-middle" id="navbar-middle"> <div class="column-middle" id="navbar-middle">
<div class="column-middle-inner"> <div class="column-middle-inner">
<div id="streamlist-toggle" {{#if embedded}}class="hide-streamlist-toggle-visibility"{{/if}} title="{{t 'Stream list' }} (q)"> <div id="streamlist-toggle" {{#if embedded}}class="hide-streamlist-toggle-visibility"{{/if}} title="{{t 'Stream list' }} (q)">
<a href="#" id="streamlist-toggle-button" role="button"><i class="fa fa-reorder" aria-hidden="true"></i> <a id="streamlist-toggle-button" role="button"><i class="fa fa-reorder" aria-hidden="true"></i>
<span id="streamlist-toggle-unreadcount">0</span> <span id="streamlist-toggle-unreadcount">0</span>
</a> </a>
</div> </div>
@@ -46,7 +46,7 @@
</div> </div>
<div class="column-right"> <div class="column-right">
<div id="userlist-toggle" title="{{t 'User list' }} (w)"> <div id="userlist-toggle" title="{{t 'User list' }} (w)">
<a href="#" id="userlist-toggle-button" role="button"><i class="fa fa-group" aria-hidden="true"></i> <a id="userlist-toggle-button" role="button"><i class="fa fa-group" aria-hidden="true"></i>
<span id="userlist-toggle-unreadcount">0</span> <span id="userlist-toggle-unreadcount">0</span>
</a> </a>
</div> </div>

View File

@@ -1,37 +1,37 @@
{{! Contents of the "remind me about this actions" popup }} {{! Contents of the "remind me about this actions" popup }}
<ul class="nav nav-list remind_me_popover"> <ul class="nav nav-list remind_me_popover">
<li> <li>
<a href="#" class="remind in_20m" data-message-id="{{message.id}}"> <a class="remind in_20m" data-message-id="{{message.id}}">
{{t "in 20 minutes" }} {{t "in 20 minutes" }}
</a> </a>
</li> </li>
<li> <li>
<a href="#" class="remind in_1h" data-message-id="{{message.id}}"> <a class="remind in_1h" data-message-id="{{message.id}}">
{{t "in 1 hour" }} {{t "in 1 hour" }}
</a> </a>
</li> </li>
<li> <li>
<a href="#" class="remind in_3h" data-message-id="{{message.id}}"> <a class="remind in_3h" data-message-id="{{message.id}}">
{{t "in 3 hours" }} {{t "in 3 hours" }}
</a> </a>
</li> </li>
<li> <li>
<a href="#" class="remind tomo" data-message-id="{{message.id}}"> <a class="remind tomo" data-message-id="{{message.id}}">
{{t "Tomorrow" }} {{t "Tomorrow" }}
</a> </a>
</li> </li>
<li> <li>
<a href="#" class="remind nxtw" data-message-id="{{message.id}}"> <a class="remind nxtw" data-message-id="{{message.id}}">
{{t "Next week" }} {{t "Next week" }}
</a> </a>
</li> </li>
<li> <li>
<a href="#" class='remind custom' data-message-id="{{message.id}}"> <a class='remind custom' data-message-id="{{message.id}}">
{{t "Select date and time" }} {{t "Select date and time" }}
</a> </a>
</li> </li>

View File

@@ -14,7 +14,7 @@
{{#if is_admin}} {{#if is_admin}}
<td> <td>
<span class="referred_by"> <span class="referred_by">
<a data-user-id="{{invited_by_user_id}}" href="#" class="view_user_profile">{{referrer_name}}</a> <a data-user-id="{{invited_by_user_id}}" class="view_user_profile">{{referrer_name}}</a>
</span> </span>
</td> </td>
{{/if}} {{/if}}

View File

@@ -26,7 +26,7 @@
{{#if no_owner }} {{#if no_owner }}
{{bot_owner_full_name}} {{bot_owner_full_name}}
{{else}} {{else}}
<a data-user-id="{{bot_owner_id}}" href="#" class="view_user_profile">{{bot_owner_full_name}}</a> <a data-user-id="{{bot_owner_id}}" class="view_user_profile">{{bot_owner_full_name}}</a>
{{/if}} {{/if}}
</span> </span>
</td> </td>

View File

@@ -23,8 +23,8 @@
<div> <div>
<a href="#" draggable="false" class="subs_set_all_users">{{t "Check all" }}</a> | <a draggable="false" class="subs_set_all_users">{{t "Check all" }}</a> |
<a href="#" draggable="false" class="subs_unset_all_users">{{t "Uncheck all" }}</a> <a draggable="false" class="subs_unset_all_users">{{t "Uncheck all" }}</a>
</div> </div>
<div id="user-checkboxes-simplebar-wrapper" data-simplebar> <div id="user-checkboxes-simplebar-wrapper" data-simplebar>

View File

@@ -26,7 +26,7 @@
{{#if has_starred_messages}} {{#if has_starred_messages}}
<li class="hidden-for-spectators"> <li class="hidden-for-spectators">
<a href="#" class="sidebar-popover-unstar-all-in-topic" data-stream-id="{{ stream_id }}" data-topic-name="{{ topic_name }}"> <a class="sidebar-popover-unstar-all-in-topic" data-stream-id="{{ stream_id }}" data-topic-name="{{ topic_name }}">
<i class="fa fa-star-o" aria-hidden="true"></i> <i class="fa fa-star-o" aria-hidden="true"></i>
{{#tr}}Unstar all messages in topic{{/tr}} {{#tr}}Unstar all messages in topic{{/tr}}
</a> </a>

View File

@@ -766,6 +766,11 @@ handlebars_rules = RuleList(
"pattern": r'"{{t "', "pattern": r'"{{t "',
"description": "Invalid quoting for HTML element with translated string.", "description": "Invalid quoting for HTML element with translated string.",
}, },
{
"pattern": r'href="#"',
"description": 'Avoid href="#" for elements with a JavaScript click handler.',
"exclude": {"static/templates/navbar.hbs"},
},
], ],
) )