Skip to content

A platform for community discussion. Free, open, simple.

FIX: Handle tag group names containing quote characters in filter suggestions (#37549)

Samafd259a29cda
diff --git a/frontend/discourse/app/lib/filter-suggestions.js b/frontend/discourse/app/lib/filter-suggestions.js
index 0b71a234440..c471eb85015 100644
--- a/frontend/discourse/app/lib/filter-suggestions.js
+++ b/frontend/discourse/app/lib/filter-suggestions.js
@@ -354,9 +354,7 @@ class FilterTypeValueSuggester {
});
return response.results.map((tagGroup) => {
- // Quote the name if it contains special characters
- const needsQuoting = /[\s&\-()']/.test(tagGroup.name);
- const quotedName = needsQuoting ? `"${tagGroup.name}"` : tagGroup.name;
+ const quotedName = this.quoteIfNeeded(tagGroup.name);
return {
name: this.buildSuggestionName(quotedName),
@@ -370,6 +368,26 @@ class FilterTypeValueSuggester {
}
}
+ quoteIfNeeded(name) {
+ const needsQuoting = /[\s&\-()'""]/.test(name);
+ if (!needsQuoting) {
+ return name;
+ }
+
+ const hasDoubleQuote = name.includes('"');
+ const hasSingleQuote = name.includes("'");
+
+ if (hasDoubleQuote && !hasSingleQuote) {
+ return `'${name}'`;
+ } else if (hasSingleQuote && !hasDoubleQuote) {
+ return `"${name}"`;
+ } else if (hasDoubleQuote && hasSingleQuote) {
+ return `'${name.replace(/'/g, "\\'")}'`;
+ } else {
+ return `"${name}"`;
+ }
+ }
+
async getUserSuggestions() {
try {
const data = { limit: 10 };
diff --git a/frontend/discourse/tests/unit/lib/filter-suggestions-test.js b/frontend/discourse/tests/unit/lib/filter-suggestions-test.js
index e37a5447319..9f7800cb782 100644
--- a/frontend/discourse/tests/unit/lib/filter-suggestions-test.js
+++ b/frontend/discourse/tests/unit/lib/filter-suggestions-test.js
@@ -340,4 +340,50 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
"cam is excluded after being used"
);
});
+
+ test("tag_group suggestions properly quote names with special characters", async function (assert) {
+ pretender.get("/tag_groups/filter/search.json", () =>
+ response({
+ results: [
+ { name: "Simple", tag_names: ["tag1"] },
+ { name: "My Tag Group", tag_names: ["tag2"] },
+ { name: 'Quote "Test" Group', tag_names: ["tag3"] },
+ { name: "It's a Group", tag_names: ["tag4"] },
+ ],
+ })
+ );
+
+ const tips = [
+ {
+ name: "tag_group:",
+ description: "Tag Group",
+ type: "tag_group",
+ priority: 1,
+ },
+ ];
+
+ const res = await FilterSuggestions.getSuggestions(
+ "tag_group:",
+ tips,
+ buildContext()
+ );
+ const terms = res.suggestions.map((s) => s.term);
+
+ assert.strictEqual(terms[0], "Simple", "simple names are not quoted");
+ assert.strictEqual(
+ terms[1],
+ '"My Tag Group"',
+ "names with spaces are quoted"
+ );
+ assert.strictEqual(
+ terms[2],
+ "'Quote \"Test\" Group'",
+ "names with double quotes use single quotes"
+ );
+ assert.strictEqual(
+ terms[3],
+ '"It\'s a Group"',
+ "names with single quotes use double quotes and escape internal single quotes"
+ );
+ });
});