Sha256: 5db4e67703f892686f0b93776d3e197c25a5e7896719c1b12e8602e8edc33c55

Contents?: true

Size: 791 Bytes

Versions: 16

Compression:

Stored size: 791 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: sanitize,
      allowed_image_hosts: @allowed_image_hosts,
    ).to_html
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
govspeak-8.3.4 lib/govspeak/html_validator.rb
govspeak-8.3.3 lib/govspeak/html_validator.rb
govspeak-8.3.2 lib/govspeak/html_validator.rb
govspeak-8.3.1 lib/govspeak/html_validator.rb
govspeak-8.3.0 lib/govspeak/html_validator.rb
govspeak-8.2.1 lib/govspeak/html_validator.rb
govspeak-8.2.0 lib/govspeak/html_validator.rb
govspeak-8.1.0 lib/govspeak/html_validator.rb
govspeak-8.0.1 lib/govspeak/html_validator.rb
govspeak-8.0.0 lib/govspeak/html_validator.rb
govspeak-7.1.1 lib/govspeak/html_validator.rb
govspeak-7.1.0 lib/govspeak/html_validator.rb
govspeak-7.0.2 lib/govspeak/html_validator.rb
govspeak-7.0.1 lib/govspeak/html_validator.rb
govspeak-7.0.0 lib/govspeak/html_validator.rb
govspeak-6.8.4 lib/govspeak/html_validator.rb