FIX: Quotes with new lines were broken

This commit is contained in:
Robin Ward 2013-08-21 22:36:02 -04:00
parent d7596840e5
commit a3757016d9
4 changed files with 86 additions and 47 deletions

View file

@ -107,46 +107,80 @@ Discourse.Dialect.on("register", function(event) {
@return {Array} the JsonML containing the markup or undefined if nothing changed.
@namespace Discourse.Dialect
**/
dialect.inline["[quote="] = function bbcodeQuote(text, orig_match) {
var bbcodePattern = new RegExp("\\[quote=?([^\\[\\]]+)?\\]([\\s\\S]*?)\\[\\/quote\\]", "igm"),
m = bbcodePattern.exec(text);
dialect.block['quote'] = function bbcodeQuote(block, next) {
var m = new RegExp("\\[quote=?([^\\[\\]]+)?\\]([\\s\\S]*)", "igm").exec(block);
if (m) {
var paramsString = m[1].replace(/\"/g, ''),
params = {'class': 'quote'},
paramsSplit = paramsString.split(/\, */),
username = paramsSplit[0],
opts = dialect.options,
startPos = block.indexOf(m[0]),
leading,
quoteContents = [],
result = [];
if (!m) { return; }
var paramsString = m[1].replace(/\"/g, ''),
params = {'class': 'quote'},
paramsSplit = paramsString.split(/\, */),
username = paramsSplit[0],
opts = dialect.options;
if (startPos > 0) {
leading = block.slice(0, startPos);
paramsSplit.forEach(function(p,i) {
if (i > 0) {
var assignment = p.split(':');
if (assignment[0] && assignment[1]) {
params['data-' + assignment[0]] = assignment[1].trim();
var para = ['p'];
this.processInline(leading).forEach(function (l) {
para.push(l);
});
result.push(para);
}
paramsSplit.forEach(function(p,i) {
if (i > 0) {
var assignment = p.split(':');
if (assignment[0] && assignment[1]) {
params['data-' + assignment[0]] = assignment[1].trim();
}
}
});
var avatarImg;
if (opts.lookupAvatarByPostNumber) {
// client-side, we can retrieve the avatar from the post
var postNumber = parseInt(params['data-post'], 10);
avatarImg = opts.lookupAvatarByPostNumber(postNumber);
} else if (opts.lookupAvatar) {
// server-side, we need to lookup the avatar from the username
avatarImg = opts.lookupAvatar(username);
}
if (m[2]) { next.unshift(MD.mk_block(m[2])); }
while (next.length > 0) {
var b = next.shift(),
n = b.match(/([\s\S]*)\[\/quote\]([\s\S]*)/m);
if (n) {
if (n[2]) {
next.unshift(MD.mk_block(n[2]));
}
quoteContents.push(n[1]);
break;
} else {
quoteContents.push(b);
}
}
});
var avatarImg;
if (opts.lookupAvatarByPostNumber) {
// client-side, we can retrieve the avatar from the post
var postNumber = parseInt(params['data-post'], 10);
avatarImg = opts.lookupAvatarByPostNumber(postNumber);
} else if (opts.lookupAvatar) {
// server-side, we need to lookup the avatar from the username
avatarImg = opts.lookupAvatar(username);
var contents = this.processInline(quoteContents.join(" \n \n"));
contents.unshift('blockquote');
result.push(['p', ['aside', params,
['div', {'class': 'title'},
['div', {'class': 'quote-controls'}],
avatarImg ? avatarImg + "\n" : "",
I18n.t('user.said',{username: username})
],
contents
]]);
return result;
}
var quote = ['aside', params,
['div', {'class': 'title'},
['div', {'class': 'quote-controls'}],
avatarImg ? avatarImg + "\n" : "",
I18n.t('user.said',{username: username})
],
['blockquote'].concat(this.processInline(m[2]))
];
return [m[0].length, quote];
};
});

View file

@ -192,9 +192,12 @@ Discourse.ComposerView = Discourse.View.extend({
this.editor = editor = Discourse.Markdown.createEditor({
lookupAvatarByPostNumber: function(postNumber) {
var quotedPost = composerView.get('controller.controllers.topic.postStream.posts').findProperty("post_number", postNumber);
if (quotedPost) {
return Discourse.Utilities.tinyAvatar(quotedPost.get("avatar_template"));
var posts = composerView.get('controller.controllers.topic.postStream.posts');
if (posts) {
var quotedPost = posts.findProperty("post_number", postNumber);
if (quotedPost) {
return Discourse.Utilities.tinyAvatar(quotedPost.get("avatar_template"));
}
}
}
});

View file

@ -88,15 +88,9 @@ test("quote formatting", function() {
format("[quote=\"eviltrout, post:1, topic:1\"]abc[/quote]\nhello",
"<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout said:" +
"</div><blockquote>abc</blockquote></aside><br/>\nhello",
"</div><blockquote>abc</blockquote></aside></p>\n\n<p>\nhello",
"handles new lines properly");
format("before[quote=\"eviltrout, post:1, topic:1\"]first[/quote]middle[quote=\"eviltrout, post:2, topic:2\"]second[/quote]after",
"before<aside class=\"quote\" data-post=\"1\" data-topic=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout said:</div><blockquote>" +
"first</blockquote></aside><br/>middle<aside class=\"quote\" data-post=\"2\" data-topic=\"2\"><div class=\"title\"><div class=\"quote-controls\"></div>" +
"eviltrout said:</div><blockquote>second</blockquote></aside><br/>after",
"can handle more than one quote");
});

View file

@ -85,15 +85,23 @@ test("Links", function() {
});
test("Quotes", function() {
cookedOptions("[quote=\"eviltrout, post: 1\"]\na quote\n\nsecond line\n[/quote]",
{ topicId: 2 },
"<p><aside class=\"quote\" data-post=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>eviltrout said:</div><blockquote>\n" +
"a quote<br/><br/>second line<br/></blockquote></aside></p>",
"works with multiple lines");
cookedOptions("1[quote=\"bob, post:1\"]my quote[/quote]2",
{ topicId: 2, lookupAvatar: function(name) { return "" + name; } },
"<p>1<aside class=\"quote\" data-post=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>bob\n" +
"bob said:</div><blockquote>my quote</blockquote></aside><br/>2</p>",
"<p>1</p>\n\n<p><aside class=\"quote\" data-post=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>bob\n" +
"bob said:</div><blockquote>my quote</blockquote></aside></p>\n\n<p>2</p>",
"handles quotes properly");
cookedOptions("1[quote=\"bob, post:1\"]my quote[/quote]2",
{ topicId: 2, lookupAvatar: function(name) { } },
"<p>1<aside class=\"quote\" data-post=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>bob said:</div><blockquote>my quote</blockquote></aside><br/>2</p>",
"<p>1</p>\n\n<p><aside class=\"quote\" data-post=\"1\"><div class=\"title\"><div class=\"quote-controls\"></div>bob said:" +
"</div><blockquote>my quote</blockquote></aside></p>\n\n<p>2</p>",
"includes no avatar if none is found");
});