From da1f103532ba30a457c1a94984c0963a0a65ab21 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 9 Jan 2018 12:33:57 +0000 Subject: [PATCH] Bugfix. Don't include .chat-events, to avoid null reference --- spec/chatbox.js | 50 +++++++++++++++++++++++++++++++--------- src/converse-chatview.js | 18 +++++++++++---- 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/spec/chatbox.js b/spec/chatbox.js index ca9b5c7b8..05eb3a4a4 100644 --- a/spec/chatbox.js +++ b/spec/chatbox.js @@ -643,7 +643,7 @@ .then(function () { expect(_converse.api.vcard.get).toHaveBeenCalled(); return test_utils.waitUntil(function () { - return chatbox.get('fullname') === mock.cur_names[0]; + return chatbox.get('fullname') === mock.cur_names[0]; }, 100); }).then(function () { done(); @@ -972,7 +972,7 @@ 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) - .c('body').t("message from today") + .c('body').t("message") .tree(); _converse.chatboxes.onMessage(msg); @@ -996,7 +996,7 @@ 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) - .c('body').t("Inbetween message") + .c('body').t("Inbetween message").up() .tree(); _converse.chatboxes.onMessage(msg); @@ -1020,7 +1020,7 @@ 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) - .c('body').t("An earlier message today") + .c('body').t("An earlier message on the next day") .tree(); _converse.chatboxes.onMessage(msg); @@ -1032,16 +1032,40 @@ 'to': _converse.bare_jid, 'from': sender_jid, 'type': 'chat'}) - .c('body').t("newer message from today") + .c('body').t("newer message from the next day") .tree(); _converse.chatboxes.onMessage(msg); + // Insert message, to also check that + // text messages are inserted correctly with + // temporary chat events in the chat contents. + msg = $msg({ + 'id': 'aeb219', + 'to': _converse.bare_jid, + 'xmlns': 'jabber:client', + 'from': sender_jid, + 'type': 'chat'}) + .c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up() + .tree(); + _converse.chatboxes.onMessage(msg); + + msg = $msg({ + 'id': 'aeb220', + 'to': _converse.bare_jid, + 'xmlns': 'jabber:client', + 'from': sender_jid, + 'type': 'chat'}) + .c('composing', {'xmlns': Strophe.NS.CHATSTATES}).up() + .c('body').t("latest message") + .tree(); + _converse.chatboxes.onMessage(msg); + var chatboxview = _converse.chatboxviews.get(sender_jid); var $chat_content = $(chatboxview.el).find('.chat-content'); chatboxview.clearSpinner(); //cleanup var $time = $chat_content.find('time'); - expect($time.length).toEqual(3); + expect($time.length).toEqual(4); $time = $chat_content.find('time:first'); expect($time.data('isodate')).toEqual('2017-12-31T00:00:00+00:00'); @@ -1058,15 +1082,19 @@ $el = $chat_content.find('.chat-message:eq(2)'); expect($el.find('.chat-msg-content').text()).toEqual('another inbetween message'); - $time = $chat_content.find('time:last'); + $time = $chat_content.find('time:nth(2)'); expect($time.data('isodate')).toEqual('2018-01-02T00:00:00+00:00'); - expect($time[0].nextElementSibling.querySelector('.chat-msg-content').textContent).toBe('An earlier message today'); + expect($time[0].nextElementSibling.querySelector('.chat-msg-content').textContent).toBe('An earlier message on the next day'); $el = $chat_content.find('.chat-message:eq(3)'); - expect($el.find('.chat-msg-content').text()).toEqual('An earlier message today'); + expect($el.find('.chat-msg-content').text()).toEqual('An earlier message on the next day'); $el = $chat_content.find('.chat-message:eq(4)'); - expect($el.find('.chat-msg-content').text()).toEqual('message from today'); - expect($el[0].nextElementSibling.querySelector('.chat-msg-content').textContent).toEqual('newer message from today'); + expect($el.find('.chat-msg-content').text()).toEqual('message'); + expect($el[0].nextElementSibling.querySelector('.chat-msg-content').textContent).toEqual('newer message from the next day'); + + $time = $chat_content.find('time:last'); + expect($time.data('isodate')).toEqual(moment().startOf('day').format()); + expect($time[0].nextElementSibling.querySelector('.chat-msg-content').textContent).toBe('latest message'); done(); }); })); diff --git a/src/converse-chatview.js b/src/converse-chatview.js index 2a5b585fa..af0319ef2 100644 --- a/src/converse-chatview.js +++ b/src/converse-chatview.js @@ -468,8 +468,14 @@ if (_.isNull(most_recent_date) || moment(most_recent_date).isBefore(cutoff)) { return most_recent_date; } + /* XXX: Besides .chat-message and .chat-date elements, there are also + * .chat-event elements. These are however temporary and + * removed once a new element is inserted into the chat + * area, so we don't query for them here, otherwise we get + * a null reference later upon element insertion. + */ const msg_dates = _.invokeMap( - sizzle('.message, .chat-info', this.content), + sizzle('.chat-message, .chat-date', this.content), Element.prototype.getAttribute, 'data-isodate' ) if (_.isObject(cutoff)) { @@ -621,6 +627,7 @@ } else if (message.get('chat_state') === _converse.GONE) { this.showStatusNotification(message.get('fullname')+' '+__('has gone away')); } + return message; }, shouldShowOnTextMessage () { @@ -674,10 +681,13 @@ } if (message.get('type') === 'error') { this.handleErrorMessage(message); - } else if (!message.get('message')) { - this.handleChatStateMessage(message); } else { - this.handleTextMessage(message); + if (message.get('chat_state')) { + this.handleChatStateMessage(message); + } + if (message.get('message')) { + this.handleTextMessage(message); + } } _converse.emit('messageAdded', { 'message': message,