Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rack middleware for proofing HTML at runtime #512

Merged
merged 5 commits into from Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -78,6 +78,19 @@ Below is mostly comprehensive list of checks that HTMLProofer can perform.

You can configure HTMLProofer to run on a file, a directory, an array of directories, or an array of links.

There's also a rack middleware.

### Using in a rails app

Add to `config/application.rb`

config.middleware.use HTMLProofer::Middleware if Rails.env.test?
config.middleware.use HTMLProofer::Middleware if Rails.env.development?

This will raise an error at runtime if your HTML is invalid.

Particularly helpful for projects which have extensive CI, since any invalid HTML will fail your build.

### Using in a script

1. Require the gem.
Expand Down
80 changes: 80 additions & 0 deletions lib/html-proofer/middleware.rb
@@ -0,0 +1,80 @@


module HTMLProofer
class Middleware

class InvalidHtmlError < StandardError
def initialize(failures)
@failures = failures
end

def message
"HTML Validation errors (skip by adding ?SKIP_VALIDATION to URL): \n#{@failures.join("\n")}"
end
end

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: [/.*/], # Don't try to check local files exist
}
end

def initialize(app)
@app = app
end

HTML_SIGNATURE = [
'<!DOCTYPE HTML',
'<HTML',
'<HEAD',
'<SCRIPT',
'<IFRAME',
'<H1',
'<DIV',
'<FONT',
'<TABLE',
'<A',
'<STYLE',
'<TITLE',
'<B',
'<BODY',
'<BR',
'<P',
'<!--'
]
DanielHeath marked this conversation as resolved.
Show resolved Hide resolved

def call(env)
result = @app.call(env)
return result if env['REQUEST_METHOD'] != 'GET'
return result if env['QUERY_STRING'] =~ /SKIP_VALIDATION/
return result if result.first != 200
body = []
result.last.each { |e| body << e }

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

if parsed[:failures].length > 0
raise InvalidHtmlError.new(parsed[:failures])
end
end
result
end
end
end
DanielHeath marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 5 additions & 2 deletions lib/html-proofer/runner.rb
Expand Up @@ -90,9 +90,8 @@ def process_files
end
end

def check_path(path)
def check_parsed(html, path)
result = { external_urls: {}, failures: [] }
html = create_nokogiri(path)

@src = [@src] if @type == :file

Expand All @@ -112,6 +111,10 @@ def check_path(path)
result
end

def check_path(path)
check_parsed create_nokogiri(path), path
end

def validate_urls
url_validator = HTMLProofer::UrlValidator.new(@logger, @external_urls, @options)
@failures.concat(url_validator.run)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions spec/html-proofer/middleware_spec.rb
@@ -0,0 +1,33 @@
require 'spec_helper'

describe 'Middleware test' do
let(:request) { {'REQUEST_METHOD' => 'GET'} }
let(:response) { File.open(response_fixture) }
let(:app) { Proc.new { |*args| [200, {}, response] } }
let(:middleware) { HTMLProofer::Middleware.new(app) }
subject { middleware.call(request) }

context 'with invalid HTML' do
let(:response_fixture) { File.join(FIXTURES_DIR, 'html', 'missing_closing_quotes.html') }
it 'raises an error' do
expect {
subject
}.to raise_error(HTMLProofer::Middleware::InvalidHtmlError)
end
end

context 'with valid HTML' do
let(:response_fixture) { File.join(FIXTURES_DIR, 'html', 'html5_tags.html') }
it 'does not raise an error' do
subject
end
end

context 'with non-HTML content' do
let(:response_fixture) { File.join(FIXTURES_DIR, 'images', 'gpl.png') }
it 'does not raise an error' do
subject
end
end

end