Search in sources :

Example 6 with SanitizedContentKind

use of com.google.template.soy.base.internal.SanitizedContentKind in project closure-templates by google.

the class CheckDelegatesVisitor method checkTemplates.

/**
 * Performs checks that only involve templates (uses templateRegistry only).
 */
private void checkTemplates() {
    DelTemplateSelector<TemplateDelegateNode> selector = templateRegistry.getDelTemplateSelector();
    // content kind, and strict html mode.
    for (Collection<TemplateDelegateNode> delTemplateGroup : selector.delTemplateNameToValues().asMap().values()) {
        TemplateDelegateNode firstDelTemplate = null;
        Set<Equivalence.Wrapper<TemplateParam>> firstRequiredParamSet = null;
        SanitizedContentKind firstContentKind = null;
        boolean firstStrictHtml = false;
        // loop over all members of the deltemplate group.
        for (TemplateDelegateNode delTemplate : delTemplateGroup) {
            if (firstDelTemplate == null) {
                // First template encountered.
                firstDelTemplate = delTemplate;
                firstRequiredParamSet = getRequiredParamSet(delTemplate);
                firstContentKind = delTemplate.getContentKind();
                firstStrictHtml = delTemplate.isStrictHtml() && firstContentKind == SanitizedContentKind.HTML;
            } else {
                // Not first template encountered.
                Set<Equivalence.Wrapper<TemplateParam>> currRequiredParamSet = getRequiredParamSet(delTemplate);
                if (!currRequiredParamSet.equals(firstRequiredParamSet)) {
                    errorReporter.report(delTemplate.getSourceLocation(), DELTEMPLATES_WITH_DIFFERENT_PARAM_DECLARATIONS, firstDelTemplate.getDelTemplateName(), firstDelTemplate.getSourceLocation().toString());
                }
                if (delTemplate.getContentKind() != firstContentKind) {
                    // TODO: This is only *truly* a requirement if the strict mode deltemplates are
                    // being called by contextual templates. For a strict-to-strict call, everything
                    // is escaped at runtime at the call sites. You could imagine delegating between
                    // either a plain-text or rich-html template. However, most developers will write
                    // their deltemplates in a parallel manner, and will want to know when the
                    // templates differ. Plus, requiring them all to be the same early-on will allow
                    // future optimizations to avoid the run-time checks, so it's better to start out
                    // as strict as possible and only open up if needed.
                    errorReporter.report(delTemplate.getSourceLocation(), STRICT_DELTEMPLATES_WITH_DIFFERENT_CONTENT_KIND, String.valueOf(firstContentKind), String.valueOf(delTemplate.getContentKind()), firstDelTemplate.getSourceLocation().toString());
                }
                // in this pass.
                if (delTemplate.isStrictHtml() != firstStrictHtml) {
                    errorReporter.report(delTemplate.getSourceLocation(), DELTEMPLATES_WITH_DIFFERENT_STRICT_HTML_MODE, firstDelTemplate.getDelTemplateName(), firstDelTemplate.getSourceLocation().toString());
                }
            }
        }
    }
}
Also used : TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind)

Example 7 with SanitizedContentKind

use of com.google.template.soy.base.internal.SanitizedContentKind in project closure-templates by google.

the class StrictHtmlValidationPass method checkTemplateNode.

private void checkTemplateNode(TemplateNode node) {
    AutoescapeMode autoescapeMode = node.getAutoescapeMode();
    if (autoescapeMode != AutoescapeMode.STRICT && node.isStrictHtml()) {
        errorReporter.report(node.getSourceLocation(), STRICT_HTML_WITHOUT_AUTOESCAPE);
        return;
    }
    // ContentKind is guaranteed to be non-null if AutoescapeMode is strict.
    SanitizedContentKind contentKind = node.getContentKind();
    if (contentKind != SanitizedContentKind.HTML && node.isStrictHtml()) {
        errorReporter.report(node.getSourceLocation(), STRICT_HTML_WITH_NON_HTML);
        return;
    }
    if (node.isStrictHtml()) {
        new HtmlTagVisitor(errorReporter).exec(node);
    }
}
Also used : AutoescapeMode(com.google.template.soy.soytree.AutoescapeMode) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind)

Example 8 with SanitizedContentKind

use of com.google.template.soy.base.internal.SanitizedContentKind in project closure-templates by google.

the class GenIncrementalDomCodeVisitor method visitIfNode.

/**
 * Generates calls in HTML/Attributes content as non-JsExprs, since Incremental DOM instructions
 * are needed and not a JavaScript expression.
 */
@Override
protected void visitIfNode(IfNode node) {
    IncrementalDomCodeBuilder jsCodeBuilder = getJsCodeBuilder();
    SanitizedContentKind currentContentKind = jsCodeBuilder.getContentKind();
    if (!isTextContent(currentContentKind)) {
        super.generateNonExpressionIfNode(node);
    } else {
        super.visitIfNode(node);
    }
}
Also used : SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind)

Example 9 with SanitizedContentKind

use of com.google.template.soy.base.internal.SanitizedContentKind in project closure-templates by google.

the class GenIncrementalDomCodeVisitor method getAttributeValues.

private List<CodeChunk.WithValue> getAttributeValues(HtmlAttributeNode node) {
    if (!node.hasValue()) {
        // the runtime knows to create an attribute.
        return ImmutableList.of(LITERAL_EMPTY_STRING);
    }
    HtmlAttributeValueNode value = (HtmlAttributeValueNode) node.getChild(1);
    if (!isComputableAsJsExprsVisitor.execOnChildren(value)) {
        String outputVar = "html_attribute_" + node.getId();
        getJsCodeBuilder().pushOutputVar(outputVar).setOutputVarInited();
        SanitizedContentKind prev = getJsCodeBuilder().getContentKind();
        getJsCodeBuilder().setContentKind(SanitizedContentKind.TEXT);
        CodeChunk appends = visitChildrenReturningCodeChunk(value);
        getJsCodeBuilder().popOutputVar();
        getJsCodeBuilder().setContentKind(prev);
        return ImmutableList.of(CodeChunk.id(outputVar).withInitialStatements(ImmutableList.<CodeChunk>of(VariableDeclaration.builder(outputVar).setRhs(CodeChunk.LITERAL_EMPTY_STRING).build(), appends)));
    }
    return genJsExprsVisitor.execOnChildren(value);
}
Also used : CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 10 with SanitizedContentKind

use of com.google.template.soy.base.internal.SanitizedContentKind in project closure-templates by google.

the class SoyTypesTest method testAllContentKindsCovered.

@Test
public void testAllContentKindsCovered() {
    Set<SoyType> types = Sets.newIdentityHashSet();
    for (SanitizedContentKind kind : SanitizedContentKind.values()) {
        SoyType typeForContentKind = SanitizedType.getTypeForContentKind(kind);
        if (kind == SanitizedContentKind.TEXT) {
            assertThat(typeForContentKind).isEqualTo(STRING_TYPE);
        } else {
            assertThat(((SanitizedType) typeForContentKind).getContentKind()).isEqualTo(kind);
        }
        // ensure there is a unique SoyType for every ContentKind
        assertThat(types.add(typeForContentKind)).isTrue();
    }
}
Also used : SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind) Test(org.junit.Test)

Aggregations

SanitizedContentKind (com.google.template.soy.base.internal.SanitizedContentKind)12 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)4 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)2 SourceLocation (com.google.template.soy.base.SourceLocation)1 Identifier (com.google.template.soy.base.internal.Identifier)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)1 Statement.returnExpression (com.google.template.soy.jbcsrc.restricted.Statement.returnExpression)1 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)1 AutoescapeMode (com.google.template.soy.soytree.AutoescapeMode)1 CallBasicNode (com.google.template.soy.soytree.CallBasicNode)1 CallDelegateNode (com.google.template.soy.soytree.CallDelegateNode)1 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)1 CallParamNode (com.google.template.soy.soytree.CallParamNode)1 HtmlAttributeValueNode (com.google.template.soy.soytree.HtmlAttributeValueNode)1 RawTextNode (com.google.template.soy.soytree.RawTextNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1 LinkedHashSet (java.util.LinkedHashSet)1 CheckReturnValue (javax.annotation.CheckReturnValue)1