compose: Fix extra space being added by quote-and-reply.

The correct behavior here is that we want to ensure there is
whitespace in between the syntax being added and the content on either
side.  Our smart_insert logic handled this for the cases that were
common with inserting emoji (etc.), but didn't handle the more complex
cases with "quote and reply".

Fixes #11702.
This commit is contained in:
Tim Abbott
2019-02-27 13:17:27 -08:00
parent 579e2e8b2b
commit f40cbdbd19
2 changed files with 33 additions and 2 deletions

View File

@@ -106,6 +106,31 @@ run_test('smart_insert', () => {
assert.equal(textbox.val(), ':octopus: abc :smile: :airplane: :heart: ');
assert(textbox.focused);
// Test handling of spaces for ```quote
textbox = make_textbox('');
textbox.caret(0);
textbox.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(textbox.focused);
textbox = make_textbox('');
textbox.caret(0);
textbox.blur();
compose_ui.smart_insert(textbox, "[Quoting…]\n");
assert.equal(textbox.insert_text, '[Quoting…]\n');
assert.equal(textbox.val(), '[Quoting…]\n');
assert(textbox.focused);
textbox = make_textbox('abc');
textbox.caret(3);
textbox.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(textbox.focused);
// 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.

View File

@@ -16,12 +16,18 @@ exports.smart_insert = function (textarea, syntax) {
var after_str = textarea.val().slice(pos);
if (pos > 0) {
if (!is_space(before_str.slice(-1))) {
// If there isn't space either at the end of the content
// before the insert or (unlikely) at the start of the syntax,
// add one.
if (!is_space(before_str.slice(-1)) && !is_space(syntax[0])) {
syntax = ' ' + syntax;
}
}
if (!(after_str.length > 0 && is_space(after_str[0]))) {
// If there isn't whitespace either at the end of the syntax or the
// start of the content after the syntax, add one.
if (!(after_str.length > 0 && is_space(after_str[0]) ||
syntax.length > 0 && is_space(syntax.slice(-1)))) {
syntax += ' ';
}