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) {
if (!params) {
if (contents && contents.length) {
var tag = contents[0];
if (tag && tag.length === 3 && tag[1].href) {
return ['a', {'href': tag[1].href, 'data-bbcode': true}, tag[1].href];
}
}
return;
Discourse.BBCode.replaceBBCode('url', function(contents) {
if (!Array.isArray(contents)) { return; }
if (contents.length === 1 && contents[0][0] === 'a') {
// single-line bbcode links shouldn't be oneboxed, so we mark this as a bbcode link.
if (typeof contents[0][1] !== 'object') { contents[0].splice(1, 0, {}); }
contents[0][1]['data-bbcode'] = true;
}
return ['a', {'href': params, 'data-bbcode': true}].concat(contents);
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]);
}
}
for (i = 0; i < result.length; i++) { event.node[i] = result[i]; }
});
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("[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("[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("[b]evil [i]trout[/i][/b]",
"<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");
});
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]",
"<a href=\"http://www.example.com\"><img src=\"http://example.com/logo.png\"></a>",
"supports [url] with an embedded [img]");