         @builder = @template.xml ? @template
         @stream = opts.stream ? new Stream
         @stream.readable ?= on
+        @opened_tags = 0
         @initialize()
 
     initialize: () ->

        console.warn "attributes of #{el.toString()} don't change anymore"

    onend: () ->
        @stream.emit 'end'




    write: (data) ->
        @stream.emit('data', data) if data

    close: () =>
        @builder.closed = yes
        @stream.emit 'end'

    # eventlisteners

    onadd: (parent, el) ->
        el._stream = new Entry el, parent
        @opened_tags++
        el._stream.write =>
            if el.closed is 'self'
                @write prettify el, "<#{el.name}#{attrStr el.attrs}/>"
            else
                @write prettify el, "<#{el.name}#{attrStr el.attrs}>"
        el.ready =>
            # close stream if builder is already closed
            @opened_tags--
            if @opened_tags is 0
                @closed?()
                @closed = yes

    onclose: (el) ->
        el._stream.write =>
