Add dir=auto on Bidi code elements #3935
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
For RTL renderings only, add
dir="auto"
to monospace elements, effectively encoding a<bdi>
within the<span>
to ensure that the browser will look at the first strongly typed character in the element and work out from that what the base direction of the element should be.Motivation and Context
When reading an HTML document in an RTL language such as Arabic or Hebrew, code blocks (typically written in Latin script) are read LTR. This makes the rendered HTML document inherently bidirectional. HTML browsers usually make a good attempt at rendering bidirectional text correctly by splitting characters into three types, LTR, RTL and neutral. The problem is that when a monospace code block begins with a neutral character (like punctuation or a number) the directionality of the character is inherited from the parent element.
Take the following
<filepath>
for example:It currently renders in HTML (without a
dir
attribute) as:Which causes the first neutral character in the
<filepath>
- the leading/
to be incorrectly rendered RTL at the end of the line:With
dir="auto"
, it should read the code block as all LTR latin text and render asWhich causes the browser to consider the first strong character it finds and then render the whole
<span>
correctly as left-to- right with the leading slash at the beginning. Note that had the<span>
started with RTL text it would have rendered correctly as well, sincedir="auto"
is only a processing instruction not a forced direction likedir="ltr"
How Has This Been Tested?
Local testing, creating documents with a
default.language=ar
Type of Changes
Documentation and Compatibility
None