Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add document outline #18

Merged
merged 1 commit into from
Jan 16, 2017
Merged
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
39 changes: 37 additions & 2 deletions a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const a11yChecker = () => {

const getElement = (element) => document.getElementsByTagName(element);
const getElement = (element) => document.querySelectorAll(element);

const checkDoctype = (() => {
const doctype = document.doctype;
Expand Down Expand Up @@ -69,10 +69,44 @@ const a11yChecker = () => {
const heading = getElement('h1');
const headingLength = heading.length;
if (headingLength > 1) {
console.warn('Should h1 exist in page once usually in warno!')
console.warn('Should h1 exist in page once usually in logo!')
}
})();

const checkDocumentOutline = (() => {
const allHeadings = [...getElement('h1, h2, h3, h4, h5, h6')];
const allHeadingNode= allHeadings.map(ele => ele.nodeName);
const uniqueHeadings = allHeadings.filter((item, pos) => allHeadingNode.indexOf(item.nodeName) === pos );
const headingsLength = uniqueHeadings.length;

if (headingsLength >= 1) {
if (uniqueHeadings[0].nodeName !== 'H1') {
console.warn('Should heading start with H1:h6')
}
}
if (headingsLength >= 2) {
if (uniqueHeadings[1].nodeName !== 'H2') {
console.warn('Should using H2 after H1 not any heading else.')
}
}
if (headingsLength >= 3) {
if (uniqueHeadings[2].nodeName !== 'H3') {
console.warn('Should using H3 after H2 not any heading else.')
}
}
if (headingsLength >= 4) {
if (uniqueHeadings[3].nodeName !== 'H4') {
console.warn('Should using H4 after H3 not any heading else.')
}
}
if (headingsLength >= 5) {
if (uniqueHeadings[4].nodeName !== 'H5') {
console.warn('Should using H5 after H4 not any heading else.')
}
}
})();


const checkImages = (() => {
const img = getElement('img');
for (let i = 0; i < img.length; i++) {
Expand Down Expand Up @@ -419,5 +453,6 @@ code: ${allElements[i].outerHTML}`)
}
}
})();

}
window.addEventListener('load', a11yChecker);
2 changes: 1 addition & 1 deletion a11y.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1>A11y checker</h1>
<a href="#hi">hi</a>
<section id="section1" class="section-prim">
<h1>accessability checker!</h1>
<h3>testing issues in HTML code.</h3>
<nav id="sec-nav" class="nav-sec">
<button id="btn-prim" class="btn-prim"></button>
<ul>
Expand Down