Skip to content

Commit

Permalink
Create rule S6850 for HTML: Heading elements should have accessible c…
Browse files Browse the repository at this point in the history
…ontent (#3652)

* Add html to rule S6850

* Add HTML documentation to S6850

Also improve the wording by replacing the term _header_ with the semantically correct _heading_ one.

* Split the rules tags

Also format the JavaScript sample with 2-spaces identation.

---------

Co-authored-by: ericmorand-sonarsource <ericmorand-sonarsource@users.noreply.github.com>
Co-authored-by: Eric MORAND <eric.morand@sonarsource.com>
Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 16, 2024
1 parent 6ba18ae commit 90b7373
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 69 deletions.
5 changes: 5 additions & 0 deletions rules/S6850/html/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tags": [
"accessibility"
]
}
45 changes: 45 additions & 0 deletions rules/S6850/html/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
include::../rule.adoc[tag=header]

=== Code examples

==== Noncompliant code example

[source,html,diff-id=1,diff-type=noncompliant]
----
<h1>JavaScript Programming Guide</h1>
<p>An introduction to JavaScript programming and its applications.</p>
<h2>JavaScript Basics</h2>
<p>Understanding the basic concepts in JavaScript programming.</p>
<h3>Variables</h3>
<p>Explanation of what variables are and how to declare them in JavaScript.</p>
<h3 aria-hidden>Data Types</h3> <!-- Noncompliant -->
<p>Overview of the different data types in JavaScript.</p>
<h3 /> <!-- Noncompliant -->
<p>Understanding how to declare and use functions in JavaScript.</p>
----

==== Compliant solution

[source,html,diff-id=1,diff-type=compliant]
----
<h1>JavaScript Programming Guide</h1>
<p>An introduction to JavaScript programming and its applications.</p>
<h2>JavaScript Basics</h2>
<p>Understanding the basic concepts in JavaScript programming.</p>
<h3>Variables</h3>
<p>Explanation of what variables are and how to declare them in JavaScript.</p>
<h3>Data Types</h3>
<p>Overview of the different data types in JavaScript.</p>
<h3>Functions</h3>
<p>Understanding how to declare and use functions in JavaScript.</p>
----

include::../rule.adoc[tag=footer]
22 changes: 1 addition & 21 deletions rules/S6850/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
{
"title": "Header elements should have accessible content",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"accessibility",
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6850",
"sqKey": "S6850",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
]
}
77 changes: 30 additions & 47 deletions rules/S6850/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
== Why is this an issue?

Header elements are represented by the tags ``++<h1>++`` through ``++<h6>++``, with ``++<h1>++`` being the highest level and ``++<h6>++`` being the lowest. These elements are used to structure the content of the page and create a hierarchical outline that can be followed by users and search engines alike.

In the context of accessibility, header elements play a crucial role. They provide a way for users, especially those using assistive technologies like screen readers, to navigate through the content of a webpage. By reading out the headers, screen readers can give users an overview of the content and allow them to jump to the section they're interested in.

If a header element is empty, it can lead to confusion as it doesn't provide any information about the content that follows. This can make navigation difficult for users relying on screen readers, resulting in a poor user experience and making the website less accessible for people with visual impairments.

Therefore, to ensure your website is accessible to all users, it's important to always include meaningful content in your header elements.

== How to fix it

Do not leave empty your header elements.
include::../rule.adoc[tag=header]

=== Code examples

Expand All @@ -19,24 +7,24 @@ Do not leave empty your header elements.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
function JavaScript101() {
return (
<>
<h1>JavaScript Programming Guide</h1>
<p>An introduction to JavaScript programming and its applications.</p>
return (
<>
<h1>JavaScript Programming Guide</h1>
<p>An introduction to JavaScript programming and its applications.</p>
<h2>JavaScript Basics</h2>
<p>Understanding the basic concepts in JavaScript programming.</p>
<h2>JavaScript Basics</h2>
<p>Understanding the basic concepts in JavaScript programming.</p>
<h3>Variables</h3>
<p>Explanation of what variables are and how to declare them in JavaScript.</p>
<h3>Variables</h3>
<p>Explanation of what variables are and how to declare them in JavaScript.</p>
<h3 aria-hidden>Data Types</h3> // Noncompliant
<p>Overview of the different data types in JavaScript.</p>
<h3 aria-hidden>Data Types</h3> // Noncompliant
<p>Overview of the different data types in JavaScript.</p>
<h3 /> // Noncompliant
<p>Understanding how to declare and use functions in JavaScript.</p>
</>
);
<h3 /> // Noncompliant
<p>Understanding how to declare and use functions in JavaScript.</p>
</>
);
}
----

Expand All @@ -45,30 +33,25 @@ function JavaScript101() {
[source,javascript,diff-id=1,diff-type=compliant]
----
function JavaScript101() {
return (
<>
<h1>JavaScript Programming Guide</h1>
<p>An introduction to JavaScript programming and its applications.</p>
return (
<>
<h1>JavaScript Programming Guide</h1>
<p>An introduction to JavaScript programming and its applications.</p>
<h2>JavaScript Basics</h2>
<p>Understanding the basic concepts in JavaScript programming.</p>
<h2>JavaScript Basics</h2>
<p>Understanding the basic concepts in JavaScript programming.</p>
<h3>Variables</h3>
<p>Explanation of what variables are and how to declare them in JavaScript.</p>
<h3>Variables</h3>
<p>Explanation of what variables are and how to declare them in JavaScript.</p>
<h3>Data Types</h3>
<p>Overview of the different data types in JavaScript.</p>
<h3>Data Types</h3>
<p>Overview of the different data types in JavaScript.</p>
<h3>Functions</h3>
<p>Understanding how to declare and use functions in JavaScript.</p>
</>
);
<h3>Functions</h3>
<p>Understanding how to declare and use functions in JavaScript.</p>
</>
);
}
----

== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements[Heading elements]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-hidden[aria-hidden]
* WCAG - https://www.w3.org/TR/UNDERSTANDING-WCAG20/navigation-mechanisms-descriptive.html[Headings and Labels]
include::../rule.adoc[tag=footer]
22 changes: 21 additions & 1 deletion rules/S6850/metadata.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
{
}
"title": "Heading elements should have accessible content",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6850",
"sqKey": "S6850",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
30 changes: 30 additions & 0 deletions rules/S6850/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
:doctype: book

# tag::header[]
== Why is this an issue?

Heading elements are represented by the tags ``++<h1>++`` through ``++<h6>++``, with ``++<h1>++`` being the highest level and ``++<h6>++`` being the lowest. These elements are used to structure the content of the page and create a hierarchical outline that can be followed by users and search engines alike.

In the context of accessibility, heading elements play a crucial role. They provide a way for users, especially those using assistive technologies like screen readers, to navigate through the content of a webpage. By reading out the headings, screen readers can give users an overview of the content and allow them to jump to the section they're interested in.

If a heading element is empty, it can lead to confusion as it doesn't provide any information about the content that follows. This can make navigation difficult for users relying on screen readers, resulting in a poor user experience and making the website less accessible for people with visual impairments.

Therefore, to ensure your website is accessible to all users, it's important to always include meaningful content in your heading elements.

== How to fix it

Do not leave empty your heading elements.

# end::header[]

=== Code examples

# tag::footer[]
== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements[Heading elements]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-hidden[aria-hidden]
* WCAG - https://www.w3.org/TR/UNDERSTANDING-WCAG20/navigation-mechanisms-descriptive.html[Headings and Labels]

# end::footer[]

0 comments on commit 90b7373

Please sign in to comment.