Class: LinkCheck

Inherits:
HTML::Proofer::CheckRunner show all
Includes:
HTML::Proofer::Utils
Defined in:
lib/html/proofer/checks/links.rb

Constant Summary

Constants included from HTML::Proofer::Utils

HTML::Proofer::Utils::STORAGE_DIR

Instance Attribute Summary

Attributes inherited from HTML::Proofer::CheckRunner

#allow_hash_href, #alt_ignores, #empty_alt_ignore, #external_urls, #href_ignores, #hydra_opts, #issues, #options, #parallel_opts, #path, #src, #typhoeus_opts, #url_ignores, #validation_opts

Instance Method Summary collapse

Methods included from HTML::Proofer::Utils

clean_content, create_nokogiri, #pluralize, swap

Methods inherited from HTML::Proofer::CheckRunner

#add_issue, #add_path_for_url, #add_to_external_urls, checks, #initialize

Constructor Details

This class inherits a constructor from HTML::Proofer::CheckRunner

Instance Method Details

#check_schemes(link, line) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/html/proofer/checks/links.rb', line 75

def check_schemes(link, line)
  case link.scheme
  when 'mailto'
    handle_mailto(link, line)
  when 'tel'
    handle_tel(link, line)
  when 'http'
    add_issue("#{link.href} is not an HTTPS link", line) if @options[:enforce_https]
  end
end


108
109
110
111
112
113
114
115
116
117
# File 'lib/html/proofer/checks/links.rb', line 108

def external_link_check(link, line)
  if !link.exists?
    add_issue("trying to find hash of #{link.href}, but #{link.absolute_path} does not exist", line)
  else
    target_html = create_nokogiri link.absolute_path
    unless hash_check target_html, link.hash
      add_issue("linking to #{link.href}, but #{link.hash} does not exist", line)
    end
  end
end

#handle_hash(link, line) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/html/proofer/checks/links.rb', line 98

def handle_hash(link, line)
  if link.internal?
    unless hash_check @html, link.hash
      add_issue("linking to internal hash ##{link.hash} that does not exist", line)
    end
  elsif link.external?
    external_link_check(link, line)
  end
end

#handle_mailto(link, line) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/html/proofer/checks/links.rb', line 86

def handle_mailto(link, line)
  if link.path.empty?
    add_issue("#{link.href} contains no email address", line)
  elsif !link.path.include?('@')
    add_issue("#{link.href} contains an invalid email address", line)
  end
end

#handle_tel(link, line) ⇒ Object



94
95
96
# File 'lib/html/proofer/checks/links.rb', line 94

def handle_tel(link, line)
  add_issue("#{link.href} contains no phone number", line) if link.path.empty?
end

#hash_check(html, href_hash) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/html/proofer/checks/links.rb', line 119

def hash_check(html, href_hash)
  decoded_href_hash = URI.decode(href_hash)
  html.xpath("//*[case_insensitive_equals(@id, '#{href_hash}')]", \
             "//*[case_insensitive_equals(@name, '#{href_hash}')]", \
             "//*[case_insensitive_equals(@id, '#{decoded_href_hash}')]", \
             "//*[case_insensitive_equals(@name, '#{decoded_href_hash}')]", \
             HTML::Proofer::XpathFunctions.new).length > 0
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/html/proofer/checks/links.rb', line 27

def run
  @html.css('a, link').each do |node|
    link = LinkCheckable.new(node, self)
    line = node.line

    next if link.ignore?
    next if link.href =~ /^javascript:/ # can't put this in ignore? because the URI does not parse
    next if link.placeholder?
    next if link.allow_hash_href? && link.href == '#'

    # is it even a valid URL?
    unless link.valid?
      add_issue("#{link.href} is an invalid URL", line)
      next
    end

    check_schemes(link, line)

    # is there even a href?
    if link.missing_href?
      add_issue('anchor has no href attribute', line)
      next
    end

    # intentionally here because we still want valid? & missing_href? to execute
    next if link.non_http_remote?

    # does the file even exist?
    if link.remote?
      add_to_external_urls(link.href, line)
      next
    elsif !link.internal?
      add_issue("internally linking to #{link.href}, which does not exist", line) unless link.exists?
    end

    # does the local directory have a trailing slash?
    if link.unslashed_directory? link.absolute_path
      add_issue("internally linking to a directory #{link.absolute_path} without trailing slash", line)
      next
    end

    # verify the target hash
    handle_hash(link, line) if link.hash
  end

  external_urls
end