When you use the cleaner on a link with multiple sub-nodes, the cleaner only applies the link to one of the children. For example: <a><h3>Test</h3><p>Test2</p></a> Gets transformed to <a></a><h3><a>Test</a></h3><p>Test2</p>, where the <p> node is no longer a link. Sample code is below
import java.io.IOException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleHtmlSerializer;
import org.htmlcleaner.TagNode;
public class HtmlCleanerTest {
public static String clean (String html) {
final CleanerProperties props = new CleanerProperties();
final HtmlCleaner cleaner = new HtmlCleaner(props);
final SimpleHtmlSerializer htmlSerializer =
new SimpleHtmlSerializer(props);
TagNode rootNode = cleaner.clean(html);
try {
return htmlSerializer.getAsString(rootNode);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static void main(String[] args) {
String html = "<html><body><a href=\"test.html\"> <h3> Test </h3> <p> Hi </p> </a></body></html>";
System.out.println(clean(html));
}
}