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

StreamingMarkupBuilder can produce invalid xml

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.7.8
    • 1.8-rc-2, 1.7.10
    • XML Processing
    • None
    • Windows and Linux

    Description

      • From #GROOVY-4115, StreamingMarkupBuilder provides option for using double quotes around attributes. But if we use this option, it can output invalid xml:
        • For example:
          def builder = new StreamingMarkupBuilder(useDoubleQuotes: true);
          builder.bind (
            {
              div(onmouseover:'foo("some text")')
            }
          }
          
        • Produce the following output, which is invalid.
              <div onmouseout="foo("some text")"></div>
          
        • Another example:
          def builder = new StreamingMarkupBuilder(useDoubleQuotes: true);
          builder.bind (
             {
               div(onmouseover:"foo('some text')")
             }
          }
          
        • Produce the following output
              <div onmouseout="foo(&apos;some text&apos;)"></div>
          
        • However, it should produce
              <div onmouseout="foo('some text')"></div>
          
      • As far as we know, MarkupBuilder will escape the apostrophe if the value is for an attribute, as opposed to element content, and if the builder is configured to surround attribute values with single quotes.
        • So if we use the MarkupBuilder to build the above html block:
          def builder = new MarkupBuilder();
          builder.setDoubleQuotes(true)
          ...
          
        • It will produce the similar xml as the above, except the apostrophe entity
          ...
          <div onmouseout= "foo('some text')"></div>
          ...
          
      • The single-quote should be displayed instead of the entity ( &apos; ) in both of the builders when attributes are surrounded by double quotes. It appears that StreamingMarkupBuilder is not communicating useDoubleQuotes to StreamingMarkupWriter. One solution we can think of is passing in useDoubleQuotes to the StreamingMarkupWriter from the StreamingMarkupBuilder in the constructor and changing StreamingMarkupWriter to use similar logic to MarkupBuilder when checking attribute value for quotes to escape.
        StreamingMarkupBuilder.java
        public bind(closure) {
        ...
        out = new StreamingMarkupWriter(out, enc, useDoubleQuotes)
        ...
        }
        
        StreamingMarkupWriter
        public void write(final c) throws IOException {
           ...
           else if(c == '\'' && this.writingAttribute && !useDoubleQuotes) {
               this.writer.write("&apos;");
           }
           else if(c == '"' && this.writingAttribute && useDoubleQuotes) {
               this.writer.write("&quot;");
           }
           ...
        }
        

      Thanks

      Attachments

        Activity

          People

            paulk Paul King
            ttrung.vo Tony Trung Thanh Vo
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: