Skip to content

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

FIX: Also update html page's title when displaying localized titles (#36139)

Natalie Taya3c12bdf51ae
diff --git a/frontend/discourse/app/routes/topic.js b/frontend/discourse/app/routes/topic.js
index d7a62be1363..2e599ab369a 100644
--- a/frontend/discourse/app/routes/topic.js
+++ b/frontend/discourse/app/routes/topic.js
@@ -55,9 +55,21 @@ export default class TopicRoute extends DiscourseRoute {
return model.get("errorTitle");
}
- const result = model.get("unicode_title") || model.get("title"),
- cat = model.get("category");
+ let result;
+
+ const titleLocalized = model.get("fancy_title_localized");
+ if (titleLocalized) {
+ const fancyTitle = model.get("fancy_title");
+ // doing this helps with decoding entities
+ // e.g., “ -> ", — -> —
+ const tempDiv = document.createElement("div");
+ tempDiv.innerHTML = fancyTitle;
+ result = tempDiv.textContent || tempDiv.innerText || fancyTitle;
+ } else {
+ result = model.get("unicode_title") || model.get("title");
+ }
+ const cat = model.get("category");
// Only display uncategorized in the title tag if it was renamed
if (
this.siteSettings.topic_page_title_includes_category &&
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index fb7c16dee28..3153080f61b 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -242,9 +242,10 @@ describe "Content Localization" do
expect(edit_localized_post_dialog).to be_open
edit_localized_post_dialog.click_yes
- expect(composer).to have_title(topic.title)
+ expect(composer).to have_input_title(topic.title)
composer.close
+ expect(composer).to be_closed
# use translation composer to edit localized title
topic_page.click_topic_edit_title
@@ -334,6 +335,42 @@ describe "Content Localization" do
expect(post_21_obj.post).to have_content("日本語コンテンツ 21")
end
end
+
+ context "for html title" do
+ fab!(:shady_topic) do
+ topic =
+ Fabricate(
+ :topic,
+ title: "topic with — <script>alert('xss')</script> …",
+ locale: "en",
+ user: site_local_user,
+ )
+ Fabricate(:post, topic:, locale: "en")
+ topic
+ end
+
+ fab!(:shady_topic_ja_localization) do
+ Fabricate(:topic_localization, topic: shady_topic, locale: "ja")
+ end
+
+ it "shows localized fancy_title in HTML title when user locale differs" do
+ sign_in(japanese_user)
+
+ topic_page.visit_topic(shady_topic)
+ expect(page).to have_title(shady_topic_ja_localization.fancy_title)
+
+ page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
+ expect(page).to have_title(shady_topic.title)
+
+ page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
+ expect(page).to have_title(shady_topic_ja_localization.fancy_title)
+
+ SiteSetting.content_localization_enabled = false
+ page.refresh
+
+ expect(page).to have_title(shady_topic.title)
+ end
+ end
end
context "for site settings" do
diff --git a/spec/system/page_objects/components/composer.rb b/spec/system/page_objects/components/composer.rb
index 9584874e525..449c5c7e526 100644
--- a/spec/system/page_objects/components/composer.rb
+++ b/spec/system/page_objects/components/composer.rb
@@ -62,8 +62,10 @@ module PageObjects
self
end
- def has_title?(text)
- find(".title-input input#reply-title", text:)
+ def has_input_title?(value)
+ puts "reply title value"
+ puts find("#{@composer_id} input#reply-title").value
+ expect(find("#{@composer_id} input#reply-title").value).to eq(value)
end
def fill_content(content)