diff --git a/resources/js/components/code-block.tsx b/resources/js/components/code-block.tsx
index eeb516b1aa..c22c298c1a 100644
--- a/resources/js/components/code-block.tsx
+++ b/resources/js/components/code-block.tsx
@@ -63,6 +63,7 @@ export function CodeBlock({
return (
diff --git a/resources/js/components/linkified-text.tsx b/resources/js/components/linkified-text.tsx
index b03028cab8..55072eeafb 100644
--- a/resources/js/components/linkified-text.tsx
+++ b/resources/js/components/linkified-text.tsx
@@ -221,7 +221,7 @@ function CommitLink({
if (isStatic) {
return (
-
+
{label}
);
@@ -231,6 +231,7 @@ function CommitLink({
+
{label}
);
@@ -566,6 +567,7 @@ function ForgeLink({
href={href}
target="_blank"
rel="noopener noreferrer nofollow ugc"
+ dir="ltr"
title={href}
aria-label={`${name}${item ? ` ${item.description}` : ''} on ${forge.name}`}
className="text-primary hover:underline"
@@ -618,6 +620,7 @@ function withWordmarks(
) : (
@@ -771,7 +774,9 @@ function withMentions(
pasteisMode,
),
isStatic ? (
- match[0]
+
+ {match[0]}
+
) : userId ? (
@@ -792,13 +798,16 @@ function withMentions(
prefetch
key={`mention-${offset + match.index}`}
href={showOrganizationMention(organizationId)}
+ dir="ltr"
className="font-medium text-primary hover:underline"
data-test="organization-mention"
>
{match[0]}
) : !linksEveryMention ? (
- match[0]
+
+ {match[0]}
+
) : (
// Anyone else may be on GitHub: their page looks them up for members. Guests and crawlers get a
// missing page, so it isn't followed, nor prefetched, which would ask for it on every hover of a feed...
@@ -806,6 +815,7 @@ function withMentions(
key={`mention-${offset + match.index}`}
href={showProfile(match[1])}
rel="nofollow"
+ dir="ltr"
className="font-medium text-primary hover:underline"
data-test="handle-mention"
>
@@ -895,13 +905,16 @@ export function LinkifiedText({
isStatic={isStatic}
/>
) : isStatic ? (
- linkText
+
+ {linkText}
+
) : (
diff --git a/resources/js/components/mention-textarea.tsx b/resources/js/components/mention-textarea.tsx
index 4f7e67b0ae..b705e72cc5 100644
--- a/resources/js/components/mention-textarea.tsx
+++ b/resources/js/components/mention-textarea.tsx
@@ -77,6 +77,10 @@ const mirroredProperties = [
'text-transform',
'text-indent',
'tab-size',
+ // `dir="auto"` lays a Hebrew or Arabic draft out right to left, so the mirror has to as well.
+ 'direction',
+ 'text-align',
+ 'unicode-bidi',
];
function triggerAt(
@@ -127,7 +131,6 @@ function triggerAt(
function characterRect(textarea: HTMLTextAreaElement, index: number) {
const style = window.getComputedStyle(textarea);
const mirror = document.createElement('div');
- const marker = document.createElement('span');
mirroredProperties.forEach((property) =>
mirror.style.setProperty(property, style.getPropertyValue(property)),
@@ -139,17 +142,29 @@ function characterRect(textarea: HTMLTextAreaElement, index: number) {
mirror.style.whiteSpace = 'pre-wrap';
mirror.style.overflowWrap = 'break-word';
- mirror.textContent = textarea.value.slice(0, index);
- marker.textContent = textarea.value.slice(index) || '.';
- mirror.appendChild(marker);
+ // The mirror holds the draft exactly as typed, so it wraps where the textarea does, and a range over the character
+ // at the index measures where it sits. Its box begins on the right when the draft runs right to left.
+ const text = document.createTextNode(
+ index < textarea.value.length ? textarea.value : `${textarea.value}.`,
+ );
+ mirror.appendChild(text);
document.body.appendChild(mirror);
+ const range = document.createRange();
+ range.setStart(text, index);
+ range.setEnd(text, index + 1);
+
const rect = textarea.getBoundingClientRect();
+ const mirrorRect = mirror.getBoundingClientRect();
+ const characterBox = range.getBoundingClientRect();
+ const start =
+ style.direction === 'rtl' ? characterBox.right : characterBox.left;
const lineHeight =
Number.parseFloat(style.lineHeight) ||
Number.parseFloat(style.fontSize) * 1.4;
- const top = rect.top + marker.offsetTop - textarea.scrollTop;
- const left = rect.left + marker.offsetLeft - textarea.scrollLeft;
+ const top =
+ rect.top + characterBox.top - mirrorRect.top - textarea.scrollTop;
+ const left = rect.left + start - mirrorRect.left - textarea.scrollLeft;
mirror.remove();
@@ -638,6 +653,7 @@ export function MentionTextarea({
return (
<>
}
{user.bio && (
-
+
)}
diff --git a/tests/Unit/LinkifiedText.test.tsx b/tests/Unit/LinkifiedText.test.tsx
index d963024a18..2851c1e244 100644
--- a/tests/Unit/LinkifiedText.test.tsx
+++ b/tests/Unit/LinkifiedText.test.tsx
@@ -150,7 +150,7 @@ test('leaves organization mentions as plain text in static bodies', () => {
/>,
);
- assert.equal(html, 'Thanks @laravel!');
+ assert.equal(html, 'Thanks @laravel!');
});
test('links mentions of anyone else to their page, which looks them up on GitHub', () => {
@@ -160,7 +160,7 @@ test('links mentions of anyone else to their page, which looks them up on GitHub
assert.equal(
html,
- 'Thanks @Original!',
+ 'Thanks @Original!',
);
});
@@ -173,7 +173,36 @@ test('leaves mentions of anyone else as text where only captured ones link, as i
/>,
);
- assert.equal(html, 'Thanks @Original!');
+ assert.equal(html, 'Thanks @Original!');
+});
+
+test('reads Arabic and Hebrew bodies right to left while mentions, links, code and the wordmark stay left to right', () => {
+ const id = '019a2b3c-4d5e-7f60-8a9b-0c1d2e3f4a5b';
+ const html = renderPage(
+ echo',
+ },
+ ]}
+ mentions={{ original: id }}
+ />,
+ );
+
+ assert.match(html, /^]*dir="ltr"[^>]*data-test="mention"/);
+ assert.ok(html.includes('dir="ltr" title="https://example.com/docs"'));
+ assert.ok(html.includes('dir="ltr" title="/dev/null"'));
+ assert.ok(html.includes('