SafeHtmlBuilder能否在保持SafeHtml不变(GWT)的情况下退出SafeHtml?
这是我的要求。我有一个文本框&用户可以在文本框中键入任何内容,系统会将其转换为html。但是,它只转换SafeHtmlBuilder能否在保持SafeHtml不变(GWT)的情况下退出SafeHtml?,gwt,Gwt,这是我的要求。我有一个文本框&用户可以在文本框中键入任何内容,系统会将其转换为html。但是,它只转换或标记&忽略所有其他标记。该文本的结果将被放入标记中 例如,文本框中的用户类型:文本测试它将输出: 文本测试 因此,如果用户在文本框中键入,那么系统应该足够聪明,知道它应该转义该,但是它必须将放在最后一个字符串中 代码可能是这样的。我使用SimpleHtmlSanitizer来清理字符串&只允许或 SafeHtml mySafeHtml=MySimpleHtmlSanitizer.sanitiz
或标记&忽略所有其他标记。该文本的结果将被放入
标记中
例如,文本框中的用户类型:文本测试
它将输出:
文本测试
因此,如果用户在文本框中键入
,那么系统应该足够聪明,知道它应该转义该
,但是它必须将
放在最后一个字符串中
代码可能是这样的。我使用SimpleHtmlSanitizer来清理字符串&只允许或
SafeHtml mySafeHtml=MySimpleHtmlSanitizer.sanitizeHtml(“文本测试”);
因此,如果我打印出mySafeHtml,它将显示如下:
文本测试
但是,如何使该字符串包含在标记中呢
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendHtmlConstant("<h1>");
builder.appendEscaped(mySafeHtml); // err here cos SafeHtmlBuilder does not excapse SafeHtml?
builder.appendHtmlConstant("</h1>");
SafeHtmlBuilder builder=new SafeHtmlBuilder();
生成器。附录HTMLConstant(“”);
builder.appendEscaped(mySafeHtml);//因为SafeThmlBuilder不退出SafeThml,所以在这里出错?
生成器。附录HTMLConstant(“”);
那么如何解决我的问题呢?我的看法是这样的,为什么不检查一下您的mySafeHtml
是否以
开头,然后有条件地追加
例如:
SafeHtmlBuilder builder = new SafeHtmlBuilder();
//check if your `mySafeHtml` starts and ends with <h1>
if( (mySafeHtml.startsWith("<h1>")
|| mySafeHtml.startsWith("<H1>"))
&& (mySafeHtml.endsWith("</h1>")
|| mySafeHtml.endsWith("</H1>"))
)
{
builder.appendEscaped(mySafeHtml); // err here cos SafeHtmlBuilder does not excapse
}
else
{
builder.appendHtmlConstant("<h1>");
builder.appendEscaped(mySafeHtml); // err here cos SafeHtmlBuilder does not excapse SafeHtml?
builder.appendHtmlConstant("</h1>");
}
SafeHtmlBuilder builder=new SafeHtmlBuilder();
//检查您的'mySafeHtml'是否以
if((mySafeHtml.startsWith(“”)
||mySafeHtml.startsWith(“”)
&&(mySafeHtml.endsWith(“”)
||mySafeHtml.endsWith(“”)
)
{
appendescape(mysafehttml);//此处出错,因为SafeHtmlBuilder不退出
}
其他的
{
生成器。附录HTMLConstant(“”);
appendEscaped(mySafeHtml);//这里出错,因为SafeHtmlBuilder没有将SafeHtml解封?
生成器。附录HTMLConstant(“”);
}
我找到了一个更好的解决方案,那就是将所有内容替换为“h1”,然后追加并清理,但这次我需要更改白名单标签以包含h1。不管怎样,谢谢你
SafeHtmlBuilder builder = new SafeHtmlBuilder();
//check if your `mySafeHtml` starts and ends with <h1>
if( (mySafeHtml.startsWith("<h1>")
|| mySafeHtml.startsWith("<H1>"))
&& (mySafeHtml.endsWith("</h1>")
|| mySafeHtml.endsWith("</H1>"))
)
{
builder.appendEscaped(mySafeHtml); // err here cos SafeHtmlBuilder does not excapse
}
else
{
builder.appendHtmlConstant("<h1>");
builder.appendEscaped(mySafeHtml); // err here cos SafeHtmlBuilder does not excapse SafeHtml?
builder.appendHtmlConstant("</h1>");
}