Is Bubbling and Capturing events order during Target phase defined anywhere?
Pretty much anywhere you look, you will find that first there is Capturing phase, then Bubbling phase. Some sources additionally mention Target phase as a separate stage.
However, sources mentioning Target phase do not specify the order of events during this phase. I assumed that events registered as Capturing will precede these registered as Bubbling. It turns out it is not always the case!
In fact - according to my small experiment results - event handler execution during Target phase depends solely on their registration order.
const extBox = document.querySelector('.external-box');
const intBox = document.querySelector('.internal-box');
const par = document.querySelector('p');
extBox.addEventListener('click', bubblingEvent, false);
intBox.addEventListener('click', bubblingEvent, false);
par.addEventListener('click', bubblingEvent, false);
par.addEventListener('click', capturingEvent, true);
extBox.addEventListener('click', capturingEvent, true);
intBox.addEventListener('click', capturingEvent, true);
function bubblingEvent(event) {
console.log(event.currentTarget.className + ': bubbling event, ' + getPhaseStr(event.eventPhase));
}
function capturingEvent(event) {
console.log(event.currentTarget.className + ': capturing event, ' + getPhaseStr(event.eventPhase));
}
function getPhaseStr (eventPhase) {
let eventPhaseStr;
switch (eventPhase) {
case 0:
eventPhaseStr = 'NONE';
break;
case 1:
eventPhaseStr = 'CAPTURING_PHASE';
break;
case 2:
eventPhaseStr = 'AT_TARGET';
break;
case 3:
eventPhaseStr = 'BUBBLING_PHASE';
break;
default:
eventPhaseStr = 'ERROR';
}
return eventPhaseStr;
}
* {
box-sizing: border-box;
}
.external-box, .internal-box {
width: 50%;
margin: 1rem;
padding: 1rem;
}
.external-box {
background-color: aquamarine;
}
.internal-box {
background-color: blueviolet;
}
p {
background-color: cornsilk;
padding: 1rem;
}
<div class='external-box'>external-box
<div class='internal-box'>internal-box
<p class='par'>paragraph</p>
</div>
</div>
In the above example we can see, that - during Target phase - event added for Bubbling phase is executed before the Capturing Phase. This is because I have registered events for the Bubbling phase first. If I register events for the Capturing phase first, and then Bubbling ones, the order will be "correct".
My question once again: is events order during Target phase defined anywhere?