Is there a way to put raw HTML inside of a Label widget with GWT? The constructor and setText() methods automatically escape the text for HTML (so < appears as <, etc).
What I need is something like:
String matched = "two";
List<String> values = Arrays.asList("one", "two", "three");
StringBuilder sb = new StringBuilder();
for (String v : values){
if (v.equals(matched)){
sb.append("<b>" + v + "<b>");
} else {
sb.append(v);
}
sb.append(", ");
}
Label label = new Label();
label.setRawText(sb.toString());
//div contains the following HTML: "one, <b>two</b>, three, "
I want to output a comma-separated list of Strings, but I want one of those Strings to be bolded. Thanks.
Sorry, I’m going to answer my own question because I found what I was looking for.
The
SafeHtmlBuilderclass is perfect for this. You tell it what strings you want to escape and what strings you do not want to escape. It works likeStringBuilderbecause you callappendmethods:Note that the
appendHtmlConstantmethod expects a complete tag. So if you want to add attributes to the tag whose values change during runtime, it won’t work. For example, this won’t work (it throws anIllegalArgumentException):