How to remove text formatting?

29 views
Skip to first unread message

ailinykh

unread,
Nov 19, 2011, 11:05:00 PM11/19/11
to google-we...@googlegroups.com
Hello, everybody!
I have text with html formatting (originally it was created by RichTextArea). I want to show it as a plain text with no formatting. This is exactly the same as RichTextArea.ExtendedFormat.removeFormat does. But I want to do it programmatically. What is the best way?

Thank you,
  Andrey

Thomas Broyer

unread,
Nov 20, 2011, 4:51:03 AM11/20/11
to google-we...@googlegroups.com
It's actually as easy as:

    DivElement elt = Document.get().createDivElement();
    elt.setInnerHTML( SimpleHtmlSanitizer.sanitizeHtml(myHtmlString) );
    String plainTextString = elt.getInnerText();

Or you could of course use code similar to the SimpleHtmlSanitizer that strings *all* the HTML tags, instead of just the non-white-listed ones; that would save you from the setInnerHTML/getInnerText dance so could possibly perform better (and the "sanitizer" code would probably be simpler as well)

ailinykh

unread,
Nov 21, 2011, 2:14:55 AM11/21/11
to google-we...@googlegroups.com
Thank you! 
This is what I need.

Andrey
Reply all
Reply to author
Forward
0 new messages