1
0
mirror of https://github.com/flarum/core.git synced 2025-08-05 16:07:34 +02:00

Fix issue with Twemoji frontend rendering (#40)

This commit is contained in:
David Wheatley
2021-08-05 09:41:41 +02:00
committed by GitHub
parent 6f925c1229
commit 05a2ded71a

View File

@@ -14,12 +14,39 @@ const options = {
}),
};
/**
* Parses an HTML string into a `<body>` node containing the HTML content.
*
* Vanilla JS implementation of jQuery's `$.parseHTML()`,
* sourced from http://youmightnotneedjquery.com/
*/
function parseHTML(str) {
const tmp = document.implementation.createHTMLDocument();
tmp.body.innerHTML = str;
return tmp.body;
}
export default function renderEmoji() {
override(Post.prototype, 'contentHtml', function (original) {
const contentHtml = original();
if (this.oldContentHtml !== contentHtml) {
this.emojifiedContentHtml = twemoji.parse(contentHtml, options);
// We need to parse the HTML string into a DOM node, then give it to Twemoji.
//
// This prevents some issues with the default find-replace that would be performed
// on a string passed to `Twemoji.parse()`.
//
// The parse function can only handle a single DOM node provided, so we need to
// wrap it in a node. In our `parseHTML` implementation, we wrap it in a `<body>`
// element. This gets stripped below.
//
// See https://github.com/flarum/core/issues/2958
const emojifiedDom = twemoji.parse(parseHTML(contentHtml), options);
// Steal the HTML string inside the emojified DOM `<body>` tag.
this.emojifiedContentHtml = emojifiedDom.innerHTML;
this.oldContentHtml = contentHtml;
}