Skip to content

Commit

Permalink
fix(respondable): work on iframes in shadow DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoFiers committed Mar 31, 2021
1 parent 694f93f commit 0b2548d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
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
44 changes: 43 additions & 1 deletion test/core/utils/respondable.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ describe('axe.utils.respondable', function() {
);
});

it('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 +255,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 +279,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

0 comments on commit 0b2548d

Please sign in to comment.