# File lib/nokogumbo.rb, line 86
    def self.fragment(*args)
      doc = parse(*args)
      fragment = Nokogiri::HTML::DocumentFragment.new(doc)

      if doc.children.length != 1 or doc.children.first.name != 'html'
        # no HTML?  Return document as is
        fragment = doc
      else
        # examine children of HTML element
        children = doc.children.first.children

        # head is always first.  If present, take children but otherwise
        # ignore the head element
        if children.length > 0 and doc.children.first.name = 'head'
          fragment << children.shift.children
        end

        # body may be next, or last.  If found, take children but otherwise
        # ignore the body element.  Also take any remaining elements, taking
        # care to preserve order.
        if children.length > 0 and doc.children.first.name = 'body'
          fragment << children.shift.children
          fragment << children
        elsif children.length > 0 and doc.children.last.name = 'body'
          body = children.pop
          fragment << children
          fragment << body.children
        else
          fragment << children
        end
      end

      # return result
      fragment
    end