Skip to content

Commit ba35ede

Browse files
committed
DEV: Add backward compatibility for tag object arrays
What is the problem? Discourse core PR #36678 changes `renderTag` to pass tag objects instead of strings to custom tag renderers. This breaks the `iconTagRenderer` function which expects the `tag` parameter to be a string and calls `escapeExpression(tag)` directly. What is the solution? Extract the tag name at the start of `iconTagRenderer` using `typeof tag === "string" ? tag : tag.name` before processing. Also rename the local `tagName` variable to `htmlTagName` to avoid confusion with the extracted tag name. This ensures backward compatibility with both the old string format and the new object format.
1 parent 08b2de3 commit ba35ede

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

javascripts/discourse/initializers/tag-icons.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ function iconTagRenderer(tag, params) {
1111
let tagIconList = settings.tag_icon_list.split("|");
1212

1313
params = params || {};
14-
const visibleName = escapeExpression(tag);
14+
// TODO(https://github.com/discourse/discourse/pull/36678): The string check can be
15+
// removed using .discourse-compatibility once the PR is merged.
16+
const tagStr = typeof tag === "string" ? tag : tag.name;
17+
const visibleName = escapeExpression(tagStr);
1518
tag = visibleName.toLowerCase();
1619

1720
const classes = ["discourse-tag"];
18-
const tagName = params.tagName || "a";
21+
const htmlTagName = params.tagName || "a";
1922
let path;
20-
if (tagName === "a" && !params.noHref) {
23+
if (htmlTagName === "a" && !params.noHref) {
2124
if ((params.isPrivateMessage || params.pmOnly) && currentUser) {
2225
const username = params.tagsForUser
2326
? params.tagsForUser
@@ -64,7 +67,7 @@ function iconTagRenderer(tag, params) {
6467

6568
let val =
6669
"<" +
67-
tagName +
70+
htmlTagName +
6871
href +
6972
" data-tag-name=" +
7073
tag +
@@ -75,7 +78,7 @@ function iconTagRenderer(tag, params) {
7578
tagIconHTML + // inject tag Icon in html
7679
(params.displayName ? escape(params.displayName) : visibleName) +
7780
"</" +
78-
tagName +
81+
htmlTagName +
7982
">";
8083

8184
if (params.count) {

spec/system/tag_icons_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe "Tag icons", type: :system do
4+
fab!(:tag) { Fabricate(:tag, name: "support") }
5+
fab!(:topic) { Fabricate(:topic, tags: [tag]) }
6+
fab!(:post) { Fabricate(:post, topic:) }
7+
fab!(:user)
8+
9+
let(:topic_page) { PageObjects::Pages::Topic.new }
10+
let!(:theme) { upload_theme_component }
11+
12+
before do
13+
SiteSetting.tagging_enabled = true
14+
theme.update_setting(:tag_icon_list, "support,question-circle,#ff0000")
15+
theme.save!
16+
sign_in(user)
17+
end
18+
19+
it "displays tag with icon on topic page" do
20+
topic_page.visit_topic(topic)
21+
expect(page).to have_css(".discourse-tag[data-tag-name='support']")
22+
expect(page).to have_css(".tag-icon[style*='color: #ff0000'] .d-icon-question-circle")
23+
end
24+
25+
it "displays tag without icon when not configured" do
26+
theme.update_setting(:tag_icon_list, "")
27+
theme.save!
28+
29+
topic_page.visit_topic(topic)
30+
expect(page).to have_css(".discourse-tag[data-tag-name='support']")
31+
expect(page).to have_no_css(".tag-icon")
32+
end
33+
end

0 commit comments

Comments
 (0)