diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js index b6c3d3456a0..1e01a57b27d 100644 --- a/app/assets/javascripts/discourse/app/components/composer-editor.js +++ b/app/assets/javascripts/discourse/app/components/composer-editor.js @@ -503,7 +503,7 @@ export default Component.extend(ComposerUpload, { // 'Create a New Topic' scenario is not supported (per conversation with codinghorror) // https://meta.discourse.org/t/taking-another-1-7-release-task/51986/7 fetchUnseenMentions(unseen, this.get("composer.topic.id")).then(() => { - linkSeenMentions($preview, this.siteSettings); + linkSeenMentions($preview[0], this.siteSettings); this._warnMentionedGroups($preview); this._warnCannotSeeMention($preview); }); @@ -734,7 +734,7 @@ export default Component.extend(ComposerUpload, { previewUpdated($preview) { // Paint mentions - const unseenMentions = linkSeenMentions($preview, this.siteSettings); + const unseenMentions = linkSeenMentions($preview[0], this.siteSettings); if (unseenMentions.length) { discourseDebounce( this, diff --git a/app/assets/javascripts/discourse/app/components/d-editor.js b/app/assets/javascripts/discourse/app/components/d-editor.js index a7a879d5b49..312c0719a18 100644 --- a/app/assets/javascripts/discourse/app/components/d-editor.js +++ b/app/assets/javascripts/discourse/app/components/d-editor.js @@ -395,7 +395,7 @@ export default Component.extend(TextareaTextManipulation, { cookedElement.innerHTML = cooked; linkSeenHashtags($(cookedElement)); - linkSeenMentions($(cookedElement), this.siteSettings); + linkSeenMentions(cookedElement, this.siteSettings); resolveCachedShortUrls(this.siteSettings, cookedElement); loadOneboxes( cookedElement, diff --git a/app/assets/javascripts/discourse/app/lib/link-mentions.js b/app/assets/javascripts/discourse/app/lib/link-mentions.js index e7cd5c29de3..ab9aca48118 100644 --- a/app/assets/javascripts/discourse/app/lib/link-mentions.js +++ b/app/assets/javascripts/discourse/app/lib/link-mentions.js @@ -1,3 +1,4 @@ +import deprecated from "discourse-common/lib/deprecated"; import { ajax } from "discourse/lib/ajax"; import { formatUsername } from "discourse/lib/utilities"; import getURL from "discourse-common/lib/get-url"; @@ -5,10 +6,9 @@ import { userPath } from "discourse/lib/url"; let maxGroupMention; -function replaceSpan($e, username, opts) { +function replaceSpan(element, username, opts) { let extra = {}; let extraClass = []; - const element = $e[0]; const a = document.createElement("a"); if (opts && opts.group) { @@ -48,30 +48,39 @@ const mentionableGroups = {}; const checked = {}; const cannotSee = []; -function updateFound($mentions, usernames) { - $mentions.each((i, e) => { - const $e = $(e); - const username = usernames[i]; +function updateFound(mentions, usernames) { + mentions.forEach((mention, index) => { + const username = usernames[index]; if (found[username.toLowerCase()]) { - replaceSpan($e, username, { cannot_see: cannotSee[username] }); + replaceSpan(mention, username, { cannot_see: cannotSee[username] }); } else if (mentionableGroups[username]) { - replaceSpan($e, username, { + replaceSpan(mention, username, { group: true, mentionable: mentionableGroups[username], }); } else if (foundGroups[username]) { - replaceSpan($e, username, { group: true }); + replaceSpan(mention, username, { group: true }); } else if (checked[username]) { - $e.addClass("mention-tested"); + mention.classList.add("mention-tested"); } }); } -export function linkSeenMentions($elem, siteSettings) { - const $mentions = $("span.mention:not(.mention-tested)", $elem); - if ($mentions.length) { - const usernames = [...$mentions.map((_, e) => $(e).text().substr(1))]; - updateFound($mentions, usernames); +export function linkSeenMentions(elem, siteSettings) { + if (elem instanceof jQuery) { + elem = elem[0]; + + deprecated("linkSeenMentions now expects a DOM node as first parameter", { + since: "2.8.0.beta7", + }); + } + + const mentions = [ + ...elem.querySelectorAll("span.mention:not(.mention-tested)"), + ]; + if (mentions.length) { + const usernames = mentions.map((m) => m.innerText.substr(1)); + updateFound(mentions, usernames); return usernames .uniq() .filter( diff --git a/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js b/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js index 53c072bdc95..c18887b6e6a 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/link-mentions-test.js @@ -32,31 +32,39 @@ module("Unit | Utility | link-mentions", function () { "invalid", ]); - let $root = $(` + let html = `