Search in sources :

Example 1 with BidiGlobalDir

use of com.google.template.soy.internal.i18n.BidiGlobalDir 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 BidiGlobalDir

use of com.google.template.soy.internal.i18n.BidiGlobalDir 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 BidiGlobalDir

use of com.google.template.soy.internal.i18n.BidiGlobalDir in project closure-templates by google.

the class JsSrcMain method genJsSrc.

/**
 * Generates JS source code given a Soy parse tree, an options object, and an optional bundle of
 * translated messages.
 *
 * @param soyTree The Soy parse tree to generate JS source code for.
 * @param templateRegistry The template registry that contains all the template information.
 * @param jsSrcOptions The compilation options relevant to this backend.
 * @param msgBundle The bundle of translated messages, or null to use the messages from the Soy
 *     source.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @return A list of strings where each string represents the JS source code that belongs in one
 *     JS file. The generated JS files correspond one-to-one to the original Soy source files.
 */
public List<String> genJsSrc(SoyFileSetNode soyTree, TemplateRegistry templateRegistry, SoyJsSrcOptions jsSrcOptions, @Nullable SoyMsgBundle msgBundle, ErrorReporter errorReporter) {
    // VeLogInstrumentationVisitor add html attributes for {velog} commands and also run desugaring
    // pass since code generator does not understand html nodes (yet).
    new VeLogInstrumentationVisitor(templateRegistry).exec(soyTree);
    try (GuiceSimpleScope.InScope inScope = apiCallScope.enter()) {
        // Seed the scoped parameters.
        BidiGlobalDir bidiGlobalDir = SoyBidiUtils.decodeBidiGlobalDirFromJsOptions(jsSrcOptions.getBidiGlobalDir(), jsSrcOptions.getUseGoogIsRtlForBidiGlobalDir());
        ApiCallScopeUtils.seedSharedParams(inScope, msgBundle, bidiGlobalDir);
        // Replace MsgNodes.
        if (jsSrcOptions.shouldGenerateGoogMsgDefs()) {
            Preconditions.checkState(bidiGlobalDir != null, "If enabling shouldGenerateGoogMsgDefs, must also set bidi global directionality.");
        } else {
            Preconditions.checkState(bidiGlobalDir == null || bidiGlobalDir.isStaticValue(), "If using bidiGlobalIsRtlCodeSnippet, must also enable shouldGenerateGoogMsgDefs.");
            new InsertMsgsVisitor(msgBundle, errorReporter).insertMsgs(soyTree);
        }
        // Combine raw text nodes before codegen.
        new CombineConsecutiveRawTextNodesPass().run(soyTree);
        return createVisitor(jsSrcOptions, typeRegistry).gen(soyTree, templateRegistry, errorReporter);
    }
}
Also used : BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) GuiceSimpleScope(com.google.template.soy.shared.internal.GuiceSimpleScope) InsertMsgsVisitor(com.google.template.soy.msgs.internal.InsertMsgsVisitor) CombineConsecutiveRawTextNodesPass(com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass)

Example 4 with BidiGlobalDir

use of com.google.template.soy.internal.i18n.BidiGlobalDir in project closure-templates by google.

the class PySrcMain method genPySrc.

/**
 * Generates Python source code given a Soy parse tree and an options object.
 *
 * @param soyTree The Soy parse tree to generate Python source code for.
 * @param pySrcOptions The compilation options relevant to this backend.
 * @param currentManifest The namespace manifest for current sources.
 * @param errorReporter The Soy error reporter that collects errors during code generation.
 * @return A list of strings where each string represents the Python source code that belongs in
 *     one Python file. The generated Python files correspond one-to-one to the original Soy
 *     source files.
 */
private List<String> genPySrc(SoyFileSetNode soyTree, SoyPySrcOptions pySrcOptions, ImmutableMap<String, String> currentManifest, ErrorReporter errorReporter) {
    try (GuiceSimpleScope.InScope inScope = apiCallScope.enter()) {
        // Seed the scoped parameters, for plugins
        BidiGlobalDir bidiGlobalDir = SoyBidiUtils.decodeBidiGlobalDirFromPyOptions(pySrcOptions.getBidiIsRtlFn());
        ApiCallScopeUtils.seedSharedParams(inScope, null, bidiGlobalDir);
        return createVisitor(pySrcOptions, currentManifest).gen(soyTree, errorReporter);
    }
}
Also used : BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) GuiceSimpleScope(com.google.template.soy.shared.internal.GuiceSimpleScope)

Example 5 with BidiGlobalDir

use of com.google.template.soy.internal.i18n.BidiGlobalDir in project closure-templates by google.

the class SharedModule method configure.

@Override
protected void configure() {
    // Install the core directives.
    install(new CoreDirectivesModule());
    // Install default directive and function modules.
    install(new BasicDirectivesModule());
    install(new BidiDirectivesModule());
    install(new BasicFunctionsModule());
    install(new BidiFunctionsModule());
    install(new I18nDirectivesModule());
    // Create the API call scope.
    GuiceSimpleScope apiCallScope = new GuiceSimpleScope();
    bindScope(ApiCallScope.class, apiCallScope);
    // Make the API call scope instance injectable.
    bind(GuiceSimpleScope.class).annotatedWith(ApiCall.class).toInstance(apiCallScope);
    // Bind unscoped providers for parameters in ApiCallScope (these throw exceptions).
    bind(String.class).annotatedWith(LocaleString.class).toProvider(GuiceSimpleScope.<String>getUnscopedProvider()).in(ApiCallScope.class);
    bind(BidiGlobalDir.class).toProvider(GuiceSimpleScope.<BidiGlobalDir>getUnscopedProvider()).in(ApiCallScope.class);
}
Also used : I18nDirectivesModule(com.google.template.soy.i18ndirectives.I18nDirectivesModule) CoreDirectivesModule(com.google.template.soy.coredirectives.CoreDirectivesModule) BidiFunctionsModule(com.google.template.soy.bidifunctions.BidiFunctionsModule) ApiCall(com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.ApiCall) BidiDirectivesModule(com.google.template.soy.bididirectives.BidiDirectivesModule) BasicFunctionsModule(com.google.template.soy.basicfunctions.BasicFunctionsModule) BidiGlobalDir(com.google.template.soy.internal.i18n.BidiGlobalDir) BasicDirectivesModule(com.google.template.soy.basicdirectives.BasicDirectivesModule) LocaleString(com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString)

Aggregations

BidiGlobalDir (com.google.template.soy.internal.i18n.BidiGlobalDir)8 Dir (com.google.template.soy.data.Dir)4 SanitizedContent (com.google.template.soy.data.SanitizedContent)4 BidiFormatter (com.google.template.soy.internal.i18n.BidiFormatter)3 GuiceSimpleScope (com.google.template.soy.shared.internal.GuiceSimpleScope)3 SoyString (com.google.template.soy.data.restricted.SoyString)2 CombineConsecutiveRawTextNodesPass (com.google.template.soy.passes.CombineConsecutiveRawTextNodesPass)2 BasicDirectivesModule (com.google.template.soy.basicdirectives.BasicDirectivesModule)1 BasicFunctionsModule (com.google.template.soy.basicfunctions.BasicFunctionsModule)1 BidiDirectivesModule (com.google.template.soy.bididirectives.BidiDirectivesModule)1 BidiFunctionsModule (com.google.template.soy.bidifunctions.BidiFunctionsModule)1 CoreDirectivesModule (com.google.template.soy.coredirectives.CoreDirectivesModule)1 ContentKind (com.google.template.soy.data.SanitizedContent.ContentKind)1 I18nDirectivesModule (com.google.template.soy.i18ndirectives.I18nDirectivesModule)1 SoyJsSrcOptions (com.google.template.soy.jssrc.SoyJsSrcOptions)1 InsertMsgsVisitor (com.google.template.soy.msgs.internal.InsertMsgsVisitor)1 ApiCall (com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.ApiCall)1 LocaleString (com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString)1