HerdStringUtils { public static String stripHtml(String fragment, String... whitelistTags) { String unEscapedFragment = StringEscapeUtils.unescapeHtml4(fragment); Document dirty = Jsoup.parseBodyFragment(unEscapedFragment); Whitelist whitelist = new Whitelist(); for (String whitelistTag : whitelistTags) { whitelistTag = StringUtils.removePattern(whitelistTag, "[^\\{IsAlphabetic}]"); whitelist.addTags(whitelistTag).addAttributes(whitelistTag, "class"); } Cleaner cleaner = new Cleaner(whitelist); Document clean = cleaner.clean(dirty); clean.outputSettings().escapeMode(Entities.EscapeMode.base).charset(StandardCharsets.UTF_8).prettyPrint(false); return clean.body().html(); }  static String decodeBase64(String base64EncodedText); static String getShortDescription(String description, Integer shortDescMaxLength); static String stripHtml(String fragment, String... whitelistTags); static void checkCsvInjection(String text, String errorMessage); static Integer convertStringToInteger(final String stringValue, final Integer defaultValue); static String sanitizeLogText(String loggingText); static final String HIDDEN_TEXT; }
@Test public void testStripHtmlDirtyFragment() { String result = HerdStringUtils.stripHtml("<li>fragment with <b>html</b></li>"); assertEquals("fragment with html", result); }
@Test public void testStripHtmlCleanFragment() { String result = HerdStringUtils.stripHtml("fragment\nwith no html"); assertEquals("fragment\nwith no html", result); }
@Test public void testStripHtmlDirtyFragmentWithPartialTags() { String result = HerdStringUtils.stripHtml("fragment<li><b> with no</b> html<l"); assertEquals("fragment with no html", result); }
@Test public void testStripHtmlDirtyFragmentWithEscapedHtml() { String result = HerdStringUtils.stripHtml("&lt;li&gt;fragment with escaped &lt;b&gt;html&lt;/b&gt;&lt;/li&gt;"); assertEquals("fragment with escaped html", result); }
@Test public void testStripHtmlDirtyFragmentWithEscapedHtmlAndUnrecognizedEntity() { String result = HerdStringUtils.stripHtml("&lt;li&gt;fragment with escaped &lt;b&gt;html&lt;/b&gt;&lt;/li&gt; & unrecognized entity &zzz;x"); assertEquals("fragment with escaped html &amp; unrecognized entity &amp;zzz;x", result); }
@Test public void testStripHtmlDirtyFragmentWithWhitelist() { String result = HerdStringUtils.stripHtml("fragment<li><b> with <hlt>no</hlt></b> html</li>", "<hlt>"); assertEquals("fragment with <hlt>no</hlt> html", result); }
@Test public void testStripHtmlDirtyFragmentWithWhitelistWithStyle() { String result = HerdStringUtils.stripHtml("fragment<li><b> with <hlt class=\"highlight\">no</hlt></b> html</li>", "<hlt>"); assertEquals("fragment with <hlt class=\"highlight\">no</hlt> html", result); }
@Test public void testStripHtmlDirtyFragmentWithMultipleWhitelistTags() { String result = HerdStringUtils.stripHtml("fragment<li><b> with <hlt>no</hlt></b> html</li>", "<hlt>", "<b>"); assertEquals("fragment<b> with <hlt>no</hlt></b> html", result); }