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

New line character is not escaped in attribute when using MarkupBuilder

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.0
    • 1.8.1
    • XML Processing
    • None

    Description

      For example:

      import groovy.xml.*
      def xml = new MarkupBuilder()
      xml.test(a:"hello\nworld"){}
      

      produces output:

      <test a='hello
      world' />
      

      But xml specification says that when parsing, new lines in attribute value should be converted to single space. So they are not preserved.

      I suggest this new method implementation (sorry that I don't provide real .patch):

          private String checkForReplacement(boolean isAttrValue, char ch) {
              switch (ch) {
                  case '&':
                      return "&amp;";
                  case '<':
                      return "&lt;";
                  case '>':
                      return "&gt;";
                  case '"':
                      // The double quote is only escaped if the value is for
                      // an attribute and the builder is configured to output
                      // attribute values inside double quotes.
                      if (isAttrValue && useDoubleQuotes) return "&quot;";
                      break;
                  case '\'':
                      // The apostrophe is only escaped 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.
                      if (isAttrValue && !useDoubleQuotes) return "&apos;";
                      break;
                  case '\n':
                      if(isAttrValue) return "&#10;";
                  case '\r':
                      if(isAttrValue) return "&#13;";
              }
              return null;
          }
      

      Attachments

        1. MarkupBuilder.java.patch
          0.5 kB
          Jakub Neubauer
        2. repair.txt
          1 kB
          Jakub Neubauer

        Activity

          People

            paulk Paul King
            jakub.neubauer Jakub Neubauer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: