@@ -155,6 +155,12 @@ public void endElement(final QName element, final Augmentations augs) throws XNI
155155 }
156156 }
157157
158+ @ Override
159+ public void doctypeDecl (final String root ,
160+ final String publicId , final String systemId , final Augmentations augs ) throws XNIException {
161+ collectedStrings_ .add ("doctype " + publicId + " - " + systemId );
162+ }
163+
158164 public List <String > getCollectedStrings () {
159165 return collectedStrings_ ;
160166 }
@@ -345,4 +351,47 @@ public int read(final char[] cbuf, final int off, final int len) throws IOExcept
345351 assertEquals (String .join (System .lineSeparator (), expected ), out .toString ().trim ());
346352 }
347353 }
354+
355+ /**
356+ * @throws Exception on error
357+ */
358+ @ Test
359+ public void insertDoctype () throws Exception {
360+ final String string = "" ;
361+
362+ final HTMLConfiguration parser = new HTMLConfiguration ();
363+ parser .setFeature (HTMLScanner .INSERT_DOCTYPE , true );
364+
365+ final EvaluateInputSourceFilter filter = new EvaluateInputSourceFilter (parser );
366+ parser .setProperty ("http://cyberneko.org/html/properties/filters" , new XMLDocumentFilter [] {filter });
367+ final XMLInputSource source = new XMLInputSource (null , "myTest" , null , new StringReader (string ), "UTF-8" );
368+ parser .parse (source );
369+
370+ final String [] expected = {
371+ "doctype -//W3C//DTD HTML 4.01 Transitional//EN - http://www.w3.org/TR/html4/loose.dtd" ,
372+ "(html" , "(head" , ")head" , "(body" , ")body" , ")html" };
373+ assertEquals (Arrays .asList (expected ).toString (), filter .getCollectedStrings ().toString ());
374+ }
375+
376+ /**
377+ * @throws Exception on error
378+ */
379+ @ Test
380+ public void insertDoctypeUppercase () throws Exception {
381+ final String string = "" ;
382+
383+ final HTMLConfiguration parser = new HTMLConfiguration ();
384+ parser .setFeature (HTMLScanner .INSERT_DOCTYPE , true );
385+ parser .setProperty (HTMLScanner .NAMES_ELEMS , "upper" );
386+
387+ final EvaluateInputSourceFilter filter = new EvaluateInputSourceFilter (parser );
388+ parser .setProperty ("http://cyberneko.org/html/properties/filters" , new XMLDocumentFilter [] {filter });
389+ final XMLInputSource source = new XMLInputSource (null , "myTest" , null , new StringReader (string ), "UTF-8" );
390+ parser .parse (source );
391+
392+ final String [] expected = {
393+ "doctype -//W3C//DTD HTML 4.01 Transitional//EN - http://www.w3.org/TR/html4/loose.dtd" ,
394+ "(HTML" , "(HEAD" , ")HEAD" , "(BODY" , ")BODY" , ")HTML" };
395+ assertEquals (Arrays .asList (expected ).toString (), filter .getCollectedStrings ().toString ());
396+ }
348397}
0 commit comments