<commit_before>require "uri"

module Watir
  class Cookies
    include Enumerable

    def initialize(page_container)
      @page_container = page_container
    end

    def each
      @page_container.document.cookie.split(";").each do |cookie|
        name, value = cookie.strip.split("=")
        yield({:name => name, :value => value})
      end
    end

    def add name, value, options={}
      options = options.map do |option|
        k, v = option
        if k == :expires
          "#{k}=#{v.gmtime.strftime("%a, %d %b %Y %H:%M:%S UTC")}"
        elsif k == :secure
          "secure" if v
        else
          "#{k}=#{v}"
        end
      end.compact.join("; ")

      options = "; #{options}" unless options.empty?
      @page_container.document.cookie = "#{name}=#{value}#{options}" 
    end

    def delete name
      options = {:expires => ::Time.now - 60 * 60 * 24}
      delete_with_options name, options

      # make sure that the cookie gets deleted
      # there's got to be some easier way to do this
      uri = URI.parse(@page_container.url)
      domain = uri.host

      paths = uri.path.split("/").reduce([]) do |paths, path|
        paths << "#{paths.last}/#{path}".squeeze("/")
      end << "/"

      subdomains = domain.split(".").reverse.reduce([]) do |subdomains, part|
        subdomain = "#{part}#{subdomains.last}"
        subdomain = "." + subdomain unless subdomain == domain
        subdomains << subdomain
      end

      subdomains.each do |subdomain|
        domain_options = options.merge :domain => subdomain
        delete_with_options name, domain_options
        delete_with_options name, domain_options.merge(:secure => true)

        paths.each do |path|
          path_options = options.merge :path => path
          delete_with_options name, path_options 
          delete_with_options name, path_options.merge(:secure => true)

          path_domain_options = domain_options.merge :path => path
          delete_with_options name, path_domain_options 
          delete_with_options name, path_domain_options.merge(:secure => true)
        end
      end
    end

    def clear
      each {|cookie| delete cookie[:name]}
    end

    def delete_with_options name, options={}
      add name, nil, options
    end

    private :delete_with_options
  end
end<commit_msg>Cookies#delete handles the situation if browser is on about:blank.<commit_after>require "uri"

module Watir
  class Cookies
    include Enumerable

    def initialize(page_container)
      @page_container = page_container
    end

    def each
      @page_container.document.cookie.split(";").each do |cookie|
        name, value = cookie.strip.split("=")
        yield({:name => name, :value => value})
      end
    end

    def add name, value, options={}
      options = options.map do |option|
        k, v = option
        if k == :expires
          "#{k}=#{v.gmtime.strftime("%a, %d %b %Y %H:%M:%S UTC")}"
        elsif k == :secure
          "secure" if v
        else
          "#{k}=#{v}"
        end
      end.compact.join("; ")

      options = "; #{options}" unless options.empty?
      @page_container.document.cookie = "#{name}=#{value}#{options}" 
    end

    def delete name
      options = {:expires => ::Time.now - 60 * 60 * 24}
      delete_with_options name, options

      # make sure that the cookie gets deleted
      # there's got to be some easier way to do this
      uri = URI.parse(@page_container.url)
      domain = uri.host
      return unless domain

      paths = uri.path.split("/").reduce([]) do |paths, path|
        paths << "#{paths.last}/#{path}".squeeze("/")
      end << "/"

      subdomains = domain.split(".").reverse.reduce([]) do |subdomains, part|
        subdomain = "#{part}#{subdomains.last}"
        subdomain = "." + subdomain unless subdomain == domain
        subdomains << subdomain
      end

      subdomains.each do |subdomain|
        domain_options = options.merge :domain => subdomain
        delete_with_options name, domain_options
        delete_with_options name, domain_options.merge(:secure => true)

        paths.each do |path|
          path_options = options.merge :path => path
          delete_with_options name, path_options 
          delete_with_options name, path_options.merge(:secure => true)

          path_domain_options = domain_options.merge :path => path
          delete_with_options name, path_domain_options 
          delete_with_options name, path_domain_options.merge(:secure => true)
        end
      end
    end

    def clear
      each {|cookie| delete cookie[:name]}
    end

    private

    def delete_with_options name, options={}
      add name, nil, options
    end

  end
end
{'fraction_non_alphanumeric': 0.12085106382978723, 'fraction_numerical': 0.002553191489361702, 'mean_word_length': 2.37473079684135, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, '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': '7292575', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1356, 'n_words': 375}