user1452701 user1452701 - 1 month ago 9
Groovy Question

MarkupBuilder don't escape element value

I am trying to generate HTML using MarkupBuilder and would like to know if I can turn of escaping values, see example below.

new MarkupBuilder().with {
p('Hello <strong>World!</strong>')
p('Hello ') { strong('World!') }

The strong tag within the first p tag is escaped. The second strong tag within the second p tag is not escaped. How can I configure MarkupBuilder to not escape the strong tag in the first p tag?

<p>Hello &lt;strong&gt;World!&lt;/strong&gt;</p>

Answer Source

escapeAttributes works for attributes. You're not setting attributes

You need to use mkp.yieldUnescaped

new MarkupBuilder().with {
    p { mkp.yieldUnescaped 'Hello <strong>World!</strong>' }
    p('Hello ') { strong('World!') }