GWT SafeHtmlBuilder如何在div中插入html代码 [英] GWT SafeHtmlBuilder how to insert html code in a div

查看:179
本文介绍了GWT SafeHtmlBuilder如何在div中插入html代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否试图对付SafeHtmlBuilder本质。问题是,我想将html代码(例如,< a>标签)放在div中并使其安全。所以这里是我的代码:

  SafeHtmlBuilder builder = new SafeHtmlBuilder(); 

uilder.append(TEMPLATES.diagramHeader(
BasicConstants.diagramHeaderId +description,
newBox.getDescription());

newDiv.setInnerHTML (builder.toSafeHtml()。asString());

我的模板:

  @Template(< div id = \{0} \> {1}< / div>)/ *描述* / 
SafeHtml diagramHeader(String idDesc,String description);

当getDescription )返回一个带有html代码的字符串(例如,< a>标记),并且呈现newDiv的内容,我没有看到超链接,我看到的是超链接的HTML代码。



我希望看到这个超链接,我该如何做到这一点?(我愿意牺牲HTML的安全性)。

Thanks!

解决方案

如果模板的 description 参数可以包含标记,那么它应该是类型 SafeHtml



然后,您可以使用 SafeHtmlUtils.fromTrustedString(newBox.getDescription()) , '重新相信 newBox.getDescription()安全。




附带说明,我不明白为什么:


  1. 您使用 SafeHtmlBuilder append()只有一次

  2. 使用 setInnerHTML 而不是 setInnerSafeHtml (也许你没有使用GWT 2.5?)


I don't know if I'm trying to do something against the very nature of SafeHtmlBuilder. The thing is that I'd like to put html code (for instance, an < a > tag) in a div and make it safe. So here is my code:

SafeHtmlBuilder builder = new SafeHtmlBuilder();

builder.append(TEMPLATES.diagramHeader(
    BasicConstants.diagramHeaderId + "description", 
    newBox.getDescription());

newDiv.setInnerHTML(builder.toSafeHtml().asString());

And my template:

@Template("<div id=\"{0}\">{1}</div>") /* Description */
SafeHtml diagramHeader(String idDesc, String description);

When getDescription() returns a string with html code (e.g., an < a > tag) and the contents of newDiv are rendered, I don't see the hyperlink, what I see is the HTML CODE of the hyperlink.

I would like to see the hyperlink, how can I do this? (I am willing to sacrifice HTML's safety for the cause).

Thanks!

解决方案

If the description argument to the template can contain markup, then it should be of type SafeHtml.

You'd then use SafeHtmlUtils.fromTrustedString(newBox.getDescription()), as you're trusting newBox.getDescription() to be safe.


As a side note, I don't understand why:

  1. you use a SafeHtmlBuilder to append() only once
  2. you use setInnerHTML instead of setInnerSafeHtml (maybe you're not using GWT 2.5?)

这篇关于GWT SafeHtmlBuilder如何在div中插入html代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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