Skip to content

Commit

Permalink
Fix internal link handling for dirs w/o root_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Nov 23, 2020
1 parent 9a9552d commit 6636e5e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/html-proofer/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ def file_path
path_dot_ext = path + @check.options[:extension] if @check.options[:assume_extension]

base = if absolute_path?(path) # path relative to root
@check.options[:root_dir] || File.dirname(@check.src)
if File.directory?(@check.src)
@check.options[:root_dir] || @check.src
else
@check.options[:root_dir] || File.dirname(@check.src)
end
elsif File.exist?(File.expand_path(path, @check.src)) || File.exist?(File.expand_path(path_dot_ext, @check.src)) # relative links, path is a file
File.dirname(@check.path)
elsif File.exist?(File.join(File.dirname(@check.path), path)) || File.exist?(File.join(File.dirname(@check.path), path_dot_ext)) # rubocop:disable Lint/DuplicateBranch; relative links in nested dir, path is a file
Expand All @@ -190,6 +194,7 @@ def file_path
@check.path
end
file = File.join(base, path)

if @check.options[:assume_extension] && File.file?("#{file}#{@check.options[:extension]}")
file = "#{file}#{@check.options[:extension]}"
elsif File.directory?(file) && !unslashed_directory?(file) # implicit index support
Expand Down
2 changes: 2 additions & 0 deletions spec/html-proofer/fixtures/links/internals/direct_link.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="page.html"></a>
<a href="/page.html"></a>
Empty file.
6 changes: 6 additions & 0 deletions spec/html-proofer/links_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,10 @@
proofer = run_proofer(file, :file)
expect(proofer.failed_tests.first).to match(/failed/)
end

it 'works for a direct link through directory' do
file = "#{FIXTURES_DIR}/links/internals"
proofer = run_proofer(file, :directory)
expect(proofer.failed_tests).to eq []
end
end

0 comments on commit 6636e5e

Please sign in to comment.