@@ -10,31 +10,7 @@ class LandmarkHasLabel < Linter
1010 include ERBLint ::Linters ::CustomHelpers
1111 include LinterRegistry
1212
13- LANDMARK_ROLES = %w[ complementary navigation region search ] . freeze
14- LANDMARK_TAGS = %w[ aside nav section ] . freeze
15- MESSAGE = "Landmark elements should have an aria-label attribute, or aria-labelledby if a heading elements exists in the landmark."
16- ROLE_TAG_MAPPING = { "complementary" => "aside" , "navigation" => "nav" , "region" => "section" } . freeze
17-
18- def get_additional_message ( tag , roles )
19- role_matched = ( roles & ROLE_TAG_MAPPING . keys ) . first
20- if role_matched
21- tag_matched = ROLE_TAG_MAPPING [ role_matched ]
22-
23- if tag . name == tag_matched
24- "The <#{ tag_matched } > element will automatically communicate a role of '#{ role_matched } '. You can safely drop the role attribute."
25- else
26- replace_message = if tag . name == "div"
27- "If possible replace this tag with a <#{ tag_matched } >."
28- else
29- "Wrapping this element in a <#{ tag_matched } > and setting a label on it is reccomended."
30- end
31-
32- "The <#{ tag_matched } > element will automatically communicate a role of '#{ role_matched } '. #{ replace_message } "
33- end
34- elsif roles . include? ( "search" ) && tag . name != "form"
35- "The 'search' role works best when applied to a <form> element. If possible replace this tag with a <form>."
36- end
37- end
13+ MESSAGE = "The navigation landmark should have a unique accessible name via `aria-label` or `aria-labelledby`."
3814
3915 class ConfigSchema < LinterConfig
4016 property :counter_enabled , accepts : [ true , false ] , default : false , reader : :counter_enabled?
@@ -44,17 +20,15 @@ class ConfigSchema < LinterConfig
4420 def run ( processed_source )
4521 tags ( processed_source ) . each do |tag |
4622 next if tag . closing?
47-
48- possible_roles = possible_attribute_values ( tag , "role" )
49- next unless LANDMARK_TAGS . include? ( tag . name ) && ( possible_roles & LANDMARK_ROLES ) . empty?
50- next if tag . attributes [ "aria-label" ] &.value &.present? || tag . attributes [ "aria-labelledby" ] &.value &.present?
51-
52- message = get_additional_message ( tag , possible_roles )
53- if message
54- generate_offense ( self . class , processed_source , tag , "#{ MESSAGE } \n #{ message } " )
55- else
56- generate_offense ( self . class , processed_source , tag )
23+ next unless possible_attribute_values ( tag , "role" ) . include? ( "navigation" ) || tag . name == "nav"
24+ if possible_attribute_values ( tag , "aria-label" ) . empty? && possible_attribute_values ( tag , "aria-labelledby" ) . empty?
25+ message = MESSAGE
26+ if tag . name != "nav"
27+ message += "Additionally, you can safely drop the `role='navigation'` and replace it with the native HTML `nav` element."
28+ end
29+ generate_offense ( self . class , processed_source , tag , message )
5730 end
31+
5832 end
5933
6034 if @config . counter_enabled?
0 commit comments