Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-1205

GStringTemplateEngine does not always parse single line comments '//' within <% and %> sections correctly

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-JSR-3
    • None
    • Groovlet / GSP
    • None

    Description

      This bug affects groovy.text.GStringTemplateEngine. SimpleTemplateEngine seems to be immune.
      If you put a single line comment (using double forward slashes '//') within a scriptlet section (enclosed in <% and %>), it will cause a parse error if there is no new line between the end of the comment and the closing '%>' sequence. This is often done to place comments into a template that the developer does not want showing up in the output. For example:

      <tag1 attr="someValue">
      <% // Some comment that I don't want showing up in the output %>
      <tag2/>
      </tag1>

      The above will cause a parse error. However, this will not:

      <tag1 attr="someValue">
      <% // Some comment that I don't want showing up in the output
      %>
      <tag2/>
      </tag1>

      Further debugging indicates that this is because GStringTemplateEngine does not place a newline in the resulting Groovy script code after outputting code enclosed in a <% %> section. (It currently places the characters "; " after the scriptlet code). For example, the following template:

      <% // This is a comment that will be filtered from output %>
      Hello World!

      Produces this Groovy script:

      package groovy.tmp.templates
      def getTemplate() { return

      { out -> delegate = new Binding(delegate); out << """"""; // This is a comment that will be filtered from output ; out << """ Hello World!"""}

      .asWritable()}

      Notice that GStringTemplateEngine appended "; " to the end of the comment '//', but since it did not append a newline after the "; ", the following "out << ..." code is considered part of the comment and so completely borks the parsing.

      I've attached a very simple TestCase that contains a failing unit test to reproduce the problem.

      Attachments

        Activity

          People

            tug John Wilson
            adepue Andy DePue
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: