问题
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