<commit_before>require 'sanitize'
require 'govspeak'

class SafeHtml < ActiveModel::Validator
  def validate(record)
    record.changes.each do |field_name, (old_value, new_value)|
      check_struct(record, field_name, new_value)
    end
  end

  private
    def check_struct(record, field_name, value)
      if value.respond_to?(:values) # e.g. Hash
        value.values.each { |entry| check_struct(record, field_name, entry) }
      elsif value.respond_to?(:each) # e.g. Array
        value.each { |entry| check_struct(record, field_name, entry) }
      elsif value.is_a?(String)
        check_field(record, field_name, value)
      end
    end

    def check_field(record, field_name, new_value)
      dirty_html = Govspeak::Document.new(new_value).to_html
      dirty_html.strip!
      clean_html = Sanitize.clean(dirty_html, sanitize_config)
      # Trying to make whitespace consistent
      if Nokogiri::HTML.parse(dirty_html).to_s != Nokogiri::HTML.parse(clean_html).to_s
        record.errors.add(field_name, "JavaScript is not allowed in #{field_name}")
      end
    end

    def sanitize_config
      config = Sanitize::Config::RELAXED.dup 
      
      config[:attributes][:all] << "id"
      config[:attributes][:all] << "class"
      config[:attributes]["a"]  << "rel"

      config[:elements] << "div"

      config
    end
end<commit_msg>Improve SafeHtml validation message<commit_after>require 'sanitize'
require 'govspeak'

class SafeHtml < ActiveModel::Validator
  def validate(record)
    record.changes.each do |field_name, (old_value, new_value)|
      check_struct(record, field_name, new_value)
    end
  end

  private
    def check_struct(record, field_name, value)
      if value.respond_to?(:values) # e.g. Hash
        value.values.each { |entry| check_struct(record, field_name, entry) }
      elsif value.respond_to?(:each) # e.g. Array
        value.each { |entry| check_struct(record, field_name, entry) }
      elsif value.is_a?(String)
        check_field(record, field_name, value)
      end
    end

    def check_field(record, field_name, new_value)
      dirty_html = Govspeak::Document.new(new_value).to_html
      dirty_html.strip!
      clean_html = Sanitize.clean(dirty_html, sanitize_config)
      # Trying to make whitespace consistent
      if Nokogiri::HTML.parse(dirty_html).to_s != Nokogiri::HTML.parse(clean_html).to_s
        record.errors.add(field_name, "Invalid Govspeak or JavaScript is not allowed in #{field_name}")
      end
    end

    def sanitize_config
      config = Sanitize::Config::RELAXED.dup 
      
      config[:attributes][:all] << "id"
      config[:attributes][:all] << "class"
      config[:attributes]["a"]  << "rel"

      config[:elements] << "div"

      config
    end
end
{'fraction_non_alphanumeric': 0.11474219317356572, 'fraction_numerical': 0.0, 'mean_word_length': 3.3114241001564944, 'pattern_counts': {'":': 0, '<': 21, '<?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': '3714629', 'n_tokens_mistral': 902, 'n_tokens_neox': 892, 'n_words': 187}