Search in sources :

Example 1 with BidiFormatter

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;
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) BidiFormatter(com.google.template.soy.internal.i18n.BidiFormatter) SoyString(com.google.template.soy.data.restricted.SoyString) BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) Dir(com.google.template.soy.data.Dir)

Example 2 with BidiFormatter

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);
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) BidiFormatter(com.google.template.soy.internal.i18n.BidiFormatter) SoyString(com.google.template.soy.data.restricted.SoyString) BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) Dir(com.google.template.soy.data.Dir) ContentKind(com.google.template.soy.data.SanitizedContent.ContentKind)

Example 3 with BidiFormatter

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);
}
Also used : SanitizedContent(com.google.template.soy.data.SanitizedContent) BidiFormatter(com.google.template.soy.internal.i18n.BidiFormatter) BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) Dir(com.google.template.soy.data.Dir)

Aggregations

Dir (com.google.template.soy.data.Dir)3 SanitizedContent (com.google.template.soy.data.SanitizedContent)3 BidiFormatter (com.google.template.soy.internal.i18n.BidiFormatter)3 BidiGlobalDir (com.google.template.soy.internal.i18n.BidiGlobalDir)3 SoyString (com.google.template.soy.data.restricted.SoyString)2 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1