Search in sources :

Example 1 with SoyString

use of com.google.template.soy.data.restricted.SoyString 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 2 with SoyString

use of com.google.template.soy.data.restricted.SoyString in project closure-templates by google.

the class DetachableContentProvider method getResolvedValue.

private SoyString getResolvedValue() {
    SoyString local = resolvedValue;
    if (local == null) {
        if (buffer != null) {
            String string = buffer.toString();
            // when it has to by making sure that renderAndResolve is used for all printing usecases
            if (contentKind != null) {
                local = UnsafeSanitizedContentOrdainer.ordainAsSafe(string, contentKind);
            } else {
                local = StringData.forValue(string);
            }
            resolvedValue = local;
        } else {
            throw new AssertionError("getResolvedValue() should only be called if the value isDone.");
        }
    }
    return local;
}
Also used : SoyString(com.google.template.soy.data.restricted.SoyString) SoyString(com.google.template.soy.data.restricted.SoyString)

Example 3 with SoyString

use of com.google.template.soy.data.restricted.SoyString in project closure-templates by google.

the class StrIndexOfFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    SoyValue arg1 = args.get(1);
    Preconditions.checkArgument(arg0 instanceof SoyString, "First argument to strIndexOf() function is not StringData or SanitizedContent: %s", arg0);
    Preconditions.checkArgument(arg1 instanceof SoyString, "Second argument to strIndexOf() function is not StringData or SanitizedContent: %s", arg1);
    String strArg0 = arg0.coerceToString();
    String strArg1 = arg1.coerceToString();
    return IntegerData.forValue(strArg0.indexOf(strArg1));
}
Also used : SoyString(com.google.template.soy.data.restricted.SoyString) SoyString(com.google.template.soy.data.restricted.SoyString) SoyValue(com.google.template.soy.data.SoyValue)

Example 4 with SoyString

use of com.google.template.soy.data.restricted.SoyString in project closure-templates by google.

the class StrLenFunction method computeForJava.

@Override
public SoyValue computeForJava(List<SoyValue> args) {
    SoyValue arg0 = args.get(0);
    Preconditions.checkArgument(arg0 instanceof SoyString, "First argument to strLen() function is not StringData or SanitizedContent: %s", arg0);
    return IntegerData.forValue(arg0.coerceToString().length());
}
Also used : SoyString(com.google.template.soy.data.restricted.SoyString) SoyValue(com.google.template.soy.data.SoyValue)

Example 5 with SoyString

use of com.google.template.soy.data.restricted.SoyString in project closure-templates by google.

the class DetachableContentProvider method resolve.

@Override
public final SoyValue resolve() {
    checkState(isDone(), "called resolve() before status() returned ready.");
    SoyString local = getResolvedValue();
    checkState(local != TombstoneValue.INSTANCE, "called resolve() after calling renderAndResolve with isLast == true");
    return local;
}
Also used : SoyString(com.google.template.soy.data.restricted.SoyString)

Aggregations

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