-
Notifications
You must be signed in to change notification settings - Fork 7
Add lint rule to make sure <details> elements have a <summary> element
#23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
f0de67f
a10071a
de0c088
43bd2da
963ed23
8d42625
8abd503
aae4f1f
bb572de
f4d75a9
6491bcc
afd2917
185195f
3a802dc
4e12784
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||
| # frozen_string_literal: true | ||||||
|
|
||||||
| require_relative "../../custom_helpers" | ||||||
|
|
||||||
| module ERBLint | ||||||
| module Linters | ||||||
| module GitHub | ||||||
| module Accessibility | ||||||
| class DetailsHasSummary < Linter | ||||||
| include ERBLint::Linters::CustomHelpers | ||||||
| include LinterRegistry | ||||||
|
|
||||||
| MESSAGE = "<details> elements need to have explict <summary> elements" | ||||||
|
|
||||||
| def run(processed_source) | ||||||
| tags(processed_source).each do |tag| | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
then you could get rid of line 21! |
||||||
| next if tag.closing? | ||||||
| next unless tag.name == "details" | ||||||
|
|
||||||
| # Find the details element index in the AST | ||||||
| index = processed_source.ast.to_a.find_index(tag.node) | ||||||
|
|
||||||
| # Get the next element in the AST | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know if there's any reason to support <details>
<p> some text </p>
<summary>Toggle</summary>
</details>
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't an easy way for One approach we've taken is keeping track of the opening/closing tag with variables. Here is an examples where we do this in dotcom: This approach might make sense if we wanted to allow
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm, I thought it needed to be the first element but that doesn't seem to be true. I wonder if it's worth supporting or if we should also validate that I'll change this to allow |
||||||
| next_node = processed_source.ast.to_a[index + 1] | ||||||
| next_tag = BetterHtml::Tree::Tag.from_node(next_node) | ||||||
|
|
||||||
| # If the next element is a summary, we're good | ||||||
| next if next_tag.name == "summary" | ||||||
|
|
||||||
| generate_offense(self.class, processed_source, tag) | ||||||
| end | ||||||
|
|
||||||
| rule_disabled?(processed_source) | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class DetailsHasSummary < LinterTestCase | ||
| def linter_class | ||
| ERBLint::Linters::GitHub::Accessibility::DetailsHasSummary | ||
| end | ||
|
|
||
| def test_warns_if_details_doesnt_have_a_summary | ||
| @file = "<details>I don't have a summary element</details>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_equal @linter.offenses.count, 1 | ||
| end | ||
|
|
||
| def test_does_not_warn_if_only_disabled_attribute_is_set | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test description needs to be updated. |
||
| @file = "<details><summary>Expand me!</summary><button>Surprise button!</button></details>" | ||
| @linter.run(processed_source) | ||
|
|
||
| assert_empty @linter.offenses | ||
| end | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.