From: Alain G. <AGi...@ta...> - 2010-11-10 19:30:50
|
Hi, I have a test case showing that the ElementRemover seems to get out of sync after having removed an element. Can someone check that I am using this class correctly? Regards, Alain Gilbert Follows my JUnit test class: package org.cyberneko.html; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import junit.framework.TestCase; import org.apache.xerces.xni.parser.XMLInputSource; import org.apache.xerces.xni.parser.XMLParserConfiguration; import org.cyberneko.html.filters.ElementRemover; import org.cyberneko.html.filters.Writer; public class ElementRemoverTest extends TestCase { private static final String DOCUMENT_FRAGMENT = "http://cyberneko.org/html/features/balance-tags/document-fragment"; private static final String IGNORE_OUTSIDE_CONTENT = "http://cyberneko.org/html/features/balance-tags/ignore-outside-content"; protected static final String NAMES_ELEMS = "http://cyberneko.org/html/properties/names/elems"; private static final String FILTERS = "http://cyberneko.org/html/properties/filters"; public void testRemoveClosedElement() throws IOException { String src = "<input type=\"text\">foo</input><span>bof</span>"; String expected = "<span>bof</span>"; assertEquals(expected, parseFragment(src)); } public void testRemoveEmptyElement() throws IOException { String src = "<input type=\"text\"/><span>bof</span>"; String expected = "<span>bof</span>"; assertEquals(expected, parseFragment(src)); } public void testRemoveUnclosedElement() throws IOException { String src = "<input type=\"text\"><span>bof</span>"; String expected = "<span>bof</span>"; assertEquals(expected, parseFragment(src)); } private String parseFragment(String pHTMLFragment) throws IOException { XMLParserConfiguration parser = new HTMLConfiguration(); // Enable HTML fragments. parser.setFeature(DOCUMENT_FRAGMENT, true); parser.setFeature(IGNORE_OUTSIDE_CONTENT, true); // Element names to lowercase. parser.setProperty(NAMES_ELEMS, "lower"); StringWriter out = new StringWriter(); ElementRemover remover = new ElementRemover(); remover.removeElement("input"); remover.acceptElement("span", null); org.apache.xerces.xni.parser.XMLDocumentFilter[] filters = {remover, new Writer(out, "UTF-8") }; parser.setProperty(FILTERS, filters); XMLInputSource source = new XMLInputSource(null, "ElementRemoval", null, new StringReader(pHTMLFragment), "UTF-8"); parser.parse(source); return out.toString(); } } |