Class: Govspeak::HtmlValidator
- Inherits:
-
Object
- Object
- Govspeak::HtmlValidator
- Defined in:
- lib/govspeak/html_validator.rb
Instance Attribute Summary collapse
-
#govspeak_string ⇒ Object
readonly
Returns the value of attribute govspeak_string.
Instance Method Summary collapse
- #govspeak_to_html ⇒ Object
-
#initialize(govspeak_string, sanitization_options = {}) ⇒ HtmlValidator
constructor
A new instance of HtmlValidator.
- #invalid? ⇒ Boolean
-
#normalise_html(html) ⇒ Object
Make whitespace in html tags consistent.
- #valid? ⇒ Boolean
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, = {}) @govspeak_string = govspeak_string.dup.force_encoding(Encoding::UTF_8) @sanitization_options = end |
Instance Attribute Details
#govspeak_string ⇒ Object (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_html ⇒ Object
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
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
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 |