def append(node)
      if node.is_a?(EndTag)
        return if empty_tag?(node.name)
        po = @pos
        while po.parent and po.name != node.name
          po = po.parent
        end
        if po.name == node.name
          @pos = po.parent
        end
      else
        rexml = to_rexml(node)

        # if node is second root element, add root element wrap html tag
        if rexml.is_a?(REXML::Element) and @pos == @doc and @doc.root
          if @doc.root.name != 'html'
            html = REXML::Element.new
            html.name = "html"
            i = @doc.root.index_in_parent-1
            while pos = @doc.delete_at(i)
              @doc.delete_element(pos) if pos.is_a?(REXML::Element)
              html << pos
            end
            @doc << html
            @pos = html
          end
          @pos = @doc.root
        end
        @pos << rexml
        if rexml.is_a?(REXML::Element) and !empty_tag?(node.name) and !node.empty?
          @pos = rexml
        end
      end
    end
def append(node)
      if node.is_a?(EndTag)
        return if empty_tag?(node.name)
        po = @pos
        while po.parent and po.name != node.name
          po = po.parent
        end
        if po.name == node.name
          @pos = po.parent
        end
      else
        rexml = to_rexml(node)

        # if node is second root element, add root element wrap html tag
        if rexml.is_a?(REXML::Element) and @pos == @doc and @doc.root
          if @doc.root.name != 'html'
            html = REXML::Element.new
            html.name = "html"
            i = @doc.root.index_in_parent-1
            while pos = @doc.delete_at(i)
              @doc.delete_element(pos) if pos.is_a?(REXML::Element)
              html << pos
            end
            @doc << html
            @pos = html
          end
          @pos = @doc.root
        end
        @pos << rexml
        if rexml.is_a?(REXML::Element) and !empty_tag?(node.name) and !node.empty?
          @pos = rexml
        end
      end
    end
append node to document