Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use internal instead of not external #541

Merged
merged 4 commits into from Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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