Class: ERBLint::Linters::GitHub::Accessibility::SvgHasAccessibleText

Inherits:
Linter
  • Object
show all
Includes:
CustomHelpers, LinterRegistry
Defined in:
lib/erblint-github/linters/github/accessibility/svg_has_accessible_text.rb

Constant Summary collapse

MESSAGE =
"`<svg>` must have accessible text. Set `aria-label`, or `aria-labelledby`, or nest a `<title>` element. However, if the `<svg>` is purely decorative, hide it with `aria-hidden='true'.\nFor more info, see https://css-tricks.com/accessible-svgs/."

Constants included from CustomHelpers

CustomHelpers::INTERACTIVE_ELEMENTS

Instance Method Summary collapse

Methods included from CustomHelpers

#basic_conditional_code_check, #counter_correct?, #focusable?, #generate_offense, #generate_offense_from_source_range, #possible_attribute_values, #simple_class_name, #tags

Instance Method Details

#run(processed_source) ⇒ Object



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
# File 'lib/erblint-github/linters/github/accessibility/svg_has_accessible_text.rb', line 15

def run(processed_source)
  current_svg = nil
  has_accessible_label = false

  tags(processed_source).each do |tag|
    # Checks whether tag is a <title> nested in an <svg>
    has_accessible_label = true if current_svg && tag.name == "title" && !tag.closing?

    next if tag.name != "svg"

    if tag.closing?
      generate_offense(self.class, processed_source, current_svg) unless has_accessible_label
      current_svg = nil
    elsif possible_attribute_values(tag, "aria-hidden").join == "true"
      has_accessible_label = true
      current_svg = tag
    else
      current_svg = tag
      aria_label = possible_attribute_values(tag, "aria-label").join
      aria_labelledby = possible_attribute_values(tag, "aria-labelledby").join

      has_accessible_label = aria_label.present? || aria_labelledby.present?
    end
  end
end