MarkupBuilder的setEscapeAttributes不起作用? [英] MarkupBuilder's setEscapeAttributes not working?

查看:96
本文介绍了MarkupBuilder的setEscapeAttributes不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期望 setEscapeAttributes(Boolean)方法能够打开和关闭转义特殊字符,即当我将构建器内容转换为字符串时,特殊字符将会不同,这取决于我们为该方法提供的价值。但是,似乎我的期望不正确,或者方法不正常。以下是一个示例代码片段:

foo.groovy

  import groovy.xml。* 
def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
println builder.isEscapeAttributes()
builder。 setEscapeAttributes(false)
println builder.isEscapeAttributes()
builder.html {
h2hello< br> world
}

println writer。 toString()

如果运行 groovy foo.groovy
$ p $ true
false

< h2> hello& lt;& gt; world< / h2>
< / html>

我期望 h2 一行

 < h2> hello< br>世界< / h2> 

那么,究竟发生了什么?我使用的是Groovy 2.1.8,这是本文的最新版本。使用 setEscapeAttributes

解决方案

/ code>会停止转义属性,所以:

  println new StringWriter()。with {writer  - > 
新的MarkupBuilder(作家).with {

//告诉构建者不要转义ATTRIBUTES
escapeAttributes = false

html {
h2(tim:'woo> yay')
}
writer.toString()
}
}

将会列印:

 < html> 
< h2 tim ='woo> yay'/>
< / html>

与此相反,如果您注释掉 escapeAttributes 上面的行:

 < html> 
< h2 tim ='woo& gt; yay'/>
< / html>

如果您想避免转义内容,您需要使用 mkp.yieldUnescaped 像这样:

  println new StringWriter()。with {writer  - > 
new MarkupBuilder(writer).with {
html {
h2 {
mkp.yieldUnescaped'hello< br> world'
}
}
writer.toString()
}
}



 < html> 
< h2> hello< br>世界< / h2>
< / html>

尽管应该小心,因为这显然是无效的xml(因为`
不是关闭)


I expect the setEscapeAttributes( Boolean ) method to switch on and off of escaping special characters, namely, when I convert the builder content into string, special characters will be different, depending on what value we feed into that method. However, it seems either my expectation is not right or the method is not working properly. Here is an example snippet:

foo.groovy

import groovy.xml.*
def writer = new StringWriter()
def builder = new MarkupBuilder( writer )
println builder.isEscapeAttributes()
builder.setEscapeAttributes( false )
println builder.isEscapeAttributes()
builder.html {
    h2 "hello<br>world"
}

println writer.toString()

If you run groovy foo.groovy, here is the output:

true
false
<html>
  <h2>hello&lt;br&gt;world</h2>
</html>

where I expect the h2 line to be

<h2>hello<br>world</h2>

So, what is going on? I am using groovy 2.1.8, the latest one as of this writing.

解决方案

using setEscapeAttributes will stop it escaping attributes, so:

println new StringWriter().with { writer ->
    new MarkupBuilder( writer ).with {

        // Tell the builder to not escape ATTRIBUTES
        escapeAttributes = false

        html {
            h2( tim:'woo>yay' )
        }
        writer.toString()
    }
}

Will print:

<html>
  <h2 tim='woo>yay' />
</html>

as opposed to this if you comment out the escapeAttributes line above:

<html>
  <h2 tim='woo&gt;yay' />
</html>

If you want to avoid escaping content, you need to use mkp.yieldUnescaped like so:

println new StringWriter().with { writer ->
    new MarkupBuilder( writer ).with {
        html {
            h2 {
                mkp.yieldUnescaped 'hello<br>world'
            }
        }
        writer.toString()
    }
}

Which will print:

<html>
  <h2>hello<br>world</h2>
</html>

Though care should be taken, as this is obviously invalid xml (as the `
is not closed)

这篇关于MarkupBuilder的setEscapeAttributes不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆