Skip to content

Commit 36f99ca

Browse files
authored
Merge pull request #115 from shamahdotdev/master
2 parents 4f8215c + acdefca commit 36f99ca

9 files changed

Lines changed: 122 additions & 1 deletion

File tree

checka11y-warnings.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ a[href="#"]::after, a[role=button]::after, a[href^="javascript:"]::after {
168168
background-color: var(--checka11y-bg-warning);
169169
}
170170

171+
section:empty::before, section > :not(h1, h2, h3, h4, h5, h6, img):last-child::before {
172+
display: block;
173+
font-size: 1rem;
174+
font-family: verdana, geneva, tahoma, sans-serif;
175+
font-weight: 700;
176+
font-style: initial;
177+
padding: var(--checka11y-space-4) var(--checka11y-space-6);
178+
border-radius: 0.75rem;
179+
letter-spacing: initial;
180+
text-decoration: none;
181+
text-transform: initial;
182+
text-shadow: none;
183+
content: "WARNING (W0012): A <section> should contain a heading element." !important;
184+
color: var(--checka11y-text-warning);
185+
border: 0.4rem solid var(--checka11y-border-warning);
186+
background-color: var(--checka11y-bg-warning);
187+
}
188+
189+
section > :is(h1, h2, h3, h4, h5, h6) ~ :not(:is(h1, h2, h3, h4, h5, h6, img)):last-child::before {
190+
content: none !important;
191+
}
192+
171193
[title]::after {
172194
display: block;
173195
font-size: 1rem;

checka11y.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,28 @@ a[href="#"]::after, a[role=button]::after, a[href^="javascript:"]::after {
792792
background-color: var(--checka11y-bg-warning);
793793
}
794794

795+
section:empty::before, section > :not(h1, h2, h3, h4, h5, h6, img):last-child::before {
796+
display: block;
797+
font-size: 1rem;
798+
font-family: verdana, geneva, tahoma, sans-serif;
799+
font-weight: 700;
800+
font-style: initial;
801+
padding: var(--checka11y-space-4) var(--checka11y-space-6);
802+
border-radius: 0.75rem;
803+
letter-spacing: initial;
804+
text-decoration: none;
805+
text-transform: initial;
806+
text-shadow: none;
807+
content: "WARNING (W0012): A <section> should contain a heading element." !important;
808+
color: var(--checka11y-text-warning);
809+
border: 0.4rem solid var(--checka11y-border-warning);
810+
background-color: var(--checka11y-bg-warning);
811+
}
812+
813+
section > :is(h1, h2, h3, h4, h5, h6) ~ :not(:is(h1, h2, h3, h4, h5, h6, img)):last-child::before {
814+
content: none !important;
815+
}
816+
795817
[title]::after {
796818
display: block;
797819
font-size: 1rem;

codes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,6 @@ A list of every Checka11y.css error & warning code with details on what the issu
124124

125125
- ### W0011
126126
The highlighted element `<a>` has been detected to have `href="#"`, `role="button"` , `href="javascript:doSomething(0)"`. Those anchor elements are often used to trigger a click event on the page. This can be disorientating to a user, especially those with visual impairments and those with cognitive disabilities. Links should redirect to a resource/page. [Read more about this here](https://www.htmhell.dev/8-anchor-tag-used-as-button).
127+
128+
- ### W0012
129+
The `<section>` element represents a generic standalone section of a document, which doesn't have a more specific semantic element to represent it. This means `<section>` element should be identified, typically by including a heading (`<h1>`-`<h6>` element) as a child of the `<section>` element. [Read more about this here](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/section)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
describe('<section>', () => {
2+
const sectionLastChildHTML = [];
3+
before(() => {
4+
cy.visit('/test/index.html');
5+
6+
// Get all last child element from a section with heading element.
7+
cy.get('section > :is( h1, h2, h3, h4, h5, h6 ) ~ :not( :is( h1, h2, h3, h4, h5, h6, img ) ):last-child')
8+
.each(element => {
9+
cy.get(element)
10+
.invoke('prop', 'outerHTML').then(html => {
11+
sectionLastChildHTML.push(html);
12+
})
13+
});
14+
});
15+
16+
it('should show warning on last child if section does not have heading element', () => {
17+
cy.get('section > :not( h1, h2, h3, h4, h5, h6, img ):last-child')
18+
.each((element) => {
19+
cy.get(element)
20+
.invoke('prop', 'outerHTML').then(html => {
21+
if(!sectionLastChildHTML.includes(html)) {
22+
cy.get(element)
23+
.before('content')
24+
.should('eq', 'WARNING (W0012): A <section> should contain a heading element.');
25+
}
26+
})
27+
});
28+
});
29+
30+
it('should show warning on if section is empty', () => {
31+
cy.get('section:empty')
32+
.each((element) => {
33+
cy.get(element)
34+
.invoke('prop', 'outerHTML').then(html => {
35+
if(!sectionLastChildHTML.includes(html)) {
36+
cy.get(element)
37+
.before('content')
38+
.should('eq', `WARNING (W0012): A <section> should contain a heading element.`);
39+
}
40+
})
41+
});
42+
});
43+
});

features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ A list of every a11y concern Checka11y.css will check for and highlight with lin
3434
- [W0009](./codes.md#W0009): Checks for focusable elements inside `aria-hidden="true"` elements.
3535
- [W0010](./codes.md#W0010): Checks for heading elements containing `role="text"` attribute.
3636
- [W0011](./codes.md#W0011): Checks for anchor tags that are used as buttons.
37+
- [W0012](./codes.md#W0012): Checks for heading element inside `<section>` element.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"Shona McKenzie (https://github.com/shonamckenzie)",
3838
"Ashar Setiawan (https://github.com/azhsetiawan)",
3939
"Bogdan Lazar (https://github.com/tricinel)",
40-
"Martin Sidorov (https://github.com/Matrix278)"
40+
"Martin Sidorov (https://github.com/Matrix278)",
41+
"Shaddam Amru Hasibuan <shaddam.a.h@gmail.com> (https:/github.com/shamahdotdev)"
4142
],
4243
"keywords": [
4344
"checka11y",

src/warnings/checka11y-warnings.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@import "./features/images";
1313
@import "./features/inline";
1414
@import "./features/links";
15+
@import "./features/section";
1516
@import "./features/title";
1617
@import "./features/underline";
1718
@import "./features/headings";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* W0012: A <section> should contain a heading element */
2+
3+
/**
4+
* TODO: Investigate refactoring to use :has() when supported by major browsers
5+
* Currently, the first selector below will give a warning to all last child element inside a section and then remove it with the second selector by selecting if there's a heading element inside a section.
6+
* This can be solved easily and more practical by using :has(h1 - h6) to check if a section contains heading element.
7+
*/
8+
9+
section {
10+
&:empty::before,
11+
> :not( h1, h2, h3, h4, h5, h6, img ):last-child::before {
12+
@include contentMessage(warning, "W0012", "A <section> should contain a heading element.");
13+
}
14+
15+
> :is( h1, h2, h3, h4, h5, h6 ) ~ :not( :is( h1, h2, h3, h4, h5, h6, img ) ):last-child::before {
16+
content: none !important;
17+
}
18+
}

test/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,5 +380,15 @@ <h2>Anchor tags should not be used as buttons</h2>
380380
<a href="javascript:void(0)" role="button">Do another thing</a>
381381
<a href="javascript:doSomething(0)">Do another thing</a>
382382
</section>
383+
<section>
384+
<h2>A <code>&lt;section&gt;</code> should contain a heading element</h2>
385+
<section>
386+
<h2>This is a heading element.</h1>
387+
</section>
388+
<section><!-- This is an empty section --></section>
389+
<section>
390+
<p>this is not a heading element, just a cool paragraph.</p>
391+
</section>
392+
</section>
383393
</body>
384394
</html>

0 commit comments

Comments
 (0)