use of com.google.template.soy.internal.i18n.BidiFormatter in project closure-templates by google.
the class BidiDirectivesRuntime method bidiSpanWrap.
public static String bidiSpanWrap(BidiGlobalDir dir, SoyValue value) {
Dir valueDir = null;
if (value instanceof SanitizedContent) {
valueDir = ((SanitizedContent) value).getContentDirection();
}
BidiFormatter bidiFormatter = BidiFormatter.getInstance(dir.toDir());
// We always treat the value as HTML, because span-wrapping is only useful when its output will
// be treated as HTML (without escaping), and because |bidiSpanWrap is not itself specified to
// do HTML escaping in Soy. (Both explicit and automatic HTML escaping, if any, is done before
// calling |bidiSpanWrap because BidiSpanWrapDirective implements SanitizedContentOperator,
// but this does not mean that the input has to be HTML SanitizedContent. In legacy usage, a
// string that is not SanitizedContent is often printed in an autoescape="false" template or by
// a print with a |noAutoescape, in which case our input is just SoyData.) If the output will be
// treated as HTML, the input had better be safe HTML/HTML-escaped (even if it isn't HTML
// SanitizedData), or we have an XSS opportunity and a much bigger problem than bidi garbling.
String wrappedValue = bidiFormatter.spanWrap(valueDir, value.coerceToString(), true);
// produce actual SanitizedContent.
return wrappedValue;
}
use of com.google.template.soy.internal.i18n.BidiFormatter in project closure-templates by google.
the class BidiDirectivesRuntime method bidiUnicodeWrap.
public static SoyString bidiUnicodeWrap(BidiGlobalDir dir, SoyValue value) {
// normalize null between tofu and jbcsrc
value = value == null ? NullData.INSTANCE : value;
ContentKind valueKind = null;
Dir valueDir = null;
if (value instanceof SanitizedContent) {
SanitizedContent sanitizedContent = (SanitizedContent) value;
valueKind = sanitizedContent.getContentKind();
valueDir = sanitizedContent.getContentDirection();
}
BidiFormatter bidiFormatter = BidiFormatter.getInstance(dir.toDir());
// We treat the value as HTML if and only if it says it's HTML, even though in legacy usage, we
// sometimes have an HTML string (not SanitizedContent) that is passed to an autoescape="false"
// template or a {print $foo |noAutoescape}, with the output going into an HTML context without
// escaping. We simply have no way of knowing if this is what is happening when we get
// non-SanitizedContent input, and most of the time it isn't.
boolean isHtml = valueKind == ContentKind.HTML;
String wrappedValue = bidiFormatter.unicodeWrap(valueDir, value.coerceToString(), isHtml);
// Unicode-wrapping safe HTML.
if (valueKind == ContentKind.TEXT || valueKind == ContentKind.HTML) {
// run.
return UnsafeSanitizedContentOrdainer.ordainAsSafe(wrappedValue, valueKind, dir.toDir());
}
// TEXT, or HTML.
if (valueKind != null) {
return StringData.forValue(wrappedValue);
}
// The input was not SanitizedContent, so our output isn't SanitizedContent either.
return StringData.forValue(wrappedValue);
}
use of com.google.template.soy.internal.i18n.BidiFormatter in project closure-templates by google.
the class BidiFunctionsRuntime method bidiDirAttr.
public static String bidiDirAttr(BidiGlobalDir dir, SoyValue value, boolean isHtml) {
Dir valueDir = null;
boolean isHtmlForValueDirEstimation = false;
if (value instanceof SanitizedContent) {
SanitizedContent sanitizedContent = (SanitizedContent) value;
valueDir = sanitizedContent.getContentDirection();
if (valueDir == null) {
isHtmlForValueDirEstimation = sanitizedContent.getContentKind() == ContentKind.HTML;
}
}
if (valueDir == null) {
isHtmlForValueDirEstimation = isHtmlForValueDirEstimation || isHtml;
valueDir = BidiUtils.estimateDirection(value.coerceToString(), isHtmlForValueDirEstimation);
}
BidiFormatter bidiFormatter = BidiFormatter.getInstance(dir.toDir());
return bidiFormatter.knownDirAttr(valueDir);
}
Aggregations