       when "image"
         element = document.createElement("img")
         element.trixPosition = position
+        element.trixLength = 1
         element.setAttribute(key, value) for key, value of attachment when key isnt "type"
         element.style[key] = attributes[key] + "px" for key in ["width", "height"] when attributes[key]?
         element

        node = document.createElement("br")
        node.trixPosition = position
        position += 1
        nodes.push(node)

      if substring.length
        node = document.createTextNode(preserveSpaces(substring))
        node.trixPosition = position
        position += substring.length
        nodes.push(node)

    nodes

    if container.nodeType is Node.TEXT_NODE
      container.trixPosition + offset
    else
      if offset is 0
        container.trixPosition
      else
        node = container.childNodes[offset - 1]
        walker = createTreeWalker(node)
        walker.lastChild()
        walker.currentNode.trixPosition + walker.currentNode.trixLength

  findContainerAndOffsetForPosition: (position) ->
    return [@element, 0] if position < 1

    node = @findNodeForPosition(position)

    if node.nodeType is Node.TEXT_NODE
      container = node
      offset = position - node.trixPosition
    else
      container = node.parentNode
      offset = [node.parentNode.childNodes...].indexOf(node) + 1

    [container, offset]

  findNodeForPosition: (position) ->
    walker = createTreeWalker(@element)
    node = walker.currentNode

    while walker.nextNode()
      startPosition = walker.currentNode.trixPosition
      endPosition = startPosition + walker.currentNode.trixLength

      if startPosition <= position <= endPosition
        node = walker.currentNode
        break
    node

  createTreeWalker = (element) ->
    whatToShow = NodeFilter.SHOW_ELEMENT + NodeFilter.SHOW_TEXT

    acceptNode = (node) ->
      if node.trixPosition? and node.trixLength?
        NodeFilter.FILTER_ACCEPT
      else
        NodeFilter.FILTER_SKIP
