Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT: Putting raw HTML inside a Label

Tags:

java

gwt

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 &lt;, 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.

like image 701
Michael Avatar asked Oct 07 '11 20:10

Michael


People also ask

What can go inside label HTML?

Elements that can be associated with a <label> element include <button> , <input> (except for type="hidden" ), <meter> , <output> , <progress> , <select> and <textarea> .

Can we use span inside label?

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.


2 Answers

Here's example to put a space in a widget, e.g. Label:

public void setTextNbsp( final Widget w ) { 
    w.getElement().setInnerHTML( "&nbsp;" );
}

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.

like image 191
user2661223 Avatar answered Nov 07 '22 00:11

user2661223


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.

like image 33
MTilsted Avatar answered Nov 07 '22 02:11

MTilsted