class Nokogiri::HTML::Document

def set_metadata_element(element)

def set_metadata_element(element)
  case
  when head = at('//head')
    head << element
  when html = at('//html')
    head = html.prepend_child(XML::Node.new('head', self))
    head.prepend_child(element)
  when first = children.find { |node|
      case node
      when XML::Element, XML::Text
        true
      end
    }
    # We reach here only if the underlying document model
    # allows <html>/<head> elements to be omitted and does not
    # automatically supply them.
    first.add_previous_sibling(element)
  else
    html = add_child(XML::Node.new('html', self))
    head = html.add_child(XML::Node.new('head', self))
    head.prepend_child(element)
  end
end