       node.__indentLocation.end = node.__location.startTag.end
 
     if not node.__location.endTag
-      node.__location.end = node.__indentLocation.end
+      # Forcing object copy because
+      # sometimes parse5 reuses location info
+      # for semi-fake nodes.
+      node.__location = {
+        start: node.__location.start
+        end: node.__indentLocation.end
+      }
 
     @setAttribs node, @text[node.__location.start...node.__indentLocation.start]
 
-  trimText: (node, lastPosition) ->
+  getEndPoint: (node) ->
+    if node.nodeName in ['#document', '#document-fragment']
+      return @text.length
+    last = null
+    parent = node.parentNode
+    ind = parent.childNodes.indexOf node
+    if ind is parent.childNodes.length - 1
+      last = if parent.__location?.endTag? then parent.__location.endTag.start else @getEndPoint parent
+    else
+      last = parent.childNodes[ind+1].__location.start
+    return last
+
+  trimText: (node) ->
     location = node.__location
-    location.end = Math.min location.end, lastPosition
+    location.end = Math.min location.end, @getEndPoint node
     text = @text[location.start...location.end].split '\n'
     i = 0
     while text[i].trim().length is 0

      if node.value.trim().length is 0
        node.remove = true
      else
        @trimText node, lastPosition
      return

    if node.nodeName is '#comment'

    if parseContext and parseContext not in ['html', 'head', 'body']
      root = htmlParser.parseFragment @text
      @cleanTree root
      @fixBounds root
    else
      root = htmlParser.parse @text
      @cleanTree root
      @fixBounds root
      if root.childNodes.length is 0
        root = htmlParser.parseFragment @text
        @cleanTree root
        @fixBounds root
    window.root = root
    window.parse5 = htmlParser
    @mark 0, root, 0, null

  mark: (indentDepth, node, depth, bounds, nomark = false) ->

    switch node.type
      when 'document'
        lastChild = null
        for child in node.childNodes
          if lastChild and lastChild.__location.end > child.__location.start then nomark = true else nomark = false
          @mark indentDepth, child, depth + 1, null, nomark
          unless nomark
            lastChild = child

      when 'emptyTag'
        unless nomark
          @htmlBlock node, depth, bounds
          for attrib in node.attributes
            if attrib.end - attrib.start > 1
              @htmlSocket node, depth + 1, null, @genBounds(attrib), ATTRIBUTE_CLASSES

      when 'blockTag'
        unless nomark
          @htmlBlock node, depth, bounds
          for attrib in node.attributes
            @htmlSocket node, depth + 1, null, @genBounds(attrib), ATTRIBUTE_CLASSES
          indentBounds = @makeIndentBounds node
          if indentBounds.start.line isnt indentBounds.end.line or indentBounds.start.column isnt indentBounds.end.column
            depth++
            prefix = @getIndentPrefix(indentBounds, indentDepth, depth)
            indentDepth += prefix.length
            @addIndent
              bounds: indentBounds
              depth: depth
              prefix: prefix
              classes: @getClasses node
            lastChild = null
          else
            unless TAGS[node.nodeName].content is 'optional' or
                (node.nodeName is 'script' and @hasAttribute node, 'src') or
                node.__indentLocation.end is node.__location.end
              @htmlSocket node, depth + 1, null, indentBounds, null, true
        for child in node.childNodes
          if lastChild and lastChild.__location.end > child.__location.start then nomark = true else nomark = false
          @mark indentDepth, child, depth + 1, null, nomark
          unless nomark
            lastChild = child

      when 'text'
        unless nomark
          @htmlBlock node, depth, bounds
          @htmlSocket node, depth + 1, null

      when 'comment'
        unless nomark
          @htmlBlock node, depth, bounds
          node.__location.start += 4
          node.__location.end -= 3
          @htmlSocket node, depth + 1, null

  isComment: (text) ->
    text.match(/<!--.*-->/)
