Bidirectional Text (BiDi)
Processing of text in which left-to-right (LTR) and right-to-left (RTL) text are mixed. It is needed for multilingual text that includes Arabic or Hebrew.
Bidirectional text (BiDi: Bidirectional Text) is the technology for handling situations where languages written from left to right (LTR: Left-to-Right) and languages written from right to left (RTL: Right-to-Left) are mixed within the same text. Writing systems written from right to left include the Arabic script, the Hebrew script, the Thaana script (Dhivehi), and the Syriac script, and the Arabic script is used not only for Arabic but also for Persian and Urdu. For websites that provide global services, BiDi support is an unavoidable challenge.
The Unicode Bidirectional Algorithm (UBA) automatically determines the directionality of each character in a text and decides the correct display order. The specification is defined in Unicode Standard Annex #9, "Unicode Bidirectional Algorithm." Each character is assigned a directional type. These types are divided into "strong" types (L for Latin letters, R for Hebrew letters, and AL for Arabic, Thaana, and Syriac letters), "weak" types (European digits, Arabic digits, and number separators), and "neutral" types (spaces and other symbols). Because digits are written from left to right even in RTL writing systems, Arabic and Hebrew text is inherently bidirectional.
Because weak and neutral characters are handled differently depending on the surrounding context, the appearance breaks down where digits and symbols are mixed. A separator is treated as part of a number only when it is sandwiched between digits on both sides; otherwise it becomes a neutral character and moves position, pulled by the surrounding RTL text. If you write "123, 456, 789" in RTL text, the digits within each number stay in left-to-right order, but the three numbers are arranged in right-to-left order. Each comma attaches to the left side of its number, so the text is displayed as "789 ,456 ,123". The + of a country code placed at the start of a phone number is also not sandwiched between digits, so it becomes a neutral character and ends up on the opposite side of the number on screen. This behavior is the reason why placing a string containing digits as-is inside RTL text is dangerous.
In HTML, you specify the direction explicitly with the dir="rtl" attribute and force the order with the <bdo> (Bidirectional Override) element. For strings whose direction is not known in advance, such as names or search terms entered by users, the standard practice is to wrap them in a <bdi> element or add dir="auto", letting the direction be decided by the first strong-direction character that appears. If you do not wrap them, punctuation and parentheses right after an RTL name are pulled toward the name, and the order of the whole sentence breaks down. The CSS direction property and logical properties (margin-inline-start, padding-inline-end, and so on) are also important for BiDi support. By using logical properties instead of physical properties (margin-left), you can automatically support switching between LTR/RTL.
In situations where markup cannot be used (plain-text notifications, CSV, commit messages, and so on), the same control is possible with Unicode directional control characters. The isolating control characters are U+2066 LRI (equivalent to dir="ltr"), U+2067 RLI (equivalent to dir="rtl"), and U+2068 FSI (equivalent to dir="auto"), and all of them are closed with U+2069 PDI. The Unicode Standard recommends using the isolating characters rather than the older embedding characters U+202A LRE / U+202B RLE (closed with U+202C PDF). However, because control characters do not cross paragraph boundaries, the default direction of the whole document must be specified in the markup. The rule of thumb is simple: if markup is available, use attributes; if not, use control characters.
BiDi text also raises security concerns. The Trojan Source attack reported in 2021 demonstrated a technique that abuses Unicode directional control characters (RLO, LRI, and so on) to make the appearance of source code differ from its actual execution order. Override control characters (U+202D LRO, U+202E RLO) and isolating control characters are all zero-width and do not appear on screen, so if you trust only the appearance in a review, you will not notice them. Major code editors and diff viewers have features that make these invisible characters visible or display warnings. In systems that accept source code or file names, it is safe to add a check that mechanically detects the inclusion of directional control characters.
In character counting, the visible character count and the internal character count of BiDi text may not match. Directional control characters (U+200E LRM, U+200F RLM, the isolating control characters U+2066 to U+2069, and so on) are zero-width and not displayed on screen, but they are counted as characters. Isolating control characters come as one opening and one closing, 2 characters per pair, so wrapping 5 names inserted in a sentence adds 10 characters by that alone. If the posting limit is 140 characters, it can happen that you reach the limit even though you have visibly written only 130 characters. As a rule of thumb, the practical compromise is to count characters with directional control characters removed before checking the limit, and to keep them as they are for saving and display. Removing them breaks the display order, so avoid stripping them uniformly.