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) {
2014-07-14 11:24:25 -04:00
var cooked = Discourse.Markdown.cook(input, {lookupAvatar: false, sanitize: true});
2014-01-21 12:04:26 -05:00
equal(cooked, "<p>" + expected + "</p>", text);
2013-06-21 14:06:20 -04:00
};
2013-06-17 11:38:30 -04:00
2014-06-30 18:10:46 -04:00
var formatQ = function(input, expected, text) {
2014-07-14 11:24:25 -04:00
var cooked = Discourse.Markdown.cook(input, {lookupAvatar: false, sanitize: true});
2014-06-30 18:10:46 -04:00
equal(cooked, expected, text);
};
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");
2014-07-14 11:24:25 -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-08-27 12:52:00 -04:00
format("[b]evil [i]trout[/i][/b]",
"<span class=\"bbcode-b\">evil <span class=\"bbcode-i\">trout</span></span>",
"allows embedding of tags");
2014-03-03 11:59:57 -05:00
format("[EMAIL]eviltrout@mailinator.com[/EMAIL]", "<a href=\"mailto:eviltrout@mailinator.com\">eviltrout@mailinator.com</a>", "supports upper case bbcode");
2014-07-24 11:34:13 -04:00
format("[b]strong [b]stronger[/b][/b]", "<span class=\"bbcode-b\">strong <span class=\"bbcode-b\">stronger</span></span>", "accepts nested bbcode tags");
2013-06-18 11:35:51 -04:00
});
2013-06-17 11:38:30 -04:00
2013-09-11 15:52:37 -04:00
test('invalid bbcode', function() {
var cooked = Discourse.Markdown.cook("[code]I am not closed\n\nThis text exists.", {lookupAvatar: false});
equal(cooked, "<p>[code]I am not closed</p>\n\n<p>This text exists.</p>", "does not raise an error with an open bbcode tag.");
});
2013-10-21 12:12:58 -04:00
test('code', function() {
2014-06-23 13:19:27 -04:00
format("[code]\nx++\n[/code]", "<pre><code class=\"lang-auto\">x++</code></pre>", "makes code into pre");
format("[code]\nx++\ny++\nz++\n[/code]", "<pre><code class=\"lang-auto\">x++\ny++\nz++</code></pre>", "makes code into pre");
format("[code]abc\n#def\n[/code]", '<pre><code class=\"lang-auto\">abc\n#def</code></pre>', 'it handles headings in a [code] block');
2013-10-21 12:12:58 -04:00
});
2014-01-12 13:38:46 -05:00
test('spoiler', function() {
2014-01-14 18:53:06 -05:00
format("[spoiler]it's a sled[/spoiler]", "<span class=\"spoiler\">it's a sled</span>", "supports spoiler tags on text");
2014-07-14 11:24:25 -04:00
format("[spoiler]<img src='http://eviltrout.com/eviltrout.png' width='50' height='50'>[/spoiler]",
"<div class=\"spoiler\"><img src=\"http://eviltrout.com/eviltrout.png\" width=\"50\" height=\"50\"></div>", "supports spoiler tags on images");
2014-01-12 13:38:46 -05: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");
2014-07-24 13:39:16 -04:00
format("[ul]\n[li]option one[/li]\n[li]option two[/li]\n[/ul]", "<ul><li>option one</li><li>option two</li></ul>", "suppresses empty lines in lists");
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-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-10-22 11:08:13 -04:00
});
test("size tags", function() {
format("[size=35]BIG [b]whoop[/b][/size]",
"<span class=\"bbcode-size-35\">BIG <span class=\"bbcode-b\">whoop</span></span>",
"supports [size=]");
format("[size=asdf]regular[/size]",
"<span class=\"bbcode-size-1\">regular</span>",
"it only supports numbers in bbcode");
2014-08-22 14:32:06 -04:00
format("[size=35]NEWLINE\n\ntest[/size]",
"<span class=\"bbcode-size-35\"><p>NEWLINE</p><p>test</p></span>",
2014-08-22 12:14:10 -04:00
"works with newlines");
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");
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");
2013-08-29 14:15:05 -04:00
formatQuote("this is <not> a bug",
"[quote=\"eviltrout, post:1, topic:2\"]\nthis is <not> a bug\n[/quote]\n\n",
"it escapes the contents of the quote");
2013-10-21 11:11:54 -04:00
format("[quote]test[/quote]",
"<aside class=\"quote\"><blockquote><p>test</p></blockquote></aside>",
"it supports quotes without params");
2013-11-07 16:06:50 -05:00
format("[quote]\n*test*\n[/quote]",
"<aside class=\"quote\"><blockquote><p><em>test</em></p></blockquote></aside>",
"it doesn't insert a new line for italics");
2014-05-27 22:46:31 -04:00
format("[quote=,script='a'><script>alert('test');//':a][/quote]",
2014-07-17 02:10:55 -04:00
"<aside class=\"quote\"><blockquote></blockquote></aside>",
2014-04-21 09:20:39 -04:00
"It will not create a script tag within an attribute");
2013-06-18 13:44:20 -04:00
});
test("quote formatting", function() {
2014-06-30 18:10:46 -04:00
formatQ("[quote=\"EvilTrout, post:123, topic:456, full:true\"][sam][/quote]",
2013-08-08 18:14:12 -04:00
"<aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">" +
2014-09-18 23:39:18 -04:00
"<div class=\"quote-controls\"></div>EvilTrout:</div><blockquote><p>[sam]</p></blockquote></aside>",
2013-08-08 18:14:12 -04:00
"it allows quotes with [] inside");
2014-06-30 18:10:46 -04:00
formatQ("[quote=\"eviltrout, post:1, topic:1\"]abc[/quote]",
2014-09-18 23:39:18 -04:00
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout:" +
2013-09-27 15:08:30 -04:00
"</div><blockquote><p>abc</p></blockquote></aside>",
2013-06-18 13:44:20 -04:00
"renders quotes properly");
2014-06-30 18:10:46 -04:00
formatQ("[quote=\"eviltrout, post:1, topic:1\"]abc[/quote]\nhello",
2014-09-18 23:39:18 -04:00
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout:" +
2014-06-30 18:10:46 -04:00
"</div><blockquote><p>abc</p></blockquote></aside>\n\n<p>hello</p>",
2013-08-08 18:14:12 -04:00
"handles new lines properly");
2013-06-18 13:44:20 -04:00
2014-07-27 10:07:47 -04:00
formatQ("[quote=\"Alice, post:1, topic:1\"]\n[quote=\"Bob, post:2, topic:1\"]\n[/quote]\n[/quote]",
2014-09-18 23:39:18 -04:00
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>Alice:" +
"</div><blockquote><aside class=\"quote\" data-post=\"2\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>Bob:" +
2014-07-27 10:07:47 -04:00
"</div><blockquote></blockquote></aside></blockquote></aside>",
"quotes can be nested");
formatQ("[quote=\"Alice, post:1, topic:1\"]\n[quote=\"Bob, post:2, topic:1\"]\n[/quote]",
2014-09-18 23:39:18 -04:00
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>Alice:" +
2014-07-27 10:07:47 -04:00
"</div><blockquote><p>[quote=\"Bob, post:2, topic:1\"]</p></blockquote></aside>",
"handles mismatched nested quote tags");
2013-06-18 13:44:20 -04:00
});
2013-10-16 10:26:35 -04:00
test("quotes with trailing formatting", function() {
var cooked = Discourse.Markdown.cook("[quote=\"EvilTrout, post:123, topic:456, full:true\"]\nhello\n[/quote]\n*Test*", {lookupAvatar: false});
equal(cooked,
2014-06-30 18:10:46 -04:00
"<aside class=\"quote\" data-post=\"123\" data-topic=\"456\" data-full=\"true\"><div class=\"title\">" +
2014-09-18 23:39:18 -04:00
"<div class=\"quote-controls\"></div>EvilTrout:</div><blockquote><p>hello</p></blockquote></aside>\n\n<p><em>Test</em></p>",
2013-10-16 10:26:35 -04:00
"it allows trailing formatting");
});
2013-06-18 13:44:20 -04:00