diff --git a/README.md b/README.md index 016cd78f..348f940f 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/bin/htmlproofer b/bin/htmlproofer index dcbc5a0c..d971ce7e 100755 --- a/bin/htmlproofer +++ b/bin/htmlproofer @@ -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 ', 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")' @@ -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? diff --git a/lib/html-proofer/check/html.rb b/lib/html-proofer/check/html.rb index ea6b64c3..b6512f18 100644 --- a/lib/html-proofer/check/html.rb +++ b/lib/html-proofer/check/html.rb @@ -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| @@ -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 diff --git a/lib/html-proofer/configuration.rb b/lib/html-proofer/configuration.rb index d8ccc09b..51f63e90 100644 --- a/lib/html-proofer/configuration.rb +++ b/lib/html-proofer/configuration.rb @@ -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 diff --git a/lib/html-proofer/middleware.rb b/lib/html-proofer/middleware.rb index 5339d547..64f625e8 100644 --- a/lib/html-proofer/middleware.rb +++ b/lib/html-proofer/middleware.rb @@ -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 diff --git a/lib/html-proofer/utils.rb b/lib/html-proofer/utils.rb index 523df91a..3733c529 100644 --- a/lib/html-proofer/utils.rb +++ b/lib/html-proofer/utils.rb @@ -15,7 +15,7 @@ def create_nokogiri(path) path end - Nokogiri::HTML5(content) + Nokogiri::HTML5(content, max_errors: -1) end def swap(href, replacement) diff --git a/spec/html-proofer/command_spec.rb b/spec/html-proofer/command_spec.rb index 0eafddde..d61059c7 100644 --- a/spec/html-proofer/command_spec.rb +++ b/spec/html-proofer/command_spec.rb @@ -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 diff --git a/spec/html-proofer/fixtures/html/parse_failure.html b/spec/html-proofer/fixtures/html/parse_failure.html new file mode 100644 index 00000000..0b3330aa --- /dev/null +++ b/spec/html-proofer/fixtures/html/parse_failure.html @@ -0,0 +1 @@ + diff --git a/spec/html-proofer/html_spec.rb b/spec/html-proofer/html_spec.rb index 76cbe909..027598d1 100644 --- a/spec/html-proofer/html_spec.rb +++ b/spec/html-proofer/html_spec.rb @@ -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