class HTMLProofer::UrlValidator::Internal

Attributes

internal_urls[R]

Public Class Methods

new(runner, internal_urls) click to toggle source
Calls superclass method HTMLProofer::UrlValidator::new
# File lib/html_proofer/url_validator/internal.rb, line 8
def initialize(runner, internal_urls)
  super(runner)

  @internal_urls = internal_urls
end

Public Instance Methods

validate() click to toggle source
# File lib/html_proofer/url_validator/internal.rb, line 14
def validate
  urls_to_check = @cache.internal_enabled? ? @runner.load_internal_cache : @internal_urls
  urls_detected = pluralize(urls_to_check.count, "internal link", "internal links")
  @logger.log(:info, "Checking #{urls_detected}")

  run_internal_link_checker(urls_to_check)

  @failed_checks
end

Private Instance Methods

find_fragments(fragment_ids, html) click to toggle source
# File lib/html_proofer/url_validator/internal.rb, line 132
        def find_fragments(fragment_ids, html)
  xpaths = fragment_ids.uniq.flat_map do |frag_id|
    escaped_frag_id = "'#{frag_id.split("'").join("', \"'\", '")}', ''"
    [
      "//*[case_sensitive_equals(@id, concat(#{escaped_frag_id}))]",
      "//*[case_sensitive_equals(@name, concat(#{escaped_frag_id}))]",
    ]
  end
  xpaths << XpathFunctions.new

  html.xpath(*xpaths)
end
hash_exists_for_url?(url) click to toggle source

verify the hash w/o just based on the URL, w/o looking at the target file

> returns nil if the has could not be verified

# File lib/html_proofer/url_validator/internal.rb, line 111
        def hash_exists_for_url?(url)
  href_hash = url.hash
  return true if blank?(href_hash)
  return true unless @runner.options[:check_internal_hash]

  # prevents searching files we didn't ask about
  return false unless url.known_extension?
  return false unless url.has_hash?

  decoded_href_hash = Addressable::URI.unescape(href_hash)
  fragment_ids = [href_hash, decoded_href_hash]
  # https://www.w3.org/TR/html5/single-page.html#scroll-to-fragid
  true if fragment_ids.include?("top")
end
hash_exists_in_html?(href_hash, html) click to toggle source
# File lib/html_proofer/url_validator/internal.rb, line 126
        def hash_exists_in_html?(href_hash, html)
  decoded_href_hash = Addressable::URI.unescape(href_hash)
  fragment_ids = [href_hash, decoded_href_hash]
  !find_fragments(fragment_ids, html).empty?
end