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());
}
}
}
}
}
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);
}
}
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);
}
}
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);
}
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();
}
}
Aggregations