Skip to content

Commit

Permalink
Merge pull request #541 from Graborg/refactor/use-internal-instead-of…
Browse files Browse the repository at this point in the history
…-not-external

refactor: use internal instead of not external
  • Loading branch information
gjtorikian committed Nov 17, 2019
2 parents f759f90 + 165b013 commit de5d878
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/html-proofer/check/links.rb
Expand Up @@ -51,7 +51,7 @@ def run

add_to_external_urls(@link.href)
next
elsif !@link.remote? && !@link.exists?
elsif @link.internal? && !@link.exists?
add_issue("internally linking to #{@link.href}, which does not exist", line: line, content: content)
end

Expand Down
25 changes: 17 additions & 8 deletions lib/html-proofer/element.rb
Expand Up @@ -139,9 +139,22 @@ def external?
!internal?
end

# path is an anchor, a query or refers to root
def internal?
hash_link || param_link || slash_link
relative_link? || internal_absolute_link?
end

def internal_absolute_link?
url.start_with?('/')
end

def relative_link?
return false if remote?

hash_link || param_link || url.start_with?('.') || url =~ /^\w/
end

def link_points_to_same_page?
hash_link || param_link
end

def hash_link
Expand All @@ -152,10 +165,6 @@ def param_link
url.start_with?('?')
end

def slash_link
url.start_with?('/')
end

def file_path
return if path.nil? || path.empty?

Expand Down Expand Up @@ -226,9 +235,9 @@ def base

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 hash_link || param_link
if link_points_to_same_page?
@html
elsif slash_link
elsif relative_link?
# link on another page, e.g. /about#Team - need to get HTML from the other page
create_nokogiri(absolute_path)
end
Expand Down
4 changes: 3 additions & 1 deletion lib/html-proofer/middleware.rb
Expand Up @@ -2,6 +2,8 @@

module HTMLProofer
class Middleware
include HTMLProofer::Utils

class InvalidHtmlError < StandardError
def initialize(failures)
@failures = failures
Expand Down Expand Up @@ -67,7 +69,7 @@ def call(env)
'response',
Middleware.options
).check_parsed(
Nokogiri::HTML(Utils.clean_content(html)), 'response'
Nokogiri::HTML(clean_content(html)), 'response'
)

raise InvalidHtmlError, parsed[:failures] unless parsed[:failures].empty?
Expand Down
6 changes: 3 additions & 3 deletions lib/html-proofer/utils.rb
Expand Up @@ -8,7 +8,7 @@ def pluralize(count, single, plural)
"#{count} #{(count == 1 ? single : plural)}"
end

def self.create_nokogiri(path)
def create_nokogiri(path)
content = if File.exist?(path) && !File.directory?(path)
File.open(path).read
else
Expand All @@ -18,7 +18,7 @@ def self.create_nokogiri(path)
Nokogiri::HTML(clean_content(content))
end

def self.swap(href, replacement)
def swap(href, replacement)
replacement.each do |link, replace|
href = href.gsub(link, replace)
end
Expand All @@ -28,7 +28,7 @@ def self.swap(href, replacement)
# address a problem with Nokogiri's parsing URL entities
# problem from http://git.io/vBYU1
# solution from http://git.io/vBYUi
def self.clean_content(string)
def clean_content(string)
string.gsub(%r{(?:https?:)?//([^>]+)}i) do |url|
url.gsub(/&(?!amp;)/, '&amp;')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/html-proofer/links_spec.rb
Expand Up @@ -6,7 +6,7 @@
it 'fails for broken internal hash (even if the file exists)' do
broken_hash_external_filepath = "#{FIXTURES_DIR}/links/broken_hash_external.html"
proofer = run_proofer(broken_hash_external_filepath, :file)
expect(proofer.failed_tests.last).to match(%r{linking to ../images/missing_image_alt.html#asdfasfdkafl, but asdfasfdkafl does not exist})
expect(proofer.failed_tests.last).to match(/linking to internal hash #asdfasfdkafl that does not exist/)
end

it 'fails for broken hashes on the web when asked (even if the file exists)' do
Expand Down
8 changes: 5 additions & 3 deletions spec/html-proofer/utils_spec.rb
Expand Up @@ -4,18 +4,20 @@

describe HTMLProofer::Utils do
describe '::create_nokogiri' do
include HTMLProofer::Utils

it 'passes for a string' do
noko = HTMLProofer::Utils.create_nokogiri '<html lang="jp">'
noko = create_nokogiri '<html lang="jp">'
expect(noko.css('html').first['lang']).to eq 'jp'
end

it 'passes for a file' do
noko = HTMLProofer::Utils.create_nokogiri "#{FIXTURES_DIR}/utils/lang-jp.html"
noko = create_nokogiri "#{FIXTURES_DIR}/utils/lang-jp.html"
expect(noko.css('html').first['lang']).to eq 'jp'
end

it 'ignores directories' do
noko = HTMLProofer::Utils.create_nokogiri "#{FIXTURES_DIR}/utils"
noko = create_nokogiri "#{FIXTURES_DIR}/utils"
expect(noko.content).to eq 'spec/html-proofer/fixtures/utils'
end
end
Expand Down

0 comments on commit de5d878

Please sign in to comment.