<commit_before>require 'browser/location'

module Browser; module DOM

class Document < Element
  def create_element(name, options = {})
    if ns = options[:namespace]
      DOM(`#@native.createElementNS(#{ns}, #{name})`)
    else
      DOM(`#@native.createElement(name)`)
    end
  end

  def window
    Window.new(`#@native.defaultView`)
  end

  def create_text(content)
    DOM(`#@native.createTextNode(#{content})`)
  end

  def [](what)
    %x{
      var result = #@native.getElementById(what);

      if (result) {
        return #{DOM(`result`)};
      }
    }

    xpath(what).first || css(what).first
  end

  alias at []

  def cookies
    Cookies.new(@native) if defined?(`#@native.cookie`)
  end

  def document
    self
  end

  def inspect
    "#<DOM::Document: #{children.inspect}>"
  end

  def location
    Location.new(`#@native.location`) if `#@native.location`
  end

  def title
    `#@native.title`
  end

  def title=(value)
    `#@native.title = value`
  end

  def root
    DOM(`#@native.documentElement`)
  end

  def head
    xpath('//head').first
  end

  def body
    DOM(`document.body`)
  end<commit_msg>dom/document: improve #head and #body<commit_after>require 'browser/location'

module Browser; module DOM

class Document < Element
  def create_element(name, options = {})
    if ns = options[:namespace]
      DOM(`#@native.createElementNS(#{ns}, #{name})`)
    else
      DOM(`#@native.createElement(name)`)
    end
  end

  def window
    Window.new(`#@native.defaultView`)
  end

  def create_text(content)
    DOM(`#@native.createTextNode(#{content})`)
  end

  def [](what)
    %x{
      var result = #@native.getElementById(what);

      if (result) {
        return #{DOM(`result`)};
      }
    }

    xpath(what).first || css(what).first
  end

  alias at []

  def cookies
    Cookies.new(@native) if defined?(`#@native.cookie`)
  end

  def document
    self
  end

  def inspect
    "#<DOM::Document: #{children.inspect}>"
  end

  def location
    Location.new(`#@native.location`) if `#@native.location`
  end

  def title
    `#@native.title`
  end

  def title=(value)
    `#@native.title = value`
  end

  def root
    DOM(`#@native.documentElement`)
  end

  def head
    DOM(`#@native.getElementsByTagName("head")[0]`)
  end

  def body
    DOM(`#@native.body`)
  end
{'fraction_non_alphanumeric': 0.1523235800344234, 'fraction_numerical': 0.0004302925989672978, 'mean_word_length': 2.914141414141414, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1755114', 'n_tokens_mistral': 807, 'n_tokens_neox': 780, 'n_words': 175}