Sha256: caa2250f9d73a76a995875cc0599e79da1c78eec060a1040c528b109d24293b9

Contents?: true

Size: 782 Bytes

Versions: 7

Compression:

Stored size: 782 Bytes

Contents

class Govspeak::HtmlValidator
  attr_reader :govspeak_string

  def initialize(govspeak_string, options = {})
    @govspeak_string = govspeak_string.dup.force_encoding(Encoding::UTF_8)
    @allowed_image_hosts = options[:allowed_image_hosts]
  end

  def invalid?
    !valid?
  end

  def valid?
    dirty_html = govspeak_to_html(sanitize: false)
    clean_html = govspeak_to_html(sanitize: true)
    normalise_html(dirty_html) == normalise_html(clean_html)
  end

private

  # Make whitespace in html tags consistent
  def normalise_html(html)
    Nokogiri::HTML5.fragment(html).to_s.gsub("\n", "")
  end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
govspeak-8.7.0 lib/govspeak/html_validator.rb
govspeak-8.6.1 lib/govspeak/html_validator.rb
govspeak-8.6.0 lib/govspeak/html_validator.rb
govspeak-8.5.1 lib/govspeak/html_validator.rb
govspeak-8.5.0 lib/govspeak/html_validator.rb
govspeak-8.4.1 lib/govspeak/html_validator.rb
govspeak-8.4.0 lib/govspeak/html_validator.rb