简体   繁体   English

使用 UiBinder 时是否需要 GWT SafeHtml?

[英]Is there a need for GWT SafeHtml when using UiBinder?

Let's say you have the following MyPanel.ui.xml :假设您有以下MyPanel.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:gwt="urn:import:com.google.gwt.user.client.ui">
    <div>
        <span id="content">Some content</span>

        <gwt:RadioButton ...>
            ...
        </gwt:RadioButton>

        <!-- etc. -->
    </div>
</ui:UiBinder>

And this "maps" to MyPanel.java :这“映射”到MyPanel.java

public class MyPanel extends Composite {
    private RadioButton radioButton;
    // ...
}

Then are there any use cases where you would want/need to use SafeHtml or SafeHtmlBuilder, or is the "Safe*" API only needed when working with HTML objects and their underlying DOM structures?那么是否有任何您想要/需要使用 SafeHtml 或 SafeHtmlBuilder 的用例,或者仅在处理HTML对象及其底层 DOM 结构时才需要“Safe*”API?

If there are use cases where UiBinder-backed composites would need to use Safe*, perhaps a simple code example would help me connect the dots.如果有 UiBinder 支持的复合材料需要使用 Safe* 的用例,也许一个简单的代码示例可以帮助我连接点。 Thanks in advance!提前致谢!

A simple example where you should use SafeHTML in conjunction with UiBinder:您应该将 SafeHTML 与 UiBinder 结合使用的简单示例:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:gwt="urn:import:com.google.gwt.user.client.ui">
    <gwt:HTMLPanel>
        <gwt:HTML ui:field="myHtml"/>
    </g:HTMLPanel>
</ui:UiBinder>
public class MyPanel extends Composite {
    private HTML myHtml;
    // ...
}

Here you should use myHtml.setHTML(SafeHTML) [*] .在这里你应该使用myHtml.setHTML(SafeHTML) [*] The reason for that is, that this is the only place in the example, where user provided content might occur.原因是,这是示例中唯一可能出现用户提供内容的地方。 User content can't occur in the UiBinder template itself (because that's static: fixed at compile time).用户内容不能出现在 UiBinder 模板本身中(因为它是静态的:在编译时固定)。

So the difference between requiring SafeHTML or not, is equivalent to the difference between trusting user provided content vs. trusting developer provided content.因此,是否需要 SafeHTML 之间的区别相当于信任用户提供的内容与信任开发人员提供的内容之间的区别。

[*] In your own example, you should use one of RadioButton's SafeHTML constructors [*] 在您自己的示例中,您应该使用 RadioButton 的 SafeHTML 构造函数之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM