class HTMLProofer::Check

Mostly handles issue management and collecting of external URLs.

Attributes

external_urls[R]
failures[R]
internal_urls[R]
options[R]

Public Class Methods

new(runner, html) click to toggle source
# File lib/html_proofer/check.rb, line 10
def initialize(runner, html)
  @runner = runner
  @html   = remove_ignored(html)

  @external_urls = {}
  @internal_urls = {}
  @failures = []
end
short_name() click to toggle source
# File lib/html_proofer/check.rb, line 80
def short_name
  name.split("::").last
end
subchecks(runner_options) click to toggle source
# File lib/html_proofer/check.rb, line 66
def subchecks(runner_options)
  # grab all known checks
  checks = ObjectSpace.each_object(Class).select do |klass|
    klass < self
  end

  # remove any checks not explicitly included
  checks.each_with_object([]) do |check, arr|
    next unless runner_options[:checks].include?(check.short_name)

    arr << check
  end
end

Public Instance Methods

add_failure(description, element: nil, line: nil, status: nil, content: nil) click to toggle source
# File lib/html_proofer/check.rb, line 27
def add_failure(description, element: nil, line: nil, status: nil, content: nil)
  @failures << Failure.new(
    @runner.current_filename,
    short_name,
    description,
    line: element.nil? ? line : element.line,
    status: status,
    content: element.nil? ? content : element.content,
  )
end
add_to_external_urls(url, line) click to toggle source
# File lib/html_proofer/check.rb, line 57
def add_to_external_urls(url, line)
  url_string = url.to_s

  @external_urls[url_string] = [] if @external_urls[url_string].nil?

  @external_urls[url_string] << { filename: url.filename, line: line }
end
add_to_internal_urls(url, line) click to toggle source
# File lib/html_proofer/check.rb, line 42
def add_to_internal_urls(url, line)
  url_string = url.raw_attribute

  @internal_urls[url_string] = [] if @internal_urls[url_string].nil?

  metadata = {
    source: url.source,
    filename: url.filename,
    line: line,
    base_url: base_url,
    found: false,
  }
  @internal_urls[url_string] << metadata
end
create_element(node) click to toggle source
# File lib/html_proofer/check.rb, line 19
def create_element(node)
  Element.new(@runner, node, base_url: base_url)
end
run() click to toggle source
# File lib/html_proofer/check.rb, line 23
def run
  raise NotImplementedError, "HTMLProofer::Check subclasses must implement #run"
end
short_name() click to toggle source
# File lib/html_proofer/check.rb, line 38
def short_name
  self.class.name.split("::").last
end

Private Instance Methods

base_url() click to toggle source
# File lib/html_proofer/check.rb, line 85
        def base_url
  return @base_url if defined?(@base_url)

  return (@base_url = "") if (base = @html.at_css("base")).nil?

  @base_url = base["href"]
end
remove_ignored(html) click to toggle source
# File lib/html_proofer/check.rb, line 93
        def remove_ignored(html)
  return if html.nil?

  html.css("code, pre, tt").each(&:unlink)
  html
end