Class SimplifyHtmlFilter

java.lang.Object
org.shredzone.commons.text.filter.SimplifyHtmlFilter
All Implemented Interfaces:
Function<CharSequence,​CharSequence>, TextFilter

public class SimplifyHtmlFilter extends Object implements TextFilter
A filter that simplifies a HTML text. Only a given set of HTML tags (and attributes) are accepted. Other HTML tags are completely removed.

This filter can be used to allow a site visitor to enter marked-up text, but remove everything that might be harmful or induce Cross Site Scripting.

Author:
Richard "Shred" Körber
  • Constructor Details

  • Method Details

    • addAcceptedTag

      public void addAcceptedTag(String tag)
      Adds a tag that is accepted by this filter, with all its attributes.
      Parameters:
      tag - HTML tag that is accepted (without angle brackets, e.g. "strong")
    • addAcceptedTag

      public void addAcceptedTag(String tag, String... attributes)
      Adds a tag that is accepted by this filter, along with accepted attributes.
      Parameters:
      tag - HTML tag that is accepted (without angle brackets, e.g. "img")
      attributes - an array of accepted attributes (e.g. "src", "alt")
    • apply

      public CharSequence apply(CharSequence text)
      Description copied from interface: TextFilter
      Applies the filter on a CharSequence and returns a new CharSequence with the modified text.
      Specified by:
      apply in interface Function<CharSequence,​CharSequence>
      Specified by:
      apply in interface TextFilter
      Parameters:
      text - CharSequence with the contents to be filtered. If this is a StringBuilder instance, its contents may have changed after invocation, and this instance should not be used any more.
      Returns:
      CharSequence with the filtered text.