Sha256: 0db97241ebc641e78da2650a824a691747cd8f82ac914e713b8fb61e7ee2d5ce

Contents?: true

Size: 798 Bytes

Versions: 1

Compression:

Stored size: 798 Bytes

Contents

require 'sanitize'

class SanitizedHtmlValidator
  def self.validate(language, yaml_object)
    validate_object(language, '', yaml_object)
  end

  def self.validate_object(language, full_key, yaml_object)
    return [] if yaml_object.nil?

    errors = []
    yaml_object.each do |key, value|
      full_subkey = (full_key.empty?) ? key : "#{full_key}.#{key}"

      if value.is_a? String
        unless valid_html?(value)
          errors << "unsanitized html in '#{language}.#{full_subkey}' (#{value})"
        end
      elsif value.is_a? Hash
        errors.concat validate_object(language, full_subkey, value)
      end
    end
    errors
  end

  def self.valid_html?(html)
    sanitized = Sanitize.clean(html, elements: [ 'strong', 'br', 'span', 'b', 'i' ]) 
    html == sanitized
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yaml-validator-0.1.4 lib/sanitized-html-validator.rb