tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

EventTarget-dispatchEvent.html (3547B)


      1 <!DOCTYPE html>
      2 <meta charset=utf-8>
      3 <title>EventTarget.dispatchEvent</title>
      4 <link rel="author" title="Olli Pettay" href="mailto:Olli.Pettay@gmail.com">
      5 <link rel="author" title="Ms2ger" href="mailto:Ms2ger@gmail.com">
      6 <link rel="help" href="https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent">
      7 <script src="/resources/testharness.js"></script>
      8 <script src="/resources/testharnessreport.js"></script>
      9 <script src="/dom/nodes/Document-createEvent.js"></script>
     10 <div id="log"></div>
     11 <script>
     12 setup({
     13  "allow_uncaught_exception": true,
     14 })
     15 
     16 test(function() {
     17  assert_throws_js(TypeError, function() { document.dispatchEvent(null) })
     18 }, "Calling dispatchEvent(null).")
     19 
     20 for (var alias in aliases) {
     21  test(function() {
     22    var e = document.createEvent(alias)
     23    assert_equals(e.type, "", "Event type should be empty string before initialization")
     24    assert_throws_dom("InvalidStateError", function() { document.dispatchEvent(e) })
     25  }, "If the event's initialized flag is not set, an InvalidStateError must be thrown (" + alias + ").")
     26 }
     27 
     28 var dispatch_dispatch = async_test("If the event's dispatch flag is set, an InvalidStateError must be thrown.")
     29 dispatch_dispatch.step(function() {
     30  var e = document.createEvent("Event")
     31  e.initEvent("type", false, false)
     32 
     33  var target = document.createElement("div")
     34  target.addEventListener("type", dispatch_dispatch.step_func(function() {
     35    assert_throws_dom("InvalidStateError", function() {
     36      target.dispatchEvent(e)
     37    })
     38    assert_throws_dom("InvalidStateError", function() {
     39      document.dispatchEvent(e)
     40    })
     41  }), false)
     42 
     43  assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")
     44 
     45  dispatch_dispatch.done()
     46 })
     47 
     48 test(function() {
     49  // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17713
     50  // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17714
     51 
     52  var e = document.createEvent("Event")
     53  e.initEvent("type", false, false)
     54 
     55  var called = []
     56 
     57  var target = document.createElement("div")
     58  target.addEventListener("type", function() {
     59    called.push("First")
     60    throw new Error()
     61  }, false)
     62 
     63  target.addEventListener("type", function() {
     64    called.push("Second")
     65  }, false)
     66 
     67  assert_equals(target.dispatchEvent(e), true, "dispatchEvent must return true")
     68  assert_array_equals(called, ["First", "Second"],
     69                      "Should have continued to call other event listeners")
     70 }, "Exceptions from event listeners must not be propagated.")
     71 
     72 async_test(function() {
     73  var results = []
     74  var outerb = document.createElement("b")
     75  var middleb = outerb.appendChild(document.createElement("b"))
     76  var innerb = middleb.appendChild(document.createElement("b"))
     77  outerb.addEventListener("x", this.step_func(function() {
     78    middleb.addEventListener("x", this.step_func(function() {
     79      results.push("middle")
     80    }), true)
     81    results.push("outer")
     82  }), true)
     83  innerb.dispatchEvent(new Event("x"))
     84  assert_array_equals(results, ["outer", "middle"])
     85  this.done()
     86 }, "Event listeners added during dispatch should be called");
     87 
     88 async_test(function() {
     89  var results = []
     90  var b = document.createElement("b")
     91  b.addEventListener("x", this.step_func(function() {
     92    results.push(1)
     93  }), true)
     94  b.addEventListener("x", this.step_func(function() {
     95    results.push(2)
     96  }), false)
     97  b.addEventListener("x", this.step_func(function() {
     98    results.push(3)
     99  }), true)
    100  b.dispatchEvent(new Event("x"))
    101  assert_array_equals(results, [1, 3, 2])
    102  this.done()
    103 }, "Capturing event listeners should be called before non-capturing ones")
    104 </script>