Skip to content

Commit

Permalink
compose_ui: Create the compose_ui.replace_syntax function.
Browse files Browse the repository at this point in the history
`replace_syntax` will replace text inside of a compose textbox. Also,
add tests to the `compose_ui` Node tests for this function.
  • Loading branch information
marco authored and timabbott committed Aug 27, 2018
1 parent e204fe3 commit fa22cf1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions frontend_tests/node_tests/compose_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ function make_textbox(s) {
widget.focused = false;
};

widget.val = function () {
return widget.s;
widget.val = function (new_val) {
if (new_val) {
widget.s = new_val;
} else {
return widget.s;
}
};

widget.trigger = function () {
Expand Down Expand Up @@ -107,3 +111,12 @@ run_test('smart_insert', () => {
// like emojis and file links.
});

run_test('replace_syntax', () => {
$('#compose-textarea').val('abcabc');

compose_ui.replace_syntax('a', 'A');
assert.equal($('#compose-textarea').val(), 'Abcabc');

compose_ui.replace_syntax(/b/g, 'B');
assert.equal($('#compose-textarea').val(), 'ABcaBc');
});
13 changes: 13 additions & 0 deletions static/js/compose_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ exports.insert_syntax_and_focus = function (syntax, textarea) {
exports.smart_insert(textarea, syntax);
};

exports.replace_syntax = function (old_syntax, new_syntax, textarea) {
// Replaces `old_syntax` with `new_syntax` text in the compose box. Due to
// the way that JavaScript handles string replacements, if `old_syntax` is
// a string it will only replace the first instance. If `old_syntax` is
// a RegExp with a global flag, it will replace all instances.

if (textarea === undefined) {
textarea = $('#compose-textarea');
}

textarea.val(textarea.val().replace(old_syntax, new_syntax));
};

return exports;

}());
Expand Down

0 comments on commit fa22cf1

Please sign in to comment.