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

Allow skipping the href requirement #475

Merged
merged 2 commits into from
May 21, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ The `HTMLProofer` constructor takes an optional hash of additional options:

| Option | Description | Default |
| :----- | :---------- | :------ |
| `allow_missing_href` | If `true`, does not flag `a` tags missing `href` (this is the default for HTML5). | `false` |
| `allow_hash_href` | If `true`, ignores the `href` `#`. | `false` |
| `alt_ignore` | An array of Strings or RegExps containing `img`s whose missing `alt` tags are safe to ignore. | `[]` |
| `assume_extension` | Automatically add extension (e.g. `.html`) to file paths, to allow extensionless URLs (as supported by Jekyll 3 and GitHub Pages) | `false` |
Expand Down
1 change: 1 addition & 0 deletions bin/htmlproofer
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Mercenary.program(:htmlproofer) do |p|

p.description 'Runs the HTML-Proofer suite on the files in PATH. For more details, see the README.'

p.option 'allow_missing_href', '--allow-missing-href', 'If `true`, does not flag `a` tags missing `href` (this is the default for HTML5).'
p.option 'allow_hash_href', '--allow-hash-href', 'If `true`, ignores the `href` `#`'
p.option 'as_links', '--as-links', 'Assumes that `PATH` is a comma-separated array of links to check.'
p.option 'alt_ignore', '--alt-ignore image1,[image2,...]', Array, 'A comma-separated list of Strings or RegExps containing `img`s whose missing `alt` tags are safe to ignore'
Expand Down
2 changes: 0 additions & 2 deletions lib/html-proofer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# rubocop:disable Naming/FileName

def require_all(path)
dir = File.join(File.dirname(__FILE__), path)
Dir[File.join(dir, '*.rb')].each do |f|
Expand Down
1 change: 1 addition & 0 deletions lib/html-proofer/check/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def run

# is there even an href?
if missing_href?
next if @link.allow_missing_href?
# HTML5 allows dropping the href: http://git.io/vBX0z
next if @html.internal_subset.name == 'html' && @html.internal_subset.external_id.nil?
add_issue('anchor has no href attribute', line: line, content: content)
Expand Down
1 change: 1 addition & 0 deletions lib/html-proofer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Configuration
require_relative 'version'

PROOFER_DEFAULTS = {
allow_missing_href: false,
allow_hash_href: false,
alt_ignore: [],
assume_extension: false,
Expand Down
4 changes: 4 additions & 0 deletions lib/html-proofer/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def ignore_empty_alt?
@check.options[:empty_alt_ignore]
end

def allow_missing_href?
@check.options[:allow_missing_href]
end

def allow_hash_href?
@check.options[:allow_hash_href]
end
Expand Down
10 changes: 10 additions & 0 deletions spec/html-proofer/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@
expect(proofer.failed_tests.length).to eq 1
end

it 'can skip expecting href for anchors in non-HTML5' do
missing_href = "#{FIXTURES_DIR}/links/blank_href_html4.html"
proofer = run_proofer(missing_href, :file, allow_missing_href: true)
expect(proofer.failed_tests.length).to eq 0

missing_href = "#{FIXTURES_DIR}/links/blank_href_htmlunknown.html"
proofer = run_proofer(missing_href, :file, allow_missing_href: true)
expect(proofer.failed_tests.length).to eq 0
end

it 'works with internal_domains' do
translated_link = "#{FIXTURES_DIR}/links/link_translated_internal_domains.html"
proofer = run_proofer(translated_link, :file, internal_domains: ['www.example.com', 'example.com'])
Expand Down