 : emit-string ( char document -- ) drop "emit-string: " write . ;
 : emit-tag ( document -- )
     "emit tag: " write
-    [ tag-name>> >string . ]
-    [ SBUF" " clone >>tag-name drop ] bi ;
+    {
+        [ [ tag-name>> >string ] [ tag>> name<< ] bi ]
+        [ push-attribute ]
+        [ tag>> . ]
+        [ <tag> >>tag drop ]
+        [ SBUF" " clone >>tag-name drop ]
+    } cleave ;
 : emit-end-tag ( document -- ) "emit end tag: " write . ;
 : emit-doctype-token ( document -- )
     "emit doctype: " write

: push-comment-token ( ch document -- ) comment-token>> push ;
: push-all-comment-token ( string document -- ) comment-token>> push-all ;

: flush-temporary-buffer ( document -- )
    "flushing character-reference: " write
    [ temporary-buffer>> >string . ]

: (attribute-value-single-quoted-state) ( document n/f string ch/f -- document n'/f string )
    {
        { [ dup CHAR: ' = ] [ drop after-attribute-value-quoted-state ] }
        { [ dup CHAR: & = ] [
            drop [ \ attribute-value-single-quoted-state >>return-state ] 2dip
            character-reference-state
        ] }
        { [ dup CHAR: \0 = ] [
            drop unexpected-null-character
            CHAR: replacement-character reach push-attribute-value
        ] }
        { [ dup f = ] [ eof-in-tag ] }
        [ reach push-attribute-value attribute-value-single-quoted-state ]
    } cond ;
