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.

message-parser: Check image and audio elements for has_attachment.

Lauryn Menarde12875a1119c
diff --git a/web/src/message_parser.ts b/web/src/message_parser.ts
index b7ca7daf9f0..3c8827d3c8b 100644
--- a/web/src/message_parser.ts
+++ b/web/src/message_parser.ts
@@ -21,7 +21,10 @@ export function message_has_image(message_content: string): boolean {
}
export function message_has_attachment(message_content: string): boolean {
- return is_element_in_message_content(message_content, "a[href^='/user_uploads']");
+ return is_element_in_message_content(
+ message_content,
+ "a[href^='/user_uploads'], img[src^='/user_uploads'], audio[src^='/user_uploads']",
+ );
}
export function message_has_reaction(message: Message): boolean {
diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjs
index e0d91b24744..4e8433f8e50 100644
--- a/web/tests/filter.test.cjs
+++ b/web/tests/filter.test.cjs
@@ -1388,6 +1388,11 @@ test("predicate_basics", ({override}) => {
);
assert.ok(!predicate({type: stream_message}));
+ const inline_audio_msg = {
+ content:
+ '<p><audio controls preload="metadata" src="/user_uploads/randompath/test.mp3" title="zulip.mp3"></audio></p>',
+ };
+
const inline_img_msg = {
content:
'<p><img alt="Screenshot" class="inline-image" data-original-content-type="image/png" data-original-dimensions="1488x1130" data-original-src="/user_uploads/randompath/test.png" src="/user_uploads/thumbnail/randompath/test.png/840x560.webp"></p>',
@@ -1437,6 +1442,7 @@ test("predicate_basics", ({override}) => {
};
predicate = get_predicate([["has", "non_valid_operand"]]);
+ assert.ok(!predicate(inline_audio_msg));
assert.ok(!predicate(inline_img_msg));
assert.ok(!predicate(img_msg));
assert.ok(!predicate(non_img_attachment_msg));
@@ -1451,6 +1457,8 @@ test("predicate_basics", ({override}) => {
}
const has_link = get_predicate([["has", "link"]]);
+ set_find_results_for_msg_content(inline_audio_msg, "a", []);
+ assert.ok(!has_link(inline_audio_msg));
set_find_results_for_msg_content(inline_img_msg, "a", []);
assert.ok(!has_link(inline_img_msg));
set_find_results_for_msg_content(img_msg, "a", ["stub"]);
@@ -1463,19 +1471,25 @@ test("predicate_basics", ({override}) => {
assert.ok(!has_link(no_has_filter_matching_msg));
const has_attachment = get_predicate([["has", "attachment"]]);
- set_find_results_for_msg_content(inline_img_msg, "a[href^='/user_uploads']", []);
- assert.ok(!has_attachment(inline_img_msg));
- set_find_results_for_msg_content(img_msg, "a[href^='/user_uploads']", ["stub"]);
+ const attachment_selector_string =
+ "a[href^='/user_uploads'], img[src^='/user_uploads'], audio[src^='/user_uploads']";
+ set_find_results_for_msg_content(inline_audio_msg, attachment_selector_string, ["stub"]);
+ assert.ok(has_attachment(inline_audio_msg));
+ set_find_results_for_msg_content(inline_img_msg, attachment_selector_string, ["stub"]);
+ assert.ok(has_attachment(inline_img_msg));
+ set_find_results_for_msg_content(img_msg, attachment_selector_string, ["stub"]);
assert.ok(has_attachment(img_msg));
- set_find_results_for_msg_content(non_img_attachment_msg, "a[href^='/user_uploads']", ["stub"]);
+ set_find_results_for_msg_content(non_img_attachment_msg, attachment_selector_string, ["stub"]);
assert.ok(has_attachment(non_img_attachment_msg));
- set_find_results_for_msg_content(link_msg, "a[href^='/user_uploads']", []);
+ set_find_results_for_msg_content(link_msg, attachment_selector_string, []);
assert.ok(!has_attachment(link_msg));
- set_find_results_for_msg_content(no_has_filter_matching_msg, "a[href^='/user_uploads']", []);
+ set_find_results_for_msg_content(no_has_filter_matching_msg, attachment_selector_string, []);
assert.ok(!has_attachment(no_has_filter_matching_msg));
const has_image = get_predicate([["has", "image"]]);
const image_selector_string = ".message_inline_image, .inline-image";
+ set_find_results_for_msg_content(inline_audio_msg, image_selector_string, []);
+ assert.ok(!has_image(inline_audio_msg));
set_find_results_for_msg_content(inline_img_msg, image_selector_string, ["stub"]);
assert.ok(has_image(inline_img_msg));
set_find_results_for_msg_content(img_msg, image_selector_string, ["stub"]);