class HTMLProofer::Middleware

Constants

HTML_SIGNATURE

Public Class Methods

new(app) click to toggle source
# File lib/html-proofer/middleware.rb, line 30
def initialize(app)
  @app = app
end
options() click to toggle source
# File lib/html-proofer/middleware.rb, line 18
def self.options
  @options ||= {
    type: :file,
    allow_missing_href: true, # Permitted in html5
    allow_hash_href: true,
    check_external_hash: true,
    check_html: true,
    url_ignore: [%r{^/}], # Don't try to check if local files exist
    validation: { report_eof_tags: true }
  }
end

Public Instance Methods

call(env) click to toggle source
# File lib/html-proofer/middleware.rb, line 54
def call(env)
  result = @app.call(env)
  return result if env['REQUEST_METHOD'] != 'GET'
  return result if /proofer-ignore/.match?(env['QUERY_STRING'])
  return result if result.first != 200

  body = []
  result.last.each { |e| body << e }

  body = body.join
  begin
    html = body.lstrip
  rescue StandardError
    return result # Invalid encoding; it's not gonna be html.
  end
  if HTML_SIGNATURE.any? { |sig| html.upcase.start_with? sig }
    parsed = HTMLProofer::Runner.new(
      'response',
      Middleware.options
    ).check_parsed(
      Nokogiri::HTML5(html, max_errors: -1), 'response'
    )

    raise InvalidHtmlError, parsed[:failures] unless parsed[:failures].empty?
  end
  result
end