Package com.google.caja.parser.js

Examples of com.google.caja.parser.js.Identifier


  public final void testSyntheticFormals() throws Exception {
    FilePosition unk = FilePosition.UNKNOWN;
    FunctionConstructor fc = new FunctionConstructor(
        unk,
        new Identifier(unk, "f"),
        Arrays.asList(
            new FormalParam(new Identifier(unk, "x")),
            new FormalParam(
                SyntheticNodes.s(new Identifier(unk, "y___")))),
        new Block(
            unk,
            Arrays.<Statement>asList(new ReturnStmt(
                unk,
                Operation.createInfix(
                    Operator.MULTIPLICATION,
                    Operation.createInfix(
                        Operator.ADDITION,
                        new Reference(new Identifier(unk, "x")),
                        new Reference(SyntheticNodes.s(
                            new Identifier(unk, "y___")))),
                    new Reference(new Identifier(unk, "z")))))));
    checkSucceeds(
        new Block(
            unk,
            Arrays.asList(
                new FunctionDeclaration((FunctionConstructor) fc.clone()))),
View Full Code Here


          FunctionConstructor ctor = (FunctionConstructor) node;
          Scope s2 = Scope.fromFunctionConstructor(scope, ctor);
          if (ctor.getIdentifierName() == null) {
            return expandAll(node, s2);
          }
          Identifier ident = ctor.getIdentifier();
          Reference identRef = new Reference(ident);
          identRef.setFilePosition(ident.getFilePosition());
          scope.addStartStatement(
              new Declaration(FilePosition.UNKNOWN, ident, identRef));
          return QuasiBuilder.substV(
              "(@var = function @ident(@formals*) { @stmts*; @body*; })",
              "var", identRef,
View Full Code Here

  private static Expression synth(Expression e) {
    e.acceptPreOrder(new Visitor() {
      public boolean visit(AncestorChain<?> chain) {
        ParseTreeNode n = chain.node;
        if (n instanceof Identifier) {
          Identifier id = (Identifier) n;
          if (id.getName() != null && id.getName().endsWith("___")) {
            SyntheticNodes.s(id);
          }
        } else if (n instanceof FunctionConstructor) {
          FunctionConstructor fc = (FunctionConstructor) n;
          if (fc.getIdentifierName() != null
View Full Code Here

        && Operation.is(parent, Operator.MEMBER_ACCESS)
        && parent.children().get(1) == ref;
  }

  private static void renameOne(MutableParseTreeNode n) {
    Identifier id = (Identifier) n.children().get(0);
    String origName = id.getName();
    ScopeInfo u = n.getAttributes().get(SCOPE);
    Scope s = (n instanceof FunctionConstructor)
        ? u.s :  u.s.thatDefines(origName);
    if (s != null) {  // Will be null for undeclared globals.
      String newName = u.withScope(s).mapping.get(origName);
      if (!newName.equals(origName)) {
        n.replaceChild(new Identifier(id.getFilePosition(), newName), id);
      }
    }
  }
View Full Code Here

    if (!needed.isEmpty()) {
      List<Operation> topAssignments = extractAssignments(
          body, newIdentSet(locals));
      List<Declaration> decls = Lists.newArrayList();
      for (Operation topAssign : topAssignments) {
        Identifier id = ((Reference) topAssign.children().get(0))
            .getIdentifier();
        decls.add(new Declaration(
            topAssign.getFilePosition(), id, topAssign.children().get(1)));
        needed.remove(id);
      }
View Full Code Here

        ParseTreeNode node = chain.node;
        if (node instanceof Declaration
            && !(node instanceof FunctionDeclaration)) {
          if (chain.parent.node instanceof CatchStmt) { return true; }
          Declaration decl = (Declaration) node;
          Identifier id = decl.getIdentifier();
          removedIdents.add(id);
          Expression init = decl.getInitializer();
          Statement replacement;
          if (init != null) {
            replacement = new ExpressionStmt(toAssignment(decl));
View Full Code Here

    Map<String, ParseTreeNode> bindings = Maps.newHashMap();
    if (!QuasiBuilder.match("{ var @p; }", node, bindings)) { return false; }
    if (bindings.size() != 1) { return false; }
    if (bindings.get("p") == null) { return false; }
    if (!(bindings.get("p") instanceof Identifier)) { return false; }
    Identifier p = (Identifier) bindings.get("p");
    if (!candidate.equals(p.getName())) { return false; }
    return true;
  }
View Full Code Here

    ParseTreeNode result = (jsonpCallback == null)
        ? obj(props)
        : QuasiBuilder.substV("@c(@o);",
            "c", new Reference(
                     new Identifier(
                         FilePosition.UNKNOWN,
                         jsonpCallback)),
            "o", obj(props));

    IOCallback callback = new IOCallback();
View Full Code Here

              ""
              + "var @handlerName = ___./*@synthetic*/markFuncFreeze("
              + "    /*@synthetic*/function ("
              + "        event, " + ReservedNames.THIS_NODE + ") { @body*; });",
              "handlerName", SyntheticNodes.s(
                  new Identifier(FilePosition.UNKNOWN, handlerFnName)),
              "body", new ParseTreeNodeContainer(b.children()));
          handlers.add(new EventHandler(attr.env, handler));
          handlerCache.put(value, handlerFnName);
        }

        FunctionConstructor eventAdapter
            = (FunctionConstructor) QuasiBuilder.substV(
            ""
            + "(/*@synthetic*/ function (event) {"
            + "  return /*@synthetic*/ (___.plugin_dispatchEvent___("
            + "      /*@synthetic*/this, event, "
            + "      ___./*@synthetic*/getId(IMPORTS___), @tail));"
            + "})",
            "tail", new Reference(SyntheticNodes.s(
                new Identifier(pos, handlerFnName))));
        eventAdapter.setFilePosition(pos);
        eventAdapter.getAttributes().set(HANDLER_NAME, handlerFnName);
        dynamicValue = eventAdapter;
        break;
      case STYLE:
        CssTree.DeclarationGroup decls = styleFromAttrib(attr);
        if (decls == null || decls.children().isEmpty()) {
          return noResult(attr);
        }

        // The validator will check that property values are well-formed,
        // marking those that aren't, and identifies all URLs.
        CssValidator v = new CssValidator(cssSchema, htmlSchema, mq)
            .withInvalidNodeMessageLevel(MessageLevel.WARNING);
        v.validateCss(AncestorChain.instance(decls));
        // The rewriter will remove any unsafe constructs.
        // and put URLs in the proper filename namespace
        new CssRewriter(meta.getUriPolicy(), cssSchema, mq)
            .withInvalidNodeMessageLevel(MessageLevel.WARNING)
            .rewrite(AncestorChain.instance(decls));
        new CssDynamicExpressionRewriter(meta).rewriteCss(decls);
        ArrayConstructor jsValue = CssDynamicExpressionRewriter.cssToJs(decls);

        if (jsValue.children().size() == 0) {
          // No declarations remain after sanitizing
          return noResult(attr);
        } else if (jsValue.children().size() == 1) {
          // Declarations have been reduced to a single, statically known
          // StringLiteral or dynamically computed Expression
          dynamicValue = jsValue.children().get(0);
        } else {
          throw new SomethingWidgyHappenedError(
              "Rewriter thinks STYLE attribute should contain plugin ID");
        }
        break;
      case URI:
        if (attributeContent.containsKey(attr.src)) {  // A javascript: URI
          Block b = this.jsFromAttrib(attr);
          if (b == null || b.children().isEmpty()) { return noResult(attr); }

          String handlerIndexName = meta.generateUniqueName("c");
          Identifier handlerIndex = SyntheticNodes.s(new Identifier(
              FilePosition.UNKNOWN, handlerIndexName));
          Statement handler = (Statement) QuasiBuilder.substV(
              ""
              + "var @handlerIndex = IMPORTS___.handlers___.push("
              + "    ___./*@synthetic*/markFuncFreeze("
 
View Full Code Here

            // Do not recurse into closures.
            if (node instanceof FunctionConstructor) { return false; }
            if (node instanceof Reference) {
              Reference r = (Reference) node;
              if (Keyword.THIS.toString().equals(r.getIdentifierName())) {
                Identifier oldRef = r.getIdentifier();
                Identifier thisNode = new Identifier(
                    oldRef.getFilePosition(), ReservedNames.THIS_NODE);
                r.replaceChild(SyntheticNodes.s(thisNode), oldRef);
              }
              return false;
            }
View Full Code Here

TOP

Related Classes of com.google.caja.parser.js.Identifier

Copyright © 2018 www.massapicom. 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.