refactor: Extract message_parser module.

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.
This commit is contained in:
Steve Howell
2021-03-22 17:57:43 +00:00
committed by Tim Abbott
parent 1cee29c2d1
commit 67a487db79
3 changed files with 27 additions and 25 deletions

View File

@@ -25,27 +25,6 @@ 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;
}
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']");
}
export function add_old_messages(messages, msg_list) {
return add_messages(messages, msg_list, {messages_are_new: false});
}