Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
filters: Fix has:image and avoid future issues for other has filters.
Mohit Gupta4167517a6fc6
diff --git a/frontend_tests/node_tests/filter.js b/frontend_tests/node_tests/filter.jsindex ea087505ab4..6f9d39487c6 100644--- a/frontend_tests/node_tests/filter.js+++ b/frontend_tests/node_tests/filter.js@@ -721,34 +721,40 @@ run_test("predicate_basics", () => {// HTML content of message is used to determine if image have link, image or attachment.// We are using jquery to parse the html and find existence of relevant tags/elements.// In tests we need to stub the calls to jquery so using zjquery's .set_find_results method.+ function set_find_results_for_msg_content(msg, jquery_selector, results) {+ $(`<div>${msg.content}</div>`).set_find_results(jquery_selector, results);+ }+const has_link = get_predicate([["has", "link"]]);- $(img_msg.content).set_find_results("a", [$("<a>")]);+ set_find_results_for_msg_content(img_msg, "a", [$("<a>")]);assert(has_link(img_msg));- $(non_img_attachment_msg.content).set_find_results("a", [$("<a>")]);+ set_find_results_for_msg_content(non_img_attachment_msg, "a", [$("<a>")]);assert(has_link(non_img_attachment_msg));- $(link_msg.content).set_find_results("a", [$("<a>")]);+ set_find_results_for_msg_content(link_msg, "a", [$("<a>")]);assert(has_link(link_msg));- $(no_has_filter_matching_msg.content).set_find_results("a", false);+ set_find_results_for_msg_content(no_has_filter_matching_msg, "a", false);assert(!has_link(no_has_filter_matching_msg));const has_attachment = get_predicate([["has", "attachment"]]);- $(img_msg.content).set_find_results("a[href^='/user_uploads']", [$("<a>")]);+ set_find_results_for_msg_content(img_msg, "a[href^='/user_uploads']", [$("<a>")]);assert(has_attachment(img_msg));- $(non_img_attachment_msg.content).set_find_results("a[href^='/user_uploads']", [$("<a>")]);+ set_find_results_for_msg_content(non_img_attachment_msg, "a[href^='/user_uploads']", [+ $("<a>"),+ ]);assert(has_attachment(non_img_attachment_msg));- $(link_msg.content).set_find_results("a[href^='/user_uploads']", false);+ set_find_results_for_msg_content(link_msg, "a[href^='/user_uploads']", false);assert(!has_attachment(link_msg));- $(no_has_filter_matching_msg.content).set_find_results("a[href^='/user_uploads']", false);+ set_find_results_for_msg_content(no_has_filter_matching_msg, "a[href^='/user_uploads']", false);assert(!has_attachment(no_has_filter_matching_msg));const has_image = get_predicate([["has", "image"]]);- $(img_msg.content).set_find_results(".message_inline_image", [$("<img>")]);+ set_find_results_for_msg_content(img_msg, ".message_inline_image", [$("<img>")]);assert(has_image(img_msg));- $(non_img_attachment_msg.content).set_find_results(".message_inline_image", false);+ set_find_results_for_msg_content(non_img_attachment_msg, ".message_inline_image", false);assert(!has_image(non_img_attachment_msg));- $(link_msg.content).set_find_results(".message_inline_image", false);+ set_find_results_for_msg_content(link_msg, ".message_inline_image", false);assert(!has_image(link_msg));- $(no_has_filter_matching_msg.content).set_find_results(".message_inline_image", false);+ set_find_results_for_msg_content(no_has_filter_matching_msg, ".message_inline_image", false);assert(!has_image(no_has_filter_matching_msg));});diff --git a/static/js/message_util.js b/static/js/message_util.jsindex fa2761c032e..ae130e15bb6 100644--- a/static/js/message_util.js+++ b/static/js/message_util.js@@ -18,16 +18,25 @@ function add_messages(messages, msg_list, opts) {return render_info;}+// We need to check if the message content contains the specified HTML+// elements. We wrap the message.content in a <div>; this is+// important because $("Text <a>link</a>").find("a") returns nothing;+// one needs an outer element wrapping an object to use this+// construction.+function is_element_in_message_content(message, element_selector) {+ return $(`<div>${message.content}</div>`).find(element_selector).length > 0;+}+exports.message_has_link = function (message) {- return $(message.content).find("a").length > 0;+ return is_element_in_message_content(message, "a");};exports.message_has_image = function (message) {- return $(message.content).find(".message_inline_image").length > 0;+ return is_element_in_message_content(message, ".message_inline_image");};exports.message_has_attachment = function (message) {- return $(message.content).find("a[href^='/user_uploads']").length > 0;+ return is_element_in_message_content(message, "a[href^='/user_uploads']");};exports.add_old_messages = function (messages, msg_list) {