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

fix(respondable): work on iframes in shadow DOM #2857

Merged
merged 1 commit into from
Apr 1, 2021
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
8 changes: 4 additions & 4 deletions lib/core/utils/respondable/assert-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export function assertIsParentWindow(win) {

export function assertIsFrameWindow(win) {
assetNotGlobalWindow(win);
const frames = Array.from(window.frames);
if (!frames.some(frame => frame === win)) {
throw new Error('Respondable target must be a frame in the current window');
}
assert(
win.parent === window,
'Respondable target must be a frame in the current window'
);
}

export function assetNotGlobalWindow(win) {
Expand Down
47 changes: 46 additions & 1 deletion test/core/utils/respondable.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('axe.utils.respondable', function() {
var postMessage = window.postMessage;
var captureError = axe.testUtils.captureError;
var isIE11 = axe.testUtils.isIE11;
var shadowSupported = axe.testUtils.shadowSupport.v1;
this.timeout(1000);

beforeEach(function(done) {
Expand Down Expand Up @@ -151,6 +152,37 @@ describe('axe.utils.respondable', function() {
);
});

(shadowSupported ? it : xit)('works with frames in shadow DOM', function(
done
) {
fixture.innerHTML = '<div id="shadow-root"></div>';
var shadowRoot = fixture
.querySelector('#shadow-root')
.attachShadow({ mode: 'open' });
frame = document.createElement('iframe');
frame.src = '../mock/frames/test.html';

frame.addEventListener('load', function() {
var called = false;
frameWin = frame.contentWindow;
frameSubscribe = frameWin.axe.utils.respondable.subscribe;

frameSubscribe('greeting', function(msg) {
assert.equal(msg, 'hello');
called = true;
});
respondable(frameWin, 'greeting', 'hello');
afterMessage(
frameWin,
captureError(function() {
assert.isTrue(called);
done();
}, done)
);
});
shadowRoot.appendChild(frame);
});

it('is not called on a different topic', function(done) {
var called = false;
frameSubscribe('otherTopic', function() {
Expand Down Expand Up @@ -226,6 +258,17 @@ describe('axe.utils.respondable', function() {
respondable(frameWin, 'greeting', new Error('expected message'));
});

(isIE11 ? it.skip : it)(
// In IE win.parent is read-only
'throws if frame.parent is not the window',
function() {
frameWin.parent = frameWin;
assert.throws(function() {
respondable(frameWin, 'greeting');
});
}
);

(isIE11 ? it.skip : it)(
// In IE win.parent is read-only
'is not called when the source is not a frame in the page',
Expand All @@ -239,8 +282,10 @@ describe('axe.utils.respondable', function() {
frameSubscribe('greeting', function() {
doneOnce(new Error('subscribe should not be called'));
});
frameWin.parent = frameWin;
respondable(frameWin, 'greeting');
// Swap parent after the message is sent, but before it is received:
frameWin.parent = frameWin;

setTimeout(
captureError(function() {
assert.isTrue(called);
Expand Down