2014-01-04 01:18:30 +01:00
/*global Dict */
2014-01-17 21:08:45 +01:00
var path = require ( 'path' ) ;
var fs = require ( 'fs' ) ;
2016-11-10 20:20:40 +01:00
var jsdom = require ( "jsdom" ) ;
var window = jsdom . jsdom ( ) . defaultView ;
2014-01-17 21:08:45 +01:00
2016-07-30 20:01:15 +02:00
global . stub _out _jquery ( ) ;
2016-07-30 19:39:36 +02:00
2016-10-30 20:54:53 +01:00
set _global ( 'page_params' , {
2016-10-30 20:58:28 +01:00
people _list : [ ] ,
2016-10-30 20:54:53 +01:00
realm _emoji : {
2016-12-28 05:07:10 +01:00
burrito : { display _url : '/static/generated/emoji/images/emoji/burrito.png' ,
source _url : '/static/generated/emoji/images/emoji/burrito.png' } ,
2016-10-30 20:54:53 +01:00
} ,
realm _filters : [
[
"#(?P<id>[0-9]{2,8})" ,
2016-12-03 23:17:57 +01:00
"https://trac.zulip.net/ticket/%(id)s" ,
2016-10-30 20:54:53 +01:00
] ,
[
"ZBUG_(?P<id>[0-9]{2,8})" ,
2016-12-03 23:17:57 +01:00
"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" ,
] ,
] ,
2016-10-30 20:54:53 +01:00
} ) ;
2014-01-24 22:52:37 +01:00
2013-12-04 17:16:08 +01:00
add _dependencies ( {
2014-01-04 01:18:30 +01:00
marked : 'third/marked/lib/marked.js' ,
2017-01-26 00:41:53 +01:00
emoji _codes : 'generated/emoji/emoji_codes.js' ,
2014-01-27 23:21:48 +01:00
emoji : 'js/emoji.js' ,
2014-01-30 22:42:19 +01:00
people : 'js/people.js' ,
2016-11-10 20:20:40 +01:00
stream _data : 'js/stream_data.js' ,
hashchange : 'js/hashchange' ,
2016-12-03 23:17:57 +01:00
fenced _code : 'js/fenced_code.js' ,
2013-12-04 17:16:08 +01:00
} ) ;
2014-01-16 20:18:55 +01:00
var doc = "" ;
set _global ( 'document' , doc ) ;
2013-12-04 17:16:08 +01:00
set _global ( '$' , function ( obj ) {
if ( typeof obj === 'function' ) {
// Run on-load setup
obj ( ) ;
} else if ( typeof obj === 'string' ) {
2014-01-16 20:18:55 +01:00
// $(document).on usage
2013-12-04 17:16:08 +01:00
// Selector usage
return { on : function ( ) { } } ;
}
} ) ;
2014-01-27 18:21:58 +01:00
set _global ( 'feature_flags' , { local _echo : true } ) ;
2016-11-10 20:20:40 +01:00
jsdom . changeURL ( window , 'http://zulip.zulipdev.com' ) ;
set _global ( 'window' , window ) ;
2016-10-30 20:58:28 +01:00
var people = global . people ;
2017-01-24 01:51:58 +01:00
var cordelia = {
2016-10-30 20:58:28 +01:00
full _name : 'Cordelia Lear' ,
user _id : 101 ,
2016-12-03 23:17:57 +01:00
email : 'cordelia@zulip.com' ,
2017-01-24 01:51:58 +01:00
} ;
people . add ( cordelia ) ;
2014-01-04 01:18:30 +01:00
2017-01-24 00:44:08 +01:00
people . add ( {
full _name : 'Leo' ,
user _id : 102 ,
email : 'leo@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
2016-11-10 20:20:40 +01:00
var stream _data = global . stream _data ;
var denmark = {
subscribed : false ,
color : 'blue' ,
name : 'Denmark' ,
stream _id : 1 ,
2016-12-03 23:17:57 +01:00
in _home _view : false ,
2016-11-10 20:20:40 +01:00
} ;
var social = {
subscribed : true ,
color : 'red' ,
name : 'social' ,
stream _id : 2 ,
in _home _view : true ,
2016-12-03 23:17:57 +01:00
invite _only : true ,
2016-11-10 20:20:40 +01:00
} ;
stream _data . add _sub ( 'Denmark' , denmark ) ;
stream _data . add _sub ( 'social' , social ) ;
2013-12-04 17:16:08 +01:00
var echo = require ( 'js/echo.js' ) ;
2015-10-13 23:34:50 +02:00
var bugdown _data = JSON . parse ( fs . readFileSync ( path . join ( _ _dirname , '../../zerver/fixtures/bugdown-data.json' ) , 'utf8' , 'r' ) ) ;
2014-01-17 21:08:45 +01:00
2013-12-04 17:16:08 +01:00
( function test _bugdown _detection ( ) {
var no _markup = [
"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**" ,
2014-01-03 22:51:00 +01:00
"No user mention @what there" ,
2014-01-04 00:21:06 +01:00
"We like to code\n~~~\ndef code():\n we = \"like to do\"\n~~~" ,
"This is a\nmultiline :emoji: here\n message" ,
2014-01-04 01:18:30 +01:00
"This is an :emoji: message" ,
"User Mention @**leo**" ,
"User Mention @**leo f**" ,
2016-10-19 16:49:53 +02:00
"User Mention @**leo with some name**" ,
2016-11-10 20:20:40 +01:00
"Stream #**Verona**" ,
2016-10-20 21:42:55 +02:00
"This contains !gravatar(leo@zulip.com)" ,
2016-12-03 23:17:57 +01:00
"And an avatar !avatar(leo@zulip.com) is here" ,
2013-12-04 17:16:08 +01:00
] ;
var markup = [
"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" ,
2016-12-03 23:17:57 +01:00
"youtube url https://www.youtube.com/watch?v=HHZ8iqswiCw&feature=youtu.be&a" ,
2013-12-04 17:16:08 +01:00
] ;
no _markup . forEach ( function ( content ) {
assert . equal ( echo . contains _bugdown ( content ) , false ) ;
} ) ;
markup . forEach ( function ( content ) {
assert . equal ( echo . contains _bugdown ( content ) , true ) ;
} ) ;
} ( ) ) ;
2014-01-17 21:08:45 +01:00
( function test _marked _shared ( ) {
var tests = bugdown _data . regular _tests ;
tests . forEach ( function ( test ) {
2017-01-20 15:21:28 +01:00
var message = { raw _content : test . input } ;
echo . apply _markdown ( message ) ;
var output = message . content ;
2014-01-17 21:08:45 +01:00
if ( test . bugdown _matches _marked ) {
assert . equal ( test . expected _output , output ) ;
} else {
assert . notEqual ( test . expected _output , output ) ;
}
} ) ;
} ( ) ) ;
2017-01-24 01:51:58 +01:00
( function test _message _flags ( ) {
var message = { raw _content : '@**Leo**' } ;
echo . apply _markdown ( message ) ;
assert ( ! _ . contains ( message . flags , 'mentioned' ) ) ;
message = { raw _content : '@**Cordelia Lear**' } ;
echo . apply _markdown ( message ) ;
assert ( _ . contains ( message . flags , 'mentioned' ) ) ;
message = { raw _content : '@**all**' } ;
echo . apply _markdown ( message ) ;
assert ( _ . contains ( message . flags , 'mentioned' ) ) ;
} ( ) ) ;
2013-12-04 17:16:08 +01:00
( function test _marked ( ) {
var test _cases = [
{ 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>' } ,
{ input : '__hello__' , expected : '<p>__hello__</p>' } ,
2016-09-12 20:50:10 +02:00
{ input : '\n```\nfenced code\n```\n\nand then after\n' ,
expected : '<div class="codehilite"><pre><span></span>fenced code\n</pre></div>\n\n\n<p>and then after</p>' } ,
2013-12-04 17:16:08 +01:00
{ input : '* a\n* list \n* here' ,
expected : '<ul>\n<li>a</li>\n<li>list </li>\n<li>here</li>\n</ul>' } ,
{ 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' ,
2014-01-03 22:51:00 +01:00
expected : '<p>1. an</p>\n<p>2. ordered </p>\n<p>3. list</p>' } ,
{ input : '\n~~~quote\nquote this for me\n~~~\nthanks\n' ,
2014-01-27 23:21:48 +01:00
expected : '<blockquote>\n<p>quote this for me</p>\n</blockquote>\n<p>thanks</p>' } ,
2014-01-04 01:18:30 +01:00
{ input : 'This is a @**Cordelia Lear** mention' ,
2017-01-20 17:43:45 +01:00
expected : '<p>This is a <span class="user-mention" data-user-id="101">@Cordelia Lear</span> mention</p>' } ,
2016-12-05 17:17:58 +01:00
{ 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>' } ,
2016-11-10 20:20:40 +01:00
{ input : 'This is a #**Denmark** stream link' ,
expected : '<p>This is a <a class="stream" data-stream-id="1" href="http://zulip.zulipdev.com/#narrow/stream/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="http://zulip.zulipdev.com/#narrow/stream/Denmark">#Denmark</a> and <a class="stream" data-stream-id="2" href="http://zulip.zulipdev.com/#narrow/stream/social">#social</a> stream links</p>' } ,
{ input : 'And this is a #**wrong** stream link' ,
expected : '<p>And this is a #**wrong** stream link</p>' } ,
2014-02-20 19:10:55 +01:00
{ input : 'mmm...:burrito:s' ,
2016-12-28 05:07:10 +01:00
expected : '<p>mmm...<img alt=":burrito:" class="emoji" src="/static/generated/emoji/images/emoji/burrito.png" title=":burrito:">s</p>' } ,
2014-01-24 22:52:37 +01:00
{ input : 'This is an :poop: message' ,
2017-02-04 23:27:24 +01:00
expected : '<p>This is an <img alt=":poop:" class="emoji" src="/static/generated/emoji/images/emoji/unicode/1f4a9.png" title=":poop:"> message</p>' } ,
2016-06-24 22:39:44 +02:00
{ input : "\ud83d\udca9" ,
2016-12-28 05:07:10 +01:00
expected : '<p><img alt="\ud83d\udca9" class="emoji" src="/static/generated/emoji/images/emoji/unicode/1f4a9.png" title="\ud83d\udca9"></p>' } ,
2014-01-06 23:42:52 +01:00
{ input : 'This is a realm filter #1234 with text after it' ,
2014-01-24 22:02:42 +01:00
expected : '<p>This is a realm filter <a href="https://trac.zulip.net/ticket/1234" target="_blank" title="https://trac.zulip.net/ticket/1234">#1234</a> with text after it</p>' } ,
{ input : 'This is a realm filter with ZGROUP_123:45 groups' ,
2016-10-19 16:49:53 +02:00
expected : '<p>This is a realm filter with <a href="https://zone_45.zulip.net/ticket/123" target="_blank" title="https://zone_45.zulip.net/ticket/123">ZGROUP_123:45</a> groups</p>' } ,
{ input : 'This is an !avatar(cordelia@zulip.com) of Cordelia Lear' ,
2016-10-20 21:42:55 +02:00
expected : '<p>This is an <img alt="cordelia@zulip.com" class="message_body_gravatar" src="/avatar/cordelia@zulip.com?s=30" title="cordelia@zulip.com"> of Cordelia Lear</p>' } ,
{ input : 'This is a !gravatar(cordelia@zulip.com) of Cordelia Lear' ,
2016-11-03 07:56:28 +01:00
expected : '<p>This is a <img alt="cordelia@zulip.com" class="message_body_gravatar" src="/avatar/cordelia@zulip.com?s=30" title="cordelia@zulip.com"> of Cordelia Lear</p>' } ,
{ input : 'Test *italic*' ,
2016-12-03 23:17:57 +01:00
expected : '<p>Test <em>italic</em></p>' } ,
2013-12-04 17:16:08 +01:00
] ;
test _cases . forEach ( function ( test _case ) {
var input = test _case . input ;
var expected = test _case . expected ;
2017-01-20 15:21:28 +01:00
var message = { raw _content : input } ;
echo . apply _markdown ( message ) ;
var output = message . content ;
2013-12-04 17:16:08 +01:00
assert . equal ( expected , output ) ;
} ) ;
2014-01-24 21:48:56 +01:00
} ( ) ) ;
( function test _subject _links ( ) {
2017-02-24 18:13:15 +01:00
var message = { type : 'stream' , subject : "No links here" } ;
2014-01-24 21:48:56 +01:00
echo . _add _subject _links ( message ) ;
assert . equal ( message . subject _links . length , [ ] ) ;
2017-02-24 18:13:15 +01:00
message = { type : 'stream' , subject : "One #123 link here" } ;
2014-01-24 21:48:56 +01:00
echo . _add _subject _links ( message ) ;
assert . equal ( message . subject _links . length , 1 ) ;
assert . equal ( message . subject _links [ 0 ] , "https://trac.zulip.net/ticket/123" ) ;
2017-02-24 18:13:15 +01:00
message = { type : 'stream' , subject : "Two #123 #456 link here" } ;
2014-01-24 21:48:56 +01:00
echo . _add _subject _links ( message ) ;
assert . equal ( message . subject _links . length , 2 ) ;
assert . equal ( message . subject _links [ 0 ] , "https://trac.zulip.net/ticket/123" ) ;
assert . equal ( message . subject _links [ 1 ] , "https://trac.zulip.net/ticket/456" ) ;
2017-02-24 18:13:15 +01:00
message = { type : 'stream' , subject : "New ZBUG_123 link here" } ;
2014-01-24 21:48:56 +01:00
echo . _add _subject _links ( message ) ;
assert . equal ( message . subject _links . length , 1 ) ;
assert . equal ( message . subject _links [ 0 ] , "https://trac2.zulip.net/ticket/123" ) ;
2017-02-24 18:13:15 +01:00
message = { type : 'stream' , subject : "New ZBUG_123 with #456 link here" } ;
2014-01-24 21:48:56 +01:00
echo . _add _subject _links ( message ) ;
assert . equal ( message . subject _links . length , 2 ) ;
assert ( message . subject _links . indexOf ( "https://trac2.zulip.net/ticket/123" ) !== - 1 ) ;
assert ( message . subject _links . indexOf ( "https://trac.zulip.net/ticket/456" ) !== - 1 ) ;
2014-01-24 22:02:42 +01:00
2017-02-24 18:13:15 +01:00
message = { type : 'stream' , subject : "One ZGROUP_123:45 link here" } ;
2014-01-24 22:02:42 +01:00
echo . _add _subject _links ( message ) ;
assert . equal ( message . subject _links . length , 1 ) ;
assert . equal ( message . subject _links [ 0 ] , "https://zone_45.zulip.net/ticket/123" ) ;
2013-12-04 17:16:08 +01:00
} ( ) ) ;
2014-01-27 17:06:59 +01:00
( function test _message _flags ( ) {
var input = "/me is testing this" ;
2017-01-20 15:21:28 +01:00
var message = { subject : "No links here" , raw _content : input } ;
2017-01-20 16:40:45 +01:00
message . flags = [ 'read' ] ;
2017-01-20 15:21:28 +01:00
echo . apply _markdown ( message ) ;
2014-01-27 17:06:59 +01:00
echo . _add _message _flags ( message ) ;
assert . equal ( message . flags . length , 2 ) ;
assert ( message . flags . indexOf ( 'read' ) !== - 1 ) ;
assert ( message . flags . indexOf ( 'is_me_message' ) !== - 1 ) ;
2014-01-28 22:51:59 +01:00
2017-01-20 16:40:45 +01:00
input = "testing this @**all** @**Cordelia Lear**" ;
2017-01-20 15:21:28 +01:00
message = { subject : "No links here" , raw _content : input } ;
echo . apply _markdown ( message ) ;
2014-01-28 22:51:59 +01:00
echo . _add _message _flags ( message ) ;
2017-01-20 16:40:45 +01:00
assert . equal ( message . flags . length , 1 ) ;
2014-01-28 22:51:59 +01:00
assert ( message . flags . indexOf ( 'mentioned' ) !== - 1 ) ;
2016-11-04 08:13:10 +01:00
input = "test @all" ;
2017-01-20 15:21:28 +01:00
message = { subject : "No links here" , raw _content : input } ;
echo . apply _markdown ( message ) ;
2016-11-04 08:13:10 +01:00
echo . _add _message _flags ( message ) ;
2017-01-20 16:40:45 +01:00
assert . equal ( message . flags . length , 1 ) ;
2016-11-04 08:13:10 +01:00
assert ( message . flags . indexOf ( 'mentioned' ) !== - 1 ) ;
input = "test @any" ;
2017-01-20 15:21:28 +01:00
message = { subject : "No links here" , raw _content : input } ;
echo . apply _markdown ( message ) ;
2016-11-04 08:13:10 +01:00
echo . _add _message _flags ( message ) ;
2017-01-20 16:40:45 +01:00
assert . equal ( message . flags . length , 0 ) ;
2016-11-04 08:13:10 +01:00
assert ( message . flags . indexOf ( 'mentioned' ) === - 1 ) ;
2014-01-27 17:06:59 +01:00
} ( ) ) ;