Support optional parameters typed as "attributes" in Soy element composition that are surrounded by an {if}. Previously, we just unconditionally printed these attributes so they would show up as "null" or "undefined" in the DOM.
GITHUB_BREAKING_CHANGES=n/a
@@ -256,7 +256,7 @@ final class SoyElementCompositionPass implements CompilerFileSetPass {
return;
} else if (attrNode.numChildren() == 1 && attributesNode != null) {
if (isOkToPutInElement(attrNode)) {
- attributesNode.addChild(attrNode.copy(new CopyState()));
+ maybePrintAttribute(attributesNode, conditional, nodeIdGen, attrNode);
return;
}
}
@@ -265,6 +265,26 @@ final class SoyElementCompositionPass implements CompilerFileSetPass {
errorReporter.report(c.getSourceLocation(), ILLEGAL_CHILD);
}
+ private static void maybePrintAttribute(
+ CallParamContentNode attributesNode,
+ Optional<ExprNode> conditional,
+ IdGenerator nodeIdGen,
+ HtmlAttributeNode attrNode) {
+ SourceLocation unknown = attributesNode.getSourceLocation().clearRange();
+ if (conditional.isPresent()) {
+ IfNode ifNode = new IfNode(nodeIdGen.genId(), unknown);
+ IfCondNode ifCondNode =
+ new IfCondNode(
+ nodeIdGen.genId(), unknown, unknown, "if", conditional.get().copy(new CopyState()));
+ ifNode.addChild(ifCondNode);
+ ifCondNode.getExpr().setType(conditional.get().getType());
+ ifCondNode.addChild(attrNode.copy(new CopyState()));
+ attributesNode.addChild(ifNode);
+ } else {
+ attributesNode.addChild(attrNode.copy(new CopyState()));
+ }
+ }
+
static boolean isOkToPutInElement(HtmlAttributeNode attrNode) {
// Any print node or call node whose type/kind is 'attributes' may appear within the root
// element HTML node.
@@ -336,18 +356,7 @@ final class SoyElementCompositionPass implements CompilerFileSetPass {
String paramName = Parameter.attrToParamName(attrName);
if (!parameterMap.containsKey(paramName)) {
// attributesNode can't be null, bad attrs caught in ResolveExpressionTypesCrossTemplatePass
- if (condition.isPresent()) {
- IfNode ifNode = new IfNode(nodeIdGen.genId(), unknown);
- IfCondNode ifCondNode =
- new IfCondNode(
- nodeIdGen.genId(), unknown, unknown, "if", condition.get().copy(new CopyState()));
- ifNode.addChild(ifCondNode);
- ifCondNode.getExpr().setType(condition.get().getType());
- ifCondNode.addChild(attr.copy(new CopyState()));
- attributesNode.addChild(ifNode);
- } else {
- attributesNode.addChild(attr.copy(new CopyState()));
- }
+ maybePrintAttribute(attributesNode, condition, nodeIdGen, attr);
return null;
}
if (isSoyAttr) {
