import Quote from 'discourse/lib/quote';
module("Discourse.BBCode");
var format = function(input, expected, text) {
var cooked = Discourse.Markdown.cook(input, {lookupAvatar: false, sanitize: true});
equal(cooked, "
" + expected + "
", text);
};
var formatQ = function(input, expected, text) {
var cooked = Discourse.Markdown.cook(input, {lookupAvatar: false, sanitize: true});
equal(cooked, expected, text);
};
test('basic bbcode', function() {
format("[b]strong[/b]", "strong", "bolds text");
format("[i]emphasis[/i]", "emphasis", "italics text");
format("[u]underlined[/u]", "underlined", "underlines text");
format("[s]strikethrough[/s]", "strikethrough", "strikes-through text");
format("[img]http://eviltrout.com/eviltrout.png[/img]", "
", "links images");
format("[email]eviltrout@mailinator.com[/email]", "eviltrout@mailinator.com", "supports [email] without a title");
format("[b]evil [i]trout[/i][/b]",
"evil trout",
"allows embedding of tags");
format("[EMAIL]eviltrout@mailinator.com[/EMAIL]", "eviltrout@mailinator.com", "supports upper case bbcode");
format("[b]strong [b]stronger[/b][/b]", "strong stronger", "accepts nested bbcode tags");
});
test('urls', function() {
format("[url]not a url[/url]", "not a url", "supports [url] that isn't a url");
format("[url]http://bettercallsaul.com[/url]", "http://bettercallsaul.com", "supports [url] without parameter");
format("[url=http://example.com]example[/url]", "example", "supports [url] with given href");
format("[url=http://www.example.com][img]http://example.com/logo.png[/img][/url]",
"
",
"supports [url] with an embedded [img]");
});
test('invalid bbcode', function() {
var cooked = Discourse.Markdown.cook("[code]I am not closed\n\nThis text exists.", {lookupAvatar: false});
equal(cooked, "[code]I am not closed
\n\nThis text exists.
", "does not raise an error with an open bbcode tag.");
});
test('code', function() {
format("[code]\nx++\n[/code]", "x++
", "makes code into pre");
format("[code]\nx++\ny++\nz++\n[/code]", "x++\ny++\nz++
", "makes code into pre");
format("[code]abc\n#def\n[/code]", 'abc\n#def
', 'it handles headings in a [code] block');
format("[code]\n s[/code]",
" s
",
"it doesn't trim leading whitespace");
});
test('spoiler', function() {
format("[spoiler]it's a sled[/spoiler]", "it's a sled", "supports spoiler tags on text");
format("[spoiler]
[/spoiler]",
"
", "supports spoiler tags on images");
format("[spoiler] This is the **bold** :smiley: [/spoiler]", " This is the bold
", "supports spoiler tags on emojis");
format("[spoiler] Why not both
?[/spoiler]", " Why not both
?", "supports images and text");
format("In a p tag a spoiler [spoiler]
[/spoiler] can work.", "In a p tag a spoiler
can work.", "supports images and text in a p tag");
});
test('lists', function() {
format("[ul][li]option one[/li][/ul]", "", "creates an ul");
format("[ol][li]option one[/li][/ol]", "- option one
", "creates an ol");
format("[ul]\n[li]option one[/li]\n[li]option two[/li]\n[/ul]", "", "suppresses empty lines in lists");
});
test('tags with arguments', function() {
format("[url=http://bettercallsaul.com]better call![/url]", "better call!", "supports [url] with a title");
format("[email=eviltrout@mailinator.com]evil trout[/email]", "evil trout", "supports [email] with a title");
format("[u][i]abc[/i][/u]", "abc", "can nest tags");
format("[b]first[/b] [b]second[/b]", "first second", "can bold two things on the same line");
});
test("size tags", function() {
format("[size=35]BIG [b]whoop[/b][/size]",
"BIG whoop",
"supports [size=]");
format("[size=asdf]regular[/size]",
"regular",
"it only supports numbers in bbcode");
format("[size=35]NEWLINE\n\ntest[/size]",
"NEWLINE
test
",
"works with newlines");
format("[size=35][quote=\"user\"]quote[/quote][/size]",
"",
"works with nested complex blocks");
});
test("quotes", function() {
var post = Discourse.Post.create({
cooked: "lorem ipsum
",
username: "eviltrout",
post_number: 1,
topic_id: 2
});
var formatQuote = function(val, expected, text) {
equal(Quote.build(post, val), expected, text);
};
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");
formatQuote("this is a bug",
"[quote=\"eviltrout, post:1, topic:2\"]\nthis is <not> a bug\n[/quote]\n\n",
"it escapes the contents of the quote");
format("[quote]test[/quote]",
"",
"it supports quotes without params");
format("[quote]\n*test*\n[/quote]",
"",
"it doesn't insert a new line for italics");
format("[quote=,script='a'>