 
 
 : (before-doctype-name-state) ( document n/f string ch/f -- document n'/f string )
-B
     {
         { [ dup "\t\n\f\s" member? ] [ drop before-doctype-name-state ] }
-        { [ dup ascii-upper-alpha? ] [ 0x20 + reach push-tag-name tag-name-state ] }
+        { [ dup ascii-upper-alpha? ] [ 0x20 + reach new-doctype-from-ch doctype-name-state ] }
         { [ dup CHAR: \0 = ] [
             drop
             unexpected-null-character
-            CHAR: replacement-character make-doctype-token reach doctype-token<<
+            CHAR: replacement-character reach new-doctype-from-ch
             doctype-name-state
         ] }
-        [ make-doctype-token reach doctype-token<< doctype-name-state ]
+        { [ dup CHAR: > = ] [
+            drop missing-doctype-name
+            pick [ new-doctype-with-quirks ] [ emit-doctype ] bi
+        ] }
+        { [ dup f = ] [
+            drop eof-in-doctype
+            pick [ new-doctype-with-quirks ] [ emit-doctype ] [ emit-eof ] tri
+        ] }
+        [ reach new-doctype-from-ch doctype-name-state ]
     } cond ;
 
 : before-doctype-name-state ( document n/f string -- document n'/f string )

    {
        { [ dup "\t\n\f\s" member? ] [ drop between-doctype-public-and-system-identifiers-state ] }
        { [ dup CHAR: > = ] [
            drop pick emit-doctype-token
            data-state
        ] }
        { [ dup CHAR: " = ] [

: (after-doctype-name-state) ( document n/f string ch/f -- document n'/f string )
    {
        { [ dup "\t\n\f\s" member? ] [ drop after-doctype-name-state ] }
        { [ dup CHAR: > = ] [ drop pick emit-doctype data-state ] }
        { [ dup f = ] [ eof-in-doctype ] }
        { [ [ "PUBLIC" take-from-insensitive? ] dip swap ] [ drop after-doctype-public-keyword-state ] }
        { [ [ "SYSTEM" take-from-insensitive? ] dip swap ] [ drop after-doctype-system-keyword-state ] }
