Skip to content

Commit

Permalink
Merge pull request #1060 from basecamp/chrome-rtl-support
Browse files Browse the repository at this point in the history
Restore detection of text direction on Chrome
  • Loading branch information
Alberto Fernández-Capel authored May 17, 2023
2 parents fe6a361 + a85487f commit 35cb6b9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/test/unit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "test/unit/attachment_test"
import "test/unit/bidi_test"
import "test/unit/block_test"
import "test/unit/composition_test"
import "test/unit/document_test"
Expand Down
9 changes: 9 additions & 0 deletions src/test/unit/bidi_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assert, test, testGroup } from "test/test_helper"
import { getDirection } from "trix/core/helpers"

testGroup("BIDI", () => {
test("detects text direction", () => {
assert.equal(getDirection("abc"), "ltr")
assert.equal(getDirection("אבג"), "rtl")
})
})
8 changes: 5 additions & 3 deletions src/trix/core/helpers/bidi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const RTL_PATTERN =

export const getDirection = (function() {
const input = makeElement("input", { dir: "auto", name: "x", dirName: "x.dir" })
const textArea = makeElement("textarea", { dir: "auto", name: "y", dirName: "y.dir" })
const form = makeElement("form")
form.appendChild(input)
form.appendChild(textArea)

const supportsDirName = (function() {
try {
return new FormData(form).has(input.dirName)
return new FormData(form).has(textArea.dirName)
} catch (error) {
return false
}
Expand All @@ -27,8 +29,8 @@ export const getDirection = (function() {

if (supportsDirName) {
return function(string) {
input.value = string
return new FormData(form).get(input.dirName)
textArea.value = string
return new FormData(form).get(textArea.dirName)
}
} else if (supportsDirSelector) {
return function(string) {
Expand Down

0 comments on commit 35cb6b9

Please sign in to comment.