dir

command
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Command dir adds dir="rtl" to the elements whose text reads right to left.

<p>مرحبا بالعالم</p>  ->  <p dir="rtl">مرحبا بالعالم</p>

The mechanism is the one in examples/gip/lang: the attribute belongs on the start tag, the evidence is the element's text, so the document is read twice and the passes are joined by lolhtml.SourceLocation. What is different is the rule inside, and the difference is the point of this program.

Direction is not decided by a majority. The Unicode Bidi Algorithm decides a paragraph's direction from its first strong character - the first letter that is itself left-to-right or right-to-left - and everything before it is skipped: digits, punctuation, quotation marks, spaces. So

<p>مرحبا this paragraph is mostly English words after that first word</p>

is a right-to-left paragraph, and a program that counted characters would call it English and be wrong about how a browser lays it out. The rule cuts the other way too, which is the part worth being honest about: a mostly-Arabic paragraph that happens to start with a Latin word is left to right, and this program leaves it alone rather than overruling the algorithm it is implementing.

Where the evidence is weak, say less. An element with no strong character at all - a number, a date, a row of punctuation - gets nothing: its direction is inherited and inheritance is the right answer. And -auto writes dir="auto" instead of dir="rtl", which is the same rule applied by the browser at render time rather than by this program at build time. For text that can change after the page is built - a template slot, a comment field - auto is the better answer, because the rule will be applied to whatever the text turns out to be.

What this program does not do: an inline run of right-to-left text inside a left-to-right paragraph is not an element-level question, and dir on a span is not the fix for it. The fix is <bdi>, which isolates the run so the surrounding text's direction is not disturbed by it, and deciding where a run begins and ends is a different program.

The elements it stays out of: what lolhtml.IsRawText names, code, kbd, samp, var and pre, anything inside a <bdo> - whose whole purpose is to override the algorithm, so a program implementing the algorithm has no business there - and any element that already says dir, in either direction. dir is inherited, so an element inside one that already says rtl gets nothing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL