Class: Govspeak::HtmlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/govspeak/html_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(govspeak_string, sanitization_options = {}) ⇒ HtmlValidator

Returns a new instance of HtmlValidator.



4
5
6
7
# File 'lib/govspeak/html_validator.rb', line 4

def initialize(govspeak_string, sanitization_options = {})
  @govspeak_string = govspeak_string.dup.force_encoding(Encoding::UTF_8)
  @sanitization_options = sanitization_options
end

Instance Attribute Details

#govspeak_stringObject (readonly)

Returns the value of attribute govspeak_string.



2
3
4
# File 'lib/govspeak/html_validator.rb', line 2

def govspeak_string
  @govspeak_string
end

Instance Method Details

#govspeak_to_htmlObject



24
25
26
# File 'lib/govspeak/html_validator.rb', line 24

def govspeak_to_html
  Govspeak::Document.new(govspeak_string, sanitize: false).to_html
end

#invalid?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/govspeak/html_validator.rb', line 9

def invalid?
  !valid?
end

#normalise_html(html) ⇒ Object

Make whitespace in html tags consistent



20
21
22
# File 'lib/govspeak/html_validator.rb', line 20

def normalise_html(html)
  Nokogiri::HTML5.fragment(html).to_s
end

#valid?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/govspeak/html_validator.rb', line 13

def valid?
  dirty_html = govspeak_to_html
  clean_html = Govspeak::HtmlSanitizer.new(dirty_html, @sanitization_options).sanitize
  normalise_html(dirty_html) == normalise_html(clean_html)
end