DefaultHTMLCleaner implements HTMLCleaner { @Override public Document clean(Reader originalHtmlContent) { return clean(originalHtmlContent, getDefaultConfiguration()); }  @Override Document clean(Reader originalHtmlContent); @Override Document clean(Reader originalHtmlContent, HTMLCleanerConfiguration configuration); @Override HTMLCleanerConfiguration getDefaultConfiguration();  }
@Disabled("See https: @Test void cleanTitleWithNamespace() { String input = "<html xmlns=\"http: + " <head>\n" + " <title>Title test</title>\n" + " </head>\n" + " <body>\n" + " <p>before</p>\n" + " <svg xmlns=\"http: + " <g>\n" + " <title>SVG Title Demo example</title>\n" + " <rect height=\"50\" style=\"fill:none; stroke:blue; stroke-width:1px\" width=\"200\" x=\"10\" " + "y=\"10\"></rect>\n" + " </g>\n" + " </svg>\n" + " <p>after</p>\n"; assertEquals(HEADER + input + FOOTER, HTMLUtils.toString(this.cleaner.clean(new StringReader(input)))); }
@Test void verifyEntitiesAreNotBroken() { Document document = this.cleaner.clean(new StringReader("<p>&Eacute;</p>")); String content = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals("É", content); assertHTML("<p>É</p>", "&Eacute;"); document = this.cleaner.clean(new StringReader("<p>&frac14;</p>")); content = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals("¼", content); assertHTML("<p>¼</p>", "&frac14;"); document = this.cleaner.clean(new StringReader("<p>&f!rac14;</p>")); content = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals("&f!rac14;", content); assertHTML("<p>&amp;f!rac14;</p>", "&f!rac14;"); document = this.cleaner.clean(new StringReader("<p>&frac12;</p>")); content = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals("½", content); assertHTML("<p>½</p>", "&frac12;"); }
@Test void entitiesWithTranslation() { String content = "<p>1&gt;2&amp;3&nbsp;4&frac12;5öüäăâîș</p>"; String expectedContent = "1>2&3 4½5öüäăâîș"; Document document = this.cleaner.clean(new StringReader(content)); String obtainedContent = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals(expectedContent, obtainedContent); assertHTML("<p>1&gt;2&amp;3 4½5öüäăâîș</p>", content); HTMLCleanerConfiguration htmlCleanerConfiguration = new DefaultHTMLCleanerConfiguration(); htmlCleanerConfiguration .setParameters(Collections.singletonMap(HTMLCleanerConfiguration.TRANSLATE_SPECIAL_ENTITIES, "true")); assertHTML("<p>1&amp;gt;2&amp;amp;3 4½5öüäăâîș</p>", "<p>1&gt;2&amp;3&nbsp;4&frac12;5öüäăâîș</p>", htmlCleanerConfiguration); }
@Test void escapeHTMLCharsInAttributes() throws Exception { String htmlInput = "<div foo=\"aaa&quot;bbb&amp;ccc&gt;ddd&lt;eee&apos;fff\">content</div>"; Document document = this.cleaner.clean(new StringReader(htmlInput)); String textContent = document.getElementsByTagName("div").item(0).getAttributes().getNamedItem("foo").getTextContent(); assertEquals("aaa\"bbb&ccc>ddd<eee'fff", textContent); htmlInput = "<div foo='aaa&quot;bbb&amp;ccc&gt;ddd&lt;eee&apos;fff'>content</div>"; document = this.cleaner.clean(new StringReader(htmlInput)); textContent = document.getElementsByTagName("div").item(0).getAttributes().getNamedItem("foo").getTextContent(); assertEquals("aaa\"bbb&ccc>ddd<eee'fff", textContent); assertHTML("<div foo=\"aaa&quot;bbb&amp;ccc&gt;ddd&lt;eee'fff\">content</div>", "<div foo=\"aaa&quot;bbb&amp;ccc&gt;ddd&lt;eee&apos;fff\">content</div>"); assertHTML("<div foo=\"aaa&quot;bbb&amp;ccc&gt;ddd&lt;eee'fff\">content</div>", "<div foo='aaa&quot;bbb&amp;ccc&gt;ddd&lt;eee&apos;fff'>content</div>"); }
@Test void controlCharacters() throws Exception { String htmlInput = "<p>\u0008</p>"; Document document = this.cleaner.clean(new StringReader(htmlInput)); String textContent = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals(" ", textContent); assertHTML(" ", "\u0008"); htmlInput = "<p>&#8;</p>"; document = this.cleaner.clean(new StringReader(htmlInput)); textContent = document.getElementsByTagName("p").item(0).getTextContent(); assertEquals("&#8;", textContent); assertHTML("<p>&#8;</p>", "&#8;"); htmlInput = "<p foo=\"&#8;\">content</p>"; document = this.cleaner.clean(new StringReader(htmlInput)); textContent = document.getElementsByTagName("p").item(0).getAttributes().getNamedItem("foo").getTextContent(); assertEquals("&#8;", textContent); assertHTML("<p foo=\"&#8;\">content</p>", "<p foo=\"&#8;\">content</p>"); }
@Test void transformedDOMContent() { String htmlInput = "<img src=\"http: Document document = this.cleaner.clean(new StringReader(htmlInput)); String textContent = document.getElementsByTagName("img").item(0).getAttributes().getNamedItem("src").getTextContent(); assertEquals("http: htmlInput = "<img src=\"http: document = this.cleaner.clean(new StringReader(htmlInput)); textContent = document.getElementsByTagName("img").item(0).getAttributes().getNamedItem("src").getTextContent(); assertEquals("http: }
@Test public void preserveDoubleEscapingInAttributes() throws Exception { CleanerProperties cleanerProperties = new CleanerProperties(); cleanerProperties.setDeserializeEntities(true); HtmlCleaner cleaner = new HtmlCleaner(cleanerProperties); TagNode tagNode = cleaner.clean("<?xml version = \"1.0\"?><div foo=\"&amp;quot;\">&amp;quot;</div>"); List<? extends TagNode> divList = tagNode.getElementListByName("div", true); assertEquals(1, divList.size()); assertEquals("&quot;", divList.get(0).getText().toString()); assertEquals("&quot;", divList.get(0).getAttributeByName("foo")); DomSerializer domSerializer = new DomSerializer(cleanerProperties, false); Document document = domSerializer.createDOM(tagNode); NodeList nodeList = document.getElementsByTagName("div"); assertEquals(1, nodeList.getLength()); assertEquals("&quot;", nodeList.item(0).getTextContent()); assertEquals("&quot;", nodeList.item(0).getAttributes().getNamedItem("foo").getTextContent()); document = this.cleaner.clean(new StringReader("<div foo=\"&amp;quot;\">&amp;quot;</div>")); nodeList = document.getElementsByTagName("div"); assertEquals(1, nodeList.getLength()); assertEquals("&quot;", nodeList.item(0).getTextContent()); assertEquals("&quot;", nodeList.item(0).getAttributes().getNamedItem("foo").getTextContent()); assertHTML("<div foo=\"&amp;quot;\">content</div>", "<div foo=\"&amp;quot;\">content</div>"); }
@Test @Disabled("See https: public void parseWithUnicodeChars() throws Exception { CleanerProperties cleanerProperties = new CleanerProperties(); cleanerProperties.setDeserializeEntities(true); cleanerProperties.setRecognizeUnicodeChars(true); cleanerProperties.setTranslateSpecialEntities(false); HtmlCleaner cleaner = new HtmlCleaner(cleanerProperties); TagNode tagNode = cleaner.clean("<?xml version = \"1.0\"?>" + "<div foo=\"&#169;\">&#169;</div>" + "<div foo=\"baz&gt;buz\">baz&gt;buz</div>" + "<div foo=\"baz&buz\">baz&buz</div>"); List<? extends TagNode> divList = tagNode.getElementListByName("div", true); assertEquals(3, divList.size()); assertEquals("©", divList.get(0).getText().toString()); assertEquals("©", divList.get(0).getAttributeByName("foo")); assertEquals("baz>buz", divList.get(1).getText().toString()); assertEquals("baz>buz", divList.get(1).getAttributeByName("foo")); assertEquals("baz&buz", divList.get(2).getText().toString()); assertEquals("baz&buz", divList.get(2).getAttributeByName("foo")); DomSerializer domSerializer = new DomSerializer(cleanerProperties, false); Document document = domSerializer.createDOM(tagNode); NodeList nodeList = document.getElementsByTagName("div"); assertEquals(3, nodeList.getLength()); assertEquals("©", nodeList.item(0).getTextContent()); assertEquals("©", nodeList.item(0).getAttributes().getNamedItem("foo").getTextContent()); assertEquals("baz>buz", nodeList.item(1).getAttributes().getNamedItem("foo").getTextContent()); assertEquals("baz&buz", nodeList.item(2).getAttributes().getNamedItem("foo").getTextContent()); assertEquals("baz>buz", nodeList.item(1).getTextContent()); assertEquals("baz&buz", nodeList.item(2).getTextContent()); }
@Test public void followingEncodedEntitiesAreProperlyKept() { String content = "<p><textarea>&#123;&#123;velocity}}machin&#123;&#123;/velocity}}</textarea></p>"; Document document = this.cleaner.clean(new StringReader(content)); String textareaContent = document.getElementsByTagName("textarea").item(0).getTextContent(); assertEquals("&#123;&#123;velocity}}machin&#123;&#123;/velocity}}", textareaContent); assertHTML("<p><textarea>&#123;&#123;velocity}}machin&#123;&#123;/velocity}}</textarea></p>", content); }