<commit_before>require 'hpricot'

module Hquery
  module Element
    def self.included(base)
      base.class_eval do
        alias_method_chain :html, :selector
      end
    end
  
    def html_with_selector(selector, value = :html_without_selector, noattr = false)
      return self.html_without_selector(selector) if value == :html_without_selector
      case !noattr && selector
      when /^\@(.+)$/
        self.raw_attr $1, value
      when /.*\[\@([^\]]+)\]/
        (self/selector).each {|e| e.raw_attr($1, value) }
      else
        (self/selector).html(value)
      end
    end

    def attr(selector, key, value = :attr_without_selector)
      if value == :attr_without_selector
        self.raw_attr(selector, key)
      else
        (self/selector).collect {|e| e.raw_attr(key, value) }
      end
    end

    def raw_attr(name, val)
      self.altered!
      self.raw_attributes ||= {}
      self.raw_attributes[name.to_s] = val
    end

    def remove(selector)
      (self/selector).remove
    end

    def select(selector, list = [{}], &block)
      timestart = Time.now
      selected = (self/selector)
      return if selected.length < 1
      last_ele = nil
      [selected.length, list.length].max.times do |index|
        obj = list[index]
        ele = selected[index] || (last_ele = (last_ele || selected.last).after(selected.first.to_s).first)
        case obj && block.arity
        when 3
          block.call ele, obj, index
        when 2
          block.call ele, obj
        when nil
          ele.parent.children.delete(ele)
        else
          block.call ele
        end
      end
    rescue
      err_string = "#{$!.inspect}\n#{$!.backtrace.join("\n")}"
      RAILS_DEFAULT_LOGGER.error err_string
      (self/selector).html(RAILS_ENV == 'production' ? "" : CGI.escapeHTML(err_string))
      raise $!
    ensure
      RAILS_DEFAULT_LOGGER.debug "hquery::select took #{Time.now - timestart}s for #{selector.inspect}"
    end
  end
end

Hpricot::Elem.send(:include, Hquery::Element)<commit_msg>Fix on-the-fly hquery to match behavior in compiled state<commit_after>require 'hpricot'

module Hquery
  module Element
    def self.included(base)
      base.class_eval do
        alias_method_chain :html, :selector
      end
    end
  
    def html_with_selector(selector, value = :html_without_selector, noattr = false)
      return self.html_without_selector(selector) if value == :html_without_selector
      case !noattr && selector
      when /^\@(.+)$/
        self.raw_attr $1, value
      when /.*\[\@([^\]]+)\]/
        (self/selector).each {|e| e.raw_attr($1, value) }
      else
        (self/selector).html(value)
      end
    end

    def attr(selector, key, value = :attr_without_selector)
      if value == :attr_without_selector
        self.raw_attr(selector, key)
      else
        (self/selector).collect {|e| e.raw_attr(key, value) }
      end
    end

    def raw_attr(name, val)
      self.altered!
      self.raw_attributes ||= {}
      self.raw_attributes[name.to_s] = val
    end

    def remove(selector)
      (self/selector).remove
    end

    def select(selector, list = [{}], &block)
      timestart = Time.now
      selected = (self/selector)
      return if selected.length < 1
      last_ele = nil
      [selected.length, list.length].max.times do |index|
        obj = list[index]
        ele = selected[index] || (last_ele = (last_ele || selected.last).after(selected.first.to_s).first)
        case obj && block.arity
        when 3
          block.call ele, obj, index
        when 2
          block.call ele, obj
        else
          if block.arity == 1
            block.call ele
          else
            ele.parent.children.delete(ele)
          end
        end
      end
    rescue
      err_string = "#{$!.inspect}\n#{$!.backtrace.join("\n")}"
      RAILS_DEFAULT_LOGGER.error err_string
      (self/selector).html(RAILS_ENV == 'production' ? "" : CGI.escapeHTML(err_string))
      raise $!
    ensure
      RAILS_DEFAULT_LOGGER.debug "hquery::select took #{Time.now - timestart}s for #{selector.inspect}"
    end
  end
end

Hpricot::Elem.send(:include, Hquery::Element)
{'fraction_non_alphanumeric': 0.1234954260953298, 'fraction_numerical': 0.0026480500722195475, 'mean_word_length': 2.2871835443037973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6305095', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1320, 'n_words': 290}