diff --git a/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb b/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb
index e662df804e6..8a8fceb2528 100644
--- a/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb
+++ b/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb
@@ -77,7 +77,8 @@ function imageFor(code) {
code = code.toLowerCase();
var url = urlFor(code);
if (url) {
- return ['img', { href: url, title: ':' + code + ':', 'class': 'emoji', alt: code }];
+ var code = ':' + code + ':';
+ return ['img', { href: url, title: code, 'class': 'emoji', alt: code }];
}
}
diff --git a/app/assets/javascripts/discourse/lib/utilities.js b/app/assets/javascripts/discourse/lib/utilities.js
index ac55bdaf498..05ed05cafc4 100644
--- a/app/assets/javascripts/discourse/lib/utilities.js
+++ b/app/assets/javascripts/discourse/lib/utilities.js
@@ -97,7 +97,10 @@ Discourse.Utilities = {
// Strip out any .click elements from the HTML before converting it to text
var div = document.createElement('div');
div.innerHTML = html;
- $('.clicks', $(div)).remove();
+ var $div = $(div);
+ // Find all emojis and replace with its title attribute.
+ $div.find('img.emoji').replaceWith(function() { return this.title });
+ $('.clicks', $div).remove();
var text = div.textContent || div.innerText || "";
return String(text).trim();
diff --git a/test/javascripts/lib/bbcode-test.js.es6 b/test/javascripts/lib/bbcode-test.js.es6
index 575ad7e7558..385569c9c05 100644
--- a/test/javascripts/lib/bbcode-test.js.es6
+++ b/test/javascripts/lib/bbcode-test.js.es6
@@ -52,7 +52,7 @@ 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] 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");
});