MarkupBuilder的setEscapeAttributes不起作用? [英] MarkupBuilder's setEscapeAttributes not working?
问题描述
我期望 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 2.1.8,这是本文的最新版本。使用 / code>会停止转义 将会列印: 与此相反,如果您注释掉 如果您想避免转义内容,您需要使用 尽管应该小心,因为这显然是无效的xml(因为` I expect the foo.groovy If you run where I expect the So, what is going on? I am using groovy 2.1.8, the latest one as of this writing. using Will print: as opposed to this if you comment out the If you want to avoid escaping content, you need to use Which will print: Though care should be taken, as this is obviously invalid xml (as the ` 这篇关于MarkupBuilder的setEscapeAttributes不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! groovy foo.groovy $ c $输出:
$ p $ true
false
< h2> hello& lt;& gt; world< / h2>
< / html>
h2
一行
< h2> hello< br>世界< / h2>
setEscapeAttributes
属性
,所以:
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>
不是关闭)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: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()
groovy foo.groovy
, here is the output:true
false
<html>
<h2>hello<br>world</h2>
</html>
h2
line to be<h2>hello<br>world</h2>
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()
}
}
<html>
<h2 tim='woo>yay' />
</html>
escapeAttributes
line above:<html>
<h2 tim='woo>yay' />
</html>
mkp.yieldUnescaped
like so:println new StringWriter().with { writer ->
new MarkupBuilder( writer ).with {
html {
h2 {
mkp.yieldUnescaped 'hello<br>world'
}
}
writer.toString()
}
}
<html>
<h2>hello<br>world</h2>
</html>
is not closed)