Sha256: 79872d3aad0a111f24bf8f37497a52102eb189eee6674cfd953250184d18847d

Contents?: true

Size: 776 Bytes

Versions: 3

Compression:

Stored size: 776 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
  end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
govspeak-6.8.3 lib/govspeak/html_validator.rb
govspeak-6.8.2 lib/govspeak/html_validator.rb
govspeak-6.8.1 lib/govspeak/html_validator.rb