Search in sources :

Example 1 with StandaloneNode

use of com.google.template.soy.soytree.SoyNode.StandaloneNode in project closure-templates by google.

the class InsertMsgsVisitor method replaceMsgNode.

private void replaceMsgNode(MsgFallbackGroupNode node) {
    // Check for plural or select message. Either report error or don't replace.
    for (MsgNode msg : node.getChildren()) {
        if (msg.isPlrselMsg()) {
            errorReporter.report(node.getSourceLocation(), ENCOUNTERED_PLURAL_OR_SELECT);
            return;
        }
    }
    // Figure out which message we're going to use, and build its list of replacement nodes.
    currReplacementNodes = null;
    if (msgBundle != null) {
        for (MsgNode msg : node.getChildren()) {
            SoyMsg translation = msgBundle.getMsg(MsgUtils.computeMsgIdForDualFormat(msg));
            if (translation != null) {
                buildReplacementNodesFromTranslation(msg, translation);
                break;
            }
        }
    }
    if (currReplacementNodes == null) {
        buildReplacementNodesFromSource(node.getChild(0));
    }
    // Replace this MsgFallbackGroupNode with the replacement nodes.
    ParentSoyNode<StandaloneNode> parent = node.getParent();
    int indexInParent = parent.getChildIndex(node);
    parent.removeChild(indexInParent);
    parent.addChildren(indexInParent, currReplacementNodes);
    currReplacementNodes = null;
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) MsgNode(com.google.template.soy.soytree.MsgNode)

Example 2 with StandaloneNode

use of com.google.template.soy.soytree.SoyNode.StandaloneNode in project closure-templates by google.

the class MsgUtils method buildMsgPartsForChildren.

// -----------------------------------------------------------------------------------------------
// Private helpers for building the list of message parts.
/**
 * Builds the list of SoyMsgParts for all the children of a given parent node.
 *
 * @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or
 *     MsgSelectDefaultNode.
 * @param msgNode The MsgNode containing 'parent'.
 */
private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren(BlockNode parent, MsgNode msgNode) {
    ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder();
    for (StandaloneNode child : parent.getChildren()) {
        if (child instanceof RawTextNode) {
            String rawText = ((RawTextNode) child).getRawText();
            msgParts.add(SoyMsgRawTextPart.of(rawText));
        } else if (child instanceof MsgPlaceholderNode) {
            PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child);
            msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example()));
        } else if (child instanceof MsgPluralNode) {
            msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode));
        } else if (child instanceof MsgSelectNode) {
            msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode));
        }
    }
    return msgParts.build();
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderInfo(com.google.template.soy.soytree.MsgNode.PlaceholderInfo) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 3 with StandaloneNode

use of com.google.template.soy.soytree.SoyNode.StandaloneNode in project closure-templates by google.

the class HtmlRewritePass method run.

@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
    new Visitor(nodeIdGen, file.getFilePath(), errorReporter).exec(file);
    /*
     * Validates that the only children of close tags can be {@code phname} attributes.
     *
     * <p>Later passes validate that phnames for close tags only appear in messages.
     */
    for (HtmlCloseTagNode closeTag : SoyTreeUtils.getAllNodesOfType(file, HtmlCloseTagNode.class)) {
        List<StandaloneNode> children = closeTag.getChildren();
        HtmlAttributeNode phNameAttribute = closeTag.getDirectAttributeNamed(MessagePlaceholders.PHNAME_ATTR);
        HtmlAttributeNode phExAttribute = closeTag.getDirectAttributeNamed(MessagePlaceholders.PHEX_ATTR);
        // the child at index 0 is the tag name
        for (int i = 1; i < children.size(); i++) {
            StandaloneNode child = children.get(i);
            if (child == phNameAttribute || child == phExAttribute) {
                // the phname and phex attributes are validated later and allowed in close nodes
                continue;
            }
            errorReporter.report(child.getSourceLocation(), UNEXPECTED_CLOSE_TAG_CONTENT);
        }
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) AbstractSoyNodeVisitor(com.google.template.soy.soytree.AbstractSoyNodeVisitor) HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode) Checkpoint(com.google.template.soy.error.ErrorReporter.Checkpoint) HtmlCloseTagNode(com.google.template.soy.soytree.HtmlCloseTagNode)

Example 4 with StandaloneNode

use of com.google.template.soy.soytree.SoyNode.StandaloneNode in project closure-templates by google.

the class HtmlAttributeNode method getStaticContent.

/**
 * Returns the static value, if one exists, or null otherwise.
 */
@Nullable
public String getStaticContent() {
    if (!hasValue()) {
        return null;
    }
    HtmlAttributeValueNode attrValue = (HtmlAttributeValueNode) getChild(1);
    if (attrValue.numChildren() == 0) {
        return "";
    }
    if (attrValue.numChildren() > 1) {
        return null;
    }
    StandaloneNode attrValueNode = attrValue.getChild(0);
    if (!(attrValueNode instanceof RawTextNode)) {
        return null;
    }
    return ((RawTextNode) attrValueNode).getRawText();
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) Nullable(javax.annotation.Nullable)

Example 5 with StandaloneNode

use of com.google.template.soy.soytree.SoyNode.StandaloneNode in project closure-templates by google.

the class HtmlCommentNode method toSourceString.

@Override
public String toSourceString() {
    StringBuilder sb = new StringBuilder();
    sb.append("<!--");
    for (StandaloneNode node : this.getChildren()) {
        sb.append(node.toSourceString());
    }
    sb.append("-->");
    return sb.toString();
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode)

Aggregations

StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)30 Test (org.junit.Test)19 RawTextNode (com.google.template.soy.soytree.RawTextNode)10 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)9 MsgNode (com.google.template.soy.soytree.MsgNode)8 PrintNode (com.google.template.soy.soytree.PrintNode)8 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)7 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)5 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)4 VarRefNode (com.google.template.soy.exprtree.VarRefNode)4 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)4 FunctionNode (com.google.template.soy.exprtree.FunctionNode)3 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)3 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)3 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)3 ImmutableList (com.google.common.collect.ImmutableList)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 StringNode (com.google.template.soy.exprtree.StringNode)2 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)2