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 tests demonstrating parse failure #555

Merged
merged 4 commits into from Feb 7, 2020
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
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -321,7 +321,9 @@ You can pass in additional options to configure this validation.

| Option | Description | Default |
| :----- | :---------- | :------ |
| `report_eof_tags` | When `check_html` is enabled, HTML markup with mismatched tags are reported as errors | `false`
| `report_invalid_tags` | When `check_html` is enabled, HTML markup that is unknown to Nokogumbo are reported as errors. | `false`
| `report_mismatched_tags` | When `check_html` is enabled, HTML markup with tags that are malformed are reported as errors | `false`
| `report_missing_doctype` | When `check_html` is enabled, HTML markup with missing or out-of-order `DOCTYPE` are reported as errors. | `false`
| `report_missing_names` | When `check_html` is enabled, HTML markup that are missing entity names are reported as errors. | `false`
| `report_script_embeds` | When `check_html` is enabled, `script` tags containing markup are reported as errors. | `false`
Expand Down
4 changes: 4 additions & 0 deletions bin/htmlproofer
Expand Up @@ -41,6 +41,8 @@ Mercenary.program(:htmlproofer) do |p|
p.option 'report_missing_names', '--report-missing-names', 'When `check_html` is enabled, HTML markup that are missing entity names are reported as errors (default: `false`)'
p.option 'report_script_embeds', '--report-script-embeds', 'When `check_html` is enabled, `script` tags containing markup are reported as errors (default: `false`)'
p.option 'report_missing_doctype', '--report-missing-doctype', 'When `check_html` is enabled, HTML markup with missing or out-of-order `DOCTYPE` are reported as errors (default: `false`)'
p.option 'report_eof_tags', '--report-eof-tags', 'When `check_html` is enabled, HTML markup with tags that are malformed are reported as errors (default: `false`)'
p.option 'report_mismatched_tags', '--report-mismatched-tags', 'When `check_html` is enabled, HTML markup with mismatched tags are reported as errors (default: `false`)'
p.option 'log_level', '--log-level <level>', String, 'Sets the logging level, as determined by Yell. One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. (default: `:info`)'
p.option 'only_4xx', '--only-4xx', 'Only reports errors for links that fall within the 4xx status code range'
p.option 'storage_dir', '--storage-dir PATH', String, 'Directory where to store the cache log (default: "tmp/.htmlproofer")'
Expand Down Expand Up @@ -82,6 +84,8 @@ Mercenary.program(:htmlproofer) do |p|
options[:validation][:report_missing_names] = opts['report_missing_names'] unless opts['report_missing_names'].nil?
options[:validation][:report_invalid_tags] = opts['report_invalid_tags'] unless opts['report_invalid_tags'].nil?
options[:validation][:report_missing_doctype] = opts['report_missing_doctype'] unless opts['report_missing_doctype'].nil?
options[:validation][:report_eof_tags] = opts['report_eof_tags'] unless opts['report_eof_tags'].nil?
options[:validation][:report_mismatched_tags] = opts['report_mismatched_tags'] unless opts['report_mismatched_tags'].nil?

options[:typhoeus] = HTMLProofer::Configuration.parse_json_option('typhoeus_config', opts['typhoeus_config']) unless opts['typhoeus_config'].nil?

Expand Down
8 changes: 7 additions & 1 deletion lib/html-proofer/check/html.rb
Expand Up @@ -6,7 +6,9 @@ class HtmlCheck < ::HTMLProofer::Check
INVALID_TAG_MSG = /Tag ([\w\-:]+) invalid/.freeze
INVALID_PREFIX = /Namespace prefix/.freeze
PARSE_ENTITY_REF = /htmlParseEntityRef: no name/.freeze
DOCTYPE_MSG = /The doctype must be the first token in the document/.freeze
DOCTYPE_MSG = /Expected a doctype token/.freeze
EOF_IN_TAG = /End of input in tag/.freeze
MISMATCHED_TAGS = /That tag isn't allowed here/.freeze

def run
@html.errors.each do |error|
Expand All @@ -24,6 +26,10 @@ def report?(message)
options[:validation][:report_missing_names]
when DOCTYPE_MSG
options[:validation][:report_missing_doctype]
when EOF_IN_TAG
options[:validation][:report_eof_tags]
when MISMATCHED_TAGS
options[:validation][:report_mismatched_tags]
else
true
end
Expand Down
4 changes: 3 additions & 1 deletion lib/html-proofer/configuration.rb
Expand Up @@ -52,7 +52,9 @@ module Configuration
report_script_embeds: false,
report_missing_names: false,
report_invalid_tags: false,
report_missing_doctype: false
report_missing_doctype: false,
report_eof_tags: false,
report_mismatched_tags: false
}.freeze

CACHE_DEFAULTS = {}.freeze
Expand Down
3 changes: 2 additions & 1 deletion lib/html-proofer/middleware.rb
Expand Up @@ -21,7 +21,8 @@ def self.options
allow_hash_href: true,
check_external_hash: true,
check_html: true,
url_ignore: [/.*/] # Don't try to check local files exist
url_ignore: [/.*/], # Don't try to check if local files exist
validation: { report_eof_tags: true }
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/html-proofer/utils.rb
Expand Up @@ -15,7 +15,7 @@ def create_nokogiri(path)
path
end

Nokogiri::HTML5(content)
Nokogiri::HTML5(content, max_errors: -1)
end

def swap(href, replacement)
Expand Down
6 changes: 3 additions & 3 deletions spec/html-proofer/command_spec.rb
Expand Up @@ -89,9 +89,9 @@
end

it 'works with check-html' do
broken = "#{FIXTURES_DIR}/html/unmatched_end_tag.html"
output = make_bin('--check-html --report-invalid-tags', broken)
expect(output).to match('HTML-Proofer finished successfully')
broken = "#{FIXTURES_DIR}/html/missing_closing_quotes.html"
output = make_bin('--check-html --report-eof-tags', broken)
expect(output).to match('1 failure')
end

it 'works with empty-alt-ignore' do
Expand Down
1 change: 1 addition & 0 deletions spec/html-proofer/fixtures/html/parse_failure.html
@@ -0,0 +1 @@
<!DOCTYPE html><html><!-- -- --></a>
7 changes: 7 additions & 0 deletions spec/html-proofer/html_spec.rb
Expand Up @@ -124,4 +124,11 @@
proofer = run_proofer(file, :file, opts)
expect(proofer.failed_tests).to eq []
end

it 'reports failures' do
opts = { check_html: true, validation: { report_mismatched_tags: true } }
file = "#{FIXTURES_DIR}/html/parse_failure.html"
proofer = run_proofer(file, :file, opts)
expect(proofer.failed_tests.first).to match(/ERROR: That tag isn't allowed here/)
end
end