Class: SanitizedHtmlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/sanitized-html-validator.rb

Class Method Summary collapse

Class Method Details

.valid_html?(html) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/sanitized-html-validator.rb', line 26

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

.validate(language, yaml_object) ⇒ Object



4
5
6
# File 'lib/sanitized-html-validator.rb', line 4

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

.validate_object(language, full_key, yaml_object) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sanitized-html-validator.rb', line 8

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