class Nokogiri::HTML4::DocumentFragment

def initialize(document, tags = nil, ctx = nil)

def initialize(document, tags = nil, ctx = nil)
  return self unless tags
  if ctx
    preexisting_errors = document.errors.dup
    node_set = ctx.parse("<div>#{tags}</div>")
    node_set.first.children.each { |child| child.parent = self } unless node_set.empty?
    self.errors = document.errors - preexisting_errors
  else
    # This is a horrible hack, but I don't care
    path = if /^\s*?<body/i.match?(tags)
      "/html/body"
    else
      "/html/body/node()"
    end
    temp_doc = HTML4::Document.parse("<html><body>#{tags}", nil, document.encoding)
    temp_doc.xpath(path).each { |child| child.parent = self }
    self.errors = temp_doc.errors
  end
  children
end