2020-08-01 03:43:15 +02:00
|
|
|
|
"use strict";
|
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
|
2020-09-04 23:49:49 +02:00
|
|
|
|
const autosize = require("autosize");
|
|
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
|
const {$t} = require("../zjsunit/i18n");
|
2021-03-29 16:43:39 +02:00
|
|
|
|
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
|
2021-07-28 18:23:34 +02:00
|
|
|
|
const noop = () => {};
|
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
set_global("document", {
|
2020-07-20 22:18:43 +02:00
|
|
|
|
execCommand() {
|
2020-07-15 00:34:28 +02:00
|
|
|
|
return false;
|
|
|
|
|
},
|
2017-12-08 16:17:20 +01:00
|
|
|
|
});
|
2021-07-28 18:23:34 +02:00
|
|
|
|
set_global("navigator", {});
|
2017-12-08 16:17:20 +01:00
|
|
|
|
|
2021-03-29 16:43:39 +02:00
|
|
|
|
mock_esm("../../static/js/message_lists", {
|
|
|
|
|
current: {},
|
|
|
|
|
});
|
|
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
|
const compose_ui = zrequire("compose_ui");
|
|
|
|
|
const people = zrequire("people");
|
|
|
|
|
const user_status = zrequire("user_status");
|
2021-03-29 16:43:39 +02:00
|
|
|
|
const hash_util = mock_esm("../../static/js/hash_util");
|
|
|
|
|
const channel = mock_esm("../../static/js/channel");
|
|
|
|
|
const compose_actions = zrequire("compose_actions");
|
|
|
|
|
const message_lists = zrequire("message_lists");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
email: "alice@zulip.com",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
user_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
|
full_name: "Alice",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
|
const bob = {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
email: "bob@zulip.com",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
user_id: 102,
|
2020-07-15 01:29:15 +02:00
|
|
|
|
full_name: "Bob",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-26 22:34:15 +02:00
|
|
|
|
people.add_active_user(alice);
|
|
|
|
|
people.add_active_user(bob);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
2017-11-09 17:32:28 +01:00
|
|
|
|
function make_textbox(s) {
|
|
|
|
|
// Simulate a jQuery textbox for testing purposes.
|
2022-01-25 11:36:19 +01:00
|
|
|
|
const $widget = {};
|
2017-11-09 17:32:28 +01:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.s = s;
|
|
|
|
|
$widget.focused = false;
|
2017-11-09 17:32:28 +01:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.caret = function (arg) {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
if (typeof arg === "number") {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.pos = arg;
|
2020-09-24 07:50:36 +02:00
|
|
|
|
return this;
|
2017-11-09 17:32:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (arg) {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.insert_pos = $widget.pos;
|
|
|
|
|
$widget.insert_text = arg;
|
|
|
|
|
const before = $widget.s.slice(0, $widget.pos);
|
|
|
|
|
const after = $widget.s.slice($widget.pos);
|
|
|
|
|
$widget.s = before + arg + after;
|
|
|
|
|
$widget.pos += arg.length;
|
2020-09-24 07:50:36 +02:00
|
|
|
|
return this;
|
2017-11-09 17:32:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
return $widget.pos;
|
2017-11-09 17:32:28 +01:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.val = function (new_val) {
|
2022-04-09 23:44:38 +02:00
|
|
|
|
/* istanbul ignore if */
|
2018-08-15 03:55:44 +02:00
|
|
|
|
if (new_val) {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.s = new_val;
|
2020-09-24 07:50:36 +02:00
|
|
|
|
return this;
|
2018-08-15 03:55:44 +02:00
|
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
|
return $widget.s;
|
2017-11-09 17:32:28 +01:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.trigger = function (type) {
|
2020-07-20 21:24:26 +02:00
|
|
|
|
if (type === "focus") {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.focused = true;
|
2020-07-20 21:24:26 +02:00
|
|
|
|
} else if (type === "blur") {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$widget.focused = false;
|
2020-07-20 21:24:26 +02:00
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
|
return this;
|
2017-11-22 04:43:45 +01:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
return $widget;
|
2017-11-09 17:32:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
|
run_test("autosize_textarea", ({override}) => {
|
2020-09-04 23:49:49 +02:00
|
|
|
|
const textarea_autosized = {};
|
|
|
|
|
|
2021-02-14 08:44:04 +01:00
|
|
|
|
override(autosize, "update", (textarea) => {
|
2020-09-04 23:49:49 +02:00
|
|
|
|
textarea_autosized.textarea = textarea;
|
|
|
|
|
textarea_autosized.autosized = true;
|
|
|
|
|
});
|
2021-02-14 08:44:04 +01:00
|
|
|
|
|
|
|
|
|
// Call autosize_textarea with an argument
|
|
|
|
|
const container = "container-stub";
|
|
|
|
|
compose_ui.autosize_textarea(container);
|
|
|
|
|
assert.equal(textarea_autosized.textarea, container);
|
2021-06-10 08:32:54 +02:00
|
|
|
|
assert.ok(textarea_autosized.autosized);
|
2020-09-04 23:49:49 +02:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("insert_syntax_and_focus", () => {
|
|
|
|
|
$("#compose-textarea").val("xyz ");
|
2021-02-23 20:32:35 +01:00
|
|
|
|
$("#compose-textarea").caret = function (syntax) {
|
2017-12-08 16:17:20 +01:00
|
|
|
|
if (syntax !== undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
$("#compose-textarea").val($("#compose-textarea").val() + syntax);
|
2020-09-24 07:50:36 +02:00
|
|
|
|
return this;
|
2017-12-08 16:17:20 +01:00
|
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
|
return 4;
|
2017-12-08 16:17:20 +01:00
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
|
compose_ui.insert_syntax_and_focus(":octopus:");
|
|
|
|
|
assert.equal($("#compose-textarea").caret(), 4);
|
|
|
|
|
assert.equal($("#compose-textarea").val(), "xyz :octopus: ");
|
2021-06-10 08:32:54 +02:00
|
|
|
|
assert.ok($("#compose-textarea").is_focused());
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-12-08 16:17:20 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("smart_insert", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
let $textbox = make_textbox("abc");
|
|
|
|
|
$textbox.caret(4);
|
|
|
|
|
|
|
|
|
|
compose_ui.smart_insert($textbox, ":smile:");
|
|
|
|
|
assert.equal($textbox.insert_pos, 4);
|
|
|
|
|
assert.equal($textbox.insert_text, " :smile: ");
|
|
|
|
|
assert.equal($textbox.val(), "abc :smile: ");
|
|
|
|
|
assert.ok($textbox.focused);
|
|
|
|
|
|
|
|
|
|
$textbox.trigger("blur");
|
|
|
|
|
compose_ui.smart_insert($textbox, ":airplane:");
|
|
|
|
|
assert.equal($textbox.insert_text, ":airplane: ");
|
|
|
|
|
assert.equal($textbox.val(), "abc :smile: :airplane: ");
|
|
|
|
|
assert.ok($textbox.focused);
|
|
|
|
|
|
|
|
|
|
$textbox.caret(0);
|
|
|
|
|
$textbox.trigger("blur");
|
|
|
|
|
compose_ui.smart_insert($textbox, ":octopus:");
|
|
|
|
|
assert.equal($textbox.insert_text, ":octopus: ");
|
|
|
|
|
assert.equal($textbox.val(), ":octopus: abc :smile: :airplane: ");
|
|
|
|
|
assert.ok($textbox.focused);
|
|
|
|
|
|
|
|
|
|
$textbox.caret($textbox.val().length);
|
|
|
|
|
$textbox.trigger("blur");
|
|
|
|
|
compose_ui.smart_insert($textbox, ":heart:");
|
|
|
|
|
assert.equal($textbox.insert_text, ":heart: ");
|
|
|
|
|
assert.equal($textbox.val(), ":octopus: abc :smile: :airplane: :heart: ");
|
|
|
|
|
assert.ok($textbox.focused);
|
2017-11-09 17:57:59 +01:00
|
|
|
|
|
2019-02-27 22:17:27 +01:00
|
|
|
|
// Test handling of spaces for ```quote
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$textbox = make_textbox("");
|
|
|
|
|
$textbox.caret(0);
|
|
|
|
|
$textbox.trigger("blur");
|
|
|
|
|
compose_ui.smart_insert($textbox, "```quote\nquoted message\n```\n");
|
|
|
|
|
assert.equal($textbox.insert_text, "```quote\nquoted message\n```\n");
|
|
|
|
|
assert.equal($textbox.val(), "```quote\nquoted message\n```\n");
|
|
|
|
|
assert.ok($textbox.focused);
|
|
|
|
|
|
|
|
|
|
$textbox = make_textbox("");
|
|
|
|
|
$textbox.caret(0);
|
|
|
|
|
$textbox.trigger("blur");
|
|
|
|
|
compose_ui.smart_insert($textbox, "translated: [Quoting…]\n");
|
|
|
|
|
assert.equal($textbox.insert_text, "translated: [Quoting…]\n");
|
|
|
|
|
assert.equal($textbox.val(), "translated: [Quoting…]\n");
|
|
|
|
|
assert.ok($textbox.focused);
|
|
|
|
|
|
|
|
|
|
$textbox = make_textbox("abc");
|
|
|
|
|
$textbox.caret(3);
|
|
|
|
|
$textbox.trigger("blur");
|
|
|
|
|
compose_ui.smart_insert($textbox, " test with space");
|
|
|
|
|
assert.equal($textbox.insert_text, " test with space ");
|
|
|
|
|
assert.equal($textbox.val(), "abc test with space ");
|
|
|
|
|
assert.ok($textbox.focused);
|
2019-02-27 22:17:27 +01:00
|
|
|
|
|
2017-11-09 17:57:59 +01:00
|
|
|
|
// Note that we don't have any special logic for strings that are
|
|
|
|
|
// already surrounded by spaces, since we are usually inserting things
|
|
|
|
|
// like emojis and file links.
|
2018-05-15 12:40:07 +02:00
|
|
|
|
});
|
2017-11-09 17:32:28 +01:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("replace_syntax", () => {
|
|
|
|
|
$("#compose-textarea").val("abcabc");
|
2018-08-15 03:55:44 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
compose_ui.replace_syntax("a", "A");
|
|
|
|
|
assert.equal($("#compose-textarea").val(), "Abcabc");
|
2018-08-15 03:55:44 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
compose_ui.replace_syntax(/b/g, "B");
|
|
|
|
|
assert.equal($("#compose-textarea").val(), "ABcaBc");
|
2019-04-13 19:50:25 +02:00
|
|
|
|
|
|
|
|
|
// Verify we correctly handle `$`s in the replacement syntax
|
2020-07-15 01:29:15 +02:00
|
|
|
|
compose_ui.replace_syntax("Bca", "$$\\pi$$");
|
|
|
|
|
assert.equal($("#compose-textarea").val(), "A$$\\pi$$Bc");
|
2018-08-15 03:55:44 +02:00
|
|
|
|
});
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
run_test("compute_placeholder_text", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
|
let opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
message_type: "stream",
|
|
|
|
|
stream: "",
|
|
|
|
|
topic: "",
|
|
|
|
|
private_message_recipient: "",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Stream narrows
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(
|
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
|
$t({defaultMessage: "Compose your message here"}),
|
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
|
|
opts.stream = "all";
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), $t({defaultMessage: "Message #all"}));
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
|
|
opts.topic = "Test";
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(
|
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
|
$t({defaultMessage: "Message #all > Test"}),
|
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
|
|
// PM Narrows
|
|
|
|
|
opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
|
message_type: "private",
|
|
|
|
|
stream: "",
|
|
|
|
|
topic: "",
|
|
|
|
|
private_message_recipient: "",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
};
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(
|
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
|
$t({defaultMessage: "Compose your message here"}),
|
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
opts.private_message_recipient = "bob@zulip.com";
|
2019-07-23 20:13:43 +02:00
|
|
|
|
user_status.set_status_text({
|
|
|
|
|
user_id: bob.user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
|
status_text: "out to lunch",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
});
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(
|
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
|
$t({defaultMessage: "Message Bob (out to lunch)"}),
|
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
|
opts.private_message_recipient = "alice@zulip.com";
|
2019-07-23 20:13:43 +02:00
|
|
|
|
user_status.set_status_text({
|
|
|
|
|
user_id: alice.user_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
|
status_text: "",
|
2019-07-23 20:13:43 +02:00
|
|
|
|
});
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(compose_ui.compute_placeholder_text(opts), $t({defaultMessage: "Message Alice"}));
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
|
|
// Group PM
|
2020-07-15 01:29:15 +02:00
|
|
|
|
opts.private_message_recipient = "alice@zulip.com,bob@zulip.com";
|
2021-04-13 06:51:54 +02:00
|
|
|
|
assert.equal(
|
|
|
|
|
compose_ui.compute_placeholder_text(opts),
|
|
|
|
|
$t({defaultMessage: "Message Alice, Bob"}),
|
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
|
});
|
2021-03-29 16:43:39 +02:00
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
|
run_test("quote_and_reply", ({override, override_rewire}) => {
|
2021-03-29 16:43:39 +02:00
|
|
|
|
const selected_message = {
|
|
|
|
|
type: "stream",
|
|
|
|
|
stream: "devel",
|
|
|
|
|
topic: "python",
|
|
|
|
|
sender_full_name: "Steve Stephenson",
|
|
|
|
|
sender_id: 90,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
override(
|
|
|
|
|
hash_util,
|
2022-03-01 19:14:26 +01:00
|
|
|
|
"by_conversation_and_time_url",
|
2021-03-29 16:43:39 +02:00
|
|
|
|
() => "https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
override(message_lists.current, "selected_message", () => selected_message);
|
|
|
|
|
override(message_lists.current, "selected_id", () => 100);
|
|
|
|
|
|
|
|
|
|
let success_function;
|
|
|
|
|
override(channel, "get", (opts) => {
|
|
|
|
|
success_function = opts.success;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// zjquery does not simulate caret handling, so we provide
|
|
|
|
|
// our own versions of val() and caret()
|
|
|
|
|
let textarea_val = "";
|
|
|
|
|
let textarea_caret_pos;
|
|
|
|
|
|
|
|
|
|
$("#compose-textarea").val = function (...args) {
|
|
|
|
|
if (args.length === 0) {
|
|
|
|
|
return textarea_val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textarea_val = args[0];
|
|
|
|
|
textarea_caret_pos = textarea_val.length;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$("#compose-textarea").caret = function (arg) {
|
|
|
|
|
if (arg === undefined) {
|
|
|
|
|
return textarea_caret_pos;
|
|
|
|
|
}
|
2020-12-31 12:53:54 +01:00
|
|
|
|
if (typeof arg === "number") {
|
|
|
|
|
textarea_caret_pos = arg;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2022-04-09 23:44:38 +02:00
|
|
|
|
/* istanbul ignore if */
|
2021-03-29 16:43:39 +02:00
|
|
|
|
if (typeof arg !== "string") {
|
|
|
|
|
console.info(arg);
|
|
|
|
|
throw new Error("We expected the actual code to pass in a string.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const before = textarea_val.slice(0, textarea_caret_pos);
|
|
|
|
|
const after = textarea_val.slice(textarea_caret_pos);
|
|
|
|
|
|
|
|
|
|
textarea_val = before + arg + after;
|
|
|
|
|
textarea_caret_pos += arg.length;
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function set_compose_content_with_caret(content) {
|
|
|
|
|
const caret_position = content.indexOf("%");
|
|
|
|
|
content = content.slice(0, caret_position) + content.slice(caret_position + 1); // remove the "%"
|
|
|
|
|
textarea_val = content;
|
|
|
|
|
textarea_caret_pos = caret_position;
|
|
|
|
|
$("#compose-textarea").trigger("focus");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get_compose_content_with_caret() {
|
|
|
|
|
const content =
|
|
|
|
|
textarea_val.slice(0, textarea_caret_pos) +
|
|
|
|
|
"%" +
|
|
|
|
|
textarea_val.slice(textarea_caret_pos); // insert the "%"
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 12:53:54 +01:00
|
|
|
|
function reset_test_state() {
|
|
|
|
|
// Reset `raw_content` property of `selected_message`.
|
|
|
|
|
delete selected_message.raw_content;
|
|
|
|
|
|
|
|
|
|
// Reset compose-box state.
|
|
|
|
|
textarea_val = "";
|
|
|
|
|
textarea_caret_pos = 0;
|
|
|
|
|
$("#compose-textarea").trigger("blur");
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 16:43:39 +02:00
|
|
|
|
set_compose_content_with_caret("hello %there"); // "%" is used to encode/display position of focus before change
|
|
|
|
|
compose_actions.quote_and_reply();
|
2021-11-28 05:57:08 +01:00
|
|
|
|
assert.equal(get_compose_content_with_caret(), "hello \ntranslated: [Quoting…]\n%there");
|
2021-03-29 16:43:39 +02:00
|
|
|
|
|
|
|
|
|
success_function({
|
|
|
|
|
raw_content: "Testing caret position",
|
|
|
|
|
});
|
|
|
|
|
assert.equal(
|
|
|
|
|
get_compose_content_with_caret(),
|
2020-12-31 12:53:54 +01:00
|
|
|
|
"hello \ntranslated: @_**Steve Stephenson|90** [said](https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado):\n```quote\nTesting caret position\n```\n%there",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
reset_test_state();
|
|
|
|
|
|
|
|
|
|
// If the caret is initially positioned at 0, it should not
|
|
|
|
|
// add a newline before the quoted message.
|
|
|
|
|
set_compose_content_with_caret("%hello there");
|
|
|
|
|
compose_actions.quote_and_reply();
|
2021-11-28 05:57:08 +01:00
|
|
|
|
assert.equal(get_compose_content_with_caret(), "translated: [Quoting…]\n%hello there");
|
2020-12-31 12:53:54 +01:00
|
|
|
|
|
|
|
|
|
success_function({
|
|
|
|
|
raw_content: "Testing with caret initially positioned at 0.",
|
|
|
|
|
});
|
|
|
|
|
assert.equal(
|
|
|
|
|
get_compose_content_with_caret(),
|
|
|
|
|
"translated: @_**Steve Stephenson|90** [said](https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado):\n```quote\nTesting with caret initially positioned at 0.\n```\n%hello there",
|
|
|
|
|
);
|
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
|
override_rewire(compose_actions, "respond_to_message", () => {
|
2020-12-31 12:53:54 +01:00
|
|
|
|
// Reset compose state to replicate the re-opening of compose-box.
|
|
|
|
|
textarea_val = "";
|
|
|
|
|
textarea_caret_pos = 0;
|
|
|
|
|
$("#compose-textarea").trigger("focus");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reset_test_state();
|
|
|
|
|
|
|
|
|
|
// If the compose-box is close, or open with no content while
|
|
|
|
|
// quoting a message, the quoted message should be placed
|
|
|
|
|
// at the beginning of compose-box.
|
|
|
|
|
compose_actions.quote_and_reply();
|
2021-11-28 05:57:08 +01:00
|
|
|
|
assert.equal(get_compose_content_with_caret(), "translated: [Quoting…]\n%");
|
2020-12-31 12:53:54 +01:00
|
|
|
|
|
|
|
|
|
success_function({
|
|
|
|
|
raw_content: "Testing with compose-box closed initially.",
|
|
|
|
|
});
|
|
|
|
|
assert.equal(
|
|
|
|
|
get_compose_content_with_caret(),
|
|
|
|
|
"translated: @_**Steve Stephenson|90** [said](https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado):\n```quote\nTesting with compose-box closed initially.\n```\n%",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
reset_test_state();
|
|
|
|
|
|
|
|
|
|
// If the compose-box is already open while quoting a message,
|
|
|
|
|
// but contains content like `\n\n \n` (only whitespaces and
|
|
|
|
|
// newlines), the compose-box should re-open and thus the quoted
|
|
|
|
|
// message should start from the beginning of compose-box.
|
|
|
|
|
set_compose_content_with_caret(" \n\n \n %");
|
|
|
|
|
compose_actions.quote_and_reply();
|
2021-11-28 05:57:08 +01:00
|
|
|
|
assert.equal(get_compose_content_with_caret(), "translated: [Quoting…]\n%");
|
2020-12-31 12:53:54 +01:00
|
|
|
|
|
|
|
|
|
success_function({
|
|
|
|
|
raw_content: "Testing with compose-box containing whitespaces and newlines only.",
|
|
|
|
|
});
|
|
|
|
|
assert.equal(
|
|
|
|
|
get_compose_content_with_caret(),
|
|
|
|
|
"translated: @_**Steve Stephenson|90** [said](https://chat.zulip.org/#narrow/stream/92-learning/topic/Tornado):\n```quote\nTesting with compose-box containing whitespaces and newlines only.\n```\n%",
|
2021-03-29 16:43:39 +02:00
|
|
|
|
);
|
|
|
|
|
});
|
2021-05-17 13:15:11 +02:00
|
|
|
|
|
2021-07-15 21:26:16 +02:00
|
|
|
|
run_test("set_compose_box_top", () => {
|
|
|
|
|
$(".header").set_height(40);
|
|
|
|
|
|
|
|
|
|
const padding_bottom = 10;
|
|
|
|
|
$(".header").css = (arg) => {
|
|
|
|
|
assert.equal(arg, "paddingBottom");
|
|
|
|
|
return padding_bottom;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let compose_top = "";
|
|
|
|
|
$("#compose").css = (arg, val) => {
|
|
|
|
|
assert.equal(arg, "top");
|
|
|
|
|
compose_top = val;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$("#navbar_alerts_wrapper").set_height(0);
|
|
|
|
|
compose_ui.set_compose_box_top(true);
|
|
|
|
|
assert.equal(compose_top, "50px");
|
|
|
|
|
|
|
|
|
|
$("#navbar_alerts_wrapper").set_height(45);
|
|
|
|
|
compose_ui.set_compose_box_top(true);
|
|
|
|
|
assert.equal(compose_top, "95px");
|
|
|
|
|
|
|
|
|
|
compose_ui.set_compose_box_top(false);
|
|
|
|
|
assert.equal(compose_top, "");
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
|
run_test("test_compose_height_changes", ({override, override_rewire}) => {
|
2021-05-17 13:15:11 +02:00
|
|
|
|
let autosize_destroyed = false;
|
|
|
|
|
override(autosize, "destroy", () => {
|
|
|
|
|
autosize_destroyed = true;
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-15 21:26:16 +02:00
|
|
|
|
let compose_box_top_set = false;
|
2022-01-08 10:27:06 +01:00
|
|
|
|
override_rewire(compose_ui, "set_compose_box_top", (set_top) => {
|
2021-07-15 21:26:16 +02:00
|
|
|
|
compose_box_top_set = set_top;
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-17 13:15:11 +02:00
|
|
|
|
compose_ui.make_compose_box_full_size();
|
|
|
|
|
assert.ok($("#compose").hasClass("compose-fullscreen"));
|
|
|
|
|
assert.ok(compose_ui.is_full_size());
|
|
|
|
|
assert.ok(autosize_destroyed);
|
2021-07-15 21:26:16 +02:00
|
|
|
|
assert.ok(compose_box_top_set);
|
2021-05-17 13:15:11 +02:00
|
|
|
|
|
|
|
|
|
compose_ui.make_compose_box_original_size();
|
|
|
|
|
assert.ok(!$("#compose").hasClass("compose-fullscreen"));
|
|
|
|
|
assert.ok(!compose_ui.is_full_size());
|
2021-07-15 21:26:16 +02:00
|
|
|
|
assert.ok(!compose_box_top_set);
|
2021-05-17 13:15:11 +02:00
|
|
|
|
});
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
2021-09-04 17:47:27 +02:00
|
|
|
|
run_test("format_text", () => {
|
|
|
|
|
let set_text = "";
|
|
|
|
|
let wrap_selection_called = false;
|
|
|
|
|
let wrap_syntax = "";
|
|
|
|
|
|
|
|
|
|
mock_esm("text-field-edit", {
|
|
|
|
|
set: (field, text) => {
|
|
|
|
|
set_text = text;
|
|
|
|
|
},
|
|
|
|
|
wrapSelection: (field, syntax) => {
|
|
|
|
|
wrap_selection_called = true;
|
|
|
|
|
wrap_syntax = syntax;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function reset_state() {
|
|
|
|
|
set_text = "";
|
|
|
|
|
wrap_selection_called = false;
|
|
|
|
|
wrap_syntax = "";
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
const $textarea = $("#compose-textarea");
|
|
|
|
|
$textarea.get = () => ({
|
2021-09-04 17:47:27 +02:00
|
|
|
|
setSelectionRange: () => {},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function init_textarea(val, range) {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$textarea.val = () => val;
|
|
|
|
|
$textarea.range = () => range;
|
2021-09-04 17:47:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const italic_syntax = "*";
|
|
|
|
|
const bold_syntax = "**";
|
|
|
|
|
|
|
|
|
|
// Bold selected text
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("abc", {
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 3,
|
|
|
|
|
text: "abc",
|
|
|
|
|
length: 3,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "bold");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "");
|
|
|
|
|
assert.equal(wrap_selection_called, true);
|
|
|
|
|
assert.equal(wrap_syntax, bold_syntax);
|
|
|
|
|
|
|
|
|
|
// Undo bold selected text, syntax not selected
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("**abc**", {
|
|
|
|
|
start: 2,
|
|
|
|
|
end: 5,
|
|
|
|
|
text: "abc",
|
|
|
|
|
length: 7,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "bold");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "abc");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Undo bold selected text, syntax selected
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("**abc**", {
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 7,
|
|
|
|
|
text: "**abc**",
|
|
|
|
|
length: 7,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "bold");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "abc");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Italic selected text
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("abc", {
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 3,
|
|
|
|
|
text: "abc",
|
|
|
|
|
length: 3,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "italic");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "");
|
|
|
|
|
assert.equal(wrap_selection_called, true);
|
|
|
|
|
assert.equal(wrap_syntax, italic_syntax);
|
|
|
|
|
|
|
|
|
|
// Undo italic selected text, syntax not selected
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("*abc*", {
|
|
|
|
|
start: 1,
|
|
|
|
|
end: 4,
|
|
|
|
|
text: "abc",
|
|
|
|
|
length: 3,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "italic");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "abc");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Undo italic selected text, syntax selected
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("*abc*", {
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 5,
|
|
|
|
|
text: "*abc*",
|
|
|
|
|
length: 5,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "italic");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "abc");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Undo bold selected text, text is both italic and bold, syntax not selected.
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("***abc***", {
|
|
|
|
|
start: 3,
|
|
|
|
|
end: 6,
|
|
|
|
|
text: "abc",
|
|
|
|
|
length: 3,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "bold");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "*abc*");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Undo bold selected text, text is both italic and bold, syntax selected.
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("***abc***", {
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 9,
|
|
|
|
|
text: "***abc***",
|
|
|
|
|
length: 9,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "bold");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "*abc*");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Undo italic selected text, text is both italic and bold, syntax not selected.
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("***abc***", {
|
|
|
|
|
start: 3,
|
|
|
|
|
end: 6,
|
|
|
|
|
text: "abc",
|
|
|
|
|
length: 3,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "italic");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "**abc**");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
|
|
|
|
|
// Undo italic selected text, text is both italic and bold, syntax selected.
|
|
|
|
|
reset_state();
|
|
|
|
|
init_textarea("***abc***", {
|
|
|
|
|
start: 0,
|
|
|
|
|
end: 9,
|
|
|
|
|
text: "***abc***",
|
|
|
|
|
length: 9,
|
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
|
compose_ui.format_text($textarea, "italic");
|
2021-09-04 17:47:27 +02:00
|
|
|
|
assert.equal(set_text, "**abc**");
|
|
|
|
|
assert.equal(wrap_selection_called, false);
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-08 10:27:06 +01:00
|
|
|
|
run_test("markdown_shortcuts", ({override_rewire}) => {
|
2021-06-22 10:31:40 +02:00
|
|
|
|
let format_text_type;
|
2022-01-25 11:36:19 +01:00
|
|
|
|
override_rewire(compose_ui, "format_text", ($textarea, type) => {
|
2021-06-22 10:31:40 +02:00
|
|
|
|
format_text_type = type;
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-28 18:23:34 +02:00
|
|
|
|
const event = {
|
|
|
|
|
key: "b",
|
|
|
|
|
target: {
|
|
|
|
|
id: "compose-textarea",
|
|
|
|
|
},
|
|
|
|
|
stopPropagation: noop,
|
|
|
|
|
preventDefault: noop,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function all_markdown_test(isCtrl, isCmd) {
|
|
|
|
|
// Test bold:
|
|
|
|
|
// Mac env = Cmd+b
|
|
|
|
|
// Windows/Linux = Ctrl+b
|
|
|
|
|
event.key = "b";
|
|
|
|
|
event.ctrlKey = isCtrl;
|
|
|
|
|
event.metaKey = isCmd;
|
|
|
|
|
compose_ui.handle_keydown(event, $("#compose-textarea"));
|
2021-06-22 10:31:40 +02:00
|
|
|
|
assert.equal(format_text_type, "bold");
|
|
|
|
|
format_text_type = undefined;
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
|
|
// Test italic:
|
|
|
|
|
// Mac = Cmd+I
|
|
|
|
|
// Windows/Linux = Ctrl+I
|
|
|
|
|
// We use event.key = "I" to emulate user using Caps Lock key.
|
|
|
|
|
event.key = "I";
|
|
|
|
|
event.shiftKey = false;
|
|
|
|
|
compose_ui.handle_keydown(event, $("#compose-textarea"));
|
2021-06-22 10:31:40 +02:00
|
|
|
|
assert.equal(format_text_type, "italic");
|
|
|
|
|
format_text_type = undefined;
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
|
|
// Test link insertion:
|
|
|
|
|
// Mac = Cmd+Shift+L
|
|
|
|
|
// Windows/Linux = Ctrl+Shift+L
|
|
|
|
|
event.key = "l";
|
|
|
|
|
event.shiftKey = true;
|
|
|
|
|
compose_ui.handle_keydown(event, $("#compose-textarea"));
|
2021-06-22 10:31:40 +02:00
|
|
|
|
assert.equal(format_text_type, "link");
|
|
|
|
|
format_text_type = undefined;
|
2021-07-28 18:23:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function cross tests the Cmd/Ctrl + Markdown shortcuts in
|
|
|
|
|
// Mac and Linux/Windows environments. So in short, this tests
|
|
|
|
|
// that e.g. Cmd+B should be ignored on Linux/Windows and Ctrl+B
|
|
|
|
|
// should be ignored on Mac.
|
|
|
|
|
function os_specific_markdown_test(isCtrl, isCmd) {
|
|
|
|
|
event.ctrlKey = isCtrl;
|
2021-06-22 10:31:40 +02:00
|
|
|
|
event.metaKey = isCmd;
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
|
|
event.key = "b";
|
|
|
|
|
compose_ui.handle_keydown(event, $("#compose-textarea"));
|
2021-06-22 10:31:40 +02:00
|
|
|
|
assert.equal(format_text_type, undefined);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
|
|
event.key = "i";
|
|
|
|
|
event.shiftKey = false;
|
|
|
|
|
compose_ui.handle_keydown(event, $("#compose-textarea"));
|
2021-06-22 10:31:40 +02:00
|
|
|
|
assert.equal(format_text_type, undefined);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
|
|
event.key = "l";
|
|
|
|
|
event.shiftKey = true;
|
|
|
|
|
compose_ui.handle_keydown(event, $("#compose-textarea"));
|
2021-06-22 10:31:40 +02:00
|
|
|
|
assert.equal(format_text_type, undefined);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// These keyboard shortcuts differ as to what key one should use
|
|
|
|
|
// on MacOS vs. other platforms: Cmd (Mac) vs. Ctrl (non-Mac).
|
|
|
|
|
|
|
|
|
|
// Default (Linux/Windows) userAgent tests:
|
|
|
|
|
navigator.platform = "";
|
|
|
|
|
|
|
|
|
|
// Check all the Ctrl + Markdown shortcuts work correctly
|
|
|
|
|
all_markdown_test(true, false);
|
|
|
|
|
// The Cmd + Markdown shortcuts should do nothing on Linux/Windows
|
|
|
|
|
os_specific_markdown_test(false, true);
|
|
|
|
|
|
|
|
|
|
// Setting following platform to test in mac env
|
|
|
|
|
navigator.platform = "MacIntel";
|
|
|
|
|
|
|
|
|
|
// Mac userAgent tests:
|
|
|
|
|
// The Ctrl + Markdown shortcuts should do nothing on mac
|
|
|
|
|
os_specific_markdown_test(true, false);
|
|
|
|
|
// Check all the Cmd + Markdown shortcuts work correctly
|
|
|
|
|
all_markdown_test(false, true);
|
|
|
|
|
|
|
|
|
|
// Reset userAgent
|
|
|
|
|
navigator.userAgent = "";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
run_test("right-to-left", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
|
const $textarea = $("#compose-textarea");
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
|
|
const event = {
|
|
|
|
|
key: "A",
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
assert.equal($textarea.hasClass("rtl"), false);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$textarea.val("```quote\nمرحبا");
|
2021-07-28 18:23:34 +02:00
|
|
|
|
compose_ui.handle_keyup(event, $("#compose-textarea"));
|
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
assert.equal($textarea.hasClass("rtl"), true);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
$textarea.val("```quote foo");
|
|
|
|
|
compose_ui.handle_keyup(event, $textarea);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
|
assert.equal($textarea.hasClass("rtl"), false);
|
2021-07-28 18:23:34 +02:00
|
|
|
|
});
|