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

parseEmptyElement = (s) ->
  if s.match(/^<\/\S+$/)
    name = s.substring(2)
    [name, 'empty element', null, ->
      attributes = (" #{k}=\"#{v}\"" for k, v of @attributes).join ''
      '<' + name + attributes + ' />'
    ]

parseElement = (s) ->
  if s.match(/^<\S+$/)
    name = s.substring(1)
    [name, 'element', null, ->
      attributes = (" #{k}=\"#{v}\"" for k, v of @attributes).join ''
      children = @children.map((i) -> i.write()).join('')
      '<' + name + attributes + '>' + children + '</' + name + '>'
    ]

parseAttribute = (s) ->
  m = s.match(/^@(\S+)\s+(.+)$/)
  if m
    [m[1], 'attribute', m[2], null]

parseText = (s) ->
  if s.match(/^>.+$/)
    name = s.substring(1)
    [name, 'text', null, -> name]

class Node
  constructor: ({ @level, @parsed, @name, @type, @value }) ->

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

  setAttribute: (name, value) ->
