Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/js/components/code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function CodeBlock({

return (
<div
dir="ltr"
className={cn(
'relative isolate my-2 rounded-md border border-transparent before:pointer-events-none before:absolute before:-inset-px before:-z-10 before:rounded-md before:border before:border-border before:bg-muted/60',
fullHtml !== undefined &&
Expand Down
3 changes: 3 additions & 0 deletions resources/js/components/commit-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export function CommitBody({
*/
linksEveryMention?: boolean;
}) {
// A body written in Arabic, Hebrew or Persian reads right to left; mentions, links and code stay left to right.
return (
<div
data-commit-body
dir="auto"
className={cn(
'overflow-clip leading-relaxed wrap-break-word whitespace-pre-wrap [overflow-clip-margin:4px]',
className,
Expand Down Expand Up @@ -98,6 +100,7 @@ export function BodySegments({
return (
<code
key={index}
dir="ltr"
className="rounded-sm bg-muted px-1 py-0.5 font-mono text-[0.875em]"
>
<CertificateText text={segment.code} />
Expand Down
23 changes: 18 additions & 5 deletions resources/js/components/linkified-text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function CommitLink({

if (isStatic) {
return (
<span title={url} className="text-primary">
<span dir="ltr" title={url} className="text-primary">
{label}
</span>
);
Expand All @@ -231,6 +231,7 @@ function CommitLink({
<Link
prefetch
href={CommitController.show({ profile: handle, commit })}
dir="ltr"
title={url}
aria-label={`@${handle}’s commit ${hash}`}
className="text-primary hover:underline"
Expand Down Expand Up @@ -555,7 +556,7 @@ function ForgeLink({

if (isStatic) {
return (
<span title={href} className="text-primary">
<span dir="ltr" title={href} className="text-primary">
{label}
</span>
);
Expand All @@ -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"
Expand Down Expand Up @@ -618,6 +620,7 @@ function withWordmarks(
) : (
<span
key={`wordmark-${offset}-${index}`}
dir="ltr"
title={part}
className="font-semibold tracking-tight whitespace-nowrap"
>
Expand Down Expand Up @@ -771,7 +774,9 @@ function withMentions(
pasteisMode,
),
isStatic ? (
match[0]
<span key={`mention-${offset + match.index}`} dir="ltr">
{match[0]}
</span>
) : userId ? (
<UserPreview
key={`mention-${offset + match.index}`}
Expand All @@ -781,6 +786,7 @@ function withMentions(
<Link
prefetch
href={show(userId)}
dir="ltr"
className="font-medium text-primary hover:underline"
data-test="mention"
>
Expand All @@ -792,20 +798,24 @@ 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]}
</Link>
) : !linksEveryMention ? (
match[0]
<span key={`mention-${offset + match.index}`} dir="ltr">
{match[0]}
</span>
) : (
// 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...
<Link
key={`mention-${offset + match.index}`}
href={showProfile(match[1])}
rel="nofollow"
dir="ltr"
className="font-medium text-primary hover:underline"
data-test="handle-mention"
>
Expand Down Expand Up @@ -895,13 +905,16 @@ export function LinkifiedText({
isStatic={isStatic}
/>
) : isStatic ? (
linkText
<span key={match.index} dir="ltr">
{linkText}
</span>
) : (
<a
key={match.index}
href={href}
target="_blank"
rel="noopener noreferrer nofollow ugc"
dir="ltr"
title={href}
className="wrap-anywhere text-primary hover:underline"
>
Expand Down
28 changes: 22 additions & 6 deletions resources/js/components/mention-textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
'text-align',
'unicode-bidi',
];

function triggerAt(
Expand Down Expand Up @@ -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)),
Expand All @@ -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();

Expand Down Expand Up @@ -638,6 +653,7 @@ export function MentionTextarea({
return (
<>
<textarea
dir="auto"
Comment thread
Akiyo974 marked this conversation as resolved.
Resolved
{...props}
ref={setRefs}
value={value}
Expand Down
5 changes: 4 additions & 1 deletion resources/js/pages/user/show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ export default function Show({
{moderation && <UserModerationStatus moderation={moderation} />}

{user.bio && (
<p className="mt-3 max-w-prose text-[15px] leading-relaxed wrap-break-word whitespace-pre-wrap">
<p
dir="auto"
Comment thread
Akiyo974 marked this conversation as resolved.
Resolved
className="mt-3 max-w-prose text-[15px] leading-relaxed wrap-break-word whitespace-pre-wrap"
>
<BioText bio={user.bio} profiles={bioProfiles} />
</p>
)}
Expand Down
35 changes: 32 additions & 3 deletions tests/Unit/LinkifiedText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ test('leaves organization mentions as plain text in static bodies', () => {
/>,
);

assert.equal(html, 'Thanks @laravel!');
assert.equal(html, 'Thanks <span dir="ltr">@laravel</span>!');
});

test('links mentions of anyone else to their page, which looks them up on GitHub', () => {
Expand All @@ -160,7 +160,7 @@ test('links mentions of anyone else to their page, which looks them up on GitHub

assert.equal(
html,
'Thanks <a rel="nofollow" class="font-medium text-primary hover:underline" data-test="handle-mention" href="/@Original">@Original</a>!',
'Thanks <a rel="nofollow" dir="ltr" class="font-medium text-primary hover:underline" data-test="handle-mention" href="/@Original">@Original</a>!',
);
});

Expand All @@ -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 <span dir="ltr">@Original</span>!');
});

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(
<CommitBody
segments={[
{
type: 'text',
text: 'שלום @original! ראו https://example.com/docs ב /dev/null ',
},
{ type: 'code', code: 'php artisan' },
{
type: 'block',
language: 'php',
filename: null,
html: '<span class="hl-keyword">echo</span>',
},
]}
mentions={{ original: id }}
/>,
);

assert.match(html, /^<div data-commit-body="true" dir="auto"/);
assert.match(html, /<a[^>]*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('<code dir="ltr"'));
assert.match(html, /<div dir="ltr" class="relative isolate my-2/);
});

test('keeps captured mention identities in regular bodies alongside highlighted code', () => {
Expand Down