 : 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

tree
tree-insert-mode
doctype-token
tag-name
attribute-name
attribute-value

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

: current-attribute ( document -- attribute/f )
    [ attribute-name>> >string f like ]
    [ attribute-value>> >string f like ] bi
    2dup or [ 2array ] [ 2drop f ] if ;

: push-when ( obj/f seq -- )
    over [ push ] [ 2drop ] if ; inline

: reset-attribute ( document -- )
    SBUF" " clone >>attribute-name
    SBUF" " clone >>attribute-value drop ;

: push-attribute ( document -- )
    [ current-attribute ]
    [ tag>> attributes>> push-when ]
    [ reset-attribute ] tri ;

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