From 2cbc4aaa50f383ff4293b702a85ba04f4b72208f Mon Sep 17 00:00:00 2001 From: JC Brand Date: Wed, 26 Jun 2019 10:59:02 +0200 Subject: [PATCH] Fixes #1494. Trim whitespace around messages --- CHANGES.md | 1 + spec/messages.js | 49 ++++++++++++++++++++++++++++++ src/converse-chatview.js | 2 +- src/headless/converse-chatboxes.js | 5 ++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 658219290..e4109ad28 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -37,6 +37,7 @@ - #1445: Participants list uses big font in embedded mode - #1455: Avatar in controlbox status-view not updated - #1465: When highlighting a roster contact, they're incorrectly shown as online +- #1494: Trim whitespace around messages - #1495: Mentions should always include a URI attribute - #1502: Fatal error when using prebind - #1532: Converse reloads on enter pressed in the filter box diff --git a/spec/messages.js b/spec/messages.js index d32fff68e..ea9444ee1 100644 --- a/spec/messages.js +++ b/spec/messages.js @@ -1396,6 +1396,26 @@ done(); })); + describe("when sent", function () { + + it("will be trimmed of leading and trailing whitespace", + mock.initConverse( + null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, + async function (done, _converse) { + + await test_utils.waitForRoster(_converse, 'current', 1); + const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; + await test_utils.openChatBoxFor(_converse, contact_jid) + const view = _converse.chatboxviews.get(contact_jid); + const message = ' \nThis message is sent from this chatbox \n \n'; + await test_utils.sendMessage(view, message); + expect(view.model.messages.at(0).get('message')).toEqual(message.trim()); + const message_el = sizzle('.chat-content .chat-msg:last .chat-msg__text', view.el).pop(); + expect(message_el.textContent).toEqual(message.trim()); + done(); + })); + }); + describe("when received from someone else", function () { @@ -1445,6 +1465,35 @@ done(); })); + it("will be trimmed of leading and trailing whitespace", + mock.initConverse( + null, ['rosterGroupsFetched'], {}, + async function (done, _converse) { + + await test_utils.waitForRoster(_converse, 'current', 1, false); + await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length, 300); + const message = '\n\n This is a received message \n\n'; + const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; + _converse.chatboxes.onMessage( + $msg({ + 'from': sender_jid, + 'to': _converse.connection.jid, + 'type': 'chat', + 'id': (new Date()).getTime() + }).c('body').t(message).up() + .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree() + ); + await test_utils.waitUntil(() => (_converse.api.chats.get().length === 2)); + const view = _converse.api.chatviews.get(sender_jid); + expect(view.model.messages.length).toEqual(1); + const msg_obj = view.model.messages.at(0); + expect(msg_obj.get('message')).toEqual(message.trim()); + const chat_content = view.el.querySelector('.chat-content'); + expect(chat_content.querySelector('.chat-msg .chat-msg__text').textContent).toEqual(message.trim()); + done(); + })); + + it("can be replaced with a correction", mock.initConverse( null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, diff --git a/src/converse-chatview.js b/src/converse-chatview.js index 1ac8dd423..531571c5c 100644 --- a/src/converse-chatview.js +++ b/src/converse-chatview.js @@ -914,7 +914,7 @@ converse.plugins.add('converse-chatview', { async onFormSubmitted (ev) { ev.preventDefault(); const textarea = this.el.querySelector('.chat-textarea'); - const message = textarea.value; + const message = textarea.value.trim(); if (_converse.message_limit && message.length > _converse.message_limit) { return; } diff --git a/src/headless/converse-chatboxes.js b/src/headless/converse-chatboxes.js index 12945d8c7..0d6576521 100644 --- a/src/headless/converse-chatboxes.js +++ b/src/headless/converse-chatboxes.js @@ -841,7 +841,10 @@ converse.plugins.add('converse-chatboxes', { if (type === 'error') { return this.getErrorMessage(stanza); } else { - return _.propertyOf(stanza.querySelector('body'))('textContent'); + const body = stanza.querySelector('body'); + if (body) { + return body.textContent.trim(); + } } },