Class: Links

Inherits:
HTML::Proofer::Checks::Check show all
Defined in:
lib/html/proofer/checks/links.rb

Instance Attribute Summary

Attributes inherited from HTML::Proofer::Checks::Check

#additional_alt_ignores, #additional_href_ignores, #external_urls, #issues, #options, #path, #src

Instance Method Summary collapse

Methods inherited from HTML::Proofer::Checks::Check

#add_issue, #add_to_external_urls, #initialize, #output_filenames, subclasses

Constructor Details

This class inherits a constructor from HTML::Proofer::Checks::Check

Instance Method Details

#hash_check(html, href_hash) ⇒ Object



69
70
71
# File 'lib/html/proofer/checks/links.rb', line 69

def hash_check(html, href_hash)
  html.xpath("//*[@id='#{href_hash}']", "//*[@name='#{href_hash}']").length > 0
end

#runObject



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
60
61
62
63
64
65
66
67
# File 'lib/html/proofer/checks/links.rb', line 17

def run
  @html.css("a, link").each do |l|
    link = Link.new l, "link", self

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

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

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

    # does the file even exist?
    if link.remote?
      add_to_external_urls link.href
    else
      self.add_issue "internally linking to #{link.href}, which does not exist" unless link.exists?
    end

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

    # verify the target hash
    if link.hash
      if link.remote?
        add_to_external_urls link.href
      elsif link.internal?
        self.add_issue "linking to internal hash ##{link.hash} that does not exist" unless hash_check @html, link.hash
      elsif link.external?
        unless link.exists?
          self.add_issue "trying to find hash of #{link.href}, but #{link.absolute_path} does not exist"
        else
          target_html = HTML::Proofer.create_nokogiri link.absolute_path
          self.add_issue "linking to #{link.href}, but #{link.hash} does not exist" unless hash_check target_html, link.hash
        end
      end
    end
  end

  external_urls
end