Fixes #1494. Trim whitespace around messages

This commit is contained in:
JC Brand 2019-06-26 10:59:02 +02:00
parent 83bc851f20
commit 2cbc4aaa50
4 changed files with 55 additions and 2 deletions

View File

@ -37,6 +37,7 @@
- #1445: Participants list uses big font in embedded mode - #1445: Participants list uses big font in embedded mode
- #1455: Avatar in controlbox status-view not updated - #1455: Avatar in controlbox status-view not updated
- #1465: When highlighting a roster contact, they're incorrectly shown as online - #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 - #1495: Mentions should always include a URI attribute
- #1502: Fatal error when using prebind - #1502: Fatal error when using prebind
- #1532: Converse reloads on enter pressed in the filter box - #1532: Converse reloads on enter pressed in the filter box

View File

@ -1396,6 +1396,26 @@
done(); 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 () { describe("when received from someone else", function () {
@ -1445,6 +1465,35 @@
done(); 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", it("can be replaced with a correction",
mock.initConverse( mock.initConverse(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},

View File

@ -914,7 +914,7 @@ converse.plugins.add('converse-chatview', {
async onFormSubmitted (ev) { async onFormSubmitted (ev) {
ev.preventDefault(); ev.preventDefault();
const textarea = this.el.querySelector('.chat-textarea'); 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) { if (_converse.message_limit && message.length > _converse.message_limit) {
return; return;
} }

View File

@ -841,7 +841,10 @@ converse.plugins.add('converse-chatboxes', {
if (type === 'error') { if (type === 'error') {
return this.getErrorMessage(stanza); return this.getErrorMessage(stanza);
} else { } else {
return _.propertyOf(stanza.querySelector('body'))('textContent'); const body = stanza.querySelector('body');
if (body) {
return body.textContent.trim();
}
} }
}, },