From 93ddeb2d88268c328471d616e29ab97ad5719c91 Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sun, 17 Nov 2019 22:28:06 +0000 Subject: [PATCH 1/2] Fix: internal hash links --- lib/html-proofer/element.rb | 4 ++-- spec/html-proofer/fixtures/links/relative_hash.html | 3 +++ spec/html-proofer/links_spec.rb | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 spec/html-proofer/fixtures/links/relative_hash.html diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index 6d1229fd..c8f28050 100644 --- a/lib/html-proofer/element.rb +++ b/lib/html-proofer/element.rb @@ -234,10 +234,10 @@ 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_absolute_link? # link on another page, e.g. /about#Team - need to get HTML from the other page create_nokogiri(absolute_path) 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) From 3d74c0b1457e4e802b8b4d05604ad9fed4a4f21c Mon Sep 17 00:00:00 2001 From: "Garen J. Torikian" Date: Sun, 17 Nov 2019 22:37:58 +0000 Subject: [PATCH 2/2] Ah, regexp. --- lib/html-proofer/element.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/html-proofer/element.rb b/lib/html-proofer/element.rb index c8f28050..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? @@ -237,9 +237,11 @@ def html # 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 internal_absolute_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