Sha256: fcafc2771138807f091bedc726fe60400e0599bac452a7a78de9b39df2f2a415

Contents?: true

Size: 809 Bytes

Versions: 1

Compression:

Stored size: 809 Bytes

Contents

require 'sanitize'

class Govspeak::HtmlValidator
  attr_reader :string

  def initialize(string)
    @string = string
  end

  def invalid?
    !valid?
  end

  def valid?
    dirty_html = govspeak_to_html
    clean_html = sanitize_html(dirty_html)
    normalise_html(dirty_html) == normalise_html(clean_html)
  end

  # Make whitespace in html tags consistent
  def normalise_html(html)
    Nokogiri::HTML.parse(html).to_s
  end

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

  def sanitize_html(dirty_html)
    Sanitize.clean(dirty_html, sanitize_config)
  end

  def sanitize_config
    config = Sanitize::Config::RELAXED.dup
    config[:attributes][:all].push("id", "class")
    config[:attributes]["a"].push("rel")
    config[:elements].push("div", "hr")
    config
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
govspeak-1.0.1 lib/govspeak/html_validator.rb