mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 14:03:30 +00:00 
			
		
		
		
	I moved four functions, verbatim, to a new module. They were in message_util before, which led to filter.js having several accidental indirect dependencies. I considered just putting these four functions in filter.js, but I think it's a nice abstraction boundary that filter.js delegates actual message parsing, and the original author apparently had a similar thought process. I also wanted to make it so that a casual reader of filter.js doesn't think we are manipulating DOM. It's true that we still indirectly require jquery here, but it's only for parsing, and it seems plausible we would eventually use a more low-level parser. I can see us maybe using these functions in something like MessageListData in the future, so speculatively splitting them out might future-proof us from some cyclical dependencies. I also think it's plausible that we will just modify our two markdown processors to attach that kind of metadata to the messages. Last but not least, I think there might be opportunity here to simplify the filter tests and remove some of the zjquery hacks. We would instead just mock the message_has_* helpers for the filter tests, and then do more detailed direct testing on the functions themselves.
		
			
				
	
	
		
			24 lines
		
	
	
		
			856 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			24 lines
		
	
	
		
			856 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
// We only use jquery for parsing.
 | 
						|
import $ from "jquery";
 | 
						|
 | 
						|
// 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;
 | 
						|
}
 | 
						|
 | 
						|
export function message_has_link(message) {
 | 
						|
    return is_element_in_message_content(message, "a");
 | 
						|
}
 | 
						|
 | 
						|
export function message_has_image(message) {
 | 
						|
    return is_element_in_message_content(message, ".message_inline_image");
 | 
						|
}
 | 
						|
 | 
						|
export function message_has_attachment(message) {
 | 
						|
    return is_element_in_message_content(message, "a[href^='/user_uploads']");
 | 
						|
}
 |