class HTMLProofer::Check::Links

def run

def run
  @html.css("a, link").each do |node|
    @link = create_element(node)
    next if @link.ignore?
    if !allow_hash_href? && @link.node["href"] == "#"
      add_failure("linking to internal hash #, which points to nowhere", element: @link)
      next
    end
    # is there even an href?
    if blank?(@link.url.raw_attribute)
      next if allow_missing_href?
      add_failure("'#{@link.node.name}' tag is missing a reference", element: @link)
      next
    end
    # is it even a valid URL?
    unless @link.url.valid?
      add_failure("#{@link.href} is an invalid URL", element: @link)
      next
    end
    if @link.url.protocol_relative?
      add_failure(
        "#{@link.url} is a protocol-relative URL, use explicit https:// instead",
        element: @link,
      )
      next
    end
    check_schemes
    # intentionally down here because we still want valid? & missing_href? to execute
    next if @link.url.non_http_remote?
    if !@link.url.internal? && @link.url.remote?
      check_sri if @runner.check_sri? && @link.link_tag?
      # we need to skip these for now; although the domain main be valid,
      # curl/Typheous inaccurately return 404s for some links. cc https://git.io/vyCFx
      next if @link.node["rel"] == "dns-prefetch"
      unless @link.url.path?
        add_failure("#{@link.url.raw_attribute} is an invalid URL", element: @link)
        next
      end
      add_to_external_urls(@link.url, @link.line)
    elsif @link.url.internal?
      # does the local directory have a trailing slash?
      if @link.url.unslashed_directory?(@link.url.absolute_path)
        add_failure(
          "internally linking to a directory #{@link.url.raw_attribute} without trailing slash",
          element: @link,
        )
        next
      end
      add_to_internal_urls(@link.url, @link.line)
    end
  end
end