From 758c46c5aa6063d4029a0d580dba4156bcab6502 Mon Sep 17 00:00:00 2001 From: Ariel Fuggini Date: Mon, 8 Feb 2021 16:55:04 -0500 Subject: [PATCH] Fixes infinite loop bug when appending .png to allowed image urls --- CHANGES.md | 1 + spec/messages.js | 23 +++++++++++++++++++++++ src/templates/directives/image.js | 7 +++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 12e2c3769..6e60e23e7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ - #1083: Add support for XEP-0393 Message Styling - #2275: Allow punctuation to immediately precede a mention +- #2400: Fixes infinite loop bug when appending .png to allowed image urls - Add support for XEP-0437 Room Activity Indicators see [muc-subscribe-to-rai](https://conversejs.org/docs/html/configuration.html#muc-subscribe-to-rai) - Bugfix: Use real JID in XEP-0372 references only when the MUC is non-anonymous - Bugfix: Connection protocol not updated based on XEP-0156 connection methods diff --git a/spec/messages.js b/spec/messages.js index 792d1ffa1..44fa86082 100644 --- a/spec/messages.js +++ b/spec/messages.js @@ -730,6 +730,29 @@ describe("A Chat Message", function () { done(); })); + it("will fall back to rendering URLs that match image_urls_regex as URLs", + mock.initConverse( + ['rosterGroupsFetched', 'chatBoxesFetched'], { + 'show_images_inline': ['twimg.com'], + 'image_urls_regex': /^https?:\/\/(www.)?(pbs\.twimg\.com\/)/i + }, + async function (done, _converse) { + + await mock.waitForRoster(_converse, 'current'); + const message = "https://pbs.twimg.com/media/string?format=jpg&name=small"; + const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; + await mock.openChatBoxFor(_converse, contact_jid); + const view = _converse.api.chatviews.get(contact_jid); + spyOn(view.model, 'sendMessage').and.callThrough(); + mock.sendMessage(view, message); + expect(view.model.sendMessage).toHaveBeenCalled(); + await u.waitUntil(() => view.el.querySelector('.chat-content .chat-msg'), 1000); + const msg = view.el.querySelector('.chat-content .chat-msg .chat-msg__text'); + await u.waitUntil(() => msg.innerHTML.replace(//g, '').trim() == + `https://pbs.twimg.com/media/string?format=jpg&name=small`, 1000); + done(); + })); + it("will render the message time as configured", mock.initConverse( ['rosterGroupsFetched', 'chatBoxesFetched'], {}, diff --git a/src/templates/directives/image.js b/src/templates/directives/image.js index 071841b58..107101462 100644 --- a/src/templates/directives/image.js +++ b/src/templates/directives/image.js @@ -1,6 +1,6 @@ import { converse } from "@converse/headless/core"; import { directive, html } from "lit-html"; - +import URI from "urijs"; /** * lit-html directive which attempts to render an element from a URL. @@ -21,7 +21,10 @@ export const renderImage = directive((src, href, onLoad, onClick) => part => { // Before giving up and falling back to just rendering a hyperlink, // we attach `.png` and try one more time. // This works with some Imgur URLs - part.setValue(renderImage(`${src}.png`, href, onLoad, onClick)); + const uri = new URI(src); + const filename = uri.filename(); + uri.filename(`${filename}.png`); + part.setValue(renderImage(uri.toString(), href, onLoad, onClick)); part.commit(); } }