A platform for community discussion. Free, open, simple.
FIX: prevent hashtag autocomplete crash when a type isn't registered (#39267)
Régis HANOL88887cd30b66
diff --git a/frontend/discourse/app/lib/hashtag-autocomplete.js b/frontend/discourse/app/lib/hashtag-autocomplete.jsindex f5f0ff32da3..d007fe37d6e 100644--- a/frontend/discourse/app/lib/hashtag-autocomplete.js+++ b/frontend/discourse/app/lib/hashtag-autocomplete.js@@ -113,8 +113,14 @@ function _searchGeneric(term, contextualHashtagConfiguration) {}function _searchRequest(term, contextualHashtagConfiguration, resultFunc) {+ // Only request types the client can actually render. Plugin-contributed+ // types may be missing when their JS isn't loaded (e.g. in safe mode).+ const registeredTypes = getHashtagTypeClassesNew();+ const order = contextualHashtagConfiguration.filter(+ (type) => type in registeredTypes+ );currentSearch = ajax("/hashtags/search.json", {- data: { term, order: contextualHashtagConfiguration },+ data: { term, order },});currentSearch.then((response) => {@@ -142,7 +148,9 @@ function _searchRequest(term, contextualHashtagConfiguration, resultFunc) {}const hashtagType = getHashtagTypeClassesNew()[result.type];- result.iconHtml = hashtagType.generateIconHTML(opts);+ if (hashtagType) {+ result.iconHtml = hashtagType.generateIconHTML(opts);+ }});resultFunc(response.results || CANCELLED_STATUS);})diff --git a/frontend/discourse/app/lib/hashtag-decorator.js b/frontend/discourse/app/lib/hashtag-decorator.jsindex 93ed2add88b..3be4a85fb47 100644--- a/frontend/discourse/app/lib/hashtag-decorator.js+++ b/frontend/discourse/app/lib/hashtag-decorator.js@@ -89,10 +89,9 @@ export function generatePlaceholderHashtagHTML(type, spanEl, data) {link.dataset.emoji = data.emoji;}- const hashtagTypeClass = new getHashtagTypeClasses()[type];- link.innerHTML = `${hashtagTypeClass.generateIconHTML(- data- )}<span>${emojiUnescape(data.text)}</span>`;+ const hashtagTypeClass = getHashtagTypeClasses()[type];+ const iconHTML = hashtagTypeClass?.generateIconHTML(data) ?? "";+ link.innerHTML = `${iconHTML}<span>${emojiUnescape(data.text)}</span>`;spanEl.replaceWith(link);}diff --git a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js b/frontend/discourse/app/static/prosemirror/extensions/hashtag.jsindex b5fbdf29eae..e2e28b464b3 100644--- a/frontend/discourse/app/static/prosemirror/extensions/hashtag.js+++ b/frontend/discourse/app/static/prosemirror/extensions/hashtag.js@@ -184,7 +184,8 @@ const extension = {getHashtagTypeClasses()[validHashtag.type];const hashtagIconHTML =validHashtag.iconHtml ||- hashtagTypeClass.generateIconHTML(validHashtag).trim();+ hashtagTypeClass?.generateIconHTML(validHashtag).trim() ||+ "";domNode.innerHTML = `${hashtagIconHTML}${tagText}`;}diff --git a/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.js b/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.jsindex 28ee9762493..bcabed879cb 100644--- a/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.js+++ b/frontend/discourse/tests/acceptance/hashtag-autocomplete-test.js@@ -1,5 +1,6 @@import { click, visit } from "@ember/test-helpers";import { test } from "qunit";+import pretender from "discourse/tests/helpers/create-pretender";import {acceptance,simulateKeys,@@ -118,3 +119,66 @@ acceptance("#hashtag autocompletion in composer", function (needs) {.exists();});});++acceptance(+ "#hashtag autocompletion with types unknown to the client",+ function (needs) {+ needs.user();+ needs.pretender((server, helper) => {+ server.get("/hashtags", () => helper.response({ category: [], tag: [] }));+ server.get("/hashtags/search.json", () => {+ return helper.response({+ results: [+ {+ id: 1,+ text: "Music Lounge",+ slug: "music",+ icon: "comment",+ relative_url: "/chat/c/music/1",+ ref: "music",+ type: "channel",+ style_type: "icon",+ },+ {+ id: 28,+ text: "Other Languages",+ slug: "other-languages",+ colors: ["FF0000"],+ icon: "folder",+ relative_url: "/c/other-languages/28",+ ref: "other-languages",+ type: "category",+ style_type: "square",+ },+ ],+ });+ });+ });++ test("only requests types that have a client-side class registered", async function (assert) {+ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await simulateKeys(".d-editor-input", "abc #filter-known");++ const searchRequest = pretender.handledRequests.findLast((r) =>+ r.url.startsWith("/hashtags/search.json")+ );+ const order = new URL(+ searchRequest.url,+ "http://dummy"+ ).searchParams.getAll("order[]");+ assert.deepEqual(order, ["category", "tag"]);+ });++ test("does not crash when the server returns a type not registered on the client", async function (assert) {+ await visit("/t/internationalization-localization/280");+ await click("#topic-footer-buttons .btn.create");+ await simulateKeys(".d-editor-input", "abc #unknown-type");++ // The server-side handler above returns a channel result despite the+ // client not registering a "channel" hashtag type class. The row should+ // still render (without an icon) and the promise must not reject.+ assert.dom(".hashtag-autocomplete__option").exists({ count: 2 });+ });+ }+);diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.jsindex 064af2040f1..9456591fae2 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/hashtag-test.js@@ -110,5 +110,32 @@ module(});});++ test("renders without crashing when the server returns a type that isn't registered on the client", async function (assert) {+ // Can happen in safe mode: plugin Ruby still runs and resolves the+ // hashtag, but the plugin JS that would register the "channel" type+ // class isn't loaded.+ pretender.get("/hashtags", () =>+ response({+ channels: [+ {+ type: "channel",+ ref: "unknown-type-channel",+ icon: "comment",+ id: 1,+ },+ ],+ })+ );++ this.siteSettings.rich_editor = true;++ await testMarkdown(+ assert,+ "Hello #unknown-type-channel",+ '<p>Hello <a class="hashtag-cooked" data-name="unknown-type-channel" data-processed="true" contenteditable="false" draggable="true">unknown-type-channel</a></p>',+ "Hello #unknown-type-channel"+ );+ });});