public class BidiFormatter extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
BidiFormatter.BidiWrappingText
The text used to bidi wrap a string.
|
| Modifier and Type | Method and Description |
|---|---|
static BidiFormatter |
getInstance(Dir contextDir)
Factory for creating an instance of BidiFormatter given the context directionality.
|
String |
knownDirAttr(Dir dir)
Returns "dir=\"ltr\"" or "dir=\"rtl\"", depending on the given directionality, if it is not
NEUTRAL or the same as the context directionality.
|
String |
markAfter(Dir dir,
String str,
boolean isHtml)
Returns a Unicode bidi mark matching the context directionality (LRM or RLM) if either the
overall or the exit directionality of a given string is opposite to the context directionality.
|
String |
spanWrap(Dir dir,
String str,
boolean isHtml)
Formats a string of given directionality for use in HTML output of the context directionality,
so an opposite-directionality string is neither garbled nor garbles its surroundings.
|
BidiFormatter.BidiWrappingText |
spanWrappingText(Dir dir,
String str,
boolean isHtml)
Operates like
spanWrap(Dir, String, boolean) but only returns the text that would be
prepended and appended to str. |
String |
unicodeWrap(Dir dir,
String str,
boolean isHtml)
Formats a string of given directionality for use in plain-text output of the context
directionality, so an opposite-directionality string is neither garbled nor garbles its
surroundings.
|
BidiFormatter.BidiWrappingText |
unicodeWrappingText(Dir dir,
String str,
boolean isHtml)
Operates like
unicodeWrap(Dir, String, boolean) but only returns the text that would
be prepended and appended to str. |
public static BidiFormatter getInstance(Dir contextDir)
spanWrap(com.google.template.soy.data.Dir, java.lang.String, boolean) avoids span wrapping unless there's a reason ('dir' attribute should be appended).contextDir - The context directionality. Must be RTL or LTR.public String knownDirAttr(Dir dir)
dir - Given directionality. Must not be null.public String spanWrap(@Nullable Dir dir, String str, boolean isHtml)
The algorithm: In case the given directionality doesn't match the context directionality, wraps the string with a 'span' element and adds a 'dir' attribute (either 'dir=\"rtl\"' or 'dir=\"ltr\"').
Directionally isolates the string so that it does not garble its surroundings. Currently, this is done by "resetting" the directionality after the string by appending a trailing Unicode bidi mark matching the context directionality (LRM or RLM) when either the overall directionality or the exit directionality of the string is opposite to that of the context. Note that as opposed to the overall directionality, the entry and exit directionalities are determined from the string itself.
If !isHtml, HTML-escapes the string regardless of wrapping.
dir - str's directionality. If null, i.e. unknown, it is estimated.str - The input stringisHtml - Whether str is HTML / HTML-escapedpublic BidiFormatter.BidiWrappingText spanWrappingText(@Nullable Dir dir, String str, boolean isHtml)
spanWrap(Dir, String, boolean) but only returns the text that would be
prepended and appended to str.dir - str's directionality. If null, i.e. unknown, it is estimated.str - The input stringisHtml - Whether str is HTML / HTML-escapedpublic String unicodeWrap(@Nullable Dir dir, String str, boolean isHtml)
spanWrap(com.google.template.soy.data.Dir, java.lang.String, boolean), this makes use of Unicode bidi formatting
characters. In HTML, its *only* valid use is inside of elements that do not allow markup, e.g.
the 'option' and 'title' elements.
The algorithm: In case the given directionality doesn't match the context directionality,
wraps the string with Unicode bidi formatting characters: RLE+str+PDF for RTL text, or
LRE+str+PDF for LTR text.
Directionally isolates the string so that it does not garble its surroundings. Currently, this is done by "resetting" the directionality after the string by appending a trailing Unicode bidi mark matching the context directionality (LRM or RLM) when either the overall directionality or the exit directionality of the string is opposite to that of the context. Note that as opposed to the overall directionality, the entry and exit directionalities are determined from the string itself.
Does *not* do HTML-escaping regardless of the value of isHtml.
dir - str's directionality. If null, i.e. unknown, it is estimated.str - The input stringisHtml - Whether str is HTML / HTML-escapedpublic BidiFormatter.BidiWrappingText unicodeWrappingText(@Nullable Dir dir, String str, boolean isHtml)
unicodeWrap(Dir, String, boolean) but only returns the text that would
be prepended and appended to str.dir - str's directionality. If null, i.e. unknown, it is estimated.str - The input stringisHtml - Whether str is HTML / HTML-escapedpublic String markAfter(@Nullable Dir dir, String str, boolean isHtml)
dir.str - String after which the mark may need to appeardir - str's overall directionality. If null, i.e. unknown, it is estimated.isHtml - Whether str is HTML / HTML-escaped