From c173b849400d134b45535a2b7b6c6e81844649ad Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sun, 17 Oct 2021 11:37:22 +0100 Subject: [PATCH 1/3] Add flag to allow no-email mailto links This defaults to erroring about mailto links with email addresses for backwards compatibility, however I'd be equally happy to invert this and have the default allow the no-email case. Fixes https://github.com/gjtorikian/html-proofer/issues/552. --- README.md | 1 + bin/htmlproofer | 1 + lib/html-proofer/check/links.rb | 2 +- lib/html-proofer/configuration.rb | 1 + lib/html-proofer/element.rb | 4 ++++ spec/html-proofer/links_spec.rb | 6 ++++++ 6 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a84aa958..78e7174d 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options: | `http_status_ignore` | An array of numbers representing status codes to ignore. | `[]` | `internal_domains`| An array of Strings containing domains that will be treated as internal urls. | `[]` | | `log_level` | Sets the logging level, as determined by [Yell](https://github.com/rudionrails/yell). One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. | `:info` +| `mailto_without_email_ignore` | If `true`, allows `mailto:` `href`s which do not contain an email address. | `false` | `only_4xx` | Only reports errors for links that fall within the 4xx status code range. | `false` | | `root_dir` | The absolute path to the directory serving your html-files. | "" | | `typhoeus_config` | A JSON-formatted string. Parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden. | `{}` | diff --git a/bin/htmlproofer b/bin/htmlproofer index 140db084..1b967d15 100755 --- a/bin/htmlproofer +++ b/bin/htmlproofer @@ -37,6 +37,7 @@ Mercenary.program(:htmlproofer) do |p| p.option 'file_ignore', '--file-ignore file1,[file2,...]', Array, 'A comma-separated list of Strings or RegExps containing file paths that are safe to ignore' p.option 'http_status_ignore', '--http-status-ignore 123,[xxx, ...]', Array, 'A comma-separated list of numbers representing status codes to ignore.' p.option 'internal_domains', '--internal-domains domain1,[domain2,...]', Array, 'A comma-separated list of Strings containing domains that will be treated as internal urls.' + p.option 'mailto_without_email_ignore', '--mailto-without-email-ignore', 'If `true`, allows `mailto:` `href`s which do not contain an email address' p.option 'report_invalid_tags', '--report-invalid-tags', 'When `check_html` is enabled, HTML markup that is unknown to Nokogumbo are reported as errors (default: `false`)' 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`)' diff --git a/lib/html-proofer/check/links.rb b/lib/html-proofer/check/links.rb index 27b7b3a2..e3d20f7f 100644 --- a/lib/html-proofer/check/links.rb +++ b/lib/html-proofer/check/links.rb @@ -95,7 +95,7 @@ def check_schemes(link, line, content) def handle_mailto(link, line, content) if link.path.empty? - add_issue("#{link.href} contains no email address", line: line, content: content) + add_issue("#{link.href} contains no email address", line: line, content: content) unless link.mailto_without_email_ignore? elsif !link.path.include?('@') add_issue("#{link.href} contains an invalid email address", line: line, content: content) end diff --git a/lib/html-proofer/configuration.rb b/lib/html-proofer/configuration.rb index 66e2dd12..5d287587 100644 --- a/lib/html-proofer/configuration.rb +++ b/lib/html-proofer/configuration.rb @@ -27,6 +27,7 @@ module Configuration http_status_ignore: [], internal_domains: [], log_level: :info, + mailto_without_email_ignore: false, only_4xx: false, url_ignore: [], url_swap: {} diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index ab781c51..38e78d61 100644 --- a/lib/html-proofer/element.rb +++ b/lib/html-proofer/element.rb @@ -142,6 +142,10 @@ def check_sri? @check.options[:check_sri] end + def mailto_without_email_ignore? + @check.options[:mailto_without_email_ignore] + end + # path is external to the file def external? !internal? diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index b9c48e81..8b07395e 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -200,6 +200,12 @@ expect(proofer.failed_tests).to eq [] end + it 'ignores blank mailto links when configured to allow them' do + blank_mail_to_link = "#{FIXTURES_DIR}/links/blank_mailto_link.html" + proofer = run_proofer(blank_mail_to_link, :file, mailto_without_email_ignore: true) + expect(proofer.failed_tests).to eq [] + end + it 'fails for blank mailto links' do blank_mail_to_link = "#{FIXTURES_DIR}/links/blank_mailto_link.html" proofer = run_proofer(blank_mail_to_link, :file) From f0988f6e0fb9fe4b868310dc3da86bd5937e955d Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sun, 17 Oct 2021 19:59:21 +0100 Subject: [PATCH 2/3] Change argument name to --ignore-empty-mailto --- README.md | 2 +- bin/htmlproofer | 2 +- lib/html-proofer/check/links.rb | 2 +- lib/html-proofer/configuration.rb | 2 +- lib/html-proofer/element.rb | 4 ++-- spec/html-proofer/links_spec.rb | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 78e7174d..551f9d77 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options: | `http_status_ignore` | An array of numbers representing status codes to ignore. | `[]` | `internal_domains`| An array of Strings containing domains that will be treated as internal urls. | `[]` | | `log_level` | Sets the logging level, as determined by [Yell](https://github.com/rudionrails/yell). One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. | `:info` -| `mailto_without_email_ignore` | If `true`, allows `mailto:` `href`s which do not contain an email address. | `false` +| `ignore_empty_mailto` | If `true`, allows `mailto:` `href`s which do not contain an email address. | `false` | `only_4xx` | Only reports errors for links that fall within the 4xx status code range. | `false` | | `root_dir` | The absolute path to the directory serving your html-files. | "" | | `typhoeus_config` | A JSON-formatted string. Parsed using `JSON.parse` and mapped on top of the default configuration values so that they can be overridden. | `{}` | diff --git a/bin/htmlproofer b/bin/htmlproofer index 1b967d15..73e9686d 100755 --- a/bin/htmlproofer +++ b/bin/htmlproofer @@ -37,7 +37,7 @@ Mercenary.program(:htmlproofer) do |p| p.option 'file_ignore', '--file-ignore file1,[file2,...]', Array, 'A comma-separated list of Strings or RegExps containing file paths that are safe to ignore' p.option 'http_status_ignore', '--http-status-ignore 123,[xxx, ...]', Array, 'A comma-separated list of numbers representing status codes to ignore.' p.option 'internal_domains', '--internal-domains domain1,[domain2,...]', Array, 'A comma-separated list of Strings containing domains that will be treated as internal urls.' - p.option 'mailto_without_email_ignore', '--mailto-without-email-ignore', 'If `true`, allows `mailto:` `href`s which do not contain an email address' + p.option 'ignore_empty_mailto', '--ignore-empty-mailto', 'If `true`, allows `mailto:` `href`s which do not contain an email address' p.option 'report_invalid_tags', '--report-invalid-tags', 'When `check_html` is enabled, HTML markup that is unknown to Nokogumbo are reported as errors (default: `false`)' 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`)' diff --git a/lib/html-proofer/check/links.rb b/lib/html-proofer/check/links.rb index e3d20f7f..fc2cefa1 100644 --- a/lib/html-proofer/check/links.rb +++ b/lib/html-proofer/check/links.rb @@ -95,7 +95,7 @@ def check_schemes(link, line, content) def handle_mailto(link, line, content) if link.path.empty? - add_issue("#{link.href} contains no email address", line: line, content: content) unless link.mailto_without_email_ignore? + add_issue("#{link.href} contains no email address", line: line, content: content) unless link.ignore_empty_mailto? elsif !link.path.include?('@') add_issue("#{link.href} contains an invalid email address", line: line, content: content) end diff --git a/lib/html-proofer/configuration.rb b/lib/html-proofer/configuration.rb index 5d287587..5584565a 100644 --- a/lib/html-proofer/configuration.rb +++ b/lib/html-proofer/configuration.rb @@ -27,7 +27,7 @@ module Configuration http_status_ignore: [], internal_domains: [], log_level: :info, - mailto_without_email_ignore: false, + ignore_empty_mailto: false, only_4xx: false, url_ignore: [], url_swap: {} diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index 38e78d61..84ab851e 100644 --- a/lib/html-proofer/element.rb +++ b/lib/html-proofer/element.rb @@ -142,8 +142,8 @@ def check_sri? @check.options[:check_sri] end - def mailto_without_email_ignore? - @check.options[:mailto_without_email_ignore] + def ignore_empty_mailto? + @check.options[:ignore_empty_mailto] end # path is external to the file diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index 8b07395e..ea11ad91 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -202,7 +202,7 @@ it 'ignores blank mailto links when configured to allow them' do blank_mail_to_link = "#{FIXTURES_DIR}/links/blank_mailto_link.html" - proofer = run_proofer(blank_mail_to_link, :file, mailto_without_email_ignore: true) + proofer = run_proofer(blank_mail_to_link, :file, ignore_empty_mailto: true) expect(proofer.failed_tests).to eq [] end From 103f26cde69c7360582b5fd543ddd66bd5ea366a Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Tue, 19 Oct 2021 09:41:58 -0400 Subject: [PATCH 3/3] lint --- lib/html-proofer/cache.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/html-proofer/cache.rb b/lib/html-proofer/cache.rb index e56be767..4a930c58 100644 --- a/lib/html-proofer/cache.rb +++ b/lib/html-proofer/cache.rb @@ -178,7 +178,7 @@ def time_ago(measurement, unit) when :months @cache_datetime >> -measurement when :weeks - @cache_datetime - measurement * 7 + @cache_datetime - (measurement * 7) when :days @cache_datetime - measurement when :hours