FIX: Watched words replacement emoji render (#19171)

This commit is contained in:
Keegan George 2022-12-01 13:38:16 -08:00 committed by GitHub
parent 6ccc0227f3
commit b8bb921ab5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -138,6 +138,33 @@ acceptance("Admin - Watched Words", function (needs) {
});
});
acceptance("Admin - Watched Words - Emoji Replacement", function (needs) {
needs.user();
needs.site({
watched_words_replace: {
"(?:\\W|^)(betis)(?=\\W|$)": {
replacement: ":poop:",
case_sensitive: false,
},
},
});
test("emoji renders successfully after replacement", async function (assert) {
await visit("/t/internationalization-localization/280");
await click("button.reply-to-post");
await fillIn(".d-editor-input", "betis betis betis");
const cooked = query(".d-editor-preview p");
const cookedChildren = Array.from(cooked.children);
const emojis = cookedChildren.filter((child) => child.nodeName === "IMG");
assert.strictEqual(emojis.length, 3, "three emojis have been rendered");
assert.strictEqual(
emojis.every((emoji) => emoji.title === ":poop:"),
true,
"all emojis are :poop:"
);
});
});
acceptance("Admin - Watched Words - Bad regular expressions", function (needs) {
needs.user();
needs.pretender((server, helper) => {

View File

@ -195,7 +195,8 @@ function applyEmoji(
emojiUnicodeReplacer,
enableShortcuts,
inlineEmoji,
customEmojiTranslation
customEmojiTranslation,
watchedWordsReplacer
) {
let result = null;
let start = 0;
@ -204,6 +205,25 @@ function applyEmoji(
content = emojiUnicodeReplacer(content);
}
if (watchedWordsReplacer) {
const watchedWordRegex = Object.keys(watchedWordsReplacer);
watchedWordRegex.forEach((watchedWord) => {
if (content?.match(watchedWord)) {
const regex = new RegExp(watchedWord, "g");
const matches = content.match(regex);
const replacement = watchedWordsReplacer[watchedWord].replacement;
matches.forEach(() => {
const matchingRegex = regex.exec(content);
if (matchingRegex) {
content = content.replace(matchingRegex[1], replacement);
}
});
}
});
}
let end = content.length;
for (let i = 0; i < content.length - 1; i++) {
@ -337,7 +357,8 @@ export function setup(helper) {
md.options.discourse.emojiUnicodeReplacer,
md.options.discourse.features.emojiShortcuts,
md.options.discourse.features.inlineEmoji,
md.options.discourse.customEmojiTranslation
md.options.discourse.customEmojiTranslation,
md.options.discourse.watchedWordsReplace
)
)
);