fix(js): match event constructor semantics
This commit is contained in:
1 parent
776c915c8c
commit
0ff1ba0ea9
2 files changed
+109
-3
No files matched your search
Vendored
+14
-3
@@ -2613,6 +2613,13 @@ class Document extends Node {
|
|||||||
// returned a generic Event for every type, which broke libraries that call
|
// returned a generic Event for every type, which broke libraries that call
|
||||||
// createEvent('CustomEvent').initCustomEvent(...) — see issue #41.
|
// createEvent('CustomEvent').initCustomEvent(...) — see issue #41.
|
||||||
createEvent(type) {
|
createEvent(type) {
|
||||||
|
const normalized = String(type || '').toLowerCase();
|
||||||
|
if (normalized === 'promiserejectionevent') {
|
||||||
|
throw new DOMException(
|
||||||
|
"The provided event type ('PromiseRejectionEvent') is invalid",
|
||||||
|
'NotSupportedError'
|
||||||
|
);
|
||||||
|
}
|
||||||
const map = {
|
const map = {
|
||||||
'customevent': CustomEvent, 'customevents': CustomEvent,
|
'customevent': CustomEvent, 'customevents': CustomEvent,
|
||||||
'mouseevent': MouseEvent, 'mouseevents': MouseEvent,
|
'mouseevent': MouseEvent, 'mouseevents': MouseEvent,
|
||||||
@@ -2627,10 +2634,9 @@ class Document extends Node {
|
|||||||
'popstateevent': PopStateEvent,
|
'popstateevent': PopStateEvent,
|
||||||
'animationevent': AnimationEvent,
|
'animationevent': AnimationEvent,
|
||||||
'transitionevent': TransitionEvent,
|
'transitionevent': TransitionEvent,
|
||||||
'promiserejectionevent': PromiseRejectionEvent,
|
|
||||||
'storageevent': StorageEvent,
|
'storageevent': StorageEvent,
|
||||||
};
|
};
|
||||||
const Cls = map[String(type || '').toLowerCase()] || Event;
|
const Cls = map[normalized] || Event;
|
||||||
return new Cls('');
|
return new Cls('');
|
||||||
}
|
}
|
||||||
createRange() { return new Range(); }
|
createRange() { return new Range(); }
|
||||||
@@ -5010,7 +5016,12 @@ globalThis.ToggleEvent = class ToggleEvent extends Event {
|
|||||||
_markNative(globalThis.ToggleEvent);
|
_markNative(globalThis.ToggleEvent);
|
||||||
|
|
||||||
globalThis.PromiseRejectionEvent = class PromiseRejectionEvent extends Event {
|
globalThis.PromiseRejectionEvent = class PromiseRejectionEvent extends Event {
|
||||||
constructor(type, init = {}) {
|
constructor(type, init) {
|
||||||
|
if (arguments.length < 2 || init == null || !('promise' in Object(init))) {
|
||||||
|
throw new TypeError(
|
||||||
|
"Failed to construct 'PromiseRejectionEvent': required member promise is undefined."
|
||||||
|
);
|
||||||
|
}
|
||||||
super(type, init);
|
super(type, init);
|
||||||
this.promise = init.promise;
|
this.promise = init.promise;
|
||||||
this.reason = init.reason;
|
this.reason = init.reason;
|
||||||
|
|||||||
@@ -3085,6 +3085,101 @@ mod tests {
|
|||||||
assert_eq!(kind, serde_json::json!(true));
|
assert_eq!(kind, serde_json::json!(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_promise_rejection_event_requires_promise() {
|
||||||
|
let mut rt = setup_runtime("<html><body></body></html>");
|
||||||
|
let result = rt
|
||||||
|
.evaluate(
|
||||||
|
r#"(() => {
|
||||||
|
const promise = Promise.resolve(1);
|
||||||
|
const event = new PromiseRejectionEvent('unhandledrejection', {
|
||||||
|
promise,
|
||||||
|
reason: 'failed'
|
||||||
|
});
|
||||||
|
let missingPromiseThrows = false;
|
||||||
|
try {
|
||||||
|
new PromiseRejectionEvent('unhandledrejection');
|
||||||
|
} catch (error) {
|
||||||
|
missingPromiseThrows = error instanceof TypeError;
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
event instanceof Event,
|
||||||
|
event.promise === promise,
|
||||||
|
event.reason === 'failed',
|
||||||
|
missingPromiseThrows
|
||||||
|
];
|
||||||
|
})()"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(result, serde_json::json!([true, true, true, true]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_create_event_rejects_promise_rejection_event() {
|
||||||
|
let mut rt = setup_runtime("<html><body></body></html>");
|
||||||
|
let result = rt
|
||||||
|
.evaluate(
|
||||||
|
r#"(() => {
|
||||||
|
try {
|
||||||
|
document.createEvent('PromiseRejectionEvent');
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
return [error.name, error instanceof DOMException];
|
||||||
|
}
|
||||||
|
})()"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
serde_json::json!(["NotSupportedError", true])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_storage_event_constructor_and_legacy_factory() {
|
||||||
|
let mut rt = setup_runtime("<html><body></body></html>");
|
||||||
|
let result = rt
|
||||||
|
.evaluate(
|
||||||
|
r#"(() => {
|
||||||
|
const event = new StorageEvent('storage', {
|
||||||
|
key: 'theme',
|
||||||
|
oldValue: 'light',
|
||||||
|
newValue: 'dark',
|
||||||
|
url: 'https://example.test/'
|
||||||
|
});
|
||||||
|
const legacy = document.createEvent('StorageEvent');
|
||||||
|
legacy.initStorageEvent(
|
||||||
|
'storage', false, false, 'count', '1', '2',
|
||||||
|
'https://example.test/', null
|
||||||
|
);
|
||||||
|
return [
|
||||||
|
event instanceof Event,
|
||||||
|
event.key,
|
||||||
|
event.oldValue,
|
||||||
|
event.newValue,
|
||||||
|
event.url,
|
||||||
|
legacy instanceof StorageEvent,
|
||||||
|
legacy.key,
|
||||||
|
legacy.newValue
|
||||||
|
];
|
||||||
|
})()"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
result,
|
||||||
|
serde_json::json!([
|
||||||
|
true,
|
||||||
|
"theme",
|
||||||
|
"light",
|
||||||
|
"dark",
|
||||||
|
"https://example.test/",
|
||||||
|
true,
|
||||||
|
"count",
|
||||||
|
"2"
|
||||||
|
])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_html_to_markdown_headings() {
|
fn test_html_to_markdown_headings() {
|
||||||
let mut rt = setup_runtime("<html><body><h1>Title</h1><h2>Sub</h2><p>Body</p></body></html>");
|
let mut rt = setup_runtime("<html><body><h1>Title</h1><h2>Sub</h2><p>Body</p></body></html>");
|
||||||
|
|||||||
Reference in new issue
Block a user