Package com.google.javascript.jscomp.regex

Examples of com.google.javascript.jscomp.regex.RegExpTree$Text


      // Split regexp into pattern and flags.
      String pattern = subtree.getFirstChild().getString();
      String flags = subtree.getChildCount() == 2
          ? subtree.getLastChild().getString() : "";
      // Parse to an AST and optimize.
      RegExpTree regexTree;
      try {
        regexTree = RegExpTree.parseRegExp(pattern, flags);
      } catch (IllegalArgumentException ex) {
        // Warnings are propagated in the CheckRegExp pass.
        return subtree;
      }
      regexTree = regexTree.simplify(flags);
      // Decompose the AST.
      String literal = regexTree.toString();
      String newPattern = literal.substring(1, literal.length() - 1);
      // Remove unnecessary flags and order them consistently for gzip.
      String newFlags = (
          // The g flags cannot match or replace more than one instance if it is
          // anchored at the front and back as in /^foo$/ and if the anchors are
          // relative to the whole string.
          // But if the regex has capturing groups, then the match operator
          // would return capturing groups without the g flag.
          (flags.contains("g")
           && (!RegExpTree.matchesWholeInput(regexTree, flags)
               || regexTree.hasCapturingGroup())
           ? "g" : "")
          // Remove the i flag if it doesn't have any effect.
          // E.g. /[a-z0-9_]/i -> /\w/
          + (flags.contains("i") && regexTree.isCaseSensitive() ? "i" : "")
          // If the regular expression contains no anchors, then the m flag has
          // no effect.
          + (flags.contains("m") && regexTree.containsAnchor() ? "m" : ""));
      // Update the original if something was done.
      if (!(newPattern.equals(pattern) && newFlags.equals(flags))) {
        subtree.getFirstChild().setString(newPattern);
        if (!newFlags.isEmpty()) {
          subtree.getLastChild().setString(newFlags);
View Full Code Here


      // Split regexp into pattern and flags.
      String pattern = subtree.getFirstChild().getString();
      String flags = subtree.getChildCount() == 2
          ? subtree.getLastChild().getString() : "";
      // Parse to an AST and optimize.
      RegExpTree regexTree;
      try {
        regexTree = RegExpTree.parseRegExp(pattern, flags);
      } catch (IllegalArgumentException ex) {
        // Warnings are propagated in the CheckRegExp pass.
        return subtree;
      }
      regexTree = regexTree.simplify(flags);
      // Decompose the AST.
      String literal = regexTree.toString();
      String newPattern = literal.substring(1, literal.length() - 1);
      // Remove unnecessary flags and order them consistently for gzip.
      String newFlags = (
          // The g flags cannot match or replace more than one instance if it is
          // anchored at the front and back as in /^foo$/ and if the anchors are
          // relative to the whole string.
          // But if the regex has capturing groups, then the match operator
          // would return capturing groups without the g flag.
          (flags.contains("g")
           && (!RegExpTree.matchesWholeInput(regexTree, flags)
               || regexTree.hasCapturingGroup())
           ? "g" : "")
          // Remove the i flag if it doesn't have any effect.
          // E.g. /[a-z0-9_]/i -> /\w/
          + (flags.contains("i") && regexTree.isCaseSensitive() ? "i" : "")
          // If the regular expression contains no anchors, then the m flag has
          // no effect.
          + (flags.contains("m") && regexTree.containsAnchor() ? "m" : ""));
      // Update the original if something was done.
      if (!(newPattern.equals(pattern) && newFlags.equals(flags))) {
        subtree.getFirstChild().setString(newPattern);
        if (!"".equals(newFlags)) {
          subtree.getLastChild().setString(newFlags);
View Full Code Here

    private void print( Object element ) throws IOException, JDOMException
    {
        if( element instanceof Text )
        {
            Text t = (Text)element;
            String s = t.getText();
            if( m_preStack.isPreMode() )
            {
                m_out.print( s );
            }
            else
View Full Code Here

  }
 

  @Test
  public void testTextEmpty() {
    Text content = new Text("");
    assertEquals("", outputString(fraw,     content));
    assertEquals("", outputString(fcompact, content));
    assertEquals("", outputString(fpretty,  content));
    assertEquals("", outputString(ftso,     content));
    assertEquals("", outputString(ftfw,     content));
View Full Code Here

    assertEquals("", outputString(ftfw,     content));
  }
 
  @Test
  public void testTextWhitespace() {
    Text content = new Text(" \r \n \t ");
    assertEquals(expect(" \r \n \t "),
        outputString(fraw,     content));
    assertEquals("",
        outputString(fcompact, content));
    assertEquals("",
View Full Code Here

        outputString(ftfw,     content));
  }

  @Test
  public void testTextWithText() {
    Text content = new Text(" \r & \n \t ");
    assertEquals(expect(" \r & \n \t "),
        outputString(fraw,     content));
    assertEquals(expect("&"),
        outputString(fcompact, content));
    assertEquals(expect("&"),
View Full Code Here

 
  @Test
  public void testMultiWhiteText() {
    Element root = new Element("root");
    root.addContent(new CDATA(" "));
    root.addContent(new Text(" "));
    root.addContent(new Text("    "));
    root.addContent(new Text(""));
    root.addContent(new Text(" "));
    root.addContent(new Text("  \n \n "));
    root.addContent(new Text("  \t "));
    root.addContent(new Text("  "));
    assertEquals(expect("<root><![CDATA[ ]]>        \n \n   \t   </root>"),
        outputString(fraw,     root));
    assertEquals(expect("<root/>"),
        outputString(fcompact, root));
    assertEquals(expect("<root/>"),
View Full Code Here

 
  @Test
  public void testMultiText() {
    Element root = new Element("root");
    root.addContent(new CDATA(" "));
    root.addContent(new Text(" "));
    root.addContent(new Text("    "));
    root.addContent(new Text(""));
    root.addContent(new Text("X"));
    root.addContent(new Text("  \n \n "));
    root.addContent(new Text("  \t "));
    root.addContent(new Text("  "));
    assertEquals(expect("<root><![CDATA[ ]]>     X  \n \n   \t   </root>"),
        outputString(fraw,     root));
    assertEquals(expect("<root>X</root>"),
        outputString(fcompact, root));
    assertEquals(expect("<root>X</root>"),
View Full Code Here

 
 
 
  @Test
  public void testXXX() {
    Text content = new Text("");
    assertEquals("",
        outputString(fraw,     content));
    assertEquals("",
        outputString(fcompact, content));
    assertEquals("",
View Full Code Here

 
 
 
  @Test
  public void testOutputText() {
    checkOutput(new Text(" hello  there  "), " hello  there  ", "hello there", "hello  there", "hello  there", " hello  there  ");
  }
View Full Code Here

TOP

Related Classes of com.google.javascript.jscomp.regex.RegExpTree$Text

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.