Package com.google.caja.parser.css

Examples of com.google.caja.parser.css.CssTree


  private void translateUrls(AncestorChain<? extends CssTree> t) {
      t.node.visitPreOrder(new ParseTreeNodeVisitor() {
          public boolean visit(ParseTreeNode node) {
            if (node instanceof CssTree.Term
                && CssPropertyPartType.URI == propertyPartType(node)) {
              CssTree term = (CssTree.Term) node;

              CssTree.CssLiteral content =
                  (CssTree.CssLiteral) term.children().get(0);
              if (content instanceof CssTree.Substitution) {
                return true// Handled by later pass.
              }

              Name propertyPart = propertyPart(node);
              String uriStr = content.getValue();
              try {
                URI baseUri = content.getFilePosition().source().getUri();
                URI relUri = new URI(uriStr);
                URI uri = baseUri.resolve(relUri);
                // Rewrite the URI.
                // TODO(mikesamuel): for content: and other URI types, use
                // mime-type of text/*.
                ExternalReference ref = new ExternalReference(
                    uri, baseUri, relUri, content.getFilePosition());
                CssTree.UriLiteral replacement;
                if (uriPolicy != null) {
                  String rewrittenUri = UriPolicyNanny.apply(
                      uriPolicy,
                      ref, UriEffect.SAME_DOCUMENT, LoaderType.SANDBOXED,
                      Collections.singletonMap(
                          UriPolicyHintKey.CSS_PROP.key, propertyPart));
                  replacement = new SafeUriLiteral(
                          content.getFilePosition(), URI.create(rewrittenUri));
                } else {
                  replacement = new UnsafeUriLiteral(
                          content.getFilePosition(), uri);
                }
                replacement.getAttributes().putAll(content.getAttributes());
                term.replaceChild(replacement, content);
              } catch (URISyntaxException ex) {
                // Should've been checked in removeUnsafeConstructs.
                throw new SomethingWidgyHappenedError(ex);
              }
            }
View Full Code Here


    return Strings.eqIgnoreCase(elementName, t.getElementName());
  }

  private static boolean selectorMatchesClass(
      CssTree.SimpleSelector t, String className) {
    CssTree first = t.children().get(0);
    return first instanceof CssTree.ClassLiteral
        && className.equals(((CssTree.ClassLiteral) first).getIdentifier());
  }
View Full Code Here

      List<String> urisExpectedSafe,
      List<String> urisExpectedUnsafe)
      throws Exception {
    final List<String> urisFoundSafe = Lists.newArrayList();
    final List<String> urisFoundUnsafe = Lists.newArrayList();
    CssTree t = css(fromString(css), false);
    new CssValidator(CssSchema.getDefaultCss21Schema(mq),
        HtmlSchema.getDefault(mq), mq)
        .validateCss(AncestorChain.instance(t));
    new CssRewriter(uriPolicy, CssSchema.getDefaultCss21Schema(mq), mq)
        .rewrite(AncestorChain.instance(t));
    t.acceptPreOrder(new Visitor() {
      public boolean visit(AncestorChain<?> ancestors) {
        ParseTreeNode node = ancestors.node;
        if (node instanceof CssTree.UriLiteral) {
          String value = ((CssTree.CssLiteral) node).getValue();
          if (node instanceof SafeUriLiteral) {
View Full Code Here

  private void runTest(String css, String golden, boolean allowSubstitutions)
      throws Exception {
    mq.getMessages().clear();
    mc.relevantKeys = Collections.singleton(CssValidator.INVALID);

    CssTree t = css(fromString(css), allowSubstitutions);

    String msg;
    {
      StringBuilder msgBuf = new StringBuilder();
      t.formatTree(mc, 0, msgBuf);
      msg = msgBuf.toString();
    }

    CssSchema cssSchema = CssSchema.getDefaultCss21Schema(mq);
    new CssValidator(cssSchema, HtmlSchema.getDefault(mq), mq)
        .validateCss(AncestorChain.instance(t));
    new CssRewriter(
        new UriPolicy() {
          public String rewriteUri(
              ExternalReference ref, UriEffect effect, LoaderType loader,
              Map<String, ?> hints) {
            URI uri = ref.getUri();

            if ("http".equals(uri.getScheme())  // Used by CajaTestCase
                && "example.org".equals(uri.getHost())
                && uri.getPath() != null
                && uri.getPath().startsWith("/")) {
              try {
                return new URI(null, null, "/foo" + uri.getPath(),
                               uri.getQuery(), uri.getFragment())
                    .toString();
              } catch (URISyntaxException ex) {
                ex.printStackTrace();
                return null;
              }
            } else if ("whitelisted-host.com".equals(uri.getHost())) {
              return uri.toString();
            } else {
              return null;
            }
          }
        },
        cssSchema, mq)
        .rewrite(AncestorChain.instance(t));

    {
      StringBuilder msgBuf = new StringBuilder();
      t.formatTree(mc, 0, msgBuf);
      msg += "\n  ->\n" + msgBuf.toString();
    }

    assertEquals(msg, golden, render(t));
  }
View Full Code Here

  private void assertCallsUriRewriterWithPropertyPart(
      String cssCode, String... expectedParts)
      throws ParseException {
    final Set<String> propertyParts = Sets.newLinkedHashSet();

    CssTree t = cssCode.trim().endsWith("}")
        ? css(fromString(cssCode)) : cssDecls(fromString(cssCode));

    CssSchema cssSchema = CssSchema.getDefaultCss21Schema(mq);
    new CssValidator(cssSchema, HtmlSchema.getDefault(mq), mq)
        .validateCss(AncestorChain.instance(t));
View Full Code Here

    // we then ensure it fails with URI-valued attribute CITE
    fails("blockquote[cite] { font-weight: bold }");
  }

  private void fails(String css) throws Exception {
    CssTree t = css(fromString(css), true);
    mq.getMessages().clear();
    CssValidator v = makeCssValidator(mq);
    assertTrue(css, !v.validateCss(ac(t)));
    MessageLevel maxLevel = MessageLevel.values()[0];
    for (Message msg : mq.getMessages()) {
View Full Code Here

    assertTrue(maxLevel.name(), MessageLevel.ERROR.compareTo(maxLevel) <= 0);
  }

  private void warns(String css) throws Exception {
    MessageQueue smq = new SimpleMessageQueue();
    CssTree t = css(fromString(css), true);
    CssValidator v = makeCssValidator(smq);
    boolean valid = v.validateCss(ac(t));
    mq.getMessages().addAll(smq.getMessages());
    assertTrue(css, valid);
    assertTrue(css, !mq.getMessages().isEmpty());
View Full Code Here

  private void runTest(String css, String golden, String... warnings)
    throws Exception {
    MessageContext mc = new MessageContext();
    mq.getMessages().clear();
    CssTree cssTree = css(fromString(css), true);
    MessageQueue smq = new SimpleMessageQueue();
    CssValidator v = makeCssValidator(smq);
    boolean valid = v.validateCss(ac(cssTree));
    mq.getMessages().addAll(smq.getMessages());

    // If no warnings are expected, the result should be valid
    if (warnings.length == 0) {
      if (!valid) {
        System.err.println(cssTree.toStringDeep());
      }
      assertTrue(css, valid);
    } else {
      removeInvalidNodes(AncestorChain.instance(cssTree));
    }

    mc.relevantKeys = new LinkedHashSet<SyntheticAttributeKey<?>>(
        Arrays.<SyntheticAttributeKey<?>>asList(
            CssValidator.CSS_PROPERTY_PART_TYPE,
            CssValidator.CSS_PROPERTY_PART));
    StringBuilder sb = new StringBuilder();
    cssTree.format(mc, sb);
    if (golden != null) {
      assertEquals(css, golden.trim(), sb.toString().trim());
    }

    List<String> actualWarnings = Lists.newArrayList();
View Full Code Here

                                        final List<String> unsafeUris) {
    css.acceptPreOrder(new Visitor() {
      public boolean visit(AncestorChain<?> ancestors) {
        ParseTreeNode node = ancestors.node;
        if (node instanceof CssTree.UriLiteral) {
          CssTree parent = (CssTree) ancestors.parent.node;
          assert(null != parent);
          String value = ((CssTree.CssLiteral) node).getValue();
          CssTree.UriLiteral repl = null;
          if (safeUris.contains(value)) {
            repl = new SafeUriLiteral(
                node.getFilePosition(), URI.create(value));
          } else if (unsafeUris.contains(value)) {
            repl = new UnsafeUriLiteral(
                node.getFilePosition(), URI.create(value));
          }
          if (repl != null) {
            parent.replaceChild(repl, node);
          } else {
            fail("URI literal " + value + " unaccounted for by test");
          }
        }
        return true;
View Full Code Here

TOP

Related Classes of com.google.caja.parser.css.CssTree

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.