Skip to content

[GH-27911] Keep right-to-left messages right-to-left when they start with a mention - #40050

Open
burrows99 wants to merge 2 commits into
mattermost:masterfrom
burrows99:fix-27911-rtl-message-starting-with-mention
Open

burrows99 wants to merge 2 commits into
mattermost:masterfrom
burrows99:fix-27911-rtl-message-starting-with-mention

Conversation

@burrows99

@burrows99 burrows99 commented Nov 15, 2026 •

Copy link
Copy Markdown

Summary

A right-to-left message that starts with a mention was laid out left-to-right.

Message text is rendered with dir='auto', so the browser takes the direction of the whole message from its first strongly directional character. When a Persian, Arabic or Hebrew message starts with @username, @all and so on, that character is a Latin letter of the mention. The message then gets a left-to-right base direction, the mention sits at the far left, and mixed text such as یک دو one two سه چهار shows its parts in the wrong order.

This PR wraps every mention rendered by AtMention in a <bdi> element (user mentions, group mentions, and the fallback used for @all, @channel, @here and unknown names). The browser skips <bdi> content when it resolves dir='auto', so the message takes its direction from its own text. The <bdi> also isolates the mention from the text around it, so inside a right-to-left message a Latin username keeps its @ in front (on master it renders as sam@).

Nothing changes for left-to-right messages, or for right-to-left messages without a mention. Text alignment is not touched.

flowchart TD
    A[Right to left message starts with a mention] --> B{Where the direction comes from}
    B -->|Before| C[The mention so the message is left to right]
    B -->|After| D[The message text so it stays right to left]
Loading

Two existing tests looked for the data-mention attribute directly on the element that holds the mention text. That text is now one level deeper, inside the <bdi>, so those assertions now look up the enclosing [data-mention] element.

QA steps:

  1. Post @someuser جلسه با تیم design ساعت ده است in a channel.
  2. Before: the mention is at the far left and the message reads left-to-right. After: the message reads right-to-left with the mention at its start.
  3. Post @someuser the design meeting starts at ten. It stays left-to-right.

Edge cases and unchanged behaviour checked:

  • In the running app, shown in the recording:
    • @all on its own line above Persian text reads right-to-left.
    • A user mention in the middle of a Persian sentence reads right-to-left, with the @ in front of the username.
    • An English message that starts with a mention stays left-to-right.
    • A Persian message without a mention is unchanged.
    • Clicking a mention still opens the profile popover.
  • By unit tests only:
    • A group mention is wrapped in <bdi>.
    • A mention that matches no user or group (the @all fallback) is wrapped in <bdi>.
    • Punctuation that trails a mention (for example the period in @someuser.) stays outside the <bdi>, for user mentions, group mentions and unknown names, so it follows the direction of the message.

Tests run locally, from webapp/channels:

  • npm test -- src/components/at_mention/at_mention.test.tsx with the change to at_mention.tsx reverted: 3 failed (the three new tests), 3 passed.
  • The same command with the change: 9 passed (this includes three tests added later for trailing punctuation; they fail without that follow-up commit).
  • npm test -- src/components/at_mention src/components/post_markdown src/components/post_view/combined_system_message src/components/post_view/post_add_channel_member src/components/markdown src/utils/message_html_to_component.test.tsx src/components/quote_selection_button src/components/channel_invite_modal src/components/advanced_text_editor/wysiwyg_editor src/plugins/published_editor.test.tsx: 26 suites, 368 tests, all passed.
  • npx eslint --quiet on the four changed files: no errors.
  • Not run: the full webapp test suite, npm run check-types, and the end-to-end tests.

AI assistance (Claude Code) was used to write and test this change.

Ticket Link

Fixes: #27911

Screenshots

Narrated recording, before then after (sound on):

evidence.mp4

The recording is the web app built from master (before) and from this branch (after), against a local server, with the same account and the same message.

Release Note

Fixed right-to-left messages being displayed left-to-right when they start with an at-mention.

🤖 Generated with Claude Code

Change Impact: Medium 🟡

Changes Breakdown:

+77 -27 Production Changes (45%)
+93 -6 Test Changes (55%)

Regression Risk: AtMention is a shared component, so its DOM output changes for every user, group, and fallback mention in post rendering. New tests cover the bdi wrapping and trailing punctuation, and existing tests were updated for the new data-mention location, but the full webapp suite, type checks, and end-to-end tests were not run.

Generated by CodeRabbitAI

A message is rendered with dir='auto', so the browser takes its direction
from the first strongly directional character. When a right-to-left message
starts with a mention, that character is the Latin text of the mention, and
the whole message is laid out left-to-right.

Wrap every mention in a <bdi> element. The browser skips <bdi> content when
it resolves dir='auto', so the message takes its direction from its own
text. The <bdi> also isolates the mention, so the @ sign stays in front of a
Latin username inside a right-to-left message.

Fixes mattermost#27911
@mattermost-build

Copy link
Copy Markdown
Contributor

Hello @burrows99,

Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here.

Per the Mattermost Contribution Guide, we need to add you to the list of approved contributors for the Mattermost project.

Please help complete the Mattermost contribution license agreement?
Once you have signed the CLA, please comment with /check-cla and confirm that the CLA check is green.

This is a standard procedure for many open source projects.

Please let us know if you have any questions.

We are very happy to have you join our growing community! If you're not yet a member, please consider joining our Contributors community channel to meet other contributors and discuss new opportunities with the core team.

@mm-cloud-bot mm-cloud-bot added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Nov 15, 2026
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

AtMention now wraps user mentions, group mentions, and fallback content in bdi elements. It renders trailing periods, underscores, and hyphens outside the wrapper. Tests check the wrappers and locate mention attributes on containing elements.

Changes: Mention rendering

Layer / File(s) Summary
Mention wrappers and validation
webapp/channels/src/components/at_mention/at_mention.tsx, webapp/channels/src/components/at_mention/at_mention.test.tsx, webapp/channels/src/components/post_markdown/post_markdown.test.tsx, webapp/channels/src/components/post_view/combined_system_message/last_users.test.tsx
AtMention wraps user mentions, group mentions, and fallback content in bdi elements. It renders trailing ., _, and - characters outside the wrapper. Tests check the wrappers and locate data-mention on the closest matching ancestor.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 7e7a8

The change wraps mentions in bidirectional isolation so right-to-left messages that start with a mention keep their direction. Trailing punctuation stays outside the wrapper. The change is small and localized, with no remaining merge-blocking risk identified in the code.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: right-to-left messages that begin with a mention keep their right-to-left direction. The raw summary shows the change wraps user, group, and fallback mentions in bdi e…
Linked Issues check Passed [#27911] The issue requires that a right-to-left message that starts with a mention keeps its bidirectional text layout. The message text uses dir='auto', so the browser takes the direction from the f…
Out of Scope Changes check Passed The at_mention.tsx changes implement the linked issue. The new trailing-punctuation handling refines the same mention rendering, so it is part of this objective and not an unrelated change. It is also…
Full details: Title check

Explanation

The title names the main change: right-to-left messages that begin with a mention keep their right-to-left direction. The raw summary shows the change wraps user, group, and fallback mentions in bdi elements, which matches this goal. The title is specific, and the ticket reference is a standard prefix.

Full details: Linked Issues check

Explanation

[#27911] The issue requires that a right-to-left message that starts with a mention keeps its bidirectional text layout. The message text uses dir='auto', so the browser takes the direction from the first strong character. A leading Latin mention forces left-to-right. At the reviewed head (7e7a87b), at_mention.tsx wraps every rendered mention in <bdi>: the user branch, the group branch, and the fallback branch (&lt;bdi&gt;{props.children}&lt;/bdi&gt; or the trailing-string variant). The browser skips <bdi> content when it resolves dir='auto', so the mention no longer sets the message direction. The new commit since the previous review keeps trailing periods, underscores, and hyphens outside the <bdi> in the fallback branch. This does not weaken the fix. It only changes where punctuation sits. Visual behaviour in a real browser is shown only by the PR recording. Automated tests cover the wrapper and the trailing-punctuation cases, as reported in the PR description.

Full details: Out of Scope Changes check

Explanation

The at_mention.tsx changes implement the linked issue. The new trailing-punctuation handling refines the same mention rendering, so it is part of this objective and not an unrelated change. It is also described in the PR commit messages. The at_mention.test.tsx, post_markdown.test.tsx, and last_users.test.tsx changes support the wrapper change. Those tests now look up the enclosing [data-mention] element because the mention text is one level deeper. The PR does not alter text alignment or the profile popover behaviour. No unrelated change was found in the reviewed code.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8636d74b-8d38-4764-9942-3cb2bf7b8342
Commits

Reviewing files that changed from the base of the PR and between dd7c8fe and 7e7a87b.

Files selected for processing: 2

  • webapp/channels/src/components/at_mention/at_mention.test.tsx
  • webapp/channels/src/components/at_mention/at_mention.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 36bd2a40-695f-4d50-994b-c247e63016c9
Commits

Reviewing files that changed from the base of the PR and between a69dda3 and dd7c8fe.

Files selected for processing: 4

  • webapp/channels/src/components/at_mention/at_mention.test.tsx
  • webapp/channels/src/components/at_mention/at_mention.tsx
  • webapp/channels/src/components/post_markdown/post_markdown.test.tsx
  • webapp/channels/src/components/post_view/combined_system_message/last_users.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

In a right-to-left message that ends with a mention and a period, the period was inside the <bdi> and showed on the wrong side of the mention. The punctuation now stays outside the <bdi>, for user mentions, group mentions and names that match no user or group.
@burrows99

Copy link
Copy Markdown
Author

/check-cla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Contributor release-note Denotes a PR that will be considered when it comes time to generate release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BiDi/RTL issue in chats when mentioning

3 participants