Menu

#215 INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified

v2.23
closed-fixed
nobody
None
5
2019-08-23
2019-08-02
Code Buddy
No

Using htmlcleaner on real world HTML I've stumbed across the following issue. I've created a minimal HTML example that triggers it along with a test case.

public class AppTest 
{
    final String HTML = "<html>"

            + "<body>"
            + "<img srcset=\"<svg%20\">"
            + "</body>"
            + "</html>";
    final boolean ESCAPE_XML = false;

    @Test
    public void testApp()
    {
        final TagNode tagNode = new HtmlCleaner().clean(HTML);
        System.out.println(tagNode);
        final CleanerProperties cleanerProperties = new CleanerProperties();
        System.out.println(cleanerProperties);
        try
        {
            final Document doc = new DomSerializer(cleanerProperties, false).createDOM(tagNode);
            System.out.println(doc);
        }
        catch (ParserConfigurationException e)
        {
            fail(e.toString());
        }
    }
}

Causing the following:

org.w3c.dom.DOMException: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. 
    at com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElement(CoreDocumentImpl.java:665)
    at org.htmlcleaner.DomSerializer.createSubnodes(DomSerializer.java:306)
    at org.htmlcleaner.DomSerializer.createSubnodes(DomSerializer.java:338)
    at org.htmlcleaner.DomSerializer.createDOM(DomSerializer.java:215)
    at com.github.liamsharp.AppTest.testApp(AppTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

You can crab the code here:
https://github.com/liamsharp/htmlcleaner/tree/invalid-character-err

Discussion

  • Scott Wilson

    Scott Wilson - 2019-08-02

    Thanks for the report! This may be related to #210.

     
  • Scott Wilson

    Scott Wilson - 2019-08-23

    I've tracked this down and fixed it - HtmlCleaner was following the HTML5 rules for valid tag names, however when outputting to DOM we have to follow the much stricter rules for XML Element Names. I now replace any invalid characters in tag names with underscores when outputting to DOM.

     
  • Scott Wilson

    Scott Wilson - 2019-08-23
    • status: open --> closed-fixed
    • Group: v 2.7 --> v2.23
     

Log in to post a comment.