The Bad Programmer

Groovy HTML Builder Encoding Issue

I ran into a situation recently, while using a Groovy HTML builder, that took me a while to figure out. The Groovy HTML Builder is actually an XML builder so it will encode ampersands (&) as &amp. This is a problem when building links with parameters. This Groovy code and test case demonstrates the problem.


class HtmlBuilderBug {
    String buildHtml() {
        def writer = new StringWriter()
        def builder = new groovy.xml.MarkupBuilder(writer)
        builder.a(href: "")
        println writer.toString()
        return writer.toString()
    public static void main(String[] args) {
        new HtmlBuilderBug().buildHtml()

The output of this code is:

<a href=";bar=2">

You will notice that the ampersand parameter separator has been encoded as “&amp”. Now this is probably what you want to happen when building XML, but not for HTML.

Here is a testcase that demonstrates the problem:

class HtmlBuilderBugTest extends GroovyTestCase {
    void testBuildHtml() {
        def htmlBuilderBug = new HtmlBuilderBug()
        String output = htmlBuilderBug.buildHtml()
        assertEquals("", output)

Output of the testcase is:

Now you may be wondering why this is a problem because a browser (at least Safari) was able to correctly follow the link when it was clicked on. In my circumstance I was using the enter key to follow a link from a drop down div (search drop down with arrow navigation and selection by enter key). Once I had the link that the enter key was pressed on I set window.href.location to go to the page, that is what choked on having the & encoded as &amp. I have a bug report open to the Groovy project, so we will see if the developers think it is legitimate or if I am just an idiot.