Class: LinkCheck
Defined Under Namespace
Classes: XpathFunctions
Constant Summary
HTMLProofer::Utils::STORAGE_DIR
Instance Attribute Summary
#element, #external_urls, #issues, #node, #options, #path, #src
Instance Method Summary
collapse
clean_content, create_nokogiri, #pluralize, swap
#add_issue, #add_path_for_url, #add_to_external_urls, #blank?, #create_element, #initialize, subchecks
Instance Method Details
#check_schemes(link, line) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/html-proofer/check/links.rb', line 61
def check_schemes(link, line)
case link.scheme
when 'mailto'
handle_mailto(link, line)
when 'tel'
handle_tel(link, line)
when 'http'
return unless @options[:enforce_https]
add_issue("#{link.href} is not an HTTPS link", line: line)
end
end
|
#external_link_check(link, line) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/html-proofer/check/links.rb', line 95
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: 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: line)
end
end
end
|
#handle_hash(link, line) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/html-proofer/check/links.rb', line 85
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: line)
end
elsif link.external?
external_link_check(link, line)
end
end
|
#handle_mailto(link, line) ⇒ Object
73
74
75
76
77
78
79
|
# File 'lib/html-proofer/check/links.rb', line 73
def handle_mailto(link, line)
if link.path.empty?
add_issue("#{link.href} contains no email address", line: line)
elsif !link.path.include?('@')
add_issue("#{link.href} contains an invalid email address", line: line)
end
end
|
#handle_tel(link, line) ⇒ Object
81
82
83
|
# File 'lib/html-proofer/check/links.rb', line 81
def handle_tel(link, line)
add_issue("#{link.href} contains no phone number", line: line) if link.path.empty?
end
|
#hash_check(html, href_hash) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/html-proofer/check/links.rb', line 106
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}')]", \
XpathFunctions.new).length > 0
end
|
#missing_href? ⇒ Boolean
4
5
6
|
# File 'lib/html-proofer/check/links.rb', line 4
def missing_href?
blank?(@link.href) && blank?(@link.name) && blank?(@link.id)
end
|
#placeholder? ⇒ Boolean
8
9
10
|
# File 'lib/html-proofer/check/links.rb', line 8
def placeholder?
(!blank?(@link.id) || !blank?(@link.name)) && @link.href.nil?
end
|
#run ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
|
# File 'lib/html-proofer/check/links.rb', line 12
def run
@html.css('a, link').each do |node|
@link = create_element(node)
line = @node.line
next if @link.ignore?
next if placeholder?
next if @link.allow_hash_href? && @link.href == '#'
unless @link.valid?
add_issue("#{@link.href} is an invalid URL", line: line)
next
end
check_schemes(@link, line)
if missing_href?
next if @html.internal_subset.name == 'html' && @html.internal_subset.external_id.nil?
add_issue('anchor has no href attribute', line: line)
next
end
next if @link.non_http_remote?
if @link.remote?
add_to_external_urls(@link.href, line)
next
elsif !@link.internal? && !@link.exists?
add_issue("internally linking to #{@link.href}, which does not exist", line: line)
end
if @link.unslashed_directory? @link.absolute_path
add_issue("internally linking to a directory #{@link.absolute_path} without trailing slash", line: line)
next
end
handle_hash(@link, line) if @link.hash
end
external_urls
end
|