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

Check html language #10

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ document.addEventListener('DOMContentLoaded', function() {

List of errors:

- `HtmlWithoutLangAttributeError`
- `HtmlWithoutLangValueError`
- `ImageWithoutAltAttributeError`
- `ElementWithoutLabelError`
- `LinkWithoutLabelOrRoleError`
Expand Down
2 changes: 1 addition & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</div>

<script type="text/javascript">
var errors = accessibilityjs.scanForProblems(document.body, function (error) {
var errors = accessibilityjs.scanForProblems(document, function (error) {
error.element.style.outline = '5px solid red'
error.element.addEventListener('click', function () {
alert([error.name, error.message].join('\n'))
Expand Down
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ export function scanForProblems(context, errorCallback, options) {
errorCallback(err)
}

const html = context.querySelector('html')

if (!html.hasAttribute('lang')) {
logError(new HtmlWithoutLangAttributeError(html))
} else {
const getLangVal = html.getAttribute('lang')

if (getLangVal === '' || getLangVal === null) {
logError(new HtmlWithoutLangValueError(html))
}
}

for (const img of context.querySelectorAll('img')) {
if (!img.hasAttribute('alt')) {
logError(new ImageWithoutAltAttributeError(img))
Expand Down Expand Up @@ -78,6 +90,22 @@ function errorSubclass(fn) {
fn.prototype.constructor = fn
}

function HtmlWithoutLangAttributeError(element) {
this.name = 'HtmlWithoutLangAttributeError'
this.stack = new Error().stack
this.element = element
this.message = `Missing lang attribute on ${inspect(element)}`
}
errorSubclass(HtmlWithoutLangAttributeError)

function HtmlWithoutLangValueError(element) {
this.name = 'HtmlWithoutLangValueError'
this.stack = new Error().stack
this.element = element
this.message = `Missing lang value on ${inspect(element)}`
}
errorSubclass(HtmlWithoutLangValueError)

function ImageWithoutAltAttributeError(element) {
this.name = 'ImageWithoutAltAttributeError'
this.stack = new Error().stack
Expand Down
2 changes: 1 addition & 1 deletion test/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(config) {
base: 'Firefox',
flags: ['-headless'],
displayName: 'HeadlessFirefox'
},
}
}
})
}
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
describe('scanForProblems should catch', () => {
it('html without lang', () => {
const html = makeElement('html', {"lang": "en"})
document.body.appendChild(html)

assert.exists(html.getAttribute('lang'), html)
})

it('html without lang value', () => {
const html = makeElement('html', {"lang": "en"})
document.body.appendChild(html)

assert.isNotEmpty(html.getAttribute('lang'), html)
})

it('empty button', () => {
const button = makeElement('button', {type: 'button'})
document.body.appendChild(button)
Expand Down