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