Why is a trailing punctuation mark rendered at the start with direction:rtl?

后端 未结 4 1793
有刺的猬
有刺的猬 2020-11-29 09:27

This is more a sort of curiosity. While working on a multilingual web application I noticed that certain characters like punctuation marks (!?.;,) at the end of a block elem

4条回答
  •  暖寄归人
    2020-11-29 09:37

    The accepted answer https://stackoverflow.com/a/20799360/477420 works if you can control markup/CSS of the value, if you have no control over HTML following approach could work.

    If you don't know if page will be rendered RTL or LTR but some text is definitely LTR (i.e. English-only) you can wrap the value with LRE/PDF marks to signify that is LTR region. Text will be rendered LTR irrespective of page's LTR or RTL direction.

    This works when you have some code that tries to render text without ability to change markup of how exactly it will show up on the page. I.e. you rendering value for "song tile" or "company name" field in some nested child component (or server side) without ability to control surrounding HTML elements.

    One drawback of this and similar approaches (like LRM proposal in this question) with adding marks to text is copy-paste of such value from the resulting HTML page will generally preserve the marks but they are not visible/zero width. While for most cases it is fine consider if that is a problem for you.

    Approximate sample code (some companies have "Inc." at the end which will end up with dot at the beginning when rendered as-is on RTL page):

     // comanyName = "Alphabet Inc." - really likes dot at the end including RTL
     if(stringIsDefinitelyAscii(companyName))
     {
         companyName = "\u202A" + companyName + "\u202C"
     }
     return companyName;
    

    Details on LRE/PDF symbols can be found in https://unicode.org/reports/tr9/#Explicit_Directional_Embeddings:

    LRE U+202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.

    PDF U+202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.

    Some approaches to figure out if string has RTL characters can be found in How to detect whether a character belongs to a Right To Left language?, JavaScript: how to check if character is RTL?, How to detect if a string contains any Right-to-Left character?.

提交回复
热议问题