2020-08-01 03:43:15 +02:00
"use strict" ;
2020-11-30 23:46:45 +01:00
const { strict : assert } = require ( "assert" ) ;
2020-12-01 00:26:16 +01:00
const markdown _test _cases = require ( "../../zerver/tests/fixtures/markdown_test_cases.json" ) ;
2020-12-01 00:21:17 +01:00
const markdown _assert = require ( "../zjsunit/markdown_assert" ) ;
2021-02-14 08:44:04 +01:00
const { set _global , zrequire } = require ( "../zjsunit/namespace" ) ;
2020-12-01 00:39:47 +01:00
const { run _test } = require ( "../zjsunit/test" ) ;
2021-03-16 23:38:59 +01:00
const blueslip = require ( "../zjsunit/zblueslip" ) ;
2021-03-25 22:35:45 +01:00
const { page _params } = require ( "../zjsunit/zpage_params" ) ;
2020-12-01 00:02:16 +01:00
2020-12-01 23:21:38 +01:00
set _global ( "location" , {
origin : "http://zulip.zulipdev.com" ,
} ) ;
2021-04-03 19:07:13 +02:00
const example _realm _filters = [
2021-03-25 22:35:45 +01:00
[ "#(?P<id>[0-9]{2,8})" , "https://trac.example.com/ticket/%(id)s" ] ,
[ "ZBUG_(?P<id>[0-9]{2,8})" , "https://trac2.zulip.net/ticket/%(id)s" ] ,
[
"ZGROUP_(?P<id>[0-9]{2,8}):(?P<zone>[0-9]{1,8})" ,
"https://zone_%(zone)s.zulip.net/ticket/%(id)s" ,
2020-12-01 23:21:38 +01:00
] ,
2021-03-25 22:35:45 +01:00
] ;
page _params . translate _emoticons = false ;
2020-07-15 01:29:15 +02:00
2020-12-01 23:21:38 +01:00
function Image ( ) {
return { } ;
}
set _global ( "Image" , Image ) ;
const doc = "" ;
set _global ( "document" , doc ) ;
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
const emoji = zrequire ( "../shared/js/emoji" ) ;
const emoji _codes = zrequire ( "../generated/emoji/emoji_codes.json" ) ;
const pygments _data = zrequire ( "../generated/pygments_data.json" ) ;
const fenced _code = zrequire ( "../shared/js/fenced_code" ) ;
2020-07-15 01:29:15 +02:00
const markdown _config = zrequire ( "markdown_config" ) ;
zjsunit: Remove rewiremock dependency.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
2021-03-06 12:47:54 +01:00
const marked = zrequire ( "../third/marked/lib/marked" ) ;
2021-02-10 04:53:22 +01:00
const markdown = zrequire ( "markdown" ) ;
2020-08-20 21:24:06 +02:00
const people = zrequire ( "people" ) ;
2021-02-10 04:53:22 +01:00
const stream _data = zrequire ( "stream_data" ) ;
const user _groups = zrequire ( "user_groups" ) ;
2020-07-15 01:29:15 +02:00
2020-07-24 01:21:48 +02:00
const emoji _params = {
2016-10-30 20:54:53 +01:00
realm _emoji : {
2020-07-15 00:34:28 +02:00
1 : {
id : 1 ,
2020-07-15 01:29:15 +02:00
name : "burrito" ,
source _url : "/static/generated/emoji/images/emoji/burrito.png" ,
2018-03-11 18:55:20 +01:00
deactivated : false ,
} ,
2016-10-30 20:54:53 +01:00
} ,
2020-07-25 23:59:49 +02:00
emoji _codes ,
2020-07-24 01:21:48 +02:00
} ;
emoji . initialize ( emoji _params ) ;
2020-09-26 06:02:29 +02:00
fenced _code . initialize ( pygments _data ) ;
2017-08-16 22:00:19 +02:00
2019-11-02 00:06:25 +01:00
const cordelia = {
2020-07-15 01:29:15 +02:00
full _name : "Cordelia Lear" ,
2016-10-30 20:58:28 +01:00
user _id : 101 ,
2020-07-15 01:29:15 +02:00
email : "cordelia@zulip.com" ,
2017-01-24 01:51:58 +01:00
} ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( cordelia ) ;
2014-01-04 01:18:30 +01:00
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2020-07-15 01:29:15 +02:00
full _name : "Leo" ,
2017-01-24 00:44:08 +01:00
user _id : 102 ,
2020-07-15 01:29:15 +02:00
email : "leo@zulip.com" ,
2017-01-24 00:44:08 +01:00
} ) ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2020-07-15 01:29:15 +02:00
full _name : "Bobby <h1>Tables</h1>" ,
2018-03-29 00:25:58 +02:00
user _id : 103 ,
2020-07-15 01:29:15 +02:00
email : "bobby@zulip.com" ,
2018-03-29 00:25:58 +02:00
} ) ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2020-07-15 01:29:15 +02:00
full _name : "Mark Twin" ,
2018-08-19 03:39:57 +02:00
user _id : 104 ,
2020-07-15 01:29:15 +02:00
email : "twin1@zulip.com" ,
2018-08-19 03:39:57 +02:00
} ) ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2020-07-15 01:29:15 +02:00
full _name : "Mark Twin" ,
2018-08-19 03:39:57 +02:00
user _id : 105 ,
2020-07-15 01:29:15 +02:00
email : "twin2@zulip.com" ,
2018-08-19 03:39:57 +02:00
} ) ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2020-07-15 01:29:15 +02:00
full _name : "Brother of Bobby|123" ,
2018-08-19 03:39:57 +02:00
user _id : 106 ,
2020-07-15 01:29:15 +02:00
email : "bobby2@zulip.com" ,
2018-08-19 03:39:57 +02:00
} ) ;
2020-05-26 22:34:15 +02:00
people . add _active _user ( {
2019-12-13 01:38:49 +01:00
full _name : "& & &" ,
user _id : 107 ,
email : "ampampamp@zulip.com" ,
} ) ;
2017-01-24 01:51:58 +01:00
people . initialize _current _user ( cordelia . user _id ) ;
2017-01-24 00:44:08 +01:00
2019-11-02 00:06:25 +01:00
const hamletcharacters = {
2017-11-22 09:11:07 +01:00
name : "hamletcharacters" ,
id : 1 ,
description : "Characters of Hamlet" ,
members : [ cordelia . user _id ] ,
} ;
2019-11-02 00:06:25 +01:00
const backend = {
2017-11-22 09:11:07 +01:00
name : "Backend" ,
id : 2 ,
description : "Backend team" ,
members : [ ] ,
} ;
2019-11-02 00:06:25 +01:00
const edgecase _group = {
2018-03-29 00:25:58 +02:00
name : "Bobby <h1>Tables</h1>" ,
id : 3 ,
description : "HTML Syntax to check for Markdown edge cases." ,
members : [ ] ,
} ;
2019-12-13 01:38:49 +01:00
const amp _group = {
name : "& & &" ,
id : 4 ,
description : "Check ampersand escaping" ,
members : [ ] ,
} ;
2020-12-01 00:57:57 +01:00
user _groups . add ( hamletcharacters ) ;
user _groups . add ( backend ) ;
user _groups . add ( edgecase _group ) ;
user _groups . add ( amp _group ) ;
2017-11-22 09:11:07 +01:00
2019-11-02 00:06:25 +01:00
const denmark = {
2016-11-10 20:20:40 +01:00
subscribed : false ,
2020-07-15 01:29:15 +02:00
color : "blue" ,
name : "Denmark" ,
2016-11-10 20:20:40 +01:00
stream _id : 1 ,
2019-05-15 08:54:25 +02:00
is _muted : true ,
2016-11-10 20:20:40 +01:00
} ;
2019-11-02 00:06:25 +01:00
const social = {
2016-11-10 20:20:40 +01:00
subscribed : true ,
2020-07-15 01:29:15 +02:00
color : "red" ,
name : "social" ,
2016-11-10 20:20:40 +01:00
stream _id : 2 ,
2019-05-15 08:54:25 +02:00
is _muted : false ,
2016-12-03 23:17:57 +01:00
invite _only : true ,
2016-11-10 20:20:40 +01:00
} ;
2019-11-02 00:06:25 +01:00
const edgecase _stream = {
2018-03-29 00:25:58 +02:00
subscribed : true ,
2020-07-15 01:29:15 +02:00
color : "green" ,
name : "Bobby <h1>Tables</h1>" ,
2018-03-29 00:25:58 +02:00
stream _id : 3 ,
2019-05-15 08:54:25 +02:00
is _muted : false ,
2018-03-29 00:25:58 +02:00
} ;
2019-11-02 00:06:25 +01:00
const edgecase _stream _2 = {
2019-06-21 20:47:09 +02:00
subscribed : true ,
2020-07-15 01:29:15 +02:00
color : "yellow" ,
name : "Bobby <h1" ,
2019-06-21 20:47:09 +02:00
stream _id : 4 ,
is _muted : false ,
} ;
2019-12-13 01:38:49 +01:00
const amp _stream = {
subscribed : true ,
2020-07-15 01:29:15 +02:00
color : "orange" ,
name : "& & &" ,
2019-12-13 01:38:49 +01:00
stream _id : 5 ,
is _muted : false ,
} ;
2020-02-09 22:02:55 +01:00
stream _data . add _sub ( denmark ) ;
stream _data . add _sub ( social ) ;
stream _data . add _sub ( edgecase _stream ) ;
stream _data . add _sub ( edgecase _stream _2 ) ;
2019-06-21 20:47:09 +02:00
// Note: edgecase_stream cannot be mentioned because it is caught by
// streamTopicHandler and it would be parsed as edgecase_stream_2.
2019-12-13 01:38:49 +01:00
stream _data . add _sub ( amp _stream ) ;
2016-11-10 20:20:40 +01:00
2021-04-03 19:07:13 +02:00
markdown . initialize ( example _realm _filters , markdown _config . get _helpers ( ) ) ;
2013-12-04 17:16:08 +01:00
2021-03-16 12:31:25 +01:00
function test ( label , f ) {
run _test ( label , ( override ) => {
2021-04-03 19:07:13 +02:00
page _params . realm _users = [ ] ;
markdown . update _linkifier _rules ( example _realm _filters ) ;
2021-03-16 12:31:25 +01:00
f ( override ) ;
} ) ;
}
test ( "markdown_detection" , ( ) => {
2019-11-02 00:06:25 +01:00
const no _markup = [
2018-05-07 03:30:13 +02:00
"This is a plaintext message" ,
"This is a plaintext: message" ,
"This is a :plaintext message" ,
"This is a :plaintext message: message" ,
"Contains a not an image.jpeg/ok file" ,
"Contains a not an http://www.google.com/ok/image.png/stop file" ,
"No png to be found here, a png" ,
"No user mention **leo**" ,
"No user mention @what there" ,
"No group mention *hamletcharacters*" ,
2020-07-15 00:34:28 +02:00
'We like to code\n~~~\ndef code():\n we = "like to do"\n~~~' ,
2018-05-07 03:30:13 +02:00
"This is a\nmultiline :emoji: here\n message" ,
"This is an :emoji: message" ,
"User Mention @**leo**" ,
"User Mention @**leo f**" ,
"User Mention @**leo with some name**" ,
"Group Mention @*hamletcharacters*" ,
"Stream #**Verona**" ,
] ;
2013-12-04 17:16:08 +01:00
2019-11-02 00:06:25 +01:00
const markup = [
2018-05-07 03:30:13 +02:00
"Contains a https://zulip.com/image.png file" ,
"Contains a https://zulip.com/image.jpg file" ,
"https://zulip.com/image.jpg" ,
"also https://zulip.com/image.jpg" ,
"https://zulip.com/image.jpg too" ,
"Contains a zulip.com/foo.jpeg file" ,
"Contains a https://zulip.com/image.png file" ,
2020-10-23 02:43:28 +02:00
"Twitter URL https://twitter.com/jacobian/status/407886996565016579" ,
2018-05-07 03:30:13 +02:00
"https://twitter.com/jacobian/status/407886996565016579" ,
"then https://twitter.com/jacobian/status/407886996565016579" ,
2020-10-23 02:43:28 +02:00
"Twitter URL http://twitter.com/jacobian/status/407886996565016579" ,
"YouTube URL https://www.youtube.com/watch?v=HHZ8iqswiCw&feature=youtu.be&a" ,
2018-05-07 03:30:13 +02:00
] ;
2013-12-04 17:16:08 +01:00
2021-01-22 22:29:08 +01:00
for ( const content of no _markup ) {
2017-07-29 02:51:33 +02:00
assert . equal ( markdown . contains _backend _only _syntax ( content ) , false ) ;
2021-01-22 22:29:08 +01:00
}
2013-12-04 17:16:08 +01:00
2021-01-22 22:29:08 +01:00
for ( const content of markup ) {
2017-07-29 02:51:33 +02:00
assert . equal ( markdown . contains _backend _only _syntax ( content ) , true ) ;
2021-01-22 22:29:08 +01:00
}
2018-05-15 12:40:07 +02:00
} ) ;
2013-12-04 17:16:08 +01:00
2021-03-16 12:31:25 +01:00
test ( "marked_shared" , ( ) => {
2020-12-01 00:26:16 +01:00
const tests = markdown _test _cases . regular _tests ;
2017-12-10 09:01:37 +01:00
2021-01-22 22:29:08 +01:00
for ( const test of tests ) {
2018-03-28 10:40:44 +02:00
// Ignore tests if specified
if ( test . ignore === true ) {
2021-01-22 22:29:08 +01:00
continue ;
2018-03-28 10:40:44 +02:00
}
2019-11-02 00:06:25 +01:00
const message = { raw _content : test . input } ;
2018-03-25 23:23:22 +02:00
page _params . translate _emoticons = test . translate _emoticons || false ;
2017-06-13 23:49:50 +02:00
markdown . apply _markdown ( message ) ;
2019-11-02 00:06:25 +01:00
const output = message . content ;
const error _message = ` Failure in test: ${ test . name } ` ;
2017-07-29 03:04:17 +02:00
if ( test . marked _expected _output ) {
2020-12-01 00:21:17 +01:00
markdown _assert . notEqual ( test . expected _output , output , error _message ) ;
markdown _assert . equal ( test . marked _expected _output , output , error _message ) ;
2017-07-29 03:17:25 +02:00
} else if ( test . backend _only _rendering ) {
assert . equal ( markdown . contains _backend _only _syntax ( test . input ) , true ) ;
2017-06-13 23:49:50 +02:00
} else {
2020-12-01 00:21:17 +01:00
markdown _assert . equal ( test . expected _output , output , error _message ) ;
2017-06-13 23:49:50 +02:00
}
2021-01-22 22:29:08 +01:00
}
2018-05-15 12:40:07 +02:00
} ) ;
2014-01-17 21:08:45 +01:00
2021-03-16 12:31:25 +01:00
test ( "message_flags" , ( ) => {
2020-07-15 01:29:15 +02:00
let message = { raw _content : "@**Leo**" } ;
2017-05-09 18:01:43 +02:00
markdown . apply _markdown ( message ) ;
2017-12-16 23:25:31 +01:00
assert ( ! message . mentioned ) ;
assert ( ! message . mentioned _me _directly ) ;
2017-01-24 01:51:58 +01:00
2020-07-15 01:29:15 +02:00
message = { raw _content : "@**Cordelia Lear**" } ;
2017-05-09 18:01:43 +02:00
markdown . apply _markdown ( message ) ;
2017-12-16 23:25:31 +01:00
assert ( message . mentioned ) ;
assert ( message . mentioned _me _directly ) ;
2017-01-24 01:51:58 +01:00
2020-07-15 01:29:15 +02:00
message = { raw _content : "@**all**" } ;
2017-05-09 18:01:43 +02:00
markdown . apply _markdown ( message ) ;
2017-12-16 23:25:31 +01:00
assert ( message . mentioned ) ;
assert ( ! message . mentioned _me _directly ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2017-01-24 01:51:58 +01:00
2021-03-16 12:31:25 +01:00
test ( "marked" , ( ) => {
2019-11-02 00:06:25 +01:00
const test _cases = [
2020-07-15 01:29:15 +02:00
{ input : "hello" , expected : "<p>hello</p>" } ,
{ input : "hello there" , expected : "<p>hello there</p>" } ,
{ input : "hello **bold** for you" , expected : "<p>hello <strong>bold</strong> for you</p>" } ,
2020-07-15 00:34:28 +02:00
{
input : "hello ***foo*** for you" ,
expected : "<p>hello <strong><em>foo</em></strong> for you</p>" ,
} ,
2020-07-15 01:29:15 +02:00
{ input : "__hello__" , expected : "<p>__hello__</p>" } ,
2020-07-15 00:34:28 +02:00
{
input : "\n```\nfenced code\n```\n\nand then after\n" ,
expected :
2020-10-19 04:49:17 +02:00
'<div class="codehilite"><pre><span></span><code>fenced code\n</code></pre></div>\n<p>and then after</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input :
"\n```\n fenced code trailing whitespace \n```\n\nand then after\n" ,
expected :
2020-10-19 04:49:17 +02:00
'<div class="codehilite"><pre><span></span><code> fenced code trailing whitespace\n</code></pre></div>\n<p>and then after</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input : "* a\n* list \n* here" ,
expected : "<ul>\n<li>a</li>\n<li>list </li>\n<li>here</li>\n</ul>" ,
} ,
{
input : "\n```c#\nfenced code special\n```\n\nand then after\n" ,
expected :
2020-10-19 04:49:17 +02:00
'<div class="codehilite" data-code-language="C#"><pre><span></span><code>fenced code special\n</code></pre></div>\n<p>and then after</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input : "\n```vb.net\nfenced code dot\n```\n\nand then after\n" ,
expected :
2020-10-19 04:49:17 +02:00
'<div class="codehilite" data-code-language="VB.net"><pre><span></span><code>fenced code dot\n</code></pre></div>\n<p>and then after</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input : "Some text first\n* a\n* list \n* here\n\nand then after" ,
expected :
"<p>Some text first</p>\n<ul>\n<li>a</li>\n<li>list </li>\n<li>here</li>\n</ul>\n<p>and then after</p>" ,
} ,
{
input : "1. an\n2. ordered \n3. list" ,
expected : "<ol>\n<li>an</li>\n<li>ordered </li>\n<li>list</li>\n</ol>" ,
} ,
{
input : "\n~~~quote\nquote this for me\n~~~\nthanks\n" ,
expected : "<blockquote>\n<p>quote this for me</p>\n</blockquote>\n<p>thanks</p>" ,
} ,
{
input : "This is a @**CordeLIA Lear** mention" ,
expected :
'<p>This is a <span class="user-mention" data-user-id="101">@Cordelia Lear</span> mention</p>' ,
} ,
{
input : "These @ @**** are not mentions" ,
expected : "<p>These @ @<em>**</em> are not mentions</p>" ,
} ,
{
input : "These # #**** are not mentions" ,
expected : "<p>These # #<em>**</em> are not mentions</p>" ,
} ,
{ input : "These @* are not mentions" , expected : "<p>These @* are not mentions</p>" } ,
{
input : "These #* #*** are also not mentions" ,
expected : "<p>These #* #*** are also not mentions</p>" ,
} ,
{
input : "This is a #**Denmark** stream link" ,
expected :
'<p>This is a <a class="stream" data-stream-id="1" href="/#narrow/stream/1-Denmark">#Denmark</a> stream link</p>' ,
} ,
{
input : "This is #**Denmark** and #**social** stream links" ,
expected :
'<p>This is <a class="stream" data-stream-id="1" href="/#narrow/stream/1-Denmark">#Denmark</a> and <a class="stream" data-stream-id="2" href="/#narrow/stream/2-social">#social</a> stream links</p>' ,
} ,
{
input : "And this is a #**wrong** stream link" ,
expected : "<p>And this is a #**wrong** stream link</p>" ,
} ,
{
input : "This is a #**Denmark>some topic** stream_topic link" ,
expected :
2020-10-07 13:17:55 +02:00
'<p>This is a <a class="stream-topic" data-stream-id="1" href="/#narrow/stream/1-Denmark/topic/some.20topic">#Denmark > some topic</a> stream_topic link</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input : "This has two links: #**Denmark>some topic** and #**social>other topic**." ,
expected :
2020-10-07 13:17:55 +02:00
'<p>This has two links: <a class="stream-topic" data-stream-id="1" href="/#narrow/stream/1-Denmark/topic/some.20topic">#Denmark > some topic</a> and <a class="stream-topic" data-stream-id="2" href="/#narrow/stream/2-social/topic/other.20topic">#social > other topic</a>.</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input : "This is not a #**Denmark>** stream_topic link" ,
expected : "<p>This is not a #**Denmark>** stream_topic link</p>" ,
} ,
{
input : "mmm...:burrito:s" ,
expected :
'<p>mmm...<img alt=":burrito:" class="emoji" src="/static/generated/emoji/images/emoji/burrito.png" title="burrito">s</p>' ,
} ,
{
input : "This is an :poop: message" ,
expected :
'<p>This is an <span aria-label="poop" class="emoji emoji-1f4a9" role="img" title="poop">:poop:</span> message</p>' ,
} ,
{
2020-10-07 10:24:09 +02:00
input : "\uD83D\uDCA9" ,
2020-07-15 00:34:28 +02:00
expected :
'<p><span aria-label="poop" class="emoji emoji-1f4a9" role="img" title="poop">:poop:</span></p>' ,
} ,
{
input : "Silent mention: @_**Cordelia Lear**" ,
expected :
'<p>Silent mention: <span class="user-mention silent" data-user-id="101">Cordelia Lear</span></p>' ,
} ,
{
input :
"> Mention in quote: @**Cordelia Lear**\n\nMention outside quote: @**Cordelia Lear**" ,
expected :
'<blockquote>\n<p>Mention in quote: <span class="user-mention silent" data-user-id="101">Cordelia Lear</span></p>\n</blockquote>\n<p>Mention outside quote: <span class="user-mention" data-user-id="101">@Cordelia Lear</span></p>' ,
} ,
2021-03-13 18:15:14 +01:00
// Test only those linkifiers which don't return True for
2017-07-30 21:07:59 +02:00
// `contains_backend_only_syntax()`. Those which return True
// are tested separately.
2020-07-15 00:34:28 +02:00
{
2021-03-13 18:15:14 +01:00
input : "This is a linkifier #1234 with text after it" ,
2020-07-15 00:34:28 +02:00
expected :
2021-03-13 18:15:14 +01:00
'<p>This is a linkifier <a href="https://trac.example.com/ticket/1234" title="https://trac.example.com/ticket/1234">#1234</a> with text after it</p>' ,
2020-07-15 00:34:28 +02:00
} ,
2021-03-13 18:15:14 +01:00
{ input : "#1234is not a linkifier." , expected : "<p>#1234is not a linkifier.</p>" } ,
2020-07-15 00:34:28 +02:00
{
2021-03-13 18:15:14 +01:00
input : "A pattern written as #1234is not a linkifier." ,
expected : "<p>A pattern written as #1234is not a linkifier.</p>" ,
2020-07-15 00:34:28 +02:00
} ,
{
2021-03-13 18:15:14 +01:00
input : "This is a linkifier with ZGROUP_123:45 groups" ,
2020-07-15 00:34:28 +02:00
expected :
2021-03-13 18:15:14 +01:00
'<p>This is a linkifier with <a href="https://zone_45.zulip.net/ticket/123" title="https://zone_45.zulip.net/ticket/123">ZGROUP_123:45</a> groups</p>' ,
2020-07-15 00:34:28 +02:00
} ,
{ input : "Test *italic*" , expected : "<p>Test <em>italic</em></p>" } ,
{
input : "T\n#**Denmark**" ,
expected :
'<p>T<br>\n<a class="stream" data-stream-id="1" href="/#narrow/stream/1-Denmark">#Denmark</a></p>' ,
} ,
{
input : "T\n@**Cordelia Lear**" ,
expected :
'<p>T<br>\n<span class="user-mention" data-user-id="101">@Cordelia Lear</span></p>' ,
} ,
{
input : "@**Mark Twin|104** and @**Mark Twin|105** are out to confuse you." ,
expected :
'<p><span class="user-mention" data-user-id="104">@Mark Twin</span> and <span class="user-mention" data-user-id="105">@Mark Twin</span> are out to confuse you.</p>' ,
} ,
{ input : "@**Invalid User|1234**" , expected : "<p>@**Invalid User|1234**</p>" } ,
{
input : "@**Cordelia LeAR|103** has a wrong user_id." ,
expected : "<p>@**Cordelia LeAR|103** has a wrong user_id.</p>" ,
} ,
{
input : "@**Brother of Bobby|123** is really the full name." ,
expected :
'<p><span class="user-mention" data-user-id="106">@Brother of Bobby|123</span> is really the full name.</p>' ,
} ,
{
input : "@**Brother of Bobby|123|106**" ,
expected :
'<p><span class="user-mention" data-user-id="106">@Brother of Bobby|123</span></p>' ,
} ,
2021-03-23 20:22:04 +01:00
{
input : "@**|106** valid user id." ,
expected :
'<p><span class="user-mention" data-user-id="106">@Brother of Bobby|123</span> valid user id.</p>' ,
} ,
{
input : "@**|123|106** comes under user|id case." ,
expected : "<p>@**|123|106** comes under user|id case.</p>" ,
} ,
{ input : "@**|1234** invalid id." , expected : "<p>@**|1234** invalid id.</p>" } ,
2020-07-15 00:34:28 +02:00
{ input : "T\n@hamletcharacters" , expected : "<p>T<br>\n@hamletcharacters</p>" } ,
{
input : "T\n@*hamletcharacters*" ,
expected :
'<p>T<br>\n<span class="user-group-mention" data-user-group-id="1">@hamletcharacters</span></p>' ,
} ,
{ input : "T\n@*notagroup*" , expected : "<p>T<br>\n@*notagroup*</p>" } ,
{
input : "T\n@*backend*" ,
expected :
'<p>T<br>\n<span class="user-group-mention" data-user-group-id="2">@Backend</span></p>' ,
} ,
{ input : "@*notagroup*" , expected : "<p>@*notagroup*</p>" } ,
{
2021-03-13 18:15:14 +01:00
input : "This is a linkifier `hello` with text after it" ,
expected : "<p>This is a linkifier <code>hello</code> with text after it</p>" ,
2020-07-15 00:34:28 +02:00
} ,
2018-03-25 18:10:59 +02:00
// Test the emoticon conversion
2020-07-15 00:34:28 +02:00
{ input : ":)" , expected : "<p>:)</p>" } ,
{
input : ":)" ,
expected :
2020-06-30 21:16:29 +02:00
'<p><span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>' ,
2020-07-15 00:34:28 +02:00
translate _emoticons : true ,
} ,
2020-10-23 02:43:28 +02:00
// Test HTML escaping in custom Zulip rules
2020-07-15 00:34:28 +02:00
{
input : "@**<h1>The Rogue One</h1>**" ,
expected : "<p>@**<h1>The Rogue One</h1>**</p>" ,
} ,
{
input : "#**<h1>The Rogue One</h1>**" ,
expected : "<p>#**<h1>The Rogue One</h1>**</p>" ,
} ,
{
input : ":<h1>The Rogue One</h1>:" ,
expected : "<p>:<h1>The Rogue One</h1>:</p>" ,
} ,
{ input : "@**O'Connell**" , expected : "<p>@**O'Connell**</p>" } ,
{
input : "@*Bobby <h1>Tables</h1>*" ,
expected :
'<p><span class="user-group-mention" data-user-group-id="3">@Bobby <h1>Tables</h1></span></p>' ,
} ,
{
input : "@*& & &amp;*" ,
expected :
'<p><span class="user-group-mention" data-user-group-id="4">@& & &amp;</span></p>' ,
} ,
{
input : "@**Bobby <h1>Tables</h1>**" ,
expected :
'<p><span class="user-mention" data-user-id="103">@Bobby <h1>Tables</h1></span></p>' ,
} ,
{
input : "@**& & &amp;**" ,
expected :
'<p><span class="user-mention" data-user-id="107">@& & &amp;</span></p>' ,
} ,
{
input : "#**Bobby <h1>Tables</h1>**" ,
expected :
2020-10-07 13:17:55 +02:00
'<p><a class="stream-topic" data-stream-id="4" href="/#narrow/stream/4-Bobby-.3Ch1/topic/Tables.3C.2Fh1.3E">#Bobby <h1 > Tables</h1></a></p>' ,
2020-07-15 00:34:28 +02:00
} ,
{
input : "#**& & &amp;**" ,
expected :
'<p><a class="stream" data-stream-id="5" href="/#narrow/stream/5-.26-.26.20.26amp.3B">#& & &amp;</a></p>' ,
} ,
{
input : "#**& & &amp;>& & &amp;**" ,
expected :
2020-10-07 13:17:55 +02:00
'<p><a class="stream-topic" data-stream-id="5" href="/#narrow/stream/5-.26-.26.20.26amp.3B/topic/.26.20.26.20.26amp.3B">#& & &amp; > & & &amp;</a></p>' ,
2020-07-15 00:34:28 +02:00
} ,
2017-06-13 23:49:50 +02:00
] ;
2021-01-22 22:29:08 +01:00
for ( const test _case of test _cases ) {
2018-03-25 18:10:59 +02:00
// Disable emoji conversion by default.
page _params . translate _emoticons = test _case . translate _emoticons || false ;
2019-11-02 00:06:25 +01:00
const input = test _case . input ;
const expected = test _case . expected ;
2017-06-13 23:49:50 +02:00
2019-11-02 00:06:25 +01:00
const message = { raw _content : input } ;
2017-06-13 23:49:50 +02:00
markdown . apply _markdown ( message ) ;
2019-11-02 00:06:25 +01:00
const output = message . content ;
2020-07-28 17:00:59 +02:00
assert . equal ( output , expected ) ;
2021-01-22 22:29:08 +01:00
}
2018-05-15 12:40:07 +02:00
} ) ;
2014-01-24 21:48:56 +01:00
2021-03-16 12:31:25 +01:00
test ( "topic_links" , ( ) => {
2020-07-15 01:29:15 +02:00
let message = { type : "stream" , topic : "No links here" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 0 ) ;
2017-06-13 23:49:50 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "One #123 link here" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 1 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://trac.example.com/ticket/123" ,
text : "#123" ,
} ) ;
2017-06-13 23:49:50 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "Two #123 #456 link here" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 2 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://trac.example.com/ticket/123" ,
text : "#123" ,
} ) ;
assert . deepEqual ( message . topic _links [ 1 ] , {
url : "https://trac.example.com/ticket/456" ,
text : "#456" ,
} ) ;
2017-06-13 23:49:50 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "New ZBUG_123 link here" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 1 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://trac2.zulip.net/ticket/123" ,
text : "ZBUG_123" ,
} ) ;
2017-06-13 23:49:50 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "New ZBUG_123 with #456 link here" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 2 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://trac2.zulip.net/ticket/123" ,
text : "ZBUG_123" ,
} ) ;
assert . deepEqual ( message . topic _links [ 1 ] , {
url : "https://trac.example.com/ticket/456" ,
text : "#456" ,
} ) ;
2017-06-13 23:49:50 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "One ZGROUP_123:45 link here" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 1 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://zone_45.zulip.net/ticket/123" ,
text : "ZGROUP_123:45" ,
} ) ;
2017-06-13 23:49:50 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "Hello https://google.com" } ;
2019-05-25 16:10:30 +02:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 1 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://google.com" ,
text : "https://google.com" ,
} ) ;
2019-05-25 16:10:30 +02:00
2020-07-15 01:29:15 +02:00
message = { type : "stream" , topic : "#456 https://google.com https://github.com" } ;
2019-05-25 16:10:30 +02:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 3 ) ;
2021-01-26 07:32:29 +01:00
assert . deepEqual ( message . topic _links [ 0 ] , {
url : "https://trac.example.com/ticket/456" ,
text : "#456" ,
} ) ;
assert . deepEqual ( message . topic _links [ 1 ] , {
url : "https://google.com" ,
text : "https://google.com" ,
} ) ;
assert . deepEqual ( message . topic _links [ 2 ] , {
url : "https://github.com" ,
text : "https://github.com" ,
} ) ;
2019-05-25 16:10:30 +02:00
2017-06-13 23:49:50 +02:00
message = { type : "not-stream" } ;
2018-11-13 16:19:59 +01:00
markdown . add _topic _links ( message ) ;
2020-02-14 13:39:04 +01:00
assert . equal ( message . topic _links . length , 0 ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2014-01-27 17:06:59 +01:00
2021-03-16 12:31:25 +01:00
test ( "message_flags" , ( ) => {
2019-11-02 00:06:25 +01:00
let input = "/me is testing this" ;
let message = { topic : "No links here" , raw _content : input } ;
2017-06-13 23:49:50 +02:00
markdown . apply _markdown ( message ) ;
2017-08-27 18:10:36 +02:00
assert . equal ( message . is _me _message , true ) ;
2017-12-16 23:25:31 +01:00
assert ( ! message . unread ) ;
2017-06-13 23:49:50 +02:00
2018-01-21 19:27:36 +01:00
input = "/me is testing\nthis" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2018-01-21 19:27:36 +01:00
markdown . apply _markdown ( message ) ;
2018-12-29 11:07:27 +01:00
assert . equal ( message . is _me _message , true ) ;
2018-01-21 19:27:36 +01:00
2017-06-13 23:49:50 +02:00
input = "testing this @**all** @**Cordelia Lear**" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2017-06-13 23:49:50 +02:00
markdown . apply _markdown ( message ) ;
2017-08-27 18:10:36 +02:00
assert . equal ( message . is _me _message , false ) ;
2017-12-16 23:25:31 +01:00
assert . equal ( message . mentioned , true ) ;
assert . equal ( message . mentioned _me _directly , true ) ;
2017-06-13 23:49:50 +02:00
2018-04-03 17:55:57 +02:00
input = "test @**everyone**" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2018-04-03 17:55:57 +02:00
markdown . apply _markdown ( message ) ;
assert . equal ( message . is _me _message , false ) ;
assert . equal ( message . mentioned , true ) ;
assert . equal ( message . mentioned _me _directly , false ) ;
input = "test @**stream**" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2018-04-03 17:55:57 +02:00
markdown . apply _markdown ( message ) ;
assert . equal ( message . is _me _message , false ) ;
assert . equal ( message . mentioned , true ) ;
assert . equal ( message . mentioned _me _directly , false ) ;
2017-06-13 23:49:50 +02:00
input = "test @all" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2017-06-13 23:49:50 +02:00
markdown . apply _markdown ( message ) ;
2018-01-24 17:18:07 +01:00
assert . equal ( message . mentioned , false ) ;
input = "test @everyone" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2018-01-24 17:18:07 +01:00
markdown . apply _markdown ( message ) ;
assert . equal ( message . mentioned , false ) ;
2017-06-13 23:49:50 +02:00
input = "test @any" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2017-06-13 23:49:50 +02:00
markdown . apply _markdown ( message ) ;
2017-12-16 23:25:31 +01:00
assert . equal ( message . mentioned , false ) ;
2017-11-22 09:11:07 +01:00
2018-01-24 17:18:07 +01:00
input = "test @alleycat.com" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2018-01-24 17:18:07 +01:00
markdown . apply _markdown ( message ) ;
assert . equal ( message . mentioned , false ) ;
2017-11-22 09:11:07 +01:00
input = "test @*hamletcharacters*" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2017-11-22 09:11:07 +01:00
markdown . apply _markdown ( message ) ;
2017-12-16 23:25:31 +01:00
assert . equal ( message . mentioned , true ) ;
2017-11-22 09:11:07 +01:00
input = "test @*backend*" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2017-11-22 09:11:07 +01:00
markdown . apply _markdown ( message ) ;
2017-12-16 23:25:31 +01:00
assert . equal ( message . mentioned , false ) ;
2018-01-24 17:18:07 +01:00
input = "test @**invalid_user**" ;
2018-12-23 16:49:14 +01:00
message = { topic : "No links here" , raw _content : input } ;
2018-01-24 17:18:07 +01:00
markdown . apply _markdown ( message ) ;
assert . equal ( message . mentioned , false ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2017-06-13 23:26:27 +02:00
2021-03-16 12:31:25 +01:00
test ( "backend_only_linkifiers" , ( ) => {
2021-03-13 18:15:14 +01:00
const backend _only _linkifiers = [
2020-07-15 01:29:15 +02:00
"Here is the PR-#123." ,
"Function abc() was introduced in (PR)#123." ,
2017-07-30 21:07:59 +02:00
] ;
2021-03-13 18:15:14 +01:00
for ( const content of backend _only _linkifiers ) {
2017-07-30 21:07:59 +02:00
assert . equal ( markdown . contains _backend _only _syntax ( content ) , true ) ;
2021-01-22 22:29:08 +01:00
}
2018-05-15 12:40:07 +02:00
} ) ;
2017-07-30 21:07:59 +02:00
2021-03-16 12:31:25 +01:00
test ( "python_to_js_linkifier" , ( ) => {
2021-03-13 18:15:14 +01:00
// The only way to reach python_to_js_linkifier is indirectly, hence the call
// to update_linkifier_rules.
markdown . update _linkifier _rules ( [ [ "/a(?im)a/g" ] , [ "/a(?L)a/g" ] ] ) ;
let actual _value = marked . InlineLexer . rules . zulip . linkifiers ;
2020-10-07 12:37:15 +02:00
let expected _value = [ /\/aa\/g(?!\w)/gim , /\/aa\/g(?!\w)/g ] ;
2017-06-13 23:49:50 +02:00
assert . deepEqual ( actual _value , expected _value ) ;
2019-02-11 22:54:18 +01:00
// Test case with multiple replacements.
2021-03-13 18:15:14 +01:00
markdown . update _linkifier _rules ( [
2020-10-07 12:37:15 +02:00
[ "#cf(?P<contest>\\d+)(?P<problem>[A-Z][\\dA-Z]*)" , "http://google.com" ] ,
2020-07-15 00:34:28 +02:00
] ) ;
2021-03-13 18:15:14 +01:00
actual _value = marked . InlineLexer . rules . zulip . linkifiers ;
2020-10-07 12:37:15 +02:00
expected _value = [ /#cf(\d+)([A-Z][\dA-Z]*)(?!\w)/g ] ;
2019-02-11 22:54:18 +01:00
assert . deepEqual ( actual _value , expected _value ) ;
2019-02-12 22:30:57 +01:00
// Test incorrect syntax.
2020-07-15 00:34:28 +02:00
blueslip . expect (
"error" ,
2021-03-13 18:15:14 +01:00
"python_to_js_linkifier: Invalid regular expression: /!@#@(!#&((!&(@#((?!\\w)/: Unterminated group" ,
2020-07-15 00:34:28 +02:00
) ;
2021-03-13 18:15:14 +01:00
markdown . update _linkifier _rules ( [ [ "!@#@(!#&((!&(@#(" , "http://google.com" ] ] ) ;
actual _value = marked . InlineLexer . rules . zulip . linkifiers ;
2019-02-12 22:30:57 +01:00
expected _value = [ ] ;
assert . deepEqual ( actual _value , expected _value ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2017-06-14 11:19:00 +02:00
2021-03-16 12:31:25 +01:00
test ( "translate_emoticons_to_names" , ( ) => {
2020-02-15 15:21:32 +01:00
// Simple test
2020-07-15 01:29:15 +02:00
const test _text = "Testing :)" ;
2020-06-30 21:16:29 +02:00
const expected = "Testing :smile:" ;
2020-02-15 15:21:32 +01:00
const result = markdown . translate _emoticons _to _names ( test _text ) ;
2020-07-28 17:00:59 +02:00
assert . equal ( result , expected ) ;
2020-02-15 15:21:32 +01:00
// Extensive tests.
// The following code loops over the test cases and each emoticon conversion
// to generate multiple test cases.
const testcases = [
2020-07-15 01:29:15 +02:00
{ name : "only emoticon" , original : "<original>" , expected : "<converted>" } ,
{ name : "space at start" , original : " <original>" , expected : " <converted>" } ,
{ name : "space at end" , original : "<original> " , expected : "<converted> " } ,
{ name : "symbol at end" , original : "<original>!" , expected : "<converted>!" } ,
{ name : "symbol at start" , original : "Hello,<original>" , expected : "Hello,<converted>" } ,
{ name : "after a word" , original : "Hello<original>" , expected : "Hello<original>" } ,
{ name : "between words" , original : "Hello<original>World" , expected : "Hello<original>World" } ,
2020-07-15 00:34:28 +02:00
{
name : "end of sentence" ,
original : "End of sentence. <original>" ,
expected : "End of sentence. <converted>" ,
} ,
{
name : "between symbols" ,
original : "Hello.<original>! World." ,
expected : "Hello.<original>! World." ,
} ,
{
name : "before end of sentence" ,
original : "Hello <original>!" ,
expected : "Hello <converted>!" ,
} ,
2020-02-15 15:21:32 +01:00
] ;
for ( const [ shortcut , full _name ] of Object . entries ( emoji _codes . emoticon _conversions ) ) {
for ( const t of testcases ) {
const converted _value = full _name ;
let original = t . original ;
let expected = t . expected ;
original = original . replace ( /(<original>)/g , shortcut ) ;
2020-07-15 00:34:28 +02:00
expected = expected
. replace ( /(<original>)/g , shortcut )
2020-02-15 15:21:32 +01:00
. replace ( /(<converted>)/g , converted _value ) ;
const result = markdown . translate _emoticons _to _names ( original ) ;
assert . equal ( result , expected ) ;
}
}
} ) ;
2020-07-25 17:50:34 +02:00
2021-03-16 12:31:25 +01:00
test ( "missing unicode emojis" , ( override ) => {
2020-10-07 10:24:09 +02:00
const message = { raw _content : "\u{1F6B2}" } ;
2020-07-25 17:50:34 +02:00
markdown . apply _markdown ( message ) ;
assert . equal (
message . content ,
'<p><span aria-label="bike" class="emoji emoji-1f6b2" role="img" title="bike">:bike:</span></p>' ,
) ;
2021-02-14 08:44:04 +01:00
override ( emoji , "get_emoji_name" , ( codepoint ) => {
// Now simulate that we don't know any emoji names.
2020-07-25 17:50:34 +02:00
assert . equal ( codepoint , "1f6b2" ) ;
// return undefined
2020-07-27 16:06:46 +02:00
} ) ;
2021-02-14 08:44:04 +01:00
markdown . apply _markdown ( message ) ;
2020-10-07 10:24:09 +02:00
assert . equal ( message . content , "<p>\u{1F6B2}</p>" ) ;
2020-07-25 17:50:34 +02:00
} ) ;