From 77f93a64719c374219848a1f9c3d99e1d21727b8 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 24 Nov 2016 02:07:32 +0100 Subject: [PATCH] Some initial work on removing jQuery --- src/converse-core.js | 133 +++++++++++++++++++++---------------------- src/converse-muc.js | 22 +++---- src/converse-otr.js | 5 +- 3 files changed, 79 insertions(+), 81 deletions(-) diff --git a/src/converse-core.js b/src/converse-core.js index 019e486e9..b528d4e62 100755 --- a/src/converse-core.js +++ b/src/converse-core.js @@ -375,12 +375,12 @@ this.giveFeedback = function (subject, klass, message) { $('.conn-feedback').each(function (idx, el) { - var $el = $(el); - $el.addClass('conn-feedback').text(subject); + el.classList.add('conn-feedback'); + el.textContent = subject; if (klass) { - $el.addClass(klass); + el.classList.add(klass); } else { - $el.removeClass('error'); + el.classList.remove('error'); } }); converse.emit('feedback', { @@ -639,7 +639,7 @@ }) .c('enable', {xmlns: Strophe.NS.CARBONS}); this.connection.addHandler(function (iq) { - if ($(iq).find('error').length > 0) { + if (iq.querySelectorAll('error').length > 0) { converse.log('ERROR: An error occured while trying to enable message carbons.'); } else { this.session.save({carbons_enabled: true}); @@ -936,10 +936,13 @@ }, subscribeToSuggestedItems: function (msg) { - $(msg).find('item').each(function () { - if (this.getAttribute('action') === 'add') { + _.each(msg.querySelectorAll('item'), function (item) { + if (item.getAttribute('action') === 'add') { converse.roster.addAndSubscribe( - this.getAttribute('jid'), null, converse.xmppstatus.get('fullname')); + item.getAttribute('jid'), + null, + converse.xmppstatus.get('fullname') + ); } }); return true; @@ -1090,10 +1093,9 @@ return true; } converse.connection.send($iq({type: 'result', id: id, from: converse.connection.jid})); - $(iq).children('query').find('item').each(function (idx, item) { - this.updateContact(item); - }.bind(this)); + var items = iq.querySelectorAll('query[xmlns="'+Strophe.NS.ROSTER+'"] item'); + _.each(items, this.updateContact.bind(this)); converse.emit('rosterPush', iq); return true; }, @@ -1112,9 +1114,8 @@ /* An IQ stanza containing the roster has been received from * the XMPP server. */ - $(iq).children('query').find('item').each(function (idx, item) { - this.updateContact(item); - }.bind(this)); + var items = iq.querySelectorAll('query[xmlns="'+Strophe.NS.ROSTER+'"] item'); + _.each(items, this.updateContact.bind(this)); converse.emit('roster', iq); }, @@ -1162,13 +1163,13 @@ * Note: this method gets completely overridden by converse-vcard.js */ var bare_jid = Strophe.getBareJidFromJid(presence.getAttribute('from')); - var nick = $(presence).children('nick[xmlns='+Strophe.NS.NICK+']').text(); + var nick_el = presence.querySelector('nick[xmlns='+Strophe.NS.NICK+']'); var user_data = { jid: bare_jid, subscription: 'none', ask: null, requesting: true, - fullname: nick || bare_jid, + fullname: nick_el && nick_el.textContent || bare_jid, }; this.create(user_data); converse.emit('contactRequest', user_data); @@ -1204,14 +1205,13 @@ }, presenceHandler: function (presence) { - var $presence = $(presence), - presence_type = presence.getAttribute('type'); + var presence_type = presence.getAttribute('type'); if (presence_type === 'error') { return true; } var jid = presence.getAttribute('from'), bare_jid = Strophe.getBareJidFromJid(jid), resource = Strophe.getResourceFromJid(jid), - chat_status = $presence.find('show').text() || 'online', - status_message = $presence.find('status'), + chat_status = _.propertyOf(presence.querySelector('show'))('textContent') || 'online', + status_message = _.propertyOf(presence.querySelector('status'))('textContent'), contact = this.get(bare_jid); if (this.isSelf(bare_jid)) { @@ -1223,18 +1223,16 @@ // synchronize_availability option set to update, // we'll update ours as well. converse.xmppstatus.save({'status': chat_status}); - if (status_message.length) { - converse.xmppstatus.save({ - 'status_message': status_message.text() - }); + if (status_message) { + converse.xmppstatus.save({'status_message': status_message}); } } return; - } else if (($presence.find('x').attr('xmlns') || '').indexOf(Strophe.NS.MUC) === 0) { + } else if (presence.querySelectorAll('x[xmlns="'+Strophe.NS.MUC+'"]').length) { return; // Ignore MUC } - if (contact && (status_message.text() !== contact.get('status'))) { - contact.save({'status': status_message.text()}); + if (contact && (status_message !== contact.get('status'))) { + contact.save({'status': status_message}); } if (presence_type === 'subscribed' && contact) { contact.ackSubscribe(); @@ -1324,35 +1322,35 @@ }); }, - getMessageAttributes: function ($message, $delay, original_stanza) { - $delay = $delay || $message.find('delay'); - var type = $message.attr('type'), + getMessageAttributes: function (message, delay, original_stanza) { + delay = delay || message.querySelector('delay'); + var type = message.getAttribute('type'), body, stamp, time, sender, from; if (type === 'error') { - body = $message.find('error').children('text').text(); + body = _.propertyOf(message.querySelector('error text'))('textContent'); } else { - body = $message.children('body').text(); + body = _.propertyOf(message.querySelector('body'))('textContent'); } - var delayed = $delay.length > 0, + var delayed = !_.isNull(delay), fullname = this.get('fullname'), is_groupchat = type === 'groupchat', - chat_state = $message.find(converse.COMPOSING).length && converse.COMPOSING || - $message.find(converse.PAUSED).length && converse.PAUSED || - $message.find(converse.INACTIVE).length && converse.INACTIVE || - $message.find(converse.ACTIVE).length && converse.ACTIVE || - $message.find(converse.GONE).length && converse.GONE; + chat_state = message.getElementsByTagName(converse.COMPOSING).length && converse.COMPOSING || + message.getElementsByTagName(converse.PAUSED).length && converse.PAUSED || + message.getElementsByTagName(converse.INACTIVE).length && converse.INACTIVE || + message.getElementsByTagName(converse.ACTIVE).length && converse.ACTIVE || + message.getElementsByTagName(converse.GONE).length && converse.GONE; if (is_groupchat) { - from = Strophe.unescapeNode(Strophe.getResourceFromJid($message.attr('from'))); + from = Strophe.unescapeNode(Strophe.getResourceFromJid(message.getAttribute('from'))); } else { - from = Strophe.getBareJidFromJid($message.attr('from')); + from = Strophe.getBareJidFromJid(message.getAttribute('from')); } if (_.isEmpty(fullname)) { fullname = from; } if (delayed) { - stamp = $delay.attr('stamp'); + stamp = delay.getAttribute('stamp'); time = stamp; } else { time = moment().format(); @@ -1368,13 +1366,13 @@ 'delayed': delayed, 'fullname': fullname, 'message': body || undefined, - 'msgid': $message.attr('id'), + 'msgid': message.getAttribute('id'), 'sender': sender, 'time': time }; }, - createMessage: function () { + createMessage: function (message, delay, original_stanza) { return this.messages.create(this.getMessageAttributes.apply(this, arguments)); } }); @@ -1421,8 +1419,7 @@ /* Handler method for all incoming error message stanzas */ // TODO: we can likely just reuse "onMessage" below - var $message = $(message), - from_jid = Strophe.getBareJidFromJid($message.attr('from')); + var from_jid = Strophe.getBareJidFromJid(message.getAttribute('from')); if (from_jid === converse.bare_jid) { return true; } @@ -1431,7 +1428,7 @@ if (!chatbox) { return true; } - chatbox.createMessage($message, null, message); + chatbox.createMessage(message, null, message); return true; }, @@ -1439,12 +1436,12 @@ /* Handler method for all incoming single-user chat "message" * stanzas. */ - var $message = $(message), - contact_jid, $forwarded, $delay, from_bare_jid, + var original_stanza = message, + contact_jid, forwarded, delay, from_bare_jid, from_resource, is_me, msgid, chatbox, resource, - from_jid = $message.attr('from'), - to_jid = $message.attr('to'), + from_jid = message.getAttribute('from'), + to_jid = message.getAttribute('to'), to_resource = Strophe.getResourceFromJid(to_jid); if (converse.filter_by_resource && (to_resource && to_resource !== converse.resource)) { @@ -1463,24 +1460,25 @@ ); return true; } - $forwarded = $message.find('forwarded'); - if ($forwarded.length) { - var $forwarded_message = $forwarded.children('message'); - if (Strophe.getBareJidFromJid($forwarded_message.attr('from')) !== from_jid) { + forwarded = message.querySelector('forwarded'); + if (!_.isNull(forwarded)) { + var forwarded_message = forwarded.querySelector('message'); + var forwarded_from = forwarded_message.getAttribute('from'); + if (Strophe.getBareJidFromJid(forwarded_from) !== from_jid) { // Prevent message forging via carbons // // https://xmpp.org/extensions/xep-0280.html#security return true; } - $message = $forwarded_message; - $delay = $forwarded.children('delay'); - from_jid = $message.attr('from'); - to_jid = $message.attr('to'); + message = forwarded_message; + delay = forwarded.querySelector('delay'); + from_jid = message.getAttribute('from'); + to_jid = message.getAttribute('to'); } from_bare_jid = Strophe.getBareJidFromJid(from_jid); from_resource = Strophe.getResourceFromJid(from_jid); is_me = from_bare_jid === converse.bare_jid; - msgid = $message.attr('id'); + msgid = message.getAttribute('id'); if (is_me) { // I am the sender, so this must be a forwarded message... contact_jid = Strophe.getBareJidFromJid(to_jid); @@ -1491,14 +1489,14 @@ } converse.emit('message', message); // Get chat box, but only create a new one when the message has a body. - chatbox = this.getChatBox(contact_jid, $message.find('body').length > 0); + chatbox = this.getChatBox(contact_jid, !_.isNull(message.querySelector('body'))); if (!chatbox) { return true; } if (msgid && chatbox.messages.findWhere({msgid: msgid})) { return true; // We already have this message stored. } - chatbox.createMessage($message, $delay, message); + chatbox.createMessage(message, delay, original_stanza); return true; }, @@ -1741,12 +1739,13 @@ }, onItems: function (stanza) { - $(stanza).find('query item').each(function (idx, item) { + var that = this; + _.each(stanza.querySelectorAll('query item'), function (item) { converse.connection.disco.info( - $(item).attr('jid'), + item.getAttribute('jid'), null, - this.onInfo.bind(this)); - }.bind(this)); + that.onInfo.bind(that)); + }); }, onInfo: function (stanza) { @@ -1757,11 +1756,11 @@ return; } $stanza.find('feature').each(function (idx, feature) { - var namespace = $(feature).attr('var'); + var namespace = feature.getAttribute('var'); this[namespace] = true; this.create({ 'var': namespace, - 'from': $stanza.attr('from') + 'from': stanza.getAttribute('from') }); }.bind(this)); } diff --git a/src/converse-muc.js b/src/converse-muc.js index ea324a5c9..d41327c2c 100755 --- a/src/converse-muc.js +++ b/src/converse-muc.js @@ -1804,25 +1804,25 @@ this.scrollDown(); }, - onChatRoomMessage: function (msg) { + onChatRoomMessage: function (message) { /* Given a stanza, create a message * Backbone.Model if appropriate. * * Parameters: * (XMLElement) msg: The received message stanza */ - var $message = $(msg), - $forwarded = $message.find('forwarded'), - $delay; - if ($forwarded.length) { - $message = $forwarded.children('message'); - $delay = $forwarded.children('delay'); + var original_stanza = message, + forwarded = message.querySelector('forwarded'), + delay; + if (!_.isNull(forwarded)) { + message = forwarded.querySelector('message'); + delay = forwarded.querySelector('delay'); } - var jid = msg.getAttribute('from'), - msgid = msg.getAttribute('id'), + var jid = message.getAttribute('from'), + msgid = message.getAttribute('id'), resource = Strophe.getResourceFromJid(jid), sender = resource && Strophe.unescapeNode(resource) || '', - subject = $message.children('subject').text(), + subject = _.propertyOf(message.querySelector('subject'))('textContent'), dupes = msgid && this.model.messages.filter(function (msg) { // Find duplicates. // Some bots (like HAL in the prosody chatroom) @@ -1839,7 +1839,7 @@ if (sender === '') { return true; } - this.model.createMessage($message, $delay, msg); + this.model.createMessage(message, delay, original_stanza); if (sender !== this.model.get('nick')) { // We only emit an event if it's not our own message converse.emit('message', msg); diff --git a/src/converse-otr.js b/src/converse-otr.js index dc1968374..e02042644 100644 --- a/src/converse-otr.js +++ b/src/converse-otr.js @@ -107,10 +107,9 @@ !(utils.isOTRMessage($message[0]) && !_.includes([UNVERIFIED, VERIFIED], this.get('otr_status'))); }, - createMessage: function ($message, $delay, original_stanza) { + createMessage: function (message, delay, original_stanza) { var converse = this.__super__.converse, - $body = $message.children('body'), - text = ($body.length > 0 ? $body.text() : undefined); + text = _.propertyOf(message.querySelector('body'))('textContent'); if ((!text) || (!converse.allow_otr)) { return this.__super__.createMessage.apply(this, arguments);