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

[DEMO ONLY, DO NOT MERGE] integration of Axe validator in DPUB-ARIA-tests.xhtml #2

Open
wants to merge 5 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
43 changes: 43 additions & 0 deletions dpub-aria-testing/content/DPUB-ARIA-tests.xhtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title>Atomic tests for DPUB ARIA</title>

<script src="https://unpkg.com/[email protected]/axe.min.js"> </script>

<script type="text/javascript">
document. addEventListener('DOMContentLoaded', () => {
window.axe
.run()
.then(results => {
// console.log(results.violations.length);
// console.log(results);

const iframe = document.createElement("iframe");
iframe.setAttribute("style", "width: 98vw; height: 90vh;");
// iframe.setAttribute("seamless", "seamless");
// iframe.setAttribute("sandbox", "allow-same-origin allow-scripts");

// const b64 = btoa(JSON.stringify(results));
// iframe.setAttribute("src", `iframe.html?results=${encodeURIComponent(b64)}`);
iframe.setAttribute("src", "iframe.html");

document.body.insertAdjacentElement("afterbegin", iframe);

if (results.passes) {
delete results.passes;
}
if (results.incomplete) {
delete results.incomplete;
}
if (results.inapplicable) {
delete results.inapplicable;
}

iframe.addEventListener('load', () => {
iframe.contentWindow.postMessage({ axe: results }, '*');
});
})
.catch(err => {
console.error(err.message);
alert(err.message);
});
});
</script>

</head>
<body>
<h1>DPUS ARIA Test Suite</h1>
Expand Down
3 changes: 3 additions & 0 deletions dpub-aria-testing/content/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* https://raw.githack.com/daisy/transitiontoepub/axe/dpub-aria-testing/content/DPUB-ARIA-tests.xhtml
* _or_:
* https://daisy-dpub-aria.surge.sh/DPUB-ARIA-tests.xhtml (`surge --domain https://daisy-dpub-aria.surge.sh`)
146 changes: 146 additions & 0 deletions dpub-aria-testing/content/iframe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/pretty-print-json.css" />
<style type="text/css">
.json-string {
white-space: break-spaces;
color: #333333;
}
pre {
font-family:monospace;
font-style:normal;
font-weight:700;
font-size:1.2em;
line-height:1.3em;
padding: 1em;
}
ul {
list-style-type: none;
}
body > ul > li:first-child {
border-left: 1em solid #333333;
}
body > ul > li > pre:first-child {
background-color:#eee;
border-left: 1em solid grey;
}
body > ul > li:nth-child(odd):not(:first-child) {
border-left: 1em solid lime;
}
body > ul > li:nth-child(even) {
border-left: 1em solid cyan;
}
body > ul > li > ul > li:nth-child(even) > pre {
border-bottom: 1px solid blue;
border-top: 1px solid blue;
border-left: 1em solid blue;
}
body > ul > li > ul > li:nth-child(odd) > pre {
border-bottom: 1px solid magenta;
border-top: 1px solid magenta;
border-left: 1em solid magenta;
}
.htmlPre {
display: inline-block;
vertical-align: top;
background-color:#eee;
font-size:0.8em;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/pretty-print-json.min.js"> </script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('message', (evt) => {
console.log('window message:', evt.data);
if (evt.data.axe) {
// const b64 = decodeURIComponent(location.search.replace("?results=", ""));
// const results = JSON.parse(atob(b64));
const results = evt.data.axe;

setTimeout(() => {
const jsonViolations = results.violations;
if (results.violations) {
delete results.violations;
}
document.body.innerHTML = `
<ul>
<li><pre>${prettyPrintJson.toHtml(results, { indent: 4, linkUrls: true, quoteKeys: false })
.replace(/<span class=json-string>"(.*)"<\/span>/g, "<span class=json-string>$1</span>")}</pre></li>

${jsonViolations && jsonViolations.length ?
jsonViolations.reduce((jsonViolationAccumulator, jsonViolation) => {

const processNodeProp = (node, k) => {
if (node[k] === null) {
delete node[k];
} else if (Array.isArray(node[k])) {
if (!node[k].length) {
delete node[k];
} else {
node[k].forEach((obj) => {
if (obj.data === null || Array.isArray(obj.data) && !obj.data.length) {
delete obj.data;
}
if (obj.relatedNodes === null || Array.isArray(obj.relatedNodes) && !obj.relatedNodes.length) {
delete obj.relatedNodes;
}
});
}
}
};
const nodes = jsonViolation.nodes;
if (jsonViolation.nodes) {
delete jsonViolation.nodes;
}
if (nodes) {
nodes.forEach((node) => {
processNodeProp(node, "any");
processNodeProp(node, "all");
processNodeProp(node, "none");
});
}
const nodesHtml = nodes && nodes.length ?
`<ul>${nodes.reduce((nodesAccumulator, node) => {
const nodeHtml = node.html;
const TOKEN = "__AXE_HTML_DAISY_DPUB_ARIA__";
if (node.html) {
// delete node.html;
node.html = TOKEN;
}
if (node.failureSummary) {
node.failureSummary = node.failureSummary.replace(/\n/g, " ");
}
if (node.target) {
node.target.forEach((t, i) => {
node.target[i] = t.replace(/"/g, '\'');
});
}
const nodeHtml_ = `<pre class="htmlPre">${nodeHtml.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/ xmlns="http:\/\/www.w3.org\/1999\/xhtml"/g, "")}</pre>`;
const pretty = prettyPrintJson.toHtml(node, { indent: 4, linkUrls: true, quoteKeys: false })
.replace(/,/g, "")
.replace(`<span class=json-string>"${TOKEN}"</span>`, nodeHtml_)
.replace(/<span class=json-string>"(.*)"<\/span>/g, "<span class=json-string>$1</span>");
return `${nodesAccumulator}
<li><pre>${pretty}</pre></li>`;
}, "")}
</ul>` :
"";

return `${jsonViolationAccumulator}
<li><pre>${prettyPrintJson.toHtml(jsonViolation, { indent: 4, linkUrls: true, quoteKeys: false })
.replace(/<span class=json-string>"(.*)"<\/span>/g, "<span class=json-string>$1</span>")}</pre>
${nodesHtml}</li>
`;
}, "") :
"<li>NO AXE VIOLATIONS</li>"}
</ul>
`;
}, 100);
}
});
});
</script>
</head>
<body>
</body>
</html>