Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

templates: Forbid testing arrays for truthiness.

Anders Kaseorgea3cbd5ebd85
diff --git a/web/src/templates.ts b/web/src/templates.ts
index d0fcdc6eabc..f81b04bab28 100644
--- a/web/src/templates.ts
+++ b/web/src/templates.ts
@@ -9,6 +9,7 @@ import {postprocess_content} from "./postprocess_content.ts";
import {user_settings} from "./user_settings.ts";
const orig_escape_expression = Handlebars.Utils.escapeExpression;
+const orig_is_empty = Handlebars.Utils.isEmpty;
Handlebars.Utils.escapeExpression = (value: unknown): string => {
/* istanbul ignore if */
@@ -25,6 +26,22 @@ Handlebars.Utils.escapeExpression = (value: unknown): string => {
return (orig_escape_expression as (value: unknown) => string)(value);
};
+Handlebars.Utils.isEmpty = (value: unknown): boolean => {
+ /* istanbul ignore if */
+ if (
+ typeof value !== "string" &&
+ typeof value !== "number" &&
+ typeof value !== "boolean" &&
+ value !== undefined &&
+ (typeof value !== "object" || Array.isArray(value))
+ ) {
+ blueslip.error(
+ `Cannot test a value of type ${typeof value} in a Zulip Handlebars template`,
+ );
+ }
+ return orig_is_empty(value);
+};
+
// Below, we register Zulip-specific extensions to the Handlebars API.
//
// IMPORTANT: When adding a new Handlebars helper, update the
diff --git a/web/templates/components/banner.hbs b/web/templates/components/banner.hbs
index 59d4f40ebce..2e8ef7ca6af 100644
--- a/web/templates/components/banner.hbs
+++ b/web/templates/components/banner.hbs
@@ -7,7 +7,8 @@
{{> @partial-block .}}
{{/if}}
</span>
- {{#if buttons}}
+ {{#unless (eq buttons undefined)}}
+ {{#if buttons.length}}
<span class="banner-action-buttons">
{{!-- squash whitespace so :empty selector works when no buttons --}}
{{~!-- squash whitespace --~}}
@@ -20,7 +21,8 @@
{{/each}}
{{~!-- squash whitespace --~}}
</span>
- {{/if}}
+ {{/if}}
+ {{/unless}}
</span>
{{#if close_button}}
{{> icon_button custom_classes="banner-close-action banner-close-button" icon="close" intent=intent}}
diff --git a/web/templates/message_body.hbs b/web/templates/message_body.hbs
index bd16e251e3a..bdbc35dd420 100644
--- a/web/templates/message_body.hbs
+++ b/web/templates/message_body.hbs
@@ -75,7 +75,7 @@
<div class="message_length_controller"></div>
-{{#if (and (not is_hidden) msg.message_reactions)}}
+{{#if (and (not is_hidden) msg.message_reactions.length)}}
{{> message_reactions . }}
{{/if}}
diff --git a/web/templates/settings/uploaded_files_list.hbs b/web/templates/settings/uploaded_files_list.hbs
index e5f4fae3200..d76de2fcfa3 100644
--- a/web/templates/settings/uploaded_files_list.hbs
+++ b/web/templates/settings/uploaded_files_list.hbs
@@ -7,7 +7,7 @@
</td>
<td>{{ create_time_str }}</td>
<td>
- {{#if messages }}
+ {{#if messages.length}}
<div class="attachment-messages">
{{#each messages}}
<a class="ind-message" href="/#narrow/id/{{ this.id }}">