FIX: Allow BBCode images within BBCode links

This commit is contained in:
Robin Ward 2014-09-22 14:42:07 -04:00
parent 384d8f25e4
commit 914217f78a
2 changed files with 16 additions and 3 deletions

View File

@ -136,7 +136,6 @@ Discourse.BBCode.replaceBBCode('li', function(contents) { return ['li'].concat(D
Discourse.BBCode.rawBBCode('img', function(contents) { return ['img', {href: contents}]; });
Discourse.BBCode.rawBBCode('email', function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; });
Discourse.BBCode.rawBBCode('url', function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; });
Discourse.BBCode.rawBBCode('spoiler', function(contents) {
if (/<img/i.test(contents)) {
return ['div', { 'class': 'spoiler' }, contents];
@ -145,8 +144,17 @@ Discourse.BBCode.rawBBCode('spoiler', function(contents) {
}
});
Discourse.BBCode.replaceBBCodeParamsRaw("url", function(param, contents) {
return ['a', {href: param, 'data-bbcode': true}].concat(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;
}
return ['a', {'href': params, 'data-bbcode': true}].concat(contents);
});
Discourse.BBCode.replaceBBCodeParamsRaw("email", function(param, contents) {

View File

@ -25,6 +25,11 @@ 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() {
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]");
});
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.");