Skip to content

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

FIX: preserve site dir for neutral text (#39213)

Sam702953a15390
diff --git a/frontend/discourse/app/components/text-field.js b/frontend/discourse/app/components/text-field.js
index 3802294dbea..2cda1efb5c1 100644
--- a/frontend/discourse/app/components/text-field.js
+++ b/frontend/discourse/app/components/text-field.js
@@ -2,6 +2,7 @@ import { computed } from "@ember/object";
import { cancel, next } from "@ember/runloop";
import { attributeBindings } from "@ember-decorators/component";
import discourseDebounce from "discourse/lib/debounce";
+import { siteDir } from "discourse/lib/text-direction";
import { i18n } from "discourse-i18n";
import TextField from "./ember-text-field";
@@ -49,9 +50,16 @@ export default class DiscourseTextField extends TextField {
next(() => this.onChange(this.value));
}
+ @computed("siteSettings.support_mixed_text_direction", "value")
get dir() {
if (this.siteSettings.support_mixed_text_direction) {
- return "auto";
+ const value = this.value?.toString() || "";
+
+ if (value) {
+ return "auto";
+ }
+
+ return siteDir();
}
}
diff --git a/frontend/discourse/app/lib/text-direction.js b/frontend/discourse/app/lib/text-direction.js
index fdf485c85fe..0838d1267ad 100644
--- a/frontend/discourse/app/lib/text-direction.js
+++ b/frontend/discourse/app/lib/text-direction.js
@@ -34,6 +34,10 @@ export function siteDir() {
return _siteDir;
}
+export function resetSiteDirForTesting() {
+ _siteDir = null;
+}
+
export function isDocumentRTL() {
return siteDir() === "rtl";
}
diff --git a/frontend/discourse/tests/integration/components/text-field-test.gjs b/frontend/discourse/tests/integration/components/text-field-test.gjs
index e194bad0ab5..1618a1ccebf 100644
--- a/frontend/discourse/tests/integration/components/text-field-test.gjs
+++ b/frontend/discourse/tests/integration/components/text-field-test.gjs
@@ -1,11 +1,31 @@
import { fillIn, render } from "@ember/test-helpers";
import { module, test } from "qunit";
import TextField from "discourse/components/text-field";
+import { resetSiteDirForTesting } from "discourse/lib/text-direction";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
module("Integration | Component | text-field", function (hooks) {
setupRenderingTest(hooks);
+ let originalHtmlDir;
+ let originalHtmlIsRtl;
+
+ hooks.beforeEach(function () {
+ originalHtmlDir = document.documentElement.getAttribute("dir");
+ originalHtmlIsRtl = document.documentElement.classList.contains("rtl");
+ });
+
+ hooks.afterEach(function () {
+ if (originalHtmlDir === null) {
+ document.documentElement.removeAttribute("dir");
+ } else {
+ document.documentElement.setAttribute("dir", originalHtmlDir);
+ }
+
+ document.documentElement.classList.toggle("rtl", originalHtmlIsRtl);
+ resetSiteDirForTesting();
+ });
+
test("renders correctly with no properties set", async function (assert) {
await render(<template><TextField /></template>);
@@ -33,6 +53,26 @@ module("Integration | Component | text-field", function (hooks) {
assert.dom("input").hasAttribute("dir", "auto");
});
+ test("uses site direction if the input is empty and `auto` if the input isn't empty", async function (assert) {
+ this.siteSettings.support_mixed_text_direction = true;
+ document.documentElement.removeAttribute("dir");
+ document.documentElement.classList.add("rtl");
+ resetSiteDirForTesting();
+
+ await render(<template><TextField /></template>);
+
+ assert.dom("input").hasAttribute("dir", "rtl");
+
+ await fillIn("input", "123");
+ assert.dom("input").hasAttribute("dir", "auto");
+
+ await fillIn("input", "hello");
+ assert.dom("input").hasAttribute("dir", "auto");
+
+ await fillIn("input", "");
+ assert.dom("input").hasAttribute("dir", "rtl");
+ });
+
test("supports onChange", async function (assert) {
this.called = false;
this.newValue = null;