diff --git a/frontend_tests/node_tests/compose_ui.js b/frontend_tests/node_tests/compose_ui.js index 4e285d16ca..a3fce18a28 100644 --- a/frontend_tests/node_tests/compose_ui.js +++ b/frontend_tests/node_tests/compose_ui.js @@ -57,13 +57,22 @@ function make_textbox(s) { assert.equal(textbox.val(), 'abc :smile: :airplane:'); assert(textbox.focused); - // Test the current slightly-broken behavior. textbox.caret(0); textbox.blur(); compose_ui.smart_insert(textbox, ':octopus:'); - assert.equal(textbox.insert_text, ':octopus:'); - assert.equal(textbox.val(), ':octopus:abc :smile: :airplane:'); + assert.equal(textbox.insert_text, ':octopus: '); + assert.equal(textbox.val(), ':octopus: abc :smile: :airplane:'); assert(textbox.focused); + textbox.caret(textbox.val().length); + textbox.blur(); + compose_ui.smart_insert(textbox, ':heart:'); + assert.equal(textbox.insert_text, ' :heart:'); + assert.equal(textbox.val(), ':octopus: abc :smile: :airplane: :heart:'); + 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. }()); diff --git a/static/js/compose_ui.js b/static/js/compose_ui.js index 12fdae0d7a..6e72e2d1f0 100644 --- a/static/js/compose_ui.js +++ b/static/js/compose_ui.js @@ -13,7 +13,7 @@ exports.smart_insert = function (textarea, syntax) { var pos = textarea.caret(); var before_str = textarea.val().slice(0, pos); - + var after_str = textarea.val().slice(pos); if (pos > 0) { if (!is_space(before_str.slice(-1))) { @@ -21,6 +21,12 @@ exports.smart_insert = function (textarea, syntax) { } } + if (after_str.length > 0) { + if (!is_space(after_str[0])) { + syntax += ' '; + } + } + textarea.caret(syntax); textarea.focus(); };