use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class SoyFileSet method pruneTranslatedMsgs.
/**
* Prunes messages from a given message bundle, keeping only messages used in this Soy file set.
*
* <p>Important: Do not use directly. This is subject to change and your code will break.
*
* <p>Note: This method memoizes intermediate results to improve efficiency in the case that it is
* called multiple times (which is a common case). Thus, this method will not work correctly if
* the underlying Soy files are modified between calls to this method.
*
* @param origTransMsgBundle The message bundle to prune.
* @return The pruned message bundle.
* @throws SoyCompilationException If compilation fails.
*/
public SoyMsgBundle pruneTranslatedMsgs(SoyMsgBundle origTransMsgBundle) {
resetErrorReporter();
if (memoizedExtractedMsgIdsForPruning == null) {
ParseResult result = parse(passManagerBuilder(SyntaxVersion.V1_0).allowUnknownGlobals().disableAllTypeChecking(), // can't resolve strict types
SoyTypeRegistry.DEFAULT_UNKNOWN, new PluginResolver(PluginResolver.Mode.ALLOW_UNDEFINED, printDirectives, soyFunctionMap, errorReporter));
throwIfErrorsPresent();
SoyFileSetNode soyTree = result.fileSet();
TemplateRegistry registry = result.registry();
List<TemplateNode> allPublicTemplates = Lists.newArrayList();
for (SoyFileNode soyFile : soyTree.getChildren()) {
for (TemplateNode template : soyFile.getChildren()) {
if (template.getVisibility() == Visibility.PUBLIC) {
allPublicTemplates.add(template);
}
}
}
Map<TemplateNode, TransitiveDepTemplatesInfo> depsInfoMap = new FindTransitiveDepTemplatesVisitor(registry).execOnMultipleTemplates(allPublicTemplates);
TransitiveDepTemplatesInfo mergedDepsInfo = TransitiveDepTemplatesInfo.merge(depsInfoMap.values());
SoyMsgBundle extractedMsgBundle = new ExtractMsgsVisitor().execOnMultipleNodes(mergedDepsInfo.depTemplateSet);
ImmutableSet.Builder<Long> extractedMsgIdsBuilder = ImmutableSet.builder();
for (SoyMsg extractedMsg : extractedMsgBundle) {
extractedMsgIdsBuilder.add(extractedMsg.getId());
}
throwIfErrorsPresent();
memoizedExtractedMsgIdsForPruning = extractedMsgIdsBuilder.build();
}
// ------ Prune. ------
ImmutableList.Builder<SoyMsg> prunedTransMsgsBuilder = ImmutableList.builder();
for (SoyMsg transMsg : origTransMsgBundle) {
if (memoizedExtractedMsgIdsForPruning.contains(transMsg.getId())) {
prunedTransMsgsBuilder.add(transMsg);
}
}
throwIfErrorsPresent();
return new SoyMsgBundleImpl(origTransMsgBundle.getLocaleString(), prunedTransMsgsBuilder.build());
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class HtmlRewritePassTest method testHtmlCommentWithPrintNode.
@Test
public void testHtmlCommentWithPrintNode() {
ErrorReporter errorReporter = ErrorReporter.createForTest();
TemplateNode node;
// Print node.
node = runPass("<!--{$foo}-->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", " PRINT_NODE", ""));
assertThatSourceString(node).isEqualTo("<!--{$foo}-->");
// Mixed print node and raw text node.
node = runPass("<!--{$foo}hello{$bar}-->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", " PRINT_NODE", " RAW_TEXT_NODE", " PRINT_NODE", ""));
assertThatSourceString(node).isEqualTo("<!--{$foo}hello{$bar}-->");
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class HtmlRewritePassTest method testDynamicAttributeValue.
@Test
public void testDynamicAttributeValue() {
TemplateNode node = runPass("{let $t : 'x' /}<div a={$t}>content</div>");
assertThatSourceString(node).isEqualTo("{let $t : 'x' /}<div a={$t}>content</div>");
assertThatASTString(node).isEqualTo("" + "LET_VALUE_NODE\n" + "HTML_OPEN_TAG_NODE\n" + " RAW_TEXT_NODE\n" + " HTML_ATTRIBUTE_NODE\n" + " RAW_TEXT_NODE\n" + " HTML_ATTRIBUTE_VALUE_NODE\n" + " PRINT_NODE\n" + "RAW_TEXT_NODE\n" + "HTML_CLOSE_TAG_NODE\n" + " RAW_TEXT_NODE\n" + "");
// try alternate quotes
node = runPass("{let $t : 'x' /}<div a=\"{$t}\">content</div>");
assertThatSourceString(node).isEqualTo("{let $t : 'x' /}<div a=\"{$t}\">content</div>");
node = runPass("{let $t : 'x' /}<div a='{$t}'>content</div>");
assertThatSourceString(node).isEqualTo("{let $t : 'x' /}<div a='{$t}'>content</div>");
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class HtmlRewritePassTest method testDynamicAttribute.
@Test
public void testDynamicAttribute() {
TemplateNode node = runPass("{let $t : 'x' /}<div {$t}>content</div>");
assertThatSourceString(node).isEqualTo("{let $t : 'x' /}<div {$t}>content</div>");
assertThatASTString(node).isEqualTo("" + "LET_VALUE_NODE\n" + "HTML_OPEN_TAG_NODE\n" + " RAW_TEXT_NODE\n" + " HTML_ATTRIBUTE_NODE\n" + " PRINT_NODE\n" + "RAW_TEXT_NODE\n" + "HTML_CLOSE_TAG_NODE\n" + " RAW_TEXT_NODE\n" + "");
// and with a value
node = runPass("{let $t : 'x' /}<div {$t}=x>content</div>");
assertThatSourceString(node).isEqualTo("{let $t : 'x' /}<div {$t}=x>content</div>");
assertThatASTString(node).isEqualTo("" + "LET_VALUE_NODE\n" + "HTML_OPEN_TAG_NODE\n" + " RAW_TEXT_NODE\n" + " HTML_ATTRIBUTE_NODE\n" + " PRINT_NODE\n" + " HTML_ATTRIBUTE_VALUE_NODE\n" + " RAW_TEXT_NODE\n" + "RAW_TEXT_NODE\n" + "HTML_CLOSE_TAG_NODE\n" + " RAW_TEXT_NODE\n" + "");
node = runPass("{let $t : 'x' /}<div {$t}={$t}>content</div>");
assertThatSourceString(node).isEqualTo("{let $t : 'x' /}<div {$t}={$t}>content</div>");
assertThatASTString(node).isEqualTo("" + "LET_VALUE_NODE\n" + "HTML_OPEN_TAG_NODE\n" + " RAW_TEXT_NODE\n" + " HTML_ATTRIBUTE_NODE\n" + " PRINT_NODE\n" + " HTML_ATTRIBUTE_VALUE_NODE\n" + " PRINT_NODE\n" + "RAW_TEXT_NODE\n" + "HTML_CLOSE_TAG_NODE\n" + " RAW_TEXT_NODE\n" + "");
node = runPass("<div {call .name /}=x>content</div>{/template}" + "{template .name kind=\"text\"}foo");
assertThatASTString(node).isEqualTo("" + "HTML_OPEN_TAG_NODE\n" + " RAW_TEXT_NODE\n" + " HTML_ATTRIBUTE_NODE\n" + " CALL_BASIC_NODE\n" + " HTML_ATTRIBUTE_VALUE_NODE\n" + " RAW_TEXT_NODE\n" + "RAW_TEXT_NODE\n" + "HTML_CLOSE_TAG_NODE\n" + " RAW_TEXT_NODE\n" + "");
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class HtmlRewritePassTest method testHtmlCommentWithOnlyRawTextNode.
@Test
public void testHtmlCommentWithOnlyRawTextNode() {
ErrorReporter errorReporter = ErrorReporter.createForTest();
TemplateNode node;
// The most common test case.
node = runPass("<!--foo-->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", " RAW_TEXT_NODE", ""));
assertThatSourceString(node).isEqualTo("<!--foo-->");
// Empty comment is allowed.
node = runPass("<!---->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", ""));
assertThatSourceString(node).isEqualTo("<!---->");
// White spaces should be preserved.
node = runPass("<!-- foo -->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", " RAW_TEXT_NODE", ""));
assertThatSourceString(node).isEqualTo("<!-- foo -->");
// script tag within HTML comment should be treated as raw text.
node = runPass("<!-- <script>alert(\"Hi\");</script> -->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("HTML_COMMENT_NODE", " RAW_TEXT_NODE", ""));
assertThatSourceString(node).isEqualTo("<!-- <script>alert(\"Hi\");</script> -->");
// This is fine since we never start a HTML comment, so it is treated as raw text.
node = runPass("-->", errorReporter);
assertThatASTString(node).isEqualTo(Joiner.on('\n').join("RAW_TEXT_NODE", ""));
assertThatSourceString(node).isEqualTo("-->");
}
Aggregations