Skip to content
This repository was archived by the owner on Oct 21, 2021. It is now read-only.

Fieldset without legend #17

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ List of errors:
- `InputMissingLabelError`
- `ButtonWithoutLabelError`
- `ARIAAttributeMissingError`
- `FieldsetMissingLegend`

## Scenario

Expand Down
5 changes: 5 additions & 0 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
<a>Link</a>
<p>Image without alt</p>
<img src="https://github.com/octocat.png" width="50" height="50">

<p>Fieldset without legend</p>
<fieldset>
<p></p>
</fieldset>
</div>

<script type="text/javascript">
Expand Down
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export function scanForProblems(context, errorCallback, options) {
logError(new InputMissingLabelError(input))
}
}

for (const fieldset of context.querySelectorAll('fieldset')) {
if (!fieldset.firstChild || fieldset.firstChild.nodeName !== 'LEGEND') {
logError(new FieldsetMissingLegend(fieldset))
}
}

if (options && options['ariaPairs']) {
for (const selector in options['ariaPairs']) {
const ARIAAttrsRequired = options['ariaPairs'][selector]
Expand Down Expand Up @@ -134,6 +141,14 @@ function ARIAAttributeMissingError(element, attr) {
}
errorSubclass(ARIAAttributeMissingError)

function FieldsetMissingLegend(element) {
this.name = 'FieldsetMissingLegend'
this.stack = new Error().stack
this.element = element
this.message = `Fieldset missing legend on ${inspect(element)}`
}
errorSubclass(FieldsetMissingLegend)

function elementIsHidden(element) {
return element.closest('[aria-hidden="true"], [hidden], [style*="display: none"]') != null
}
Expand Down
16 changes: 16 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ describe('scanForProblems should catch', () => {

assert.equal(button.getAttribute('data-error'), 'ARIAAttributeMissingError')
})

it('fieldset without legend', () => {
const fieldset = makeElement('fieldset', {"id": "fieldsetid"})
document.body.appendChild(fieldset)
const button = makeElement('button')
document.getElementById('fieldsetid').appendChild(button)
assert.equal(fieldset.getAttribute('data-error'), 'FieldsetMissingLegend')
})
})

describe('scanForProblems should not catch', () => {
Expand Down Expand Up @@ -83,6 +91,14 @@ describe('scanForProblems should not catch', () => {
assert.notOk(label.getAttribute('data-error'))
assert.notOk(input.getAttribute('data-error'))
})

it('fieldset with legend', () => {
const fieldset = makeElement('fieldset', {"id": "fieldsetid"})
document.body.appendChild(fieldset)
const legend = makeElement('legend')
document.getElementById('fieldsetid').appendChild(legend)
assert.equal(fieldset.getAttribute('data-error'), 'FieldsetMissingLegend')
})
})

function makeElement(tag, attributes, content) {
Expand Down