Mike
Mike

Reputation: 1336

GWT SafeHtmlBuilder how to insert html code in a div

I don't know if I'm trying to do something against the very nature of SafeHtmlBuilder. The thing is that I'd like to put html code (for instance, an < a > tag) in a div and make it safe. So here is my code:

SafeHtmlBuilder builder = new SafeHtmlBuilder();

builder.append(TEMPLATES.diagramHeader(
    BasicConstants.diagramHeaderId + "description", 
    newBox.getDescription());

newDiv.setInnerHTML(builder.toSafeHtml().asString());

And my template:

@Template("<div id=\"{0}\">{1}</div>") /* Description */
SafeHtml diagramHeader(String idDesc, String description);

When getDescription() returns a string with html code (e.g., an < a > tag) and the contents of newDiv are rendered, I don't see the hyperlink, what I see is the HTML CODE of the hyperlink.

I would like to see the hyperlink, how can I do this? (I am willing to sacrifice HTML's safety for the cause).

Thanks!

Upvotes: 3

Views: 3664

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64541

If the description argument to the template can contain markup, then it should be of type SafeHtml.

You'd then use SafeHtmlUtils.fromTrustedString(newBox.getDescription()), as you're trusting newBox.getDescription() to be safe.


As a side note, I don't understand why:

  1. you use a SafeHtmlBuilder to append() only once
  2. you use setInnerHTML instead of setInnerSafeHtml (maybe you're not using GWT 2.5?)

Upvotes: 4

Related Questions