Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit abfa4bf

Browse files
committed
Allow dispatching the same event multiple times
1 parent 5bb221b commit abfa4bf

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/wrappers/events.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,12 @@
643643
},
644644
dispatchEvent: function(event) {
645645
var target = getTargetToListenAt(this);
646-
return target.dispatchEvent_(unwrap(event));
646+
var nativeEvent = unwrap(event);
647+
// Allow dispatching the same event again. This is safe because if user
648+
// code calls this during an existing dispatch of the same event the
649+
// native dispatchEvent throws (that is required by the spec).
650+
handledEventsTable.set(nativeEvent, false);
651+
return target.dispatchEvent_(nativeEvent);
647652
}
648653
};
649654

@@ -654,7 +659,6 @@
654659
forwardMethodsToWrapper(constructors, methodNames);
655660
}
656661

657-
658662
var originalElementFromPoint = document.elementFromPoint;
659663

660664
function elementFromPoint(self, document, x, y) {

test/js/events.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,43 @@ test('retarget order (multiple shadow roots)', function() {
12771277
text.dispatchEvent(new Event('x'));
12781278
});
12791279

1280+
test('dispatch same event object twice', function() {
1281+
var e = new Event('x', {bubbles: true});
1282+
var doc = wrap(document);
1283+
1284+
var count = 0;
1285+
function handler(e) {
1286+
count++;
1287+
};
1288+
1289+
doc.addEventListener('x', handler);
1290+
1291+
document.dispatchEvent(e);
1292+
assert.equal(count, 1);
1293+
document.dispatchEvent(e);
1294+
assert.equal(count, 2);
1295+
document.dispatchEvent(e);
1296+
assert.equal(count, 3);
1297+
1298+
doc.removeEventListener('x', handler);
1299+
});
1300+
1301+
test('Ensure nested dispatch is not allowed', function() {
1302+
var e = new Event('x', {bubbles: true});
1303+
var doc = wrap(document);
1304+
1305+
var count = 0;
1306+
1307+
doc.addEventListener('x', function f(e) {
1308+
count++;
1309+
assert.throws(function() {
1310+
doc.dispatchEvent(e);
1311+
});
1312+
});
1313+
1314+
doc.dispatchEvent(e);
1315+
1316+
assert.equal(count, 1);
1317+
});
1318+
12801319
});

0 commit comments

Comments
 (0)