diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index 6d1229fd..dffb6b96 100644 --- a/lib/html-proofer/element.rb +++ b/lib/html-proofer/element.rb @@ -150,7 +150,7 @@ def internal_absolute_link? def relative_link? return false if remote? - hash_link || param_link || url.start_with?('.') || url =~ /^\w/ + hash_link || param_link || url.start_with?('.') || url =~ /^\S/ end def link_points_to_same_page? @@ -234,12 +234,14 @@ def base end def html - # If link is on the same page, then URL is on the current page so can use the same HTML as for current page + # If link is on the same page, then URL is on the current page. use the same HTML as for current page if link_points_to_same_page? @html - elsif relative_link? + elsif internal? # link on another page, e.g. /about#Team - need to get HTML from the other page create_nokogiri(absolute_path) + else + raise NotImplementedError, 'HTMLProofer should not have gotten here. Please report this as a bug.' end end end diff --git a/spec/html-proofer/fixtures/links/relative_hash.html b/spec/html-proofer/fixtures/links/relative_hash.html new file mode 100644 index 00000000..8263bbab --- /dev/null +++ b/spec/html-proofer/fixtures/links/relative_hash.html @@ -0,0 +1,3 @@ + diff --git a/spec/html-proofer/links_spec.rb b/spec/html-proofer/links_spec.rb index 45a3a353..aa786fe0 100644 --- a/spec/html-proofer/links_spec.rb +++ b/spec/html-proofer/links_spec.rb @@ -134,6 +134,12 @@ expect(proofer.failed_tests.first).to match(/linking to internal hash #25-method-not-allowed that does not exist/) end + it 'should understand relative hash' do + link_with_https_filepath = "#{FIXTURES_DIR}/links/relative_hash.html" + proofer = run_proofer(link_with_https_filepath, :file) + expect(proofer.failed_tests).to eq [] + end + it 'properly resolves implicit /index.html in link paths' do link_to_folder = "#{FIXTURES_DIR}/links/link_to_folder.html" proofer = run_proofer(link_to_folder, :file)