11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<meta charset=utf-8> 31cb0ef41Sopenharmony_ci<title>EventTarget.dispatchEvent</title> 41cb0ef41Sopenharmony_ci<link rel="author" title="Olli Pettay" href="mailto:Olli.Pettay@gmail.com"> 51cb0ef41Sopenharmony_ci<link rel="author" title="Ms2ger" href="mailto:Ms2ger@gmail.com"> 61cb0ef41Sopenharmony_ci<link rel="help" href="https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent"> 71cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script> 81cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script> 91cb0ef41Sopenharmony_ci<script src="/dom/nodes/Document-createEvent.js"></script> 101cb0ef41Sopenharmony_ci<div id="log"></div> 111cb0ef41Sopenharmony_ci<script> 121cb0ef41Sopenharmony_cisetup({ 131cb0ef41Sopenharmony_ci "allow_uncaught_exception": true, 141cb0ef41Sopenharmony_ci}) 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_citest(function() { 171cb0ef41Sopenharmony_ci assert_throws_js(TypeError, function() { document.dispatchEvent(null) }) 181cb0ef41Sopenharmony_ci}, "Calling dispatchEvent(null).") 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_cifor (var alias in aliases) { 211cb0ef41Sopenharmony_ci test(function() { 221cb0ef41Sopenharmony_ci var e = document.createEvent(alias) 231cb0ef41Sopenharmony_ci assert_equals(e.type, "", "Event type should be empty string before initialization") 241cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", function() { document.dispatchEvent(e) }) 251cb0ef41Sopenharmony_ci }, "If the event's initialized flag is not set, an InvalidStateError must be thrown (" + alias + ").") 261cb0ef41Sopenharmony_ci} 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_civar dispatch_dispatch = async_test("If the event's dispatch flag is set, an InvalidStateError must be thrown.") 291cb0ef41Sopenharmony_cidispatch_dispatch.step(function() { 301cb0ef41Sopenharmony_ci var e = document.createEvent("Event") 311cb0ef41Sopenharmony_ci e.initEvent("type", false, false) 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci var target = document.createElement("div") 341cb0ef41Sopenharmony_ci target.addEventListener("type", dispatch_dispatch.step_func(function() { 351cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", function() { 361cb0ef41Sopenharmony_ci target.dispatchEvent(e) 371cb0ef41Sopenharmony_ci }) 381cb0ef41Sopenharmony_ci assert_throws_dom("InvalidStateError", function() { 391cb0ef41Sopenharmony_ci document.dispatchEvent(e) 401cb0ef41Sopenharmony_ci }) 411cb0ef41Sopenharmony_ci }), false) 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true") 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci dispatch_dispatch.done() 461cb0ef41Sopenharmony_ci}) 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_citest(function() { 491cb0ef41Sopenharmony_ci // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17713 501cb0ef41Sopenharmony_ci // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17714 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci var e = document.createEvent("Event") 531cb0ef41Sopenharmony_ci e.initEvent("type", false, false) 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci var called = [] 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci var target = document.createElement("div") 581cb0ef41Sopenharmony_ci target.addEventListener("type", function() { 591cb0ef41Sopenharmony_ci called.push("First") 601cb0ef41Sopenharmony_ci throw new Error() 611cb0ef41Sopenharmony_ci }, false) 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci target.addEventListener("type", function() { 641cb0ef41Sopenharmony_ci called.push("Second") 651cb0ef41Sopenharmony_ci }, false) 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true") 681cb0ef41Sopenharmony_ci assert_array_equals(called, ["First", "Second"], 691cb0ef41Sopenharmony_ci "Should have continued to call other event listeners") 701cb0ef41Sopenharmony_ci}, "Exceptions from event listeners must not be propagated.") 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciasync_test(function() { 731cb0ef41Sopenharmony_ci var results = [] 741cb0ef41Sopenharmony_ci var outerb = document.createElement("b") 751cb0ef41Sopenharmony_ci var middleb = outerb.appendChild(document.createElement("b")) 761cb0ef41Sopenharmony_ci var innerb = middleb.appendChild(document.createElement("b")) 771cb0ef41Sopenharmony_ci outerb.addEventListener("x", this.step_func(function() { 781cb0ef41Sopenharmony_ci middleb.addEventListener("x", this.step_func(function() { 791cb0ef41Sopenharmony_ci results.push("middle") 801cb0ef41Sopenharmony_ci }), true) 811cb0ef41Sopenharmony_ci results.push("outer") 821cb0ef41Sopenharmony_ci }), true) 831cb0ef41Sopenharmony_ci innerb.dispatchEvent(new Event("x")) 841cb0ef41Sopenharmony_ci assert_array_equals(results, ["outer", "middle"]) 851cb0ef41Sopenharmony_ci this.done() 861cb0ef41Sopenharmony_ci}, "Event listeners added during dispatch should be called"); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ciasync_test(function() { 891cb0ef41Sopenharmony_ci var results = [] 901cb0ef41Sopenharmony_ci var b = document.createElement("b") 911cb0ef41Sopenharmony_ci b.addEventListener("x", this.step_func(function() { 921cb0ef41Sopenharmony_ci results.push(1) 931cb0ef41Sopenharmony_ci }), true) 941cb0ef41Sopenharmony_ci b.addEventListener("x", this.step_func(function() { 951cb0ef41Sopenharmony_ci results.push(2) 961cb0ef41Sopenharmony_ci }), false) 971cb0ef41Sopenharmony_ci b.addEventListener("x", this.step_func(function() { 981cb0ef41Sopenharmony_ci results.push(3) 991cb0ef41Sopenharmony_ci }), true) 1001cb0ef41Sopenharmony_ci b.dispatchEvent(new Event("x")) 1011cb0ef41Sopenharmony_ci assert_array_equals(results, [1, 3, 2]) 1021cb0ef41Sopenharmony_ci this.done() 1031cb0ef41Sopenharmony_ci}, "Capturing event listeners should be called before non-capturing ones") 1041cb0ef41Sopenharmony_ci</script> 105