Skip to content

[nice to have] Synthetic event handlers should not be allowed on events that cannot bubble #1284

@Arcasias

Description

@Arcasias

Situation

const { Component, mount, useState, xml } = owl;

const dispatchMouseEnter = (el) => {
    const mouseEnterEvent = new MouseEvent("mouseenter", { bubbles: false });
    el.dispatchEvent(mouseEnterEvent);
};

let handled = 0;

class App extends Component {
    static template = xml`
        <main class="App">
            <button t-on-click="() => state.synthetic = !state.synthetic">
                Toggle synthetic
            </button>
            <ul>
                <t t-foreach="[1, 2, 3]" t-as="i" t-key="i">
                    <t t-if="state.synthetic">
                        <li t-on-mouseenter.synthetic="inc" t-esc="i" />
                    </t>
                    <t t-else="">
                        <li t-on-mouseenter="inc" t-esc="i" />
                    </t>
                </t>
            </ul>
        </main>
    `;
    
    state = useState({ synthetic: false });
    
    inc() {
        handled++;
    }
}

await mount(App, document.body, { dev: true });

dispatchMouseEnter(document.querySelector("li"));

if (handled === 1) {
    console.log("First event handled");
} else {
    console.error("First event not handled");
}

document.querySelector("button").click();
await new Promise((r) => setTimeout(r, 100));

dispatchMouseEnter(document.querySelector("li"));


if (handled === 2) {
    console.log("Second event handled");
} else {
    console.error("Second event not handled");
}

Issue

As is shown in the console, the first dispatch (with synthetic = false) is handled and the second (with synthetic = true) is not. This is because mouseenter events (along with some others, like focus, blur, load, etc.) do not bubble up to the synthetic listener.

Proposition

I think it could be relevant to prevent or discourage the use of .synthetic on a defined list of events that do not bubble, by logging an error or a warning for instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions