Skip to content

MessageEvent cannot be prevented even if cancelable is true #51767

Open
@kettanaito

Description

@kettanaito

Version

v18.14.2 (the same on LTS v20.11.0)

Platform

Darwin imac.home 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

// example.js
let t = new EventTarget()
t.addEventListener('message', (e) => console.log('cancelable?', e.cancelable))
t.dispatchEvent(new MessageEvent('message', { cancelable: true }))
node example.js

How often does it reproduce? Is there a required condition?

The issue can be reproduced every time.

What is the expected behavior? Why is that the expected behavior?

I expect the following:

  1. e.cancelable is true in the event listener for "message'.
  2. Calling e.preventDefault() will set e.defaultPrevented to true.

In other words, I expect the same behavior as with the regular Event instance:

let t = new EventTarget()
t.addEventListener('message', (e) => console.log('cancelable?', e.cancelable))

// Regular Event behaves correctly and can be canceled. 
t.dispatchEvent(new Event('message', { cancelable: true }))

What do you see instead?

The cancelable attribute on the MessageEvent instance is always false, no matter the cancelable value in the event init dictionary.

Calling e.preventDefault() has no effect on e.defaultPrevented (much likely because the cancelable attribute is false.

Additional information

I'm constructing a MessageEvent that I then dispatch on EventTarget and want to have the listeners being able to cancel that event (prevent its default).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions