   appendChild: (n) ->
     @children.push n
     n.parent = @
+
+  write: ->
+    @parsed[3].apply @, []
 
 parse = (s) ->
   root = Node.parse '<root'

    prev = n
  root

write = (n) ->
  if n.type is 'empty element'
    attributes = (" #{k}=\"#{v}\"" for k, v of n.attributes).join ''
    '<' + n.name + attributes + ' />'
  else if n.type is 'element'
    attributes = (" #{k}=\"#{v}\"" for k, v of n.attributes).join ''
    children = n.children.map(write).join('')
    '<' + n.name + attributes + '>' + children + '</' + n.name + '>'
  else if n.type is 'text'
    n.name

module.exports = (s) ->
  root = parse s
  root.children.map(write).join('')

      parseAttribute
      parseText
      (s) ->
        [s, 'text', null, -> s]
    ].some (f) ->
      parsed = f node
    new Node {
      level, parsed,
      name: parsed[0], type: parsed[1], value: parsed[2]
    }

  setAttribute: (name, value) ->
    @attributes[name] = value
