@Test public void testRemoveEmptyNodesByConvertXmlSerializedHtmlToLegalHtml() { String[] tags = new String[] { "b", "strong", "em", "i", "u", "li", "ul", "ol", "a", "div", "blockquote" }; for (String tag : tags) { String out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <" + tag + "/>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <" + tag + "></" + tag + ">test"); Assert.assertEquals(out, "lorem ipsum test"); } String out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <b />test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <b ></b>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <ol><li/></ol>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <ul><li></li></ul>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper .convertXmlSerializedHtmlToLegalHtml("lorem ipsum <div><div><div/></div></div>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper .convertXmlSerializedHtmlToLegalHtml("lorem ipsum <a href=\"example.com/test/\"/>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper .convertXmlSerializedHtmlToLegalHtml("lorem ipsum <a href=\"example.com/test/\" target=\"blank\"></a>test"); Assert.assertEquals(out, "lorem ipsum test"); out = HTMLHelper.convertXmlSerializedHtmlToLegalHtml("lorem ipsum <div>content</div>test"); Assert.assertEquals(out, "lorem ipsum <div>content</div>test"); }
public static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml) { boolean scanForEmptyNodes = true; while (scanForEmptyNodes) { int length = xmlSerializedHtml.length(); for (String tagName : NODES_TO_REMOVE_WHEN_EMPTY) { xmlSerializedHtml = xmlSerializedHtml.replaceAll("<" + tagName + "(?:\\s*|\\s[^>]+)(?:/\\s*>|></" + tagName + ">)", ""); } scanForEmptyNodes = length != xmlSerializedHtml.length(); } xmlSerializedHtml = xmlSerializedHtml.replaceAll("&#8203;", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("\u200B", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("<p\\s*(?:/\\s*>|>\\s*</p>)", "<p><br/></p>"); return xmlSerializedHtml.replaceAll("\u00A0", "&#160;"); }
HTMLHelper { public static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml) { boolean scanForEmptyNodes = true; while (scanForEmptyNodes) { int length = xmlSerializedHtml.length(); for (String tagName : NODES_TO_REMOVE_WHEN_EMPTY) { xmlSerializedHtml = xmlSerializedHtml.replaceAll("<" + tagName + "(?:\\s*|\\s[^>]+)(?:/\\s*>|></" + tagName + ">)", ""); } scanForEmptyNodes = length != xmlSerializedHtml.length(); } xmlSerializedHtml = xmlSerializedHtml.replaceAll("&#8203;", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("\u200B", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("<p\\s*(?:/\\s*>|>\\s*</p>)", "<p><br/></p>"); return xmlSerializedHtml.replaceAll("\u00A0", "&#160;"); } }
HTMLHelper { public static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml) { boolean scanForEmptyNodes = true; while (scanForEmptyNodes) { int length = xmlSerializedHtml.length(); for (String tagName : NODES_TO_REMOVE_WHEN_EMPTY) { xmlSerializedHtml = xmlSerializedHtml.replaceAll("<" + tagName + "(?:\\s*|\\s[^>]+)(?:/\\s*>|></" + tagName + ">)", ""); } scanForEmptyNodes = length != xmlSerializedHtml.length(); } xmlSerializedHtml = xmlSerializedHtml.replaceAll("&#8203;", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("\u200B", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("<p\\s*(?:/\\s*>|>\\s*</p>)", "<p><br/></p>"); return xmlSerializedHtml.replaceAll("\u00A0", "&#160;"); } private  HTMLHelper(); }
HTMLHelper { public static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml) { boolean scanForEmptyNodes = true; while (scanForEmptyNodes) { int length = xmlSerializedHtml.length(); for (String tagName : NODES_TO_REMOVE_WHEN_EMPTY) { xmlSerializedHtml = xmlSerializedHtml.replaceAll("<" + tagName + "(?:\\s*|\\s[^>]+)(?:/\\s*>|></" + tagName + ">)", ""); } scanForEmptyNodes = length != xmlSerializedHtml.length(); } xmlSerializedHtml = xmlSerializedHtml.replaceAll("&#8203;", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("\u200B", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("<p\\s*(?:/\\s*>|>\\s*</p>)", "<p><br/></p>"); return xmlSerializedHtml.replaceAll("\u00A0", "&#160;"); } private  HTMLHelper(); static String closeBrTags(String htmlContent); static boolean containsNonEmptyTextNodes(String htmlContent); static String convertHTMLListToText(String htmlContent); static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml); static String encapsulateAttributesInQuotes(String htmlContent); static String htmlToPlaintext(String htmlContent); static String htmlToPlaintextExt(String htmlContent, boolean convertLists); static String plaintextToHTML(String plainText); static String removeComments(String htmlContent); static String removeHeadElement(String htmlContent); static String removeNamespaces(String htmlContent); static String removeUnclosedMetaElements(String htmlContent); static String stripLinebreaks(String htmlContent); }
HTMLHelper { public static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml) { boolean scanForEmptyNodes = true; while (scanForEmptyNodes) { int length = xmlSerializedHtml.length(); for (String tagName : NODES_TO_REMOVE_WHEN_EMPTY) { xmlSerializedHtml = xmlSerializedHtml.replaceAll("<" + tagName + "(?:\\s*|\\s[^>]+)(?:/\\s*>|></" + tagName + ">)", ""); } scanForEmptyNodes = length != xmlSerializedHtml.length(); } xmlSerializedHtml = xmlSerializedHtml.replaceAll("&#8203;", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("\u200B", StringUtils.EMPTY); xmlSerializedHtml = xmlSerializedHtml.replaceAll("<p\\s*(?:/\\s*>|>\\s*</p>)", "<p><br/></p>"); return xmlSerializedHtml.replaceAll("\u00A0", "&#160;"); } private  HTMLHelper(); static String closeBrTags(String htmlContent); static boolean containsNonEmptyTextNodes(String htmlContent); static String convertHTMLListToText(String htmlContent); static String convertXmlSerializedHtmlToLegalHtml(String xmlSerializedHtml); static String encapsulateAttributesInQuotes(String htmlContent); static String htmlToPlaintext(String htmlContent); static String htmlToPlaintextExt(String htmlContent, boolean convertLists); static String plaintextToHTML(String plainText); static String removeComments(String htmlContent); static String removeHeadElement(String htmlContent); static String removeNamespaces(String htmlContent); static String removeUnclosedMetaElements(String htmlContent); static String stripLinebreaks(String htmlContent); static final Set<String> NODES_TO_REMOVE_WHEN_EMPTY; static final String LINE_TERMINATORS_REGEX_CLASS; }