Skip to content

Commit

Permalink
Merge pull request #115 from github/kh-fix-visually-hidden-bug
Browse files Browse the repository at this point in the history
Simplify no-visually-hidden-interactive-elements and fix bug
  • Loading branch information
khiga8 authored Oct 19, 2023
2 parents bd72cdf + 946261f commit f8539b9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ inherit_gem:
- [GitHub::Accessibility::NoAriaLabelMisuse](./docs/rules/accessibility/no-aria-label-misuse.md)
- [GitHub::Accessibility::NoPositiveTabIndex](./docs/rules/accessibility/no-positive-tab-index.md)
- [GitHub::Accessibility::NoRedundantImageAlt](./docs/rules/accessibility/no-redundant-image-alt.md)
- [GitHub::Accessibility::NoVisuallyHiddenInteractiveElements](./docs/rules/accessibility/no-visually-hidden-interactive-elements.md)
- [GitHub::Accessibility::NoTitleAttribute](./docs/rules/accessibility/no-title-attribute.md)
- [GitHub::Accessibility::SvgHasAccessibleText](./docs/rules/accessibility/svg-has-accessible-text.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ class NoVisuallyHiddenInteractiveElements < Linter
MESSAGE = "Avoid visually hidding interactive elements. Visually hiding interactive elements can be confusing to sighted keyboard users as it appears their focus has been lost when they navigate to the hidden element"

def run(processed_source)
visually_hidden = false

tags(processed_source).each do |tag|
next if tag.closing?
classes = possible_attribute_values(tag, "class")
visually_hidden = true if classes.include?("sr-only")
next unless classes.include?("sr-only") || visually_hidden
if INTERACTIVE_ELEMENTS.include?(tag.name)
if classes.include?("sr-only") && INTERACTIVE_ELEMENTS.include?(tag.name)
generate_offense(self.class, processed_source, tag)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def test_does_not_warn_if_element_is_interactive_and_shown_on_focus
assert_empty @linter.offenses
end

def test_warn_if_element_is_interactive_in_a_visually_hidden_parent
@file = "<div class='sr-only'><button>Submit</button></div>"
def test_does_not_warn_on_unexpected_elements
@file = <<~ERB
<span class="sr-only"></span>
<button></button>
ERB
@linter.run(processed_source)

assert_equal(1, @linter.offenses.count)
error_messages = @linter.offenses.map(&:message).sort
assert_match(/Avoid visually hidding interactive elements. Visually hiding interactive elements can be confusing to sighted keyboard users as it appears their focus has been lost when they navigate to the hidden element/, error_messages.last)
assert_empty @linter.offenses
end
end

0 comments on commit f8539b9

Please sign in to comment.