Esempio n. 1
0
 private void doContextualEscaping(SoyFileSetNode soyTree) throws SoySyntaxException {
   List<TemplateNode> extraTemplates = contextualAutoescaper.rewrite(soyTree);
   // TODO: Run the redundant template remover here and rename after CL 16642341 is in.
   if (!extraTemplates.isEmpty()) {
     // TODO: pull out somewhere else.  Ideally do the merge as part of the redundant template
     // removal.
     Map<String, SoyFileNode> containingFile = Maps.newHashMap();
     for (SoyFileNode fileNode : soyTree.getChildren()) {
       for (TemplateNode templateNode : fileNode.getChildren()) {
         String name =
             templateNode instanceof TemplateDelegateNode
                 ? ((TemplateDelegateNode) templateNode).getDelTemplateName()
                 : templateNode.getTemplateName();
         containingFile.put(DerivedTemplateUtils.getBaseName(name), fileNode);
       }
     }
     for (TemplateNode extraTemplate : extraTemplates) {
       String name =
           extraTemplate instanceof TemplateDelegateNode
               ? ((TemplateDelegateNode) extraTemplate).getDelTemplateName()
               : extraTemplate.getTemplateName();
       containingFile.get(DerivedTemplateUtils.getBaseName(name)).addChild(extraTemplate);
     }
   }
 }
  public void testWithoutCssRenamingMap() {

    TemplateNode template =
        (TemplateNode) SharedTestUtils.getNode(SharedTestUtils.parseSoyFiles(TEST_FILE_CONTENT));

    // Before.
    assertEquals(9, template.numChildren());
    CssNode cn1 = (CssNode) template.getChild(1);
    assertEquals("AAA", cn1.getSelectorText());
    CssNode cn7 = (CssNode) template.getChild(7);
    assertEquals("$goo", cn7.getComponentNameText());
    assertEquals("BBB", cn7.getSelectorText());

    (new RenameCssVisitor(null)).exec(template);
    (new CombineConsecutiveRawTextNodesVisitor()).exec(template);

    // After.
    assertEquals(5, template.numChildren());
    RawTextNode rtn0 = (RawTextNode) template.getChild(0);
    assertEquals("<div class=\"AAA ", rtn0.getRawText());
    PrintNode pn1 = (PrintNode) template.getChild(1);
    assertEquals("$goo", pn1.getExprText());
    RawTextNode rtn2 = (RawTextNode) template.getChild(2);
    assertEquals("-AAA BBB ", rtn2.getRawText());
    PrintNode pn3 = (PrintNode) template.getChild(3);
    assertEquals("$goo", pn3.getExprText());
    RawTextNode rtn4 = (RawTextNode) template.getChild(4);
    assertEquals("-BBB\">", rtn4.getRawText());
  }
  /**
   * Visits a {@link TemplateNode}, processing those that have kind html or attributes and making
   * sure that the autoescape mode is strict.
   */
  @Override
  protected void visitTemplateNode(TemplateNode node) {
    if (node.getContentKind() != ContentKind.HTML
        && node.getContentKind() != ContentKind.ATTRIBUTES) {
      return;
    }

    if (node.getAutoescapeMode() != AutoescapeMode.STRICT) {
      errorReporter.report(node.getSourceLocation(), NON_STRICT_TEMPLATE);
    }

    visitSoyNode(node, true);
  }
  public void testWithCssRenamingMap() {

    TemplateNode template =
        (TemplateNode) SharedTestUtils.getNode(SharedTestUtils.parseSoyFiles(TEST_FILE_CONTENT));

    // Before.
    assertEquals(9, template.numChildren());
    CssNode cn1 = (CssNode) template.getChild(1);
    assertEquals("AAA", cn1.getSelectorText());
    CssNode cn7 = (CssNode) template.getChild(7);
    assertEquals("$goo", cn7.getComponentNameText());
    assertEquals("BBB", cn7.getSelectorText());

    // Use a CSS renaming map that only renames 'AAA'.
    SoyCssRenamingMap cssRenamingMap =
        new SoyCssRenamingMap() {
          @Override
          public String get(String key) {
            return key.equals("AAA") ? "XXX" : null;
          }
        };

    (new RenameCssVisitor(cssRenamingMap)).exec(template);
    (new CombineConsecutiveRawTextNodesVisitor()).exec(template);

    // After.
    assertEquals(5, template.numChildren());
    RawTextNode rtn0 = (RawTextNode) template.getChild(0);
    assertEquals("<div class=\"XXX ", rtn0.getRawText());
    PrintNode pn1 = (PrintNode) template.getChild(1);
    assertEquals("$goo", pn1.getExprText());
    RawTextNode rtn2 = (RawTextNode) template.getChild(2);
    assertEquals("-XXX BBB ", rtn2.getRawText());
    PrintNode pn3 = (PrintNode) template.getChild(3);
    assertEquals("$goo", pn3.getExprText());
    RawTextNode rtn4 = (RawTextNode) template.getChild(4);
    assertEquals("-BBB\">", rtn4.getRawText());
  }
Esempio n. 5
0
 @Override
 protected void visitTemplateNode(TemplateNode node) {
   this.currTemplateNameForUserMsgs = node.getTemplateNameForUserMsgs();
   this.currDelPackageName = node.getDelPackageName();
   visitChildren(node);
 }
 @Override
 protected void visitTemplateNode(TemplateNode node) {
   if (node.getAutoescapeMode() != AutoescapeMode.STRICT) {
     errorReporter.report(node.getSourceLocation(), INVALID_AUTOESCAPING);
   }
 }