Csswg-drafts: [css-writing-modes] direction: auto

Created on 7 Dec 2017  Â·  8Comments  Â·  Source: w3c/csswg-drafts

HTML supports attribute dir= auto | ltr | rtl.
CSS supports direction: ltr | rtl. Why is missing direction: auto?

dir=auto is a huge benefit for bidirectional texts, direction: auto would be a huge benefit too.
Spec https://drafts.csswg.org/css-writing-modes-4/#propdef-direction

Closed Rejected as Wontfix by Editor Discretion Tracked in DoC css-writing-modes-3

Most helpful comment

I may be misunderstanding your first paragraph, @laukstein, so my apologies if I am. :dir(rtl) is not equivalent to [dir=rtl]. All elements in an HTML document have intrinsic/semantic directionality, even if they do not set a dir attribute. [dir=rtl] selects only elements that explicitly override their intrinsic/semantic directionality by setting their dir= attributes. In addition, :dir(rtl) may match even elements that have dir=auto, as long as those elements’ plain-text contents are RTL according to the Unicode bidi algorithm, such as <p dir=auto>لغة عربية</p>. But [dir=rtl] will not match such elements.

Thus, :dir can indeed use autodetection of intrinsic text direction, but that autodetection would always have already occurred at the HTML semantic level, not the CSS styling level. There can logically be no “default direction” in :dir or in CSS at all, for either LTR or RTL. It really does not make conceptual sense at the styling level. Intrinsic text direction must be determined at the markup level, outside of CSS...just like :disabled or :link must rely on definitions of “disabled element” or “link element” outside of CSS: in HTML, DocBook, or whatever flavor of markup.

In addition, it seems that dir=auto can exist while authoring HTML…only because it’s possible for semantic directionality to be undetermined while generating DOM from markup. In contrast, direction: auto would not make sense while styling DOM, because the directionality of every element would have already been determined at that stage. By the time they are rendered, every element already has an intrinsic semantic directionality. <p dir=auto>لغة عربية</p> may have a dir attribute of auto, but it has an intrinsic/semantic directionality of RTL, and that’s why :dir(rtl) would match it. Applying a “direction: auto” CSS rule to that element would not make sense; the element is already intrinsically isolated RTL at the styling stage, so autodetecting directionality again would not make sense. It’s intrinsically RTL, so it should always be styled as RTL.

More broadly, the CSS specification strongly discourages style-sheet authors ever using direction or unicode-bidi CSS properties on HTML documents, ever. Users of conformant user agents expect an element’s styled directionality to always match its intrinsic/semantic directionality. Authors who write HTML documents are expected to never use any method of specifying bidi other than the universally standardized dir=, <bdo>, and <bdi>. The rationale is that semantic directionality is strongly coupled to the intrinsic meaning of text, and that semantic directionality should be universally preserved in its rendering, whether or not any particular stylesheet is applied to the document.

(Note that the dir=auto is also discouraged in general: “The heuristic used by this state is very crude (it just looks at the first character with a strong directionality, in a manner analogous to the Paragraph Level determination in the bidirectional algorithm). Authors are urged to only use this value as a last resort when the direction of the text is truly unknown and no better server-side heuristic can be applied.”)

At least, I think that’s what the specifications’ authors meant. I’m not an expert.

Having said all that, if you really wanted to autodetect an HTML document’s text direction during the styling stage—while ignoring the intrinsic semantic directionality determined by the standard markup—then unicode-bidi: plaintext might work for you. But this puts your HTML document at risk for being rendered by user agents as nonsense, whenever your stylesheet fails to load or whenever CSS is not supported at all.

All 8 comments

I assume this is partly because people simply shouldn't be using 'direction'. It exists for styling arbitrary XML dialects; in HTML you should always be using the actual dir property, as the direction is an important part of the actual content.

@tabatkins then why not to depreciate/drop CSS direction: ltr | rtl from spec? Or to make it correct to use also in HTML?

It may be preferable to add an informative note that states that the property is intended for styling arbitrary XML, and that in HTML the dir attribute should be used instead. Though, for the sake of styling arbitrary XML, a new auto value for the CSS property may still be appropriate.

Why would there be a problem on using direction in HTML? All other HTML visually interacting things are moved to CSS (except also title tooltip, is proposed on CSS styling too), why to do exception for dir. I think the correct thing would be to make it fully supported also in HTML, including support direction: auto. Your thoughts?

@laukstein It is already the case that semantic directionality in HTML is styled using the direction CSS property in the default user-agent style sheet. This is done using the :dir CSS pseudo-class, which as you know matches DOM elements by their host language’s effective semantic direction. :dir, of course, already matches elements with auto directionality using their automatically detected effective directionality. At this time, the user agent by this time must have already determined whether directionality is effectively LTR or RTL, even for auto-directional elements.

As for why you shouldn’t use the direction CSS property on HTML beyond the user-agent defaults, I’m not an expert, but I guess that would override the default user-agent style rules for bidirectionally and negate much of the value of HTML’s universal standardization of bidi markup.

As you probably already know, the finding of W3C and the Unicode Consortium is that bidirectional text should always use markup in HTML (cf. UTR #20 / “Unicode in XML and other Markup Languages: Bidi Embedding Controls”, “CSS vs. markup for bidi support”, “Inline markup and bidirectional text in HTML”). Overriding directionality is important enough to the meaning of a text that it got universally standardized by Unicode for plain text with U+202A to U+202E. If different plain-text-rendering implementations implemented bidirectionally differently, then that universality of the meaning is lost and the standardization of encoding is compromised.

<bdo> and dir= have to act as universal HTML replacements for the universal plain-text control characters. For an HTML document, it is important that the bidi markup’s meaning is preserved independently and regardless of whatever stylesheet happens to be applied. Otherwise, the document’s textual meaning can change, e.g., simply when a stylesheet fails to load.

TL;DR: I’m not an expert, but direction is already used on dir= by HTML’s default user-agent style sheet…and in HTML only <bdo> and dir= should be universally used to override bidi, just like U+202A to U+202E in plain text—or else HTML documents’ intrinsic textual meanings become volatile with regard to whatever stylesheets are loaded or not.

(Aside: This again does not apply to arbitrary XML, which needs directionality to style whatever bidi markup it uses, just like HTML’s user-agent style sheet does. I don’t know yet whether a directionality: auto would be appropriate for arbitrary XML…Maybe the real issue is that :dir is not well defined for arbitrary XML, but that also has no perfect solutions: coupling :dir to XML with ITS attributes requires blessing a non-core namespace…and creating an xml:dir attribute would have its own compatibility problems…)

:dir(ltr | rtl) doesn't auto detect text direction (if is LTR or RTL writing text) but would be the same as [dir=ltr | rtl] where direction default ltr.

@js-choi I still don't understand why dir=auto in HTML is fine and direction: auto wouldn't be fine? Also why not to use direction in CSS, where it is defined by spec like to apply to [dir=ltr] { direction: ltr; unicode-bidi: isolate; } and [dir=rtl] { direction: rtl; unicode-bidi: isolate; }, and everyone follows this spec (in HTML), see https://drafts.csswg.org/css-writing-modes-3/#bidi-example

I may be misunderstanding your first paragraph, @laukstein, so my apologies if I am. :dir(rtl) is not equivalent to [dir=rtl]. All elements in an HTML document have intrinsic/semantic directionality, even if they do not set a dir attribute. [dir=rtl] selects only elements that explicitly override their intrinsic/semantic directionality by setting their dir= attributes. In addition, :dir(rtl) may match even elements that have dir=auto, as long as those elements’ plain-text contents are RTL according to the Unicode bidi algorithm, such as <p dir=auto>لغة عربية</p>. But [dir=rtl] will not match such elements.

Thus, :dir can indeed use autodetection of intrinsic text direction, but that autodetection would always have already occurred at the HTML semantic level, not the CSS styling level. There can logically be no “default direction” in :dir or in CSS at all, for either LTR or RTL. It really does not make conceptual sense at the styling level. Intrinsic text direction must be determined at the markup level, outside of CSS...just like :disabled or :link must rely on definitions of “disabled element” or “link element” outside of CSS: in HTML, DocBook, or whatever flavor of markup.

In addition, it seems that dir=auto can exist while authoring HTML…only because it’s possible for semantic directionality to be undetermined while generating DOM from markup. In contrast, direction: auto would not make sense while styling DOM, because the directionality of every element would have already been determined at that stage. By the time they are rendered, every element already has an intrinsic semantic directionality. <p dir=auto>لغة عربية</p> may have a dir attribute of auto, but it has an intrinsic/semantic directionality of RTL, and that’s why :dir(rtl) would match it. Applying a “direction: auto” CSS rule to that element would not make sense; the element is already intrinsically isolated RTL at the styling stage, so autodetecting directionality again would not make sense. It’s intrinsically RTL, so it should always be styled as RTL.

More broadly, the CSS specification strongly discourages style-sheet authors ever using direction or unicode-bidi CSS properties on HTML documents, ever. Users of conformant user agents expect an element’s styled directionality to always match its intrinsic/semantic directionality. Authors who write HTML documents are expected to never use any method of specifying bidi other than the universally standardized dir=, <bdo>, and <bdi>. The rationale is that semantic directionality is strongly coupled to the intrinsic meaning of text, and that semantic directionality should be universally preserved in its rendering, whether or not any particular stylesheet is applied to the document.

(Note that the dir=auto is also discouraged in general: “The heuristic used by this state is very crude (it just looks at the first character with a strong directionality, in a manner analogous to the Paragraph Level determination in the bidirectional algorithm). Authors are urged to only use this value as a last resort when the direction of the text is truly unknown and no better server-side heuristic can be applied.”)

At least, I think that’s what the specifications’ authors meant. I’m not an expert.

Having said all that, if you really wanted to autodetect an HTML document’s text direction during the styling stage—while ignoring the intrinsic semantic directionality determined by the standard markup—then unicode-bidi: plaintext might work for you. But this puts your HTML document at risk for being rendered by user agents as nonsense, whenever your stylesheet fails to load or whenever CSS is not supported at all.

Thank you @js-choi for perfectly and thoroughly explaining why the spec works the way it does. :) This was a deliberate decision made in consultation with the HTMLWG, the i18nWG, and the bidi expert Aharon Lanin (who spearheaded the major update of bidi controls in HTML, CSS, and Unicode that gave us dir=auto and bidi isolation). Marking as wontfix per previous discussions.

Was this page helpful?
0 / 5 - 0 ratings