2013-06-20 13:58:54 -04:00
/*global md5:true */
2013-06-17 11:38:30 -04:00
module ( "Discourse.BBCode" ) ;
2013-06-18 11:35:51 -04:00
var format = function ( input , expected , text ) {
2013-08-12 14:05:58 -04:00
var cooked = Discourse . Markdown . cook ( input , { lookupAvatar : false } ) ;
equal ( cooked , "<p>" + expected + "</p>" , text ) ;
2013-06-21 14:06:20 -04:00
} ;
2013-06-17 11:38:30 -04:00
2013-06-18 11:35:51 -04:00
test ( 'basic bbcode' , function ( ) {
2013-08-08 18:14:12 -04:00
format ( "[b]strong[/b]" , "<span class=\"bbcode-b\">strong</span>" , "bolds text" ) ;
format ( "[i]emphasis[/i]" , "<span class=\"bbcode-i\">emphasis</span>" , "italics text" ) ;
format ( "[u]underlined[/u]" , "<span class=\"bbcode-u\">underlined</span>" , "underlines text" ) ;
format ( "[s]strikethrough[/s]" , "<span class=\"bbcode-s\">strikethrough</span>" , "strikes-through text" ) ;
format ( "[code]\nx++\n[/code]" , "<pre>\nx++<br/>\n</pre>" , "makes code into pre" ) ;
format ( "[code]\nx++\ny++\nz++\n[/code]" , "<pre>\nx++<br/>\ny++<br/>\nz++<br/>\n</pre>" , "makes code into pre" ) ;
2013-06-17 11:38:30 -04:00
format ( "[spoiler]it's a sled[/spoiler]" , "<span class=\"spoiler\">it's a sled</span>" , "supports spoiler tags" ) ;
2013-08-08 18:14:12 -04:00
format ( "[img]http://eviltrout.com/eviltrout.png[/img]" , "<img src=\"http://eviltrout.com/eviltrout.png\"/>" , "links images" ) ;
2013-06-17 11:38:30 -04:00
format ( "[url]http://bettercallsaul.com[/url]" , "<a href=\"http://bettercallsaul.com\">http://bettercallsaul.com</a>" , "supports [url] without a title" ) ;
format ( "[email]eviltrout@mailinator.com[/email]" , "<a href=\"mailto:eviltrout@mailinator.com\">eviltrout@mailinator.com</a>" , "supports [email] without a title" ) ;
2013-06-18 11:35:51 -04:00
} ) ;
2013-06-17 11:38:30 -04:00
2013-06-18 11:35:51 -04:00
test ( 'lists' , function ( ) {
2013-06-17 11:38:30 -04:00
format ( "[ul][li]option one[/li][/ul]" , "<ul><li>option one</li></ul>" , "creates an ul" ) ;
format ( "[ol][li]option one[/li][/ol]" , "<ol><li>option one</li></ol>" , "creates an ol" ) ;
2013-06-18 11:35:51 -04:00
} ) ;
2013-06-17 11:38:30 -04:00
2013-06-18 11:35:51 -04:00
test ( 'color' , function ( ) {
2013-06-17 11:38:30 -04:00
format ( "[color=#00f]blue[/color]" , "<span style=\"color: #00f\">blue</span>" , "supports [color=] with a short hex value" ) ;
format ( "[color=#ffff00]yellow[/color]" , "<span style=\"color: #ffff00\">yellow</span>" , "supports [color=] with a long hex value" ) ;
format ( "[color=red]red[/color]" , "<span style=\"color: red\">red</span>" , "supports [color=] with an html color" ) ;
format ( "[color=javascript:alert('wat')]noop[/color]" , "noop" , "it performs a noop on invalid input" ) ;
2013-06-18 11:35:51 -04:00
} ) ;
2013-06-17 11:38:30 -04:00
2013-06-18 11:35:51 -04:00
test ( 'tags with arguments' , function ( ) {
2013-08-08 18:14:12 -04:00
format ( "[size=35]BIG [b]whoop[/b][/size]" , "<span class=\"bbcode-size-35\">BIG <span class=\"bbcode-b\">whoop</span></span>" , "supports [size=]" ) ;
2013-06-17 11:38:30 -04:00
format ( "[url=http://bettercallsaul.com]better call![/url]" , "<a href=\"http://bettercallsaul.com\">better call!</a>" , "supports [url] with a title" ) ;
format ( "[email=eviltrout@mailinator.com]evil trout[/email]" , "<a href=\"mailto:eviltrout@mailinator.com\">evil trout</a>" , "supports [email] with a title" ) ;
2013-08-08 18:14:12 -04:00
format ( "[u][i]abc[/i][/u]" , "<span class=\"bbcode-u\"><span class=\"bbcode-i\">abc</span></span>" , "can nest tags" ) ;
format ( "[b]first[/b] [b]second[/b]" , "<span class=\"bbcode-b\">first</span> <span class=\"bbcode-b\">second</span>" , "can bold two things on the same line" ) ;
2013-06-18 13:44:20 -04:00
} ) ;
test ( "quotes" , function ( ) {
var post = Discourse . Post . create ( {
cooked : "<p><b>lorem</b> ipsum</p>" ,
username : "eviltrout" ,
post _number : 1 ,
topic _id : 2
} ) ;
var formatQuote = function ( val , expected , text ) {
2013-08-08 18:14:12 -04:00
equal ( Discourse . Quote . build ( post , val ) , expected , text ) ;
2013-06-21 14:06:20 -04:00
} ;
2013-06-18 13:44:20 -04:00
formatQuote ( undefined , "" , "empty string for undefined content" ) ;
formatQuote ( null , "" , "empty string for null content" ) ;
formatQuote ( "" , "" , "empty string for empty string content" ) ;
formatQuote ( "lorem" , "[quote=\"eviltrout, post:1, topic:2\"]\nlorem\n[/quote]\n\n" , "correctly formats quotes" ) ;
2013-08-08 18:14:12 -04:00
2013-06-18 13:44:20 -04:00
formatQuote ( " lorem \t " ,
"[quote=\"eviltrout, post:1, topic:2\"]\nlorem\n[/quote]\n\n" ,
"trims white spaces before & after the quoted contents" ) ;
formatQuote ( "lorem ipsum" ,
"[quote=\"eviltrout, post:1, topic:2, full:true\"]\nlorem ipsum\n[/quote]\n\n" ,
"marks quotes as full when the quote is the full message" ) ;
formatQuote ( "**lorem** ipsum" ,
"[quote=\"eviltrout, post:1, topic:2, full:true\"]\n**lorem** ipsum\n[/quote]\n\n" ,
"keeps BBCode formatting" ) ;
} ) ;
test ( "quote formatting" , function ( ) {
2013-08-08 18:14:12 -04:00
format ( "[quote=\"EvilTrout, post:123, topic:456, full:true\"][sam][/quote]" ,
"<aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">" +
"<div class=\"quote-controls\"></div>EvilTrout said:</div><blockquote>[sam]</blockquote></aside>" ,
"it allows quotes with [] inside" ) ;
2013-06-18 13:44:20 -04:00
format ( "[quote=\"eviltrout, post:1, topic:1\"]abc[/quote]" ,
2013-08-08 18:14:12 -04:00
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout said:" +
"</div><blockquote>abc</blockquote></aside>" ,
2013-06-18 13:44:20 -04:00
"renders quotes properly" ) ;
2013-08-08 18:14:12 -04:00
format ( "[quote=\"eviltrout, post:1, topic:1\"]abc[/quote]\nhello" ,
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout said:" +
2013-08-26 15:21:23 -04:00
"</div><blockquote>abc</blockquote></aside></p>\n\n<p>hello" ,
2013-08-08 18:14:12 -04:00
"handles new lines properly" ) ;
2013-06-18 13:44:20 -04:00
} ) ;