Details
-
Bug
-
Status: Closed
-
Critical
-
Resolution: Fixed
-
1.7.8
-
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('some text')"></div>
- However, it should produce
<div onmouseout="foo('some text')"></div>
- For example:
- 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> ...
- So if we use the MarkupBuilder to build the above html block:
- The single-quote should be displayed instead of the entity ( ' ) 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) ... }
StreamingMarkupWriterpublic void write(final c) throws IOException { ... else if(c == '\'' && this.writingAttribute && !useDoubleQuotes) { this.writer.write("'"); } else if(c == '"' && this.writingAttribute && useDoubleQuotes) { this.writer.write("""); } ... }
Thanks