11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<meta charset=utf-8>
31cb0ef41Sopenharmony_ci<title> Dispatch additional events inside an event listener </title>
41cb0ef41Sopenharmony_ci<link rel="help" href="https://dom.spec.whatwg.org/#concept-event-dispatch">
51cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script>
61cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script>
71cb0ef41Sopenharmony_ci<div id=log></div>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci<table id="table" border="1" style="display: none">
101cb0ef41Sopenharmony_ci    <tbody id="table-body">
111cb0ef41Sopenharmony_ci    <tr id="table-row">
121cb0ef41Sopenharmony_ci        <td id="table-cell">Shady Grove</td>
131cb0ef41Sopenharmony_ci        <td>Aeolian</td>
141cb0ef41Sopenharmony_ci    </tr>
151cb0ef41Sopenharmony_ci    <tr id="parent">
161cb0ef41Sopenharmony_ci        <td id="target">Over the river, Charlie</td>
171cb0ef41Sopenharmony_ci        <td>Dorian</td>
181cb0ef41Sopenharmony_ci    </tr>
191cb0ef41Sopenharmony_ci    </tbody>
201cb0ef41Sopenharmony_ci</table>
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci<script>
231cb0ef41Sopenharmony_citest(function() {
241cb0ef41Sopenharmony_ci  var event_type = "bar";
251cb0ef41Sopenharmony_ci  var target = document.getElementById("target");
261cb0ef41Sopenharmony_ci  var parent = document.getElementById("parent");
271cb0ef41Sopenharmony_ci  var tbody = document.getElementById("table-body");
281cb0ef41Sopenharmony_ci  var table = document.getElementById("table");
291cb0ef41Sopenharmony_ci  var body = document.body;
301cb0ef41Sopenharmony_ci  var html = document.documentElement;
311cb0ef41Sopenharmony_ci  var targets = [window, document, html, body, table, tbody, parent, target];
321cb0ef41Sopenharmony_ci  var expected_targets = [
331cb0ef41Sopenharmony_ci    window,
341cb0ef41Sopenharmony_ci    document,
351cb0ef41Sopenharmony_ci    html,
361cb0ef41Sopenharmony_ci    body,
371cb0ef41Sopenharmony_ci    table,
381cb0ef41Sopenharmony_ci    tbody,
391cb0ef41Sopenharmony_ci    parent,
401cb0ef41Sopenharmony_ci    target,
411cb0ef41Sopenharmony_ci    target,
421cb0ef41Sopenharmony_ci    target, // The additional listener for target runs as we copy its listeners twice
431cb0ef41Sopenharmony_ci    parent,
441cb0ef41Sopenharmony_ci    tbody,
451cb0ef41Sopenharmony_ci    table,
461cb0ef41Sopenharmony_ci    body,
471cb0ef41Sopenharmony_ci    html,
481cb0ef41Sopenharmony_ci    document,
491cb0ef41Sopenharmony_ci    window
501cb0ef41Sopenharmony_ci  ];
511cb0ef41Sopenharmony_ci  var expected_listeners = [0,0,0,0,0,0,0,0,1,3,1,1,1,1,1,1,1];
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  var actual_targets = [], actual_listeners = [];
541cb0ef41Sopenharmony_ci  var test_event_function = function(i) {
551cb0ef41Sopenharmony_ci    return this.step_func(function(evt) {
561cb0ef41Sopenharmony_ci      actual_targets.push(evt.currentTarget);
571cb0ef41Sopenharmony_ci      actual_listeners.push(i);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci      if (evt.eventPhase != evt.BUBBLING_PHASE && evt.currentTarget.foo != 1) {
601cb0ef41Sopenharmony_ci        evt.currentTarget.removeEventListener(event_type, event_handlers[0], true);
611cb0ef41Sopenharmony_ci        evt.currentTarget.addEventListener(event_type, event_handlers[2], true);
621cb0ef41Sopenharmony_ci        evt.currentTarget.foo = 1;
631cb0ef41Sopenharmony_ci      }
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci      if (evt.eventPhase != evt.CAPTURING_PHASE && evt.currentTarget.foo != 3) {
661cb0ef41Sopenharmony_ci        evt.currentTarget.removeEventListener(event_type, event_handlers[0], false);
671cb0ef41Sopenharmony_ci        evt.currentTarget.addEventListener(event_type, event_handlers[3], false);
681cb0ef41Sopenharmony_ci        evt.currentTarget.foo = 3;
691cb0ef41Sopenharmony_ci      }
701cb0ef41Sopenharmony_ci    });
711cb0ef41Sopenharmony_ci  }.bind(this);
721cb0ef41Sopenharmony_ci  var event_handlers = [
731cb0ef41Sopenharmony_ci    test_event_function(0),
741cb0ef41Sopenharmony_ci    test_event_function(1),
751cb0ef41Sopenharmony_ci    test_event_function(2),
761cb0ef41Sopenharmony_ci    test_event_function(3),
771cb0ef41Sopenharmony_ci  ];
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  for (var i = 0; i < targets.length; ++i) {
801cb0ef41Sopenharmony_ci    targets[i].addEventListener(event_type, event_handlers[0], true);
811cb0ef41Sopenharmony_ci    targets[i].addEventListener(event_type, event_handlers[1], false);
821cb0ef41Sopenharmony_ci  }
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci  var evt = document.createEvent("Event");
851cb0ef41Sopenharmony_ci  evt.initEvent(event_type, true, true);
861cb0ef41Sopenharmony_ci  target.dispatchEvent(evt);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  assert_array_equals(actual_targets, expected_targets, "actual_targets");
891cb0ef41Sopenharmony_ci  assert_array_equals(actual_listeners, expected_listeners, "actual_listeners");
901cb0ef41Sopenharmony_ci});
911cb0ef41Sopenharmony_ci</script>
92