Package com.google.caja.parser.js

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


      values.add(new IntegerLiteral(unk, valueMaker.apply(e)));
    }
    return new ExpressionStmt(unk,
        (Expression) QuasiBuilder.substV(
            "html4.@i = { @k*: @v* };",
            "i", new Reference(new Identifier(unk, key)),
            "k", new ParseTreeNodeContainer(keys),
            "v", new ParseTreeNodeContainer(values)));
  }
View Full Code Here


  public final void testDoesVarReferenceArrayMember() throws Exception {
    ScopeTree global = ScopeTree.create(
        AncestorChain.instance(js(fromString(REFERENCE_EXAMPLE))));
    ScopeTree inner = global.children().get(0);
    assertTrue(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("i")), inner, new HashSet<String>()));
    // Can't determine what arr[0].toString() is.
    assertFalse(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("j")), inner, new HashSet<String>()));
    // i is, and k is defined in terms of numeric operations on i.
    // As long as this works, single use temporary variables will not prevent
    // this optimization from working.
    assertTrue(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("k")), inner, new HashSet<String>()));
    // l is modified in a closure using a value that is not provably numeric.
    assertFalse(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("l")), inner, new HashSet<String>()));
    // m is modified by a pre-increment which is not a numeric operator
    // for reasons discussed in isNumericOperator, but it always assigns a
    // numeric value, so m is numeric.
    assertTrue(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("m")), inner, new HashSet<String>()));
    // n is defined in an outer scope, but all uses of it are numeric.
    assertTrue(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("n")), inner, new HashSet<String>()));
    // o is assigned the result of an addition, but both operands are numeric
    // or undefined.
    assertTrue(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("o")), inner, new HashSet<String>()));
    // Initialization of arr is out of the control of the code sampled.
    assertFalse(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("arr")), inner, new HashSet<String>()));
    // x is not defined in this scope, so must be suspect
    assertFalse(ArrayIndexOptimization.doesVarReferenceVisibleProperty(
        new Reference(ident("x")), inner, new HashSet<String>()));
  }
View Full Code Here

                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

          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

      // And recurse to the body manually so as to avoid recursing to the
      // exception declaration.
      attachScopes(AncestorChain.instance(ac, cs.getBody()), scope);
      return false;
    } else if (n instanceof Reference) {
      Reference r = (Reference) n;
      String rName = r.getIdentifierName();
      Scope definingScope = scope.s.thatDefines(rName);
      assert (definingScope != null) || scope.s.isOuter(rName) : rName;
      scope.uses.add(new Use(scope.withScope(definingScope), rName));
      if ("eval".equals(rName)) { infected = true; }
      infected = infected || "eval".equals(rName);
View Full Code Here

          Expression init = decl.getInitializer();
          Statement replacement;
          if (init != null) {
            replacement = new ExpressionStmt(toAssignment(decl));
          } else if (chain.parent.node instanceof ForEachLoop) {
            replacement = new ExpressionStmt(new Reference(id));
          } else {
            replacement = new Noop(decl.getFilePosition());
          }
          changes.add(Pair.pair(chain.cast(Statement.class), replacement));
          return true;
View Full Code Here

  }

  private static Expression toAssignment(Declaration decl) {
    return Operation.create(
        decl.getFilePosition(), Operator.ASSIGN,
        new Reference(decl.getIdentifier()), decl.getInitializer());
  }
View Full Code Here

      Expression e = ((ExpressionStmt) first).getExpression();
      if (!Operation.is(e, Operator.ASSIGN)) { break; }
      Operation op = (Operation) e;
      Expression lhs = op.children().get(0);
      if (!(lhs instanceof Reference)) { break; }
      Reference r = (Reference) lhs;
      if (!unassigned.contains(r.getIdentifier())) { break; }
      // Don't return two with the same name, because we don't want to have
      // multiple var declarations for the same name.
      //     var foo = 1, bar = 2, foo = 3;
      // might not be legal in ES5 strict mode.
      unassigned.remove(r.getIdentifier());
      extracted.add(op);
      body.removeChild(first);
    }
    return extracted;
  }
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));
View Full Code Here

            + "(/*@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("
              // There is no node or event object available to code in
              // javascript: URIs.
              + "        /*@synthetic*/function () { @body*; })) - 1;",
              "handlerIndex", handlerIndex,
              "body", new ParseTreeNodeContainer(b.children()));
          handlers.add(new EventHandler(attr.env, handler));
          handlerCache.put(value, handlerIndexName);

          Operation urlAdapter = (Operation) QuasiBuilder.substV(
              ""
              + "'javascript:' + /*@synthetic*/encodeURIComponent("
              + "   'try{void ___.plugin_dispatchToHandler___('"
              + "    + ___./*@synthetic*/getId(IMPORTS___)"
              + "    + ',' + @handlerIndex + ',[{}])}catch(_){}')",
              "handlerIndex", new Reference(handlerIndex));
          urlAdapter.setFilePosition(pos);
          urlAdapter.getAttributes().set(HANDLER_NAME, handlerIndexName);
          dynamicValue = urlAdapter;
        } else {
          URI uri;
View Full Code Here

TOP

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

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.