How to unescape string in GWT

*爱你&永不变心* 提交于 2019-12-23 13:23:16

问题


I used SafeHtmlUtils.htmlEscape(text) and I want to use opposite function. Could you tell me if in gwt there is function like unescapeHtml()


回答1:


If (and please only if) you can trust the text not to contain malicious content, you can use

import com.google.gwt.user.client.ui.HTML;

String unescaped = new HTML(text).getText();



回答2:


If nothing has been done to do this trick (say some GWT project around), you can implement it in a JSNI method using javascript snip you can found here : [CSS/HTML/Js] Unescaping HTML in javascript




回答3:


I wanted something easy to unit test so I implemented 'htmlUnescape()' which reverses the effects of SafeHtmlUtils.htmlEscape() and SafeHtmlBuilder.appendEscapedLines(). They all exist in the 'shared' package and work fine in a JVM.

This utility class might be useful to you...

private static class Replacer {
    private final String target;
    private final String replacement;
    private final RegExp re;

    private Replacer(String target, String replacement) {
        this.target = target;
        this.replacement = replacement;
        re = RegExp.compile(RegExp.quote(target), "g");
    }

    public String process(String s) {
        if (s.contains(target)) return re.replace(s, replacement);
        return s;
    }
}


来源:https://stackoverflow.com/questions/11537003/how-to-unescape-string-in-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!