Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
converse.js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
converse.js
Commits
a014eca9
Commit
a014eca9
authored
Jul 09, 2020
by
JC Brand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a `renderImage` directive which handles errors
parent
8943609d
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
55 additions
and
47 deletions
+55
-47
spec/http-file-upload.js
spec/http-file-upload.js
+6
-6
spec/messages.js
spec/messages.js
+25
-4
src/templates/directives/body.js
src/templates/directives/body.js
+1
-2
src/templates/directives/image.js
src/templates/directives/image.js
+16
-0
src/templates/image.js
src/templates/image.js
+2
-1
src/utils/html.js
src/utils/html.js
+5
-34
No files found.
spec/http-file-upload.js
View file @
a014eca9
...
@@ -318,9 +318,9 @@ describe("XEP-0363: HTTP File Upload", function () {
...
@@ -318,9 +318,9 @@ describe("XEP-0363: HTTP File Upload", function () {
`</message>`
);
`</message>`
);
await
u
.
waitUntil
(()
=>
view
.
el
.
querySelector
(
'
.chat-image
'
),
1000
);
await
u
.
waitUntil
(()
=>
view
.
el
.
querySelector
(
'
.chat-image
'
),
1000
);
// Check that the image renders
// Check that the image renders
expect
(
view
.
el
.
querySelector
(
'
.chat-msg .chat-msg__media
'
).
innerHTML
.
trim
()).
toEqual
(
expect
(
view
.
el
.
querySelector
(
'
.chat-msg .chat-msg__media
'
).
innerHTML
.
replace
(
/<!---->/g
,
''
).
trim
()).
toEqual
(
`<
!----><
a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg"></a>
<!---->
`
);
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg"></a>`
);
XMLHttpRequest
.
prototype
.
send
=
send_backup
;
XMLHttpRequest
.
prototype
.
send
=
send_backup
;
done
();
done
();
}));
}));
...
@@ -422,9 +422,9 @@ describe("XEP-0363: HTTP File Upload", function () {
...
@@ -422,9 +422,9 @@ describe("XEP-0363: HTTP File Upload", function () {
`</message>`
);
`</message>`
);
await
u
.
waitUntil
(()
=>
view
.
el
.
querySelector
(
'
.chat-image
'
),
1000
);
await
u
.
waitUntil
(()
=>
view
.
el
.
querySelector
(
'
.chat-image
'
),
1000
);
// Check that the image renders
// Check that the image renders
expect
(
view
.
el
.
querySelector
(
'
.chat-msg .chat-msg__media
'
).
innerHTML
.
trim
()).
toEqual
(
expect
(
view
.
el
.
querySelector
(
'
.chat-msg .chat-msg__media
'
).
innerHTML
.
replace
(
/<!---->/g
,
''
).
trim
()).
toEqual
(
`<
!----><
a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg"></a>
<!---->
`
);
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg"></a>`
);
XMLHttpRequest
.
prototype
.
send
=
send_backup
;
XMLHttpRequest
.
prototype
.
send
=
send_backup
;
done
();
done
();
...
...
spec/messages.js
View file @
a014eca9
...
@@ -952,7 +952,7 @@ describe("A Chat Message", function () {
...
@@ -952,7 +952,7 @@ describe("A Chat Message", function () {
let
msg
=
sizzle
(
'
.chat-content .chat-msg:last .chat-msg__text
'
).
pop
();
let
msg
=
sizzle
(
'
.chat-content .chat-msg:last .chat-msg__text
'
).
pop
();
expect
(
msg
.
innerHTML
.
replace
(
/<!---->/g
,
''
).
trim
()).
toEqual
(
expect
(
msg
.
innerHTML
.
replace
(
/<!---->/g
,
''
).
trim
()).
toEqual
(
`<a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<img class="chat-image img-thumbnail" src="
https://conversejs.org
/logo/conversejs-filled.svg">`
+
`</a>`
);
`</a>`
);
message
+=
"
?param1=val1¶m2=val2
"
;
message
+=
"
?param1=val1¶m2=val2
"
;
...
@@ -982,6 +982,27 @@ describe("A Chat Message", function () {
...
@@ -982,6 +982,27 @@ describe("A Chat Message", function () {
done
();
done
();
}));
}));
it
(
"
will fall back to rendering images as URLs
"
,
mock
.
initConverse
(
[
'
rosterGroupsFetched
'
,
'
chatBoxesFetched
'
],
{},
async
function
(
done
,
_converse
)
{
await
mock
.
waitForRoster
(
_converse
,
'
current
'
);
const
base_url
=
'
https://conversejs.org
'
;
const
message
=
base_url
+
"
/logo/non-existing.svg
"
;
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
);
await
u
.
waitUntil
(()
=>
view
.
el
.
querySelectorAll
(
'
.chat-content .chat-image
'
).
length
,
1000
)
expect
(
view
.
model
.
sendMessage
).
toHaveBeenCalled
();
const
msg
=
sizzle
(
'
.chat-content .chat-msg:last .chat-msg__text
'
).
pop
();
await
u
.
waitUntil
(()
=>
msg
.
innerHTML
.
replace
(
/<!---->/g
,
''
).
trim
()
==
`<a target="_blank" rel="noopener" href="https://conversejs.org/logo/non-existing.svg">https://conversejs.org/logo/non-existing.svg</a>`
);
done
();
}));
it
(
"
will render the message time as configured
"
,
it
(
"
will render the message time as configured
"
,
mock
.
initConverse
(
mock
.
initConverse
(
[
'
rosterGroupsFetched
'
,
'
chatBoxesFetched
'
],
{},
[
'
rosterGroupsFetched
'
,
'
chatBoxesFetched
'
],
{},
...
@@ -1971,9 +1992,9 @@ describe("A Chat Message", function () {
...
@@ -1971,9 +1992,9 @@ describe("A Chat Message", function () {
expect
(
u
.
hasClass
(
'
chat-msg__text
'
,
msg
)).
toBe
(
true
);
expect
(
u
.
hasClass
(
'
chat-msg__text
'
,
msg
)).
toBe
(
true
);
expect
(
msg
.
textContent
).
toEqual
(
'
Have you seen this funny image?
'
);
expect
(
msg
.
textContent
).
toEqual
(
'
Have you seen this funny image?
'
);
const
media
=
view
.
el
.
querySelector
(
'
.chat-msg .chat-msg__media
'
);
const
media
=
view
.
el
.
querySelector
(
'
.chat-msg .chat-msg__media
'
);
expect
(
media
.
innerHTML
.
replace
(
/
(\r\n
|
\n
|
\r)
/gm
,
""
)).
toEqual
(
expect
(
media
.
innerHTML
.
replace
(
/
<!---->/g
,
''
).
replace
(
/
(\r\n
|
\n
|
\r)
/gm
,
""
)).
toEqual
(
`<
!----><
a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<a class="chat-image__link" target="_blank" rel="noopener" href="
${
base_url
}
/logo/conversejs-filled.svg">`
+
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg"></a>
<!---->
`
);
`<img class="chat-image img-thumbnail" src="
${
base_url
}
/logo/conversejs-filled.svg"></a>`
);
done
();
done
();
}));
}));
});
});
...
...
src/templates/directives/body.js
View file @
a014eca9
...
@@ -206,9 +206,8 @@ class MessageBodyRenderer {
...
@@ -206,9 +206,8 @@ class MessageBodyRenderer {
export
const
renderBodyText
=
directive
(
component
=>
part
=>
{
export
const
renderBodyText
=
directive
(
component
=>
part
=>
{
const
model
=
component
.
model
;
const
renderer
=
new
MessageBodyRenderer
(
component
);
const
renderer
=
new
MessageBodyRenderer
(
component
);
part
.
setValue
(
renderer
.
render
());
part
.
setValue
(
renderer
.
render
());
part
.
commit
()
;
const
model
=
component
.
model
;
model
.
collection
?.
trigger
(
'
rendered
'
,
model
);
model
.
collection
?.
trigger
(
'
rendered
'
,
model
);
});
});
src/templates/directives/image.js
0 → 100644
View file @
a014eca9
import
{
directive
,
html
}
from
"
lit-html
"
;
export
const
renderImage
=
directive
(
url
=>
part
=>
{
function
onError
()
{
part
.
setValue
(
converse
.
env
.
utils
.
convertUrlToHyperlink
(
url
));
part
.
commit
();
}
part
.
setValue
(
html
`<a href="
${
url
}
"
class="chat-image__link"
target="_blank"
rel="noopener"
><img class="chat-image img-thumbnail" src="
${
url
}
" @error=
${
onError
}
/></a>`
);
});
src/templates/image.js
View file @
a014eca9
import
{
html
}
from
"
lit-html
"
;
import
{
html
}
from
"
lit-html
"
;
import
{
renderImage
}
from
"
./directives/image.js
"
;
export
default
(
o
)
=>
html
`
<a href="
${
o
.
url
}
" class="chat-image__link" target="_blank" rel="noopener"><img class="chat-image img-thumbnail" src="
${
o
.
url
}
"/></a>
`
;
export
default
(
o
)
=>
html
`
${
renderImage
(
o
.
url
)}
`
;
src/utils/html.js
View file @
a014eca9
...
@@ -99,11 +99,7 @@ function renderImageURL (_converse, uri) {
...
@@ -99,11 +99,7 @@ function renderImageURL (_converse, uri) {
if
(
!
_converse
.
api
.
settings
.
get
(
'
show_images_inline
'
))
{
if
(
!
_converse
.
api
.
settings
.
get
(
'
show_images_inline
'
))
{
return
u
.
convertURIoHyperlink
(
uri
);
return
u
.
convertURIoHyperlink
(
uri
);
}
}
const
{
__
}
=
_converse
;
return
tpl_image
({
'
url
'
:
uri
.
toString
()});
return
tpl_image
({
'
url
'
:
uri
.
toString
(),
'
label_download
'
:
__
(
'
Download image "%1$s"
'
,
getFileName
(
uri
))
})
}
}
function
renderFileURL
(
_converse
,
uri
)
{
function
renderFileURL
(
_converse
,
uri
)
{
...
@@ -162,24 +158,6 @@ u.applyDragResistance = function (value, default_value) {
...
@@ -162,24 +158,6 @@ u.applyDragResistance = function (value, default_value) {
};
};
function
loadImage
(
url
)
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
err_msg
=
`Could not determine whether it's an image:
${
url
}
`
;
const
img
=
new
Image
();
const
timer
=
window
.
setTimeout
(()
=>
reject
(
new
Error
(
err_msg
)),
20000
);
img
.
onerror
=
img
.
onabort
=
function
()
{
clearTimeout
(
timer
);
reject
(
new
Error
(
err_msg
));
};
img
.
onload
=
function
()
{
clearTimeout
(
timer
);
resolve
(
img
);
};
img
.
src
=
url
;
});
}
u
.
calculateElementHeight
=
function
(
el
)
{
u
.
calculateElementHeight
=
function
(
el
)
{
/* Return the height of the passed in DOM element,
/* Return the height of the passed in DOM element,
* based on the heights of its children.
* based on the heights of its children.
...
@@ -311,24 +289,17 @@ u.escapeHTML = function (string) {
...
@@ -311,24 +289,17 @@ u.escapeHTML = function (string) {
.
replace
(
/"/g
,
"
"
"
);
.
replace
(
/"/g
,
"
"
"
);
};
};
u
.
convertToImageTag
=
async
function
(
url
)
{
u
.
convertToImageTag
=
function
(
url
)
{
const
uri
=
getURI
(
url
);
const
uri
=
getURI
(
url
);
const
img_url_without_ext
=
[
'
imgur.com
'
,
'
pbs.twimg.com
'
].
includes
(
uri
.
hostname
());
const
img_url_without_ext
=
[
'
imgur.com
'
,
'
pbs.twimg.com
'
].
includes
(
uri
.
hostname
());
let
src
;
if
(
u
.
isImageURL
(
url
)
||
img_url_without_ext
)
{
if
(
u
.
isImageURL
(
url
)
||
img_url_without_ext
)
{
if
(
img_url_without_ext
)
{
if
(
img_url_without_ext
)
{
const
format
=
(
uri
.
hostname
()
===
'
pbs.twimg.com
'
)
?
uri
.
search
(
true
).
format
:
'
png
'
;
const
format
=
(
uri
.
hostname
()
===
'
pbs.twimg.com
'
)
?
uri
.
search
(
true
).
format
:
'
png
'
;
src
=
uri
.
removeSearch
(
/.*/
).
toString
()
+
`.
${
format
}
`
;
return
tpl_image
({
'
url
'
:
uri
.
removeSearch
(
/.*/
).
toString
()
+
`.
${
format
}
`
})
;
}
else
{
}
else
{
src
=
url
;
return
tpl_image
({
url
});
}
try
{
await
loadImage
(
src
);
}
catch
(
e
)
{
log
.
error
(
e
);
return
u
.
convertUrlToHyperlink
(
url
);
}
}
return
tpl_image
({
url
,
src
});
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment