Examples of JsExpr


Examples of com.google.template.soy.jssrc.restricted.JsExpr


  public void testApplyForJsSrc() {

    IdDirective idDirective = new IdDirective();
    JsExpr dataRef = new JsExpr("opt_data.myKey", Integer.MAX_VALUE);
    assertEquals("opt_data.myKey",
                 idDirective.applyForJsSrc(dataRef, ImmutableList.<JsExpr>of()).getText());
  }
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

    }
  }


  @Override public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg0 = args.get(0);
    JsExpr arg1 = args.get(1);

    return new JsExpr(
        "Math.min(" + arg0.getText() + ", " + arg1.getText() + ")", Integer.MAX_VALUE);
  }
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

    return BooleanData.forValue(! (arg instanceof UndefinedData || arg instanceof NullData));
  }


  @Override public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);
    JsExpr nullJsExpr = new JsExpr("null", Integer.MAX_VALUE);
    // Note: In JavaScript, "x != null" is equivalent to "x !== undefined && x !== null".
    return SoyJsCodeUtils.genJsExprUsingSoySyntax(
        Operator.NOT_EQUAL, Lists.<JsExpr>newArrayList(arg, nullJsExpr));
  }
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

      if (contentNode instanceof MsgHtmlTagNode &&
          !isComputableAsJsExprsVisitor.exec(contentNode)) {
        // This is a MsgHtmlTagNode that is not computable as JS expressions. Visit it to
        // generate code to define the 'htmlTag<n>' variable.
        visit(contentNode);
        contentJsExprs.add(new JsExpr("htmlTag" + contentNode.getId(), Integer.MAX_VALUE));

      } else if (contentNode instanceof CallNode) {
        // If the CallNode has any CallParamContentNode children (i.e. this GoogMsgNode's
        // grandchildren) that are not computable as JS expressions, visit them to generate code
        // to define their respective 'param<n>' variables.
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

    }
  }


  @Override public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);

    return new JsExpr("Math.ceil(" + arg.getText() + ")", Integer.MAX_VALUE);
  }
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

    }
  }


  @Override public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr value = args.get(0);
    JsExpr numDigitsAfterPt = (args.size() == 2) ? args.get(1) : null;

    int numDigitsAfterPtAsInt = 0;
    if (numDigitsAfterPt != null) {
      try {
        numDigitsAfterPtAsInt = Integer.parseInt(numDigitsAfterPt.getText());
      } catch (NumberFormatException nfe) {
        numDigitsAfterPtAsInt = Integer.MIN_VALUE;  // indicates it's not a simple integer literal
      }
    }

    if (numDigitsAfterPtAsInt == 0) {
      // Case 1: round() has only one argument or the second argument is 0.
      return new JsExpr("Math.round(" + value.getText() + ")", Integer.MAX_VALUE);

    } else if ((numDigitsAfterPtAsInt >= 0 && numDigitsAfterPtAsInt <= 12) ||
               numDigitsAfterPtAsInt == Integer.MIN_VALUE) {
      String shiftExprText;
      if (numDigitsAfterPtAsInt >= 0 && numDigitsAfterPtAsInt <= 12) {
        shiftExprText = "1" + "000000000000".substring(0, numDigitsAfterPtAsInt);
      } else {
        shiftExprText = "Math.pow(10, " + numDigitsAfterPt.getText() + ")";
      }
      JsExpr shift = new JsExpr(shiftExprText, Integer.MAX_VALUE);
      JsExpr valueTimesShift = SoyJsCodeUtils.genJsExprUsingSoySyntax(
          Operator.TIMES, Lists.newArrayList(value, shift));
      return new JsExpr(
          "Math.round(" + valueTimesShift.getText() + ") / " + shift.getText(),
          Operator.DIVIDE_BY.getPrecedence());

    } else if (numDigitsAfterPtAsInt < 0 && numDigitsAfterPtAsInt >= -12) {
      String shiftExprText = "1" + "000000000000".substring(0, -numDigitsAfterPtAsInt);
      JsExpr shift = new JsExpr(shiftExprText, Integer.MAX_VALUE);
      JsExpr valueDivideByShift = SoyJsCodeUtils.genJsExprUsingSoySyntax(
          Operator.DIVIDE_BY, Lists.newArrayList(value, shift));
      return new JsExpr(
          "Math.round(" + valueDivideByShift.getText() + ") * " + shift.getText(),
          Operator.TIMES.getPrecedence());

    } else {
      throw new IllegalArgumentException(
          "Second argument to round() function is " + numDigitsAfterPtAsInt +
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

    if (jsSrcOptions.getCodeStyle() != SoyJsSrcOptions.CodeStyle.STRINGBUILDER) {
      throw new AssertionError();
    }

    JsExpr callExpr =
        genCallExprHelper(callNode, localVarTranslations, jsCodeBuilder.getOutputVarName());
    jsCodeBuilder.indent().append(callExpr.getText(), ";\n");
  }
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

   */
  private JsExpr genCallExprHelper(
      CallNode callNode, Deque<Map<String, JsExpr>> localVarTranslations,
      @Nullable String outputVarNameForStringbuilder) {

    JsExpr objToPass = genObjToPass(callNode, localVarTranslations);

    // Build the JS expr text for the callee.
    String calleeExprText;
    if (callNode instanceof CallBasicNode) {
      // Case 1: Basic call.
      calleeExprText = ((CallBasicNode) callNode).getCalleeName();
    } else {
      // Case 2: Delegate call.
      CallDelegateNode callDelegateNode = (CallDelegateNode) callNode;
      String calleeIdExprText =
          "soy.$$getDelTemplateId('" + callDelegateNode.getDelCalleeName() + "')";
      ExprRootNode<?> variantSoyExpr = callDelegateNode.getDelCalleeVariantExpr();
      String variantJsExprText;
      if (variantSoyExpr == null) {
        // Case 2a: Delegate call with empty variant.
        variantJsExprText = "''";
      } else {
        // Case 2b: Delegate call with variant expression.
        JsExpr variantJsExpr = jsExprTranslator.translateToJsExpr(
            variantSoyExpr, variantSoyExpr.toSourceString(), localVarTranslations);
        variantJsExprText = variantJsExpr.getText();
      }
      calleeExprText =
          "soy.$$getDelegateFn(" +
              calleeIdExprText + ", " + variantJsExprText + ", " +
              (callDelegateNode.allowsEmptyDefault() ? "true" : "false") + ")";
    }

    // Generate the main call expression.
    String callExprText;
    if (outputVarNameForStringbuilder != null) {
      callExprText = calleeExprText + "(" +
          objToPass.getText() + ", " + outputVarNameForStringbuilder +
          (isUsingIjData ? ", opt_ijData" : "") + ")";
    } else {
      callExprText = calleeExprText + "(" +
          objToPass.getText() + (isUsingIjData ? ", null, opt_ijData" : "") + ")";
    }

    JsExpr result = new JsExpr(callExprText, Integer.MAX_VALUE);

    // In strict mode, escaping directives may apply to the call site.
    for (String directiveName : callNode.getEscapingDirectiveNames()) {
      SoyJsSrcPrintDirective directive = soyJsSrcDirectivesMap.get(directiveName);
      Preconditions.checkNotNull(directive,
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

   * @return The JS expression for the object to pass in the call.
   */
  public JsExpr genObjToPass(CallNode callNode, Deque<Map<String, JsExpr>> localVarTranslations) {

    // ------ Generate the expression for the original data to pass ------
    JsExpr dataToPass;
    if (callNode.isPassingAllData()) {
      dataToPass = new JsExpr("opt_data", Integer.MAX_VALUE);
    } else if (callNode.isPassingData()) {
      dataToPass = jsExprTranslator.translateToJsExpr(
          callNode.getDataExpr(), null, localVarTranslations);
    } else {
      dataToPass = new JsExpr("null", Integer.MAX_VALUE);
    }

    // ------ Case 1: No additional params ------
    if (callNode.numChildren() == 0) {
      return dataToPass;
    }

    // ------ Build an object literal containing the additional params ------
    StringBuilder paramsObjSb = new StringBuilder();
    paramsObjSb.append('{');

    boolean isFirst = true;
    for (CallParamNode child : callNode.getChildren()) {

      if (isFirst) {
        isFirst = false;
      } else {
        paramsObjSb.append(", ");
      }

      String key = child.getKey();
      paramsObjSb.append(key).append(": ");

      if (child instanceof CallParamValueNode) {
        CallParamValueNode cpvn = (CallParamValueNode) child;
        JsExpr valueJsExpr = jsExprTranslator.translateToJsExpr(
            cpvn.getValueExprUnion().getExpr(), cpvn.getValueExprText(), localVarTranslations);
        paramsObjSb.append(valueJsExpr.getText());

      } else {
        CallParamContentNode cpcn = (CallParamContentNode) child;
        JsExpr valueJsExpr;
        if (isComputableAsJsExprsVisitor.exec(cpcn)) {
          valueJsExpr = JsExprUtils.concatJsExprs(
              genJsExprsVisitorFactory.create(localVarTranslations).exec(cpcn));
        } else {
          // This is a param with content that cannot be represented as JS expressions, so we assume
          // that code has been generated to define the temporary variable 'param<n>'.
          String paramExpr = "param" + cpcn.getId();
          if (jsSrcOptions.getCodeStyle() == SoyJsSrcOptions.CodeStyle.STRINGBUILDER) {
            paramExpr += ".toString()";
          }
          valueJsExpr = new JsExpr(paramExpr, Integer.MAX_VALUE);
        }

        // If the param node had a content kind specified, it was autoescaped in the
        // corresponding context. Hence the result of evaluating the param block is wrapped
        // in a SanitizedContent instance of the appropriate kind.

        // The expression for the constructor of SanitizedContent of the appropriate kind (e.g.,
        // "new SanitizedHtml"), or null if the node has no 'kind' attribute.
        valueJsExpr = JsExprUtils.maybeWrapAsSanitizedContent(cpcn.getContentKind(), valueJsExpr);

        paramsObjSb.append(valueJsExpr.getText());
      }
    }

    paramsObjSb.append('}');

    // ------ Cases 2 and 3: Additional params with and without original data to pass ------
    if (callNode.isPassingData()) {
      return new JsExpr(
          "soy.$$augmentMap(" + dataToPass.getText() + ", " + paramsObjSb.toString() + ")",
          Integer.MAX_VALUE);
    } else {
      return new JsExpr(paramsObjSb.toString(), Integer.MAX_VALUE);
    }
  }
View Full Code Here

Examples of com.google.template.soy.jssrc.restricted.JsExpr

    return toSoyData(((SoyListData) arg).length());
  }


  @Override public JsExpr computeForJsSrc(List<JsExpr> args) {
    JsExpr arg = args.get(0);

    String exprText = arg.getPrecedence() == Integer.MAX_VALUE ?
                      arg.getText() + ".length" : "(" + arg.getText() + ").length";
    return new JsExpr(exprText, Integer.MAX_VALUE);
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.