Permalink
Browse files

Implement experimental feature of inferring safe print tags that don'…

…t need autoescaping.

Revision created by MOE tool push_codebase.
MOE_MIGRATION=57002


git-svn-id: http://closure-templates.googlecode.com/svn/trunk@19 ba6e359a-b4f9-11de-880b-11b4e8d67c75
  • Loading branch information...
kai@google.com
kai@google.com committed Jul 19, 2010
1 parent f718ec9 commit f796c6569dd57ebb397ef7340ef664b761202526
Showing 502 changed files with 3,847 additions and 629 deletions.
@@ -38,18 +38,22 @@
import com.google.template.soy.parseinfo.passes.GenerateParseInfoVisitor;
import com.google.template.soy.parsepasses.CheckOverridesVisitor;
import com.google.template.soy.parsepasses.HandleCssCommandVisitor;
import com.google.template.soy.parsepasses.InferSafePrintNodesVisitor;
import com.google.template.soy.parsepasses.PerformAutoescapeVisitor;
import com.google.template.soy.parsepasses.PrependNamespacesVisitor;
import com.google.template.soy.shared.SoyGeneralOptions;
import com.google.template.soy.shared.SoyGeneralOptions.CssHandlingScheme;
import com.google.template.soy.shared.SoyGeneralOptions.SafePrintTagsInferenceLevel;
import com.google.template.soy.sharedpasses.AssertSyntaxVersionV2Visitor;
import com.google.template.soy.sharedpasses.CheckSoyDocVisitor;
import com.google.template.soy.sharedpasses.ClearSoyDocStringsVisitor;
import com.google.template.soy.sharedpasses.ReplaceStringPrintNodesVisitor;
import com.google.template.soy.sharedpasses.SubstituteGlobalsVisitor;
import com.google.template.soy.soyparse.SoyFileSetParser;
import com.google.template.soy.soytree.PrintNode;
import com.google.template.soy.soytree.SoyFileSetNode;
import com.google.template.soy.soytree.SoytreeUtils;
import com.google.template.soy.soytree.TemplateNode;
import com.google.template.soy.tofu.SoyTofu;
import com.google.template.soy.tofu.internal.BaseTofu.BaseTofuFactory;
import com.google.template.soy.xliffmsgplugin.XliffMsgPlugin;
@@ -212,6 +216,32 @@ public Builder add(CharSequence content, String filePath) {
}
/**
* Sets the level of safe-print-tags inference. For all inferred safe print tags, the compiler
* automatically adds the '|noAutoescape' directive.
* <pre>
* NONE: No inference. '@safe' declarations are ignored.
* SIMPLE: Infers safe print tags based on '@safe' declarations in the current template's
* SoyDoc (same behavior for public and private templates).
* ADVANCED: Infers safe print tags based on '@safe' declarations in SoyDoc of public
* templates. For private templates, infers safe data from the data being passed in all
* the calls to the private template from the rest of the Soy code (in the same compiled
* bundle).
* </pre>
* Also, for inference levels SIMPLE and ADVANCED, the compiler checks calls between templates.
* Specifically, if the callee template's SoyDoc specifies '@safe' declarations, then the data
* being passed in the call must be known to be safe for all of those declared safe data paths.
* Otherwise, a data-safety-mismatch error is reported.
*
* @param inferenceLevel The level of safe-print-tags inference to set.
* @return This builder.
*/
public Builder setSafePrintTagsInferenceLevel(SafePrintTagsInferenceLevel inferenceLevel) {
this.options.setSafePrintTagsInferenceLevel(inferenceLevel);
return this;
}
/**
* Sets the scheme for handling {@code css} commands.
*
@@ -543,6 +573,19 @@ private SoyFileSetNode parse() throws SoySyntaxException {
(new PrependNamespacesVisitor()).exec(soyTree);
(new CheckOverridesVisitor()).exec(soyTree);
(new HandleCssCommandVisitor(options.getCssHandlingScheme())).exec(soyTree);
SafePrintTagsInferenceLevel safePrintTagsInferenceLevel =
options.getSafePrintTagsInferenceLevel();
if (safePrintTagsInferenceLevel != SafePrintTagsInferenceLevel.NONE) {
List<PrintNode> safePrintNodes =
(new InferSafePrintNodesVisitor(safePrintTagsInferenceLevel, true)).exec(soyTree);
if (safePrintTagsInferenceLevel == SafePrintTagsInferenceLevel.ADVANCED) {
System.err.println("Inferred safe print tags (inference level ADVANCED):");
for (PrintNode safePrintNode : safePrintNodes) {
TemplateNode template = safePrintNode.getNearestAncestor(TemplateNode.class);
System.err.println(template.getTemplateName() + ": " + safePrintNode.toSourceString());
}
}
}
performAutoescapeVisitor.exec(soyTree);
if (options.getCompileTimeGlobals() != null) {
@@ -22,6 +22,7 @@
import com.google.template.soy.jssrc.SoyJsSrcOptions;
import com.google.template.soy.jssrc.SoyJsSrcOptions.CodeStyle;
import com.google.template.soy.shared.SoyGeneralOptions.CssHandlingScheme;
import com.google.template.soy.shared.SoyGeneralOptions.SafePrintTagsInferenceLevel;
import com.google.template.soy.xliffmsgplugin.XliffMsgPluginModule;
import org.kohsuke.args4j.Argument;
@@ -68,6 +69,24 @@
" pt_br.")
private String outputPathFormat = "";
@Option(name = "--safePrintTagsInferenceLevel",
usage = "The level of safe-print-tags inference ('none', 'simple', or 'advanced'). For" +
" all inferred safe print tags, the compiler automatically adds the" +
" '|noAutoescape' directive." +
" NONE: No inference. '@safe' declarations are ignored." +
" SIMPLE: Infers safe print tags based on '@safe' declarations in the" +
" current template's SoyDoc (same behavior for public and private templates)." +
" ADVANCED: Infers safe print tags based on '@safe' declarations in SoyDoc" +
" of public templates. For private templates, infers safe data from the data" +
" being passed in all the calls to the private template from the rest of the" +
" Soy code (in the same compiled bundle)." +
" Also, for inference levels SIMPLE and ADVANCED, the compiler checks calls" +
" between templates. Specifically, if the callee template's SoyDoc specifies" +
" '@safe' declarations, then the data being passed in the call must be known" +
" to be safe for all of those declared safe data paths. Otherwise, a" +
" data-safety-mismatch error is reported.")
private String safePrintTagsInferenceLevel = "none";
@Option(name = "--codeStyle",
usage = "The code style to use when generating JS code ('stringbuilder' or 'concat').")
private CodeStyle codeStyle = CodeStyle.STRINGBUILDER;
@@ -200,6 +219,8 @@ private void execMain(String[] args) throws IOException, SoySyntaxException {
sfsBuilder.setCssHandlingScheme(
cssHandlingSchemeUc.equals("GOOG") ?
CssHandlingScheme.BACKEND_SPECIFIC : CssHandlingScheme.valueOf(cssHandlingSchemeUc));
sfsBuilder.setSafePrintTagsInferenceLevel(
SafePrintTagsInferenceLevel.valueOf(safePrintTagsInferenceLevel.toUpperCase()));
if (compileTimeGlobalsFile.length() > 0) {
sfsBuilder.setCompileTimeGlobals(new File(compileTimeGlobalsFile));
}
@@ -97,7 +97,7 @@ public HandleCssCommandVisitor(CssHandlingScheme cssHandlingScheme) {
newNode = new RawTextNode(nodeIdGen.genStringId(), cssNode.getCommandText());
} else if (cssHandlingScheme == CssHandlingScheme.REFERENCE) {
PrintNode newPrintNode = new PrintNode(
nodeIdGen.genStringId(), cssNode.getCommandText() + "|noAutoescape",
nodeIdGen.genStringId(), false, cssNode.getCommandText() + "|noAutoescape",
cssNode.getCommandText());
PrintDirectiveNode newPrintDirectiveNode =
new PrintDirectiveNode(nodeIdGen.genStringId(), "|noAutoescape", "");
Oops, something went wrong.

0 comments on commit f796c65

Please sign in to comment.