2020-08-01 03:43:15 +02:00
"use strict" ;
2020-07-15 01:29:15 +02:00
zrequire ( "hash_util" ) ;
2020-07-25 00:12:17 +02:00
const emoji = zrequire ( "emoji" , "shared/js/emoji" ) ;
2020-07-15 01:29:15 +02:00
const emoji _codes = zrequire ( "emoji_codes" , "generated/emoji/emoji_codes.json" ) ;
2020-07-23 22:41:45 +02:00
const fenced _code = zrequire ( "fenced_code" , "shared/js/fenced_code" ) ;
2020-07-15 01:29:15 +02:00
const markdown _config = zrequire ( "markdown_config" ) ;
2020-07-28 00:31:25 +02:00
const marked = zrequire ( "marked" , "third/marked/lib/marked" ) ;
2020-07-23 23:09:03 +02:00
2020-07-15 01:29:15 +02:00
zrequire ( "markdown" ) ;
2020-07-23 23:09:03 +02:00
zrequire ( "message_store" ) ;
zrequire ( "people" ) ;
zrequire ( "stream_data" ) ;
zrequire ( "user_groups" ) ;
2020-07-15 01:29:15 +02:00
set _global ( "location" , {
origin : "http://zulip.zulipdev.com" ,
2017-04-04 15:31:21 +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
} ;
set _global ( "page_params" , {
realm _users : [ ] ,
2016-10-30 20:54:53 +01:00
realm _filters : [
2020-07-15 00:34:28 +02: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" ] ,
2016-10-30 20:54:53 +01:00
[
"ZGROUP_(?P<id>[0-9]{2,8}):(?P<zone>[0-9]{1,8})" ,
2016-12-03 23:17:57 +01:00
"https://zone_%(zone)s.zulip.net/ticket/%(id)s" ,
] ,
] ,
2018-01-15 19:36:32 +01:00
translate _emoticons : false ,
2016-10-30 20:54:53 +01:00
} ) ;
2014-01-24 22:52:37 +01:00
2020-07-02 01:55:18 +02:00
function Image ( ) {
2018-05-07 03:30:13 +02:00
return { } ;
2020-07-02 01:55:18 +02:00
}
2020-07-15 01:29:15 +02:00
set _global ( "Image" , Image ) ;
2020-07-24 01:21:48 +02:00
emoji . initialize ( emoji _params ) ;
2017-08-16 22:00:19 +02:00
2019-11-02 00:06:25 +01:00
const doc = "" ;
2020-07-15 01:29:15 +02:00
set _global ( "document" , doc ) ;
2014-01-16 20:18:55 +01:00
2020-07-15 01:29:15 +02:00
set _global ( "$" , global . make _zjquery ( ) ) ;
2013-12-04 17:16:08 +01: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 : [ ] ,
} ;
2017-11-22 09:11:07 +01:00
global . user _groups . add ( hamletcharacters ) ;
global . user _groups . add ( backend ) ;
2018-03-29 00:25:58 +02:00
global . user _groups . add ( edgecase _group ) ;
2019-12-13 01:38:49 +01:00
global . user _groups . add ( amp _group ) ;
2017-11-22 09:11:07 +01:00
2019-11-02 00:06:25 +01:00
const stream _data = global . stream _data ;
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
2017-06-15 23:48:29 +02:00
// Check the default behavior of fenced code blocks
// works properly before markdown is initialized.
2020-07-15 01:29:15 +02:00
run _test ( "fenced_block_defaults" , ( ) => {
const input = "\n```\nfenced code\n```\n\nand then after\n" ;
2020-07-15 00:34:28 +02:00
const expected =
'\n\n<div class="codehilite"><pre><span></span><code>fenced code\n</code></pre></div>\n\n\n\nand then after\n\n' ;
2019-11-02 00:06:25 +01:00
const output = fenced _code . process _fenced _code ( input ) ;
2017-06-15 23:48:29 +02:00
assert . equal ( output , expected ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2017-06-15 23:48:29 +02:00
2020-07-15 00:34:28 +02:00
markdown . initialize ( page _params . realm _filters , markdown _config . get _helpers ( ) ) ;
2013-12-04 17:16:08 +01:00
2020-07-15 01:29:15 +02:00
const markdown _data = global . read _fixture _data ( "markdown_test_cases.json" ) ;
2014-01-17 21:08:45 +01:00
2020-07-15 01:29:15 +02:00
run _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" ,
"twitter url https://twitter.com/jacobian/status/407886996565016579" ,
"https://twitter.com/jacobian/status/407886996565016579" ,
"then https://twitter.com/jacobian/status/407886996565016579" ,
"twitter url http://twitter.com/jacobian/status/407886996565016579" ,
"youtube url https://www.youtube.com/watch?v=HHZ8iqswiCw&feature=youtu.be&a" ,
] ;
2013-12-04 17:16:08 +01:00
2020-07-02 01:45:54 +02:00
no _markup . forEach ( ( content ) => {
2017-07-29 02:51:33 +02:00
assert . equal ( markdown . contains _backend _only _syntax ( content ) , false ) ;
2013-12-04 17:16:08 +01:00
} ) ;
2020-07-02 01:45:54 +02:00
markup . forEach ( ( content ) => {
2017-07-29 02:51:33 +02:00
assert . equal ( markdown . contains _backend _only _syntax ( content ) , true ) ;
2013-12-04 17:16:08 +01:00
} ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2013-12-04 17:16:08 +01:00
2020-07-15 01:29:15 +02:00
run _test ( "marked_shared" , ( ) => {
2020-06-29 23:14:41 +02:00
const tests = markdown _data . regular _tests ;
2017-12-10 09:01:37 +01:00
2020-07-02 01:45:54 +02:00
tests . forEach ( ( test ) => {
2018-03-28 10:40:44 +02:00
// Ignore tests if specified
if ( test . ignore === true ) {
return ;
}
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-06-29 23:09:59 +02:00
global . markdown _assert . notEqual ( test . expected _output , output , error _message ) ;
global . 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-06-29 23:09:59 +02:00
global . markdown _assert . equal ( test . expected _output , output , error _message ) ;
2017-06-13 23:49:50 +02:00
}
} ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2014-01-17 21:08:45 +01:00
2020-07-15 01:29:15 +02:00
run _test ( "message_flags" , ( ) => {
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
2020-07-15 01:29:15 +02:00
run _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 :
'<div class="codehilite"><pre><span></span><code>fenced code\n</code></pre></div>\n\n\n<p>and then after</p>' ,
} ,
{
input :
"\n```\n fenced code trailing whitespace \n```\n\nand then after\n" ,
expected :
'<div class="codehilite"><pre><span></span><code> fenced code trailing whitespace\n</code></pre></div>\n\n\n<p>and then after</p>' ,
} ,
{
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 :
'<div class="codehilite"><pre><span></span><code>fenced code special\n</code></pre></div>\n\n\n<p>and then after</p>' ,
} ,
{
input : "\n```vb.net\nfenced code dot\n```\n\nand then after\n" ,
expected :
'<div class="codehilite"><pre><span></span><code>fenced code dot\n</code></pre></div>\n\n\n<p>and then after</p>' ,
} ,
{
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 :
'<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>' ,
} ,
{
input : "This has two links: #**Denmark>some topic** and #**social>other topic**." ,
expected :
'<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>' ,
} ,
{
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>' ,
} ,
{
input : "\ud83d\udca9" ,
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>' ,
} ,
2017-07-30 21:07:59 +02:00
// Test only those realm filters which don't return True for
// `contains_backend_only_syntax()`. Those which return True
// are tested separately.
2020-07-15 00:34:28 +02:00
{
input : "This is a realm filter #1234 with text after it" ,
expected :
'<p>This is a realm filter <a href="https://trac.example.com/ticket/1234" title="https://trac.example.com/ticket/1234">#1234</a> with text after it</p>' ,
} ,
{ input : "#1234is not a realm filter." , expected : "<p>#1234is not a realm filter.</p>" } ,
{
input : "A pattern written as #1234is not a realm filter." ,
expected : "<p>A pattern written as #1234is not a realm filter.</p>" ,
} ,
{
input : "This is a realm filter with ZGROUP_123:45 groups" ,
expected :
'<p>This is a realm filter with <a href="https://zone_45.zulip.net/ticket/123" title="https://zone_45.zulip.net/ticket/123">ZGROUP_123:45</a> groups</p>' ,
} ,
{ 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>' ,
} ,
{ 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>" } ,
{
input : "This is a realm filter `hello` with text after it" ,
expected : "<p>This is a realm filter <code>hello</code> with text after it</p>" ,
} ,
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 ,
} ,
2018-05-07 03:30:13 +02:00
// Test HTML Escape 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 :
'<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>' ,
} ,
{
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 :
'<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>' ,
} ,
2017-06-13 23:49:50 +02:00
] ;
2020-07-02 01:45:54 +02:00
test _cases . forEach ( ( test _case ) => {
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 ) ;
2017-06-13 23:49:50 +02:00
} ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2014-01-24 21:48:56 +01:00
2020-07-15 01:29:15 +02:00
run _test ( "topic_links" , ( ) => {
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 ) ;
2020-05-29 00:20:42 +02:00
assert . equal ( message . topic _links [ 0 ] , "https://trac.example.com/ticket/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 ) ;
2020-05-29 00:20:42 +02:00
assert . equal ( message . topic _links [ 0 ] , "https://trac.example.com/ticket/123" ) ;
assert . equal ( message . topic _links [ 1 ] , "https://trac.example.com/ticket/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 ) ;
assert . equal ( message . topic _links [ 0 ] , "https://trac2.zulip.net/ticket/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 ) ;
assert ( message . topic _links . includes ( "https://trac2.zulip.net/ticket/123" ) ) ;
2020-05-29 00:20:42 +02:00
assert ( message . topic _links . includes ( "https://trac.example.com/ticket/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 ) ;
assert . equal ( message . topic _links [ 0 ] , "https://zone_45.zulip.net/ticket/123" ) ;
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 ) ;
assert . equal ( message . topic _links [ 0 ] , "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 ) ;
assert ( message . topic _links . includes ( "https://google.com" ) ) ;
assert ( message . topic _links . includes ( "https://github.com" ) ) ;
2020-05-29 00:20:42 +02:00
assert ( message . topic _links . includes ( "https://trac.example.com/ticket/456" ) ) ;
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
2020-07-15 01:29:15 +02:00
run _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
2020-07-15 01:29:15 +02:00
run _test ( "backend_only_realm_filters" , ( ) => {
2019-11-02 00:06:25 +01:00
const backend _only _realm _filters = [
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
] ;
2020-07-02 01:45:54 +02:00
backend _only _realm _filters . forEach ( ( content ) => {
2017-07-30 21:07:59 +02:00
assert . equal ( markdown . contains _backend _only _syntax ( content ) , true ) ;
} ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2017-07-30 21:07:59 +02:00
2020-07-15 01:29:15 +02:00
run _test ( "python_to_js_filter" , ( ) => {
2017-06-13 23:49:50 +02:00
// The only way to reach python_to_js_filter is indirectly, hence the call
2020-02-17 14:43:59 +01:00
// to update_realm_filter_rules.
2020-07-15 01:29:15 +02:00
markdown . update _realm _filter _rules ( [ [ "/a(?im)a/g" ] , [ "/a(?L)a/g" ] ] ) ;
2019-11-02 00:06:25 +01:00
let actual _value = marked . InlineLexer . rules . zulip . realm _filters ;
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.
2020-07-15 00:34:28 +02:00
markdown . update _realm _filter _rules ( [
[ "#cf(?P<contest>[0-9]+)(?P<problem>[A-Z][0-9A-Z]*)" , "http://google.com" ] ,
] ) ;
2019-02-11 22:54:18 +01:00
actual _value = marked . InlineLexer . rules . zulip . realm _filters ;
expected _value = [ /#cf([0-9]+)([A-Z][0-9A-Z]*)(?![\w])/g ] ;
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" ,
"python_to_js_filter: Invalid regular expression: /!@#@(!#&((!&(@#((?![\\w])/: Unterminated group" ,
) ;
2020-07-15 01:29:15 +02:00
markdown . update _realm _filter _rules ( [ [ "!@#@(!#&((!&(@#(" , "http://google.com" ] ] ) ;
2019-02-12 22:30:57 +01:00
actual _value = marked . InlineLexer . rules . zulip . realm _filters ;
expected _value = [ ] ;
assert . deepEqual ( actual _value , expected _value ) ;
2018-05-15 12:40:07 +02:00
} ) ;
2017-06-14 11:19:00 +02:00
2020-07-15 01:29:15 +02:00
run _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
run _test ( "missing unicode emojis" , ( ) => {
const message = { raw _content : "\u{1f6b2}" } ;
markdown . apply _markdown ( message ) ;
assert . equal (
message . content ,
'<p><span aria-label="bike" class="emoji emoji-1f6b2" role="img" title="bike">:bike:</span></p>' ,
) ;
// Now simulate that we don't know any emoji names.
function fake _get _emoji _name ( codepoint ) {
assert . equal ( codepoint , "1f6b2" ) ;
// return undefined
}
2020-07-27 16:06:46 +02:00
with _field ( emoji , "get_emoji_name" , fake _get _emoji _name , ( ) => {
markdown . apply _markdown ( message ) ;
} ) ;
2020-07-25 17:50:34 +02:00
assert . equal ( message . content , "<p>\u{1f6b2}</p>" ) ;
} ) ;