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(string) ⇒ HtmlValidator

Returns a new instance of HtmlValidator.



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

def initialize(string)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



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

def string
  @string
end

Instance Method Details

#govspeak_to_htmlObject



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

def govspeak_to_html
  Govspeak::Document.new(string).to_html
end

#invalid?Boolean

Returns:

  • (Boolean)


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

def invalid?
  !valid?
end

#normalise_html(html) ⇒ Object

Make whitespace in html tags consistent



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

def normalise_html(html)
  Nokogiri::HTML.parse(html).to_s
end

#valid?Boolean

Returns:

  • (Boolean)


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

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