             el._stream_ready = yes
 
     ondata: (el, data) ->
-        el._stream.write =>
+        el._stream?.write =>
             @write data
 
     ontext: (el, text) ->
-        el._stream.write =>
+        el._stream?.write =>
             @write prettify el, text
 
     onraw: (el, html) ->
-        el._stream.write =>
+        el._stream?.write =>
             @write html
 
     onattr: (el, key, value) ->

        return unless @autoremove
        el.ready =>
            if el.closed is 'removed'
                @onremove(el) if el._stream
            else
                el.remove() # prevent memory leak

    onremove: (el) ->
        # close stream if builder is already closed
        @opened_tags--
        if @opened_tags is 0 and @builder.closed is 'pending' and not @queue.length

            delete el._stream

    onclose: (el) ->
        el._stream?.write =>
            unless el.isempty or el is el.builder
                @write prettify el, "</#{el.name}>"
            # call next callback of the registered 'ready' checker
