FIX: reworked handling URL bbcode tags

This commit is contained in:
Jens Maier 2014-09-24 19:52:19 +02:00
parent b1567488b7
commit dcd7a149e2
2 changed files with 29 additions and 12 deletions

View File

@ -144,17 +144,32 @@ Discourse.BBCode.rawBBCode('spoiler', function(contents) {
} }
}); });
Discourse.BBCode.register('url', function(contents, params) { Discourse.BBCode.replaceBBCode('url', function(contents) {
if (!params) { if (!Array.isArray(contents)) { return; }
if (contents && contents.length) { if (contents.length === 1 && contents[0][0] === 'a') {
var tag = contents[0]; // single-line bbcode links shouldn't be oneboxed, so we mark this as a bbcode link.
if (tag && tag.length === 3 && tag[1].href) { if (typeof contents[0][1] !== 'object') { contents[0].splice(1, 0, {}); }
return ['a', {'href': tag[1].href, 'data-bbcode': true}, tag[1].href]; contents[0][1]['data-bbcode'] = true;
}
return ['concat'].concat(contents);
});
Discourse.BBCode.replaceBBCodeParamsRaw('url', function(param, contents) {
var url = param.replace(/(^")|("$)/g, '');
return ['a', {'href': url}].concat(this.processInline(contents));
});
Discourse.Dialect.on('parseNode', function(event) {
if (!Array.isArray(event.node)) { return; }
var result = [ event.node[0] ];
var nodes = event.node.slice(1);
var i, j;
for (i = 0; i < nodes.length; i++) {
if (Array.isArray(nodes[i]) && nodes[i][0] === 'concat') {
for (j = 1; j < nodes[i].length; j++) { result.push(nodes[i][j]); }
} else {
result.push(nodes[i]);
} }
} }
return; for (i = 0; i < result.length; i++) { event.node[i] = result[i]; }
}
return ['a', {'href': params, 'data-bbcode': true}].concat(contents);
}); });
Discourse.BBCode.replaceBBCodeParamsRaw("email", function(param, contents) { Discourse.BBCode.replaceBBCodeParamsRaw("email", function(param, contents) {

View File

@ -16,7 +16,6 @@ test('basic bbcode', function() {
format("[u]underlined[/u]", "<span class=\"bbcode-u\">underlined</span>", "underlines 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("[s]strikethrough[/s]", "<span class=\"bbcode-s\">strikethrough</span>", "strikes-through text");
format("[img]http://eviltrout.com/eviltrout.png[/img]", "<img src=\"http://eviltrout.com/eviltrout.png\">", "links images"); format("[img]http://eviltrout.com/eviltrout.png[/img]", "<img src=\"http://eviltrout.com/eviltrout.png\">", "links images");
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"); format("[email]eviltrout@mailinator.com[/email]", "<a href=\"mailto:eviltrout@mailinator.com\">eviltrout@mailinator.com</a>", "supports [email] without a title");
format("[b]evil [i]trout[/i][/b]", format("[b]evil [i]trout[/i][/b]",
"<span class=\"bbcode-b\">evil <span class=\"bbcode-i\">trout</span></span>", "<span class=\"bbcode-b\">evil <span class=\"bbcode-i\">trout</span></span>",
@ -25,7 +24,10 @@ test('basic bbcode', function() {
format("[b]strong [b]stronger[/b][/b]", "<span class=\"bbcode-b\">strong <span class=\"bbcode-b\">stronger</span></span>", "accepts nested bbcode tags"); format("[b]strong [b]stronger[/b][/b]", "<span class=\"bbcode-b\">strong <span class=\"bbcode-b\">stronger</span></span>", "accepts nested bbcode tags");
}); });
test('url with images', function() { 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]", "<a href=\"http://bettercallsaul.com\">http://bettercallsaul.com</a>", "supports [url] without parameter");
format("[url=http://example.com]example[/url]", "<a href=\"http://example.com\">example</a>", "supports [url] with given href");
format("[url=http://www.example.com][img]http://example.com/logo.png[/img][/url]", format("[url=http://www.example.com][img]http://example.com/logo.png[/img][/url]",
"<a href=\"http://www.example.com\"><img src=\"http://example.com/logo.png\"></a>", "<a href=\"http://www.example.com\"><img src=\"http://example.com/logo.png\"></a>",
"supports [url] with an embedded [img]"); "supports [url] with an embedded [img]");