markdown: Inject function for emoticon translations.

We want our parser to be as re-entrant as possible.
This commit is contained in:
Steve Howell
2022-04-04 16:05:02 +00:00
committed by Tim Abbott
parent 03c15c8c14
commit 17b60efdc7
2 changed files with 15 additions and 6 deletions

View File

@@ -771,10 +771,16 @@ test("backend_only_linkifiers", () => {
});
test("translate_emoticons_to_names", () => {
const get_emoticon_translations = emoji.get_emoticon_translations;
function translate_emoticons_to_names(src) {
return markdown.translate_emoticons_to_names({src, get_emoticon_translations});
}
// Simple test
const test_text = "Testing :)";
const expected = "Testing :smile:";
const result = markdown.translate_emoticons_to_names(test_text);
const result = translate_emoticons_to_names(test_text);
assert.equal(result, expected);
// Extensive tests.
@@ -813,7 +819,7 @@ test("translate_emoticons_to_names", () => {
expected: `Hello ${full_name}!`,
},
]) {
const result = markdown.translate_emoticons_to_names(original);
const result = translate_emoticons_to_names(original);
assert.equal(result, expected);
}
}

View File

@@ -39,9 +39,9 @@ function contains_preview_link(content) {
return preview_regexes.some((re) => re.test(content));
}
export function translate_emoticons_to_names(text) {
export function translate_emoticons_to_names({src, get_emoticon_translations}) {
// Translates emoticons in a string to their colon syntax.
let translated = text;
let translated = src;
let replacement_text;
const terminal_symbols = ",.;?!()[] \"'\n\t"; // From composebox_typeahead
const symbols_except_space = terminal_symbols.replace(" ", "");
@@ -67,7 +67,7 @@ export function translate_emoticons_to_names(text) {
return match;
};
for (const translation of helpers.get_emoticon_translations()) {
for (const translation of get_emoticon_translations()) {
// We can't pass replacement_text directly into
// emoticon_replacer, because emoticon_replacer is
// a callback for `replace()`. Instead we just mutate
@@ -490,7 +490,10 @@ export function parse({raw_content, helper_config}) {
// In this scenario, the message has to be from the user, so the only
// requirement should be that they have the setting on.
return translate_emoticons_to_names(src);
return translate_emoticons_to_names({
src,
get_emoticon_translations: helper_config.get_emoticon_translations,
});
}
// Disable headings