Skip to content

Commit

Permalink
fix(respondable): work with CDP Page.setDocumentContent (#3921)
Browse files Browse the repository at this point in the history
* chore(origin): fix CDP Page.setDocumentContent origin null hanging audits

* add test

* 🤖 Automated formatting fixes

* fix

---------

Co-authored-by: j-mendez <jeff@a11ywatch.com>
Co-authored-by: straker <straker@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 7, 2023
1 parent b3c52bb commit 66f23e5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
6 changes: 4 additions & 2 deletions doc/examples/puppeteer/package.json
Expand Up @@ -4,10 +4,12 @@
"private": true,
"main": "axe-puppeteer.js",
"scripts": {
"test": "node axe-puppeteer.js https://deque.com"
"test": "npm run test:url && npm run test:set-content",
"test:url": "node axe-puppeteer.js https://deque.com",
"test:set-content": "node set-content.js"
},
"devDependencies": {
"axe-core": "^4.6.2",
"puppeteer": "^19.5.0"
"puppeteer": "^19.7.2"
}
}
34 changes: 34 additions & 0 deletions doc/examples/puppeteer/set-content.js
@@ -0,0 +1,34 @@
const assert = require('assert');
const puppeteer = require('puppeteer');
const axe = require('axe-core');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.setContent(`
<html lang="en">
<head>
<title>Test Page</title>
</head>
<body>
<main>
<h1>Hello World</h1>
<button id="empty-button"></button>
<iframe title="iframe" srcdoc="<button id='iframe-empty-button'></button>"></iframe>
</main>
</body>
</html>
`);

await page.evaluate(axe.source);
const frames = page.frames();
for (let i = 0; i < frames.length; i++) {
await frames[i].evaluate(axe.source);
}

const results = await page.evaluate(`window.axe.run()`);
assert(results.violations.length);

await browser.close();
})();
9 changes: 7 additions & 2 deletions lib/core/base/audit.js
Expand Up @@ -18,11 +18,16 @@ const dotRegex = /\{\{.+?\}\}/g;
function getDefaultOrigin() {
// @see https://html.spec.whatwg.org/multipage/webappapis.html#dom-origin-dev
// window.origin does not exist in ie11
if (window.origin) {
// prevent origin default "null" string on CDP `Page.setDocumentContent` https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setDocumentContent
if (window.origin && window.origin !== 'null') {
return window.origin;
}
// window.location does not exist in node when we run the build
if (window.location && window.location.origin) {
if (
window.location &&
window.location.origin &&
window.location.origin !== 'null'
) {
return window.location.origin;
}
}
Expand Down

0 comments on commit 66f23e5

Please sign in to comment.