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
5a1b308e
Commit
5a1b308e
authored
May 28, 2016
by
JC Brand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Render images from URLs
parent
c738d085
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
49 deletions
+120
-49
css/converse.css
css/converse.css
+3
-0
sass/_chatbox.scss
sass/_chatbox.scss
+4
-0
spec/chatbox.js
spec/chatbox.js
+85
-39
src/utils.js
src/utils.js
+28
-10
No files found.
css/converse.css
View file @
5a1b308e
...
...
@@ -1347,6 +1347,9 @@
#conversejs
.chatbox
.chat-body
.chat-info.chat-date
{
display
:
inline-block
;
margin-top
:
1em
;
}
#conversejs
.chatbox
.chat-body
.chat-image
{
max-width
:
100%
;
max-height
:
100%
;
}
#conversejs
.chatbox
.chat-body
.chat-message
{
margin
:
0.3em
;
}
#conversejs
.chatbox
.chat-body
.chat-message
span
{
...
...
sass/_chatbox.scss
View file @
5a1b308e
...
...
@@ -154,6 +154,10 @@
margin-top
:
1em
;
}
}
.chat-image
{
max-width
:
100%
;
max-height
:
100%
;
}
.chat-message
{
margin
:
0
.3em
;
span
{
...
...
spec/chatbox.js
View file @
5a1b308e
...
...
@@ -819,19 +819,6 @@
expect
(
msg
.
html
()).
toEqual
(
'
<p>This message contains <em>some</em> <b>markup</b></p>
'
);
}.
bind
(
converse
));
it
(
"
can contain hyperlinks, which will be clickable
"
,
function
()
{
var
contact_jid
=
mock
.
cur_names
[
0
].
replace
(
/ /g
,
'
.
'
).
toLowerCase
()
+
'
@localhost
'
;
test_utils
.
openChatBoxFor
(
contact_jid
);
var
view
=
this
.
chatboxviews
.
get
(
contact_jid
);
var
message
=
'
This message contains a hyperlink: www.opkode.com
'
;
spyOn
(
view
,
'
sendMessage
'
).
andCallThrough
();
test_utils
.
sendMessage
(
view
,
message
);
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
var
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
This message contains a hyperlink: <a target="_blank" rel="noopener" href="http://www.opkode.com">www.opkode.com</a>
'
);
}.
bind
(
converse
));
it
(
"
should display emoticons correctly
"
,
function
()
{
var
contact_jid
=
mock
.
cur_names
[
0
].
replace
(
/ /g
,
'
.
'
).
toLowerCase
()
+
'
@localhost
'
;
test_utils
.
openChatBoxFor
(
contact_jid
);
...
...
@@ -856,40 +843,99 @@
}
}.
bind
(
converse
));
it
(
"
can contain hyperlinks, which will be clickable
"
,
function
()
{
var
contact_jid
=
mock
.
cur_names
[
0
].
replace
(
/ /g
,
'
.
'
).
toLowerCase
()
+
'
@localhost
'
;
test_utils
.
openChatBoxFor
(
contact_jid
);
var
view
=
converse
.
chatboxviews
.
get
(
contact_jid
);
var
message
=
'
This message contains a hyperlink: www.opkode.com
'
;
spyOn
(
view
,
'
sendMessage
'
).
andCallThrough
();
runs
(
function
()
{
test_utils
.
sendMessage
(
view
,
message
);
});
waits
(
500
);
runs
(
function
()
{
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
var
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
This message contains a hyperlink: <a target="_blank" rel="noopener" href="http://www.opkode.com">www.opkode.com</a>
'
);
});
});
it
(
"
will have properly escaped URLs
"
,
function
()
{
if
(
/PhantomJS/
.
test
(
window
.
navigator
.
userAgent
))
{
// Flaky under PhantomJS due to timeouts
return
;
}
// TODO: make these local urls
var
message
,
msg
;
var
contact_jid
=
mock
.
cur_names
[
0
].
replace
(
/ /g
,
'
.
'
).
toLowerCase
()
+
'
@localhost
'
;
test_utils
.
openChatBoxFor
(
contact_jid
);
var
view
=
this
.
chatboxviews
.
get
(
contact_jid
);
var
view
=
converse
.
chatboxviews
.
get
(
contact_jid
);
spyOn
(
view
,
'
sendMessage
'
).
andCallThrough
();
runs
(
function
()
{
message
=
"
http://www.opkode.com/'onmouseover='alert(1)'whatever
"
;
test_utils
.
sendMessage
(
view
,
message
);
});
waits
(
500
);
runs
(
function
()
{
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="http://www.opkode.com/%27onmouseover=%27alert%281%29%27whatever">http://www.opkode.com/
\'
onmouseover=
\'
alert(1)
\'
whatever</a>
'
);
var
message
=
"
http://www.opkode.com/'onmouseover='alert(1)'whatever
"
;
test_utils
.
sendMessage
(
view
,
message
);
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
var
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="http://www.opkode.com/%27onmouseover=%27alert%281%29%27whatever">http://www.opkode.com/
\'
onmouseover=
\'
alert(1)
\'
whatever</a>
'
);
message
=
'
http://www.opkode.com/"onmouseover="alert(1)"whatever
'
;
test_utils
.
sendMessage
(
view
,
message
);
});
waits
(
500
);
runs
(
function
()
{
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="http://www.opkode.com/%22onmouseover=%22alert%281%29%22whatever">http://www.opkode.com/"onmouseover="alert(1)"whatever</a>
'
);
message
=
'
http://www.opkode.com/"onmouseover="alert(1)"whatever
'
;
test_utils
.
sendMessage
(
view
,
message
);
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="http://www.opkode.com/%22onmouseover=%22alert%281%29%22whatever">http://www.opkode.com/"onmouseover="alert(1)"whatever</a>
'
);
message
=
"
https://en.wikipedia.org/wiki/Ender's_Game
"
;
test_utils
.
sendMessage
(
view
,
message
);
});
waits
(
500
);
runs
(
function
()
{
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Ender%27s_Game">https://en.wikipedia.org/wiki/Ender
\'
s_Game</a>
'
);
message
=
"
https://en.wikipedia.org/wiki/Ender's_Game
"
;
test_utils
.
sendMessage
(
view
,
message
);
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Ender%27s_Game">https://en.wikipedia.org/wiki/Ender
\'
s_Game</a>
'
);
message
=
"
https://en.wikipedia.org/wiki/Ender%27s_Game
"
;
test_utils
.
sendMessage
(
view
,
message
);
});
waits
(
500
);
runs
(
function
()
{
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Ender%27s_Game">https://en.wikipedia.org/wiki/Ender%27s_Game</a>
'
);
});
});
message
=
"
https://en.wikipedia.org/wiki/Ender%27s_Game
"
;
test_utils
.
sendMessage
(
view
,
message
);
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
text
()).
toEqual
(
message
);
expect
(
msg
.
html
()).
toEqual
(
'
<a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Ender%27s_Game">https://en.wikipedia.org/wiki/Ender%27s_Game</a>
'
);
}.
bind
(
converse
));
it
(
"
will render images from their URLs
"
,
function
()
{
if
(
/PhantomJS/
.
test
(
window
.
navigator
.
userAgent
))
{
// Doesn't work when running tests in PhantomJS, since
// the page is loaded via file:///
return
;
}
var
message
=
document
.
URL
.
split
(
window
.
location
.
pathname
)[
0
]
+
"
/logo/conversejs.svg
"
;
var
contact_jid
=
mock
.
cur_names
[
0
].
replace
(
/ /g
,
'
.
'
).
toLowerCase
()
+
'
@localhost
'
;
test_utils
.
openChatBoxFor
(
contact_jid
);
var
view
=
converse
.
chatboxviews
.
get
(
contact_jid
);
spyOn
(
view
,
'
sendMessage
'
).
andCallThrough
();
runs
(
function
()
{
test_utils
.
sendMessage
(
view
,
message
);
});
waits
(
500
);
runs
(
function
()
{
expect
(
view
.
sendMessage
).
toHaveBeenCalled
();
var
msg
=
view
.
$el
.
find
(
'
.chat-content
'
).
find
(
'
.chat-message
'
).
last
().
find
(
'
.chat-msg-content
'
);
expect
(
msg
.
html
()).
toEqual
(
'
<img src="
'
+
message
+
'
" class="chat-image">
'
);
});
});
}.
bind
(
converse
));
...
...
src/utils.js
View file @
5a1b308e
...
...
@@ -20,6 +20,16 @@
'
list-multi
'
:
'
dropdown
'
};
var
isImage
=
function
(
url
)
{
var
deferred
=
new
$
.
Deferred
();
$
(
"
<img>
"
,
{
src
:
url
,
error
:
deferred
.
reject
,
load
:
deferred
.
resolve
});
return
deferred
.
promise
();
};
$
.
expr
[
'
:
'
].
emptyVal
=
function
(
obj
){
return
obj
.
value
===
''
;
};
...
...
@@ -34,19 +44,27 @@
return
false
;
};
$
.
fn
.
throttledHTML
=
_
.
throttle
(
$
.
fn
.
html
,
500
);
$
.
fn
.
addHyperlinks
=
function
()
{
if
(
this
.
length
>
0
)
{
this
.
each
(
function
(
i
,
obj
)
{
var
x
=
$
(
obj
).
html
();
var
list
=
x
.
match
(
/
\b(
https
?
:
\/\/
|www
\.
|https
?
:
\/\/
www
\.)[^\s
<
]{2,200}\b
/g
);
if
(
list
)
{
for
(
i
=
0
;
i
<
list
.
length
;
i
++
)
{
var
prot
=
list
[
i
].
indexOf
(
'
http://
'
)
===
0
||
list
[
i
].
indexOf
(
'
https://
'
)
===
0
?
''
:
'
http://
'
;
var
escaped_url
=
encodeURI
(
decodeURI
(
list
[
i
])).
replace
(
/
[
!'()
]
/g
,
escape
).
replace
(
/
\*
/g
,
"
%2A
"
);
x
=
x
.
replace
(
list
[
i
],
'
<a target="_blank" rel="noopener" href="
'
+
prot
+
escaped_url
+
'
">
'
+
list
[
i
]
+
'
</a>
'
);
}
}
$
(
obj
).
html
(
x
);
var
$obj
=
$
(
obj
);
var
x
=
$obj
.
html
();
_
.
each
(
x
.
match
(
/
\b(
https
?
:
\/\/
|www
\.
|https
?
:
\/\/
www
\.)[^\s
<
]{2,200}\b
/g
),
function
(
url
)
{
isImage
(
url
)
.
then
(
function
()
{
event
.
target
.
className
=
'
chat-image
'
;
x
=
x
.
replace
(
url
,
event
.
target
.
outerHTML
);
$obj
.
throttledHTML
(
x
);
})
.
fail
(
function
()
{
var
prot
=
url
.
indexOf
(
'
http://
'
)
===
0
||
url
.
indexOf
(
'
https://
'
)
===
0
?
''
:
'
http://
'
;
var
escaped_url
=
encodeURI
(
decodeURI
(
url
)).
replace
(
/
[
!'()
]
/g
,
escape
).
replace
(
/
\*
/g
,
"
%2A
"
);
x
=
x
.
replace
(
url
,
'
<a target="_blank" rel="noopener" href="
'
+
prot
+
escaped_url
+
'
">
'
+
url
+
'
</a>
'
);
$obj
.
throttledHTML
(
x
);
});
});
});
}
return
this
;
...
...
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