eventDispatch_worker.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. const fakeEventType = "foo";
  6. function testEventTarget(event) {
  7. if (event.target !== self) {
  8. throw new Error("Event has a bad target!");
  9. }
  10. if (event.currentTarget) {
  11. throw new Error("Event has a bad currentTarget!");
  12. }
  13. postMessage(event.data);
  14. }
  15. addEventListener(fakeEventType, function(event) {
  16. throw new Error("Trusted event listener received untrusted event!");
  17. }, false, false);
  18. addEventListener(fakeEventType, function(event) {
  19. if (event.target !== self || event.currentTarget !== self) {
  20. throw new Error("Fake event has bad target!");
  21. }
  22. if (event.isTrusted) {
  23. throw new Error("Event should be untrusted!");
  24. }
  25. event.stopImmediatePropagation();
  26. postMessage(event.data);
  27. }, false, true);
  28. addEventListener(fakeEventType, function(event) {
  29. throw new Error("This shouldn't get called because of stopImmediatePropagation.");
  30. }, false, true);
  31. var count = 0;
  32. onmessage = function(event) {
  33. if (event.target !== self || event.currentTarget !== self) {
  34. throw new Error("Event has bad target!");
  35. }
  36. if (!count++) {
  37. var exception;
  38. try {
  39. self.dispatchEvent(event);
  40. }
  41. catch(e) {
  42. exception = e;
  43. }
  44. if (!exception) {
  45. throw new Error("Recursive dispatch didn't fail!");
  46. }
  47. event = new MessageEvent(fakeEventType, { bubbles: event.bubbles,
  48. cancelable: event.cancelable,
  49. data: event.data,
  50. origin: "*",
  51. source: null
  52. });
  53. self.dispatchEvent(event);
  54. return;
  55. }
  56. setTimeout(testEventTarget, 0, event);
  57. };