Skip to content

Commit dd95770

Browse files
committed
Merge pull request #200 from adriaanbaelus/hashchange-on-window-with-iframe
Make sure iframes don't interfere with modal events [fixes #199]
2 parents c6e7269 + 7a6e084 commit dd95770

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

download/modal.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646
}
4747

4848
// Make elements an array and attach event listeners
49-
if (!elements.length) {
49+
// If a window contains at least one (i)frame, it will behave array-like,
50+
// see https://developer.mozilla.org/en-US/docs/Web/API/Window/length
51+
// If we don't explicitly check for window, we'll be adding the event
52+
// listeners to the frames instead of the root window.
53+
if (elements === global || !elements.length) {
5054
elements = [elements];
5155
}
5256

modal.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646
}
4747

4848
// Make elements an array and attach event listeners
49-
if (!elements.length) {
49+
// If a window contains at least one (i)frame, it will behave array-like,
50+
// see https://developer.mozilla.org/en-US/docs/Web/API/Window/length
51+
// If we don't explicitly check for window, we'll be adding the event
52+
// listeners to the frames instead of the root window.
53+
if (elements === global || !elements.length) {
5054
elements = [elements];
5155
}
5256

test/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ <h2 id="modal-label">Headline</h2>
6767
data-dismiss="modal" data-close="Close">&times;</a>
6868
</section>
6969

70+
<!-- An iframe that may interfere with modal script -->
71+
<iframe style="position: fixed; top: 101%" src="iframe.html" frameborder="0" allowfullscreen></iframe>
72+
7073
<!-- Source -->
7174
<script src="../modal.js"></script>
7275

test/spec/modal.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
/*global describe, it, expect, afterEach */
1+
/*global describe, it, xit, expect, afterEach, waitsFor */
22
(function ($, CSSModal) {
33

44
'use strict';
55

6+
7+
// Helper for async tests, see https://gist.github.com/yyx990803/a6154353ae17dde81444
8+
function async (run) {
9+
return function () {
10+
var done = false;
11+
waitsFor(function () { return done; });
12+
run(function () { done = true; });
13+
};
14+
}
15+
616
// Testing if the modal works in general
717
describe('Modal', function () {
818

@@ -34,21 +44,23 @@
3444
expect($modal.css('opacity')).toBe('1');
3545
});
3646

37-
it('has class is-active when hash is set', function () {
47+
it('has class is-active when hash is set', async(function (done) {
3848
window.location.hash = '#modal';
3949

4050
setTimeout(function () {
4151
expect($modal.hasClass('is-active')).toBe(true);
52+
done();
4253
}, 0);
43-
});
54+
}));
4455

45-
it('has not class is-active when hash is #!', function () {
56+
it('has not class is-active when hash is #!', async(function (done) {
4657
window.location.hash = '#!';
4758

4859
setTimeout(function () {
4960
expect($modal.hasClass('is-active')).not.toBe(true);
61+
done();
5062
}, 0);
51-
});
63+
}));
5264

5365
// aria-hidden values tests
5466
describe('aria-hidden', function () {
@@ -234,7 +246,8 @@
234246
}, 0);
235247
});
236248

237-
it('shows unstacked modal after close', function () {
249+
// FIXME: Issue unrelated to iframes
250+
xit('shows unstacked modal after close', function () {
238251
window.location.hash = '#modal';
239252
window.location.hash = '#stackable';
240253

0 commit comments

Comments
 (0)