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

Issue 11 be compatible with rails view annotation comments #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
validate_html (0.1.0)
validate_html (0.2.0)
nokogiri

GEM
Expand Down Expand Up @@ -65,6 +65,7 @@ GEM
mini_mime (>= 0.1.1)
method_source (1.0.0)
mini_mime (1.1.2)
mini_portile2 (2.8.4)
minitest (5.17.0)
net-imap (0.3.1)
net-protocol
Expand All @@ -74,9 +75,8 @@ GEM
timeout
net-smtp (0.3.2)
net-protocol
nokogiri (1.13.10-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-linux)
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
parallel (1.22.1)
parser (3.1.2.1)
Expand Down Expand Up @@ -165,6 +165,7 @@ GEM

PLATFORMS
arm64-darwin-21
arm64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down
20 changes: 19 additions & 1 deletion lib/validate_html/rack_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module ValidateHTML
#
# This can be used with any rack app
class RackMiddleware
ANNOTATE_RENDERED_VIEW_WITH_FILENAMES_PREFIX_COMMENT_REGEX = /\A<!-- BEGIN .+?-->/.freeze

def initialize(app)
@app = app
end
Expand All @@ -25,13 +27,29 @@ def call(env)

return [status, headers, response] unless html_content_type?(headers)

ValidateHTML.validate_html(body, content_type: headers['Content-Type'], name: path)
body_for_validation = remove_annotate_rendered_view_with_filenames_prefix_comment(body)
ValidateHTML.validate_html(body_for_validation, content_type: headers['Content-Type'], name: path)

[status, headers, response]
end

private

# If:
#
# config.action_view.annotate_rendered_view_with_filenames = true
#
# is set in Rails then each rendered view and partial will have a comment
# at the top like:
#
# <!-- BEGIN app/views/thing/index.html.erb -->
#
# When this comment is present before the doctype, it causes the HTML to be
# invalid in a way that is not useful so we remove it.
def remove_annotate_rendered_view_with_filenames_prefix_comment(body)
body.sub(ANNOTATE_RENDERED_VIEW_WITH_FILENAMES_PREFIX_COMMENT_REGEX, '')
end

def checkable_path?(path)
!ValidateHTML.configuration.ignored_paths_re.match?(path)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/validate_html/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ValidateHTML
VERSION = '0.1.0'
VERSION = '0.2.0'
end
17 changes: 17 additions & 0 deletions spec/validate_html/rack_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@
end
end

context 'with config.action_view.annotate_rendered_view_with_filenames=true HTML' do
let(:body_with_leading_comment_removed) { '<!DOCTYPE html><html><body><p>hi</p></body></html>' }
let(:body) { "<!-- BEGIN app/views/layouts/site.html.erb -->#{body_with_leading_comment_removed}" }

it 'does not raise an error' do
expect(middleware.call(env)).to eq [200, headers, body]
end

it 'calls ValidateHTML.validate_html with the leading comment removed' do
allow(ValidateHTML).to receive(:validate_html)

middleware.call(env)

expect(ValidateHTML).to have_received(:validate_html).with(body_with_leading_comment_removed, anything)
end
end

context 'with invalid html in an array for some reason' do
let(:body) { ['<strong><em>Emphasis</strong></em>'] }

Expand Down
2 changes: 1 addition & 1 deletion spec/validate_html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let(:valid_html) { '<strong><em>Very Emphasized</em></strong>' }

it 'has a version number' do
expect(ValidateHTML::VERSION).to eq '0.1.0'
expect(ValidateHTML::VERSION).to eq '0.2.0'
end

describe '.validate_html' do
Expand Down
Loading