Skip to content

Commit

Permalink
Merge pull request #475 from gjtorikian/allow-omit-href
Browse files Browse the repository at this point in the history
Allow skipping the `href` requirement
  • Loading branch information
gjtorikian authored May 21, 2018
2 parents 0d39b1a + 6e0fda6 commit 104ebc2
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 2 deletions.
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

0 comments on commit 104ebc2

Please sign in to comment.