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.
Elements that can be associated with a <label> element include <button> , <input> (except for type="hidden" ), <meter> , <output> , <progress> , <select> and <textarea> .
While a label could be substituted with a span that has an id with a value matching the input's aria-labelledby attribute, people won't be able to click the span to focus the input in the same way a label allows.
Here's example to put a space in a widget, e.g. Label:
public void setTextNbsp( final Widget w ) {
w.getElement().setInnerHTML( " " );
}
Other HTML entities could be used as well. Take care with this not to open security holes. SafeHtml, etc. might need consideration if doing something more dynamic.
Use the HTML class (Or more likely: The InlineHTML class instead of the Label class. InlineHTML works like label, except that you can give it html.
And just a security warning: if part of the input to your InlineHTML object is input by the user, remember to strip html out of that part, so users can't insert their own scripts into your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With