Skip to content

Node.js based forum software built for the modern web

fix: browser title rendered LTR for RTL users when the page title starts with Latin text (#14780)

palmoni5561de8a4efd4
diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index 2e1b252ab3..69fc4310a1 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -330,10 +330,18 @@ helpers.buildTitle = async function (pageTitle, userLang, template) {
], userLang);
const titleLayout = meta.config.titleLayout || `${pageTitle ? '{pageTitle} | ' : ''}{browserTitle}`;
- const title = titleLayout
+ let title = titleLayout
.replace('{pageTitle}', () => titleTranslated)
.replace('{browserTitle}', () => browserTitleTranslated);
+ // The browser tab has no dir attribute, so it picks the title's direction from
+ // its first strongly-directional character. An RTL user viewing a page whose
+ // title starts with a Latin string (e.g. a username) would otherwise get an
+ // LTR title. Prefix a right-to-left mark to keep the direction stable.
+ if (translator.languageDirection(userLang) === 'rtl' && !title.startsWith('\u200F')) {
+ title = `\u200F${title}`;
+ }
+
return utils.decodeHTMLEntities(title);
};
diff --git a/test/controllers.js b/test/controllers.js
index aba0699b6a..61aa814ed9 100644
--- a/test/controllers.js
+++ b/test/controllers.js
@@ -2136,6 +2136,31 @@ describe('Controllers', () => {
assert.deepStrictEqual(selectedCids, [category2.cid]);
assert.strictEqual(selectedCategory.cid, category2.cid);
});
+
+ describe('.buildTitle()', () => {
+ it('should not prefix a right-to-left mark for ltr languages', async () => {
+ const title = await controllerHelpers.buildTitle('shishko', 'en-GB', 'chats');
+ assert(!title.startsWith('\u200F'));
+ assert(title.startsWith('shishko | '));
+ });
+
+ it('should prefix a right-to-left mark for rtl languages', async () => {
+ const title = await controllerHelpers.buildTitle('shishko', 'he', 'chats');
+ assert(title.startsWith('\u200Fshishko | '));
+ });
+
+ it('should not double the right-to-left mark if the layout already has one', async () => {
+ const oldLayout = meta.config.titleLayout;
+ meta.config.titleLayout = '\u200F{pageTitle} | {browserTitle}';
+ try {
+ const title = await controllerHelpers.buildTitle('shishko', 'he', 'chats');
+ assert(title.startsWith('\u200Fshishko | '));
+ assert(!title.startsWith('\u200F\u200F'));
+ } finally {
+ meta.config.titleLayout = oldLayout;
+ }
+ });
+ });
});
after((done) => {