DEV: uses vanilla js and DOM to replace link mentions (#13959)

- uses DOM apis
- do not concat strings
- ensures string is set as innerText and not HTML
- do not work on jquery objects
This commit is contained in:
Joffrey JAFFEUX 2021-08-06 09:26:54 +02:00 committed by GitHub
parent 86f1f82f7b
commit bf43d8eb40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 18 deletions

View File

@ -6,32 +6,40 @@ import { userPath } from "discourse/lib/url";
let maxGroupMention; let maxGroupMention;
function replaceSpan($e, username, opts) { function replaceSpan($e, username, opts) {
let extra = ""; let extra = {};
let extraClass = ""; let extraClass = [];
const element = $e[0];
const a = document.createElement("a");
if (opts && opts.group) { if (opts && opts.group) {
if (opts.mentionable) { if (opts.mentionable) {
extra = `data-name='${username}' data-mentionable-user-count='${opts.mentionable.user_count}' data-max-mentions='${maxGroupMention}'`; extra = {
extraClass = "notify"; name: username,
mentionableUserCount: opts.mentionable.user_count,
maxMentions: maxGroupMention,
};
extraClass.push("notify");
} }
$e.replaceWith(
`<a href='${ a.setAttribute("href", getURL("/g/") + username);
getURL("/g/") + username a.classList.add("mention-group", ...extraClass);
}' class='mention-group ${extraClass}' ${extra}>@${username}</a>` a.innerText = `@${username}`;
);
} else { } else {
if (opts && opts.cannot_see) { if (opts && opts.cannot_see) {
extra = `data-name='${username}'`; extra = { name: username };
extraClass = "cannot-see"; extraClass.push("cannot-see");
} }
$e.replaceWith(
`<a href='${userPath( a.href = userPath(username.toLowerCase());
username.toLowerCase() a.classList.add("mention", ...extraClass);
)}' class='mention ${extraClass}' ${extra}>@${formatUsername( a.innerText = `@${formatUsername(username)}`;
username
)}</a>`
);
} }
Object.keys(extra).forEach((key) => {
a.dataset[key] = extra[key];
});
element.replaceWith(a);
} }
const found = {}; const found = {};