Source code
Revision control
Copy as Markdown
Other Tools
Test Info:
- This WPT test may be referenced by the following Test IDs:
- /speculation-rules/inline-speculation-rules-errors.html - WPT Dashboard Interop Dashboard
<!DOCTYPE html>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-element">
<link rel="help" href="https://html.spec.whatwg.org/multipage/webappapis.html#register-speculation-rules">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
setup({ allow_uncaught_exception: true });
promise_test(async t => {
const script = document.createElement('script');
script.type = 'speculationrules';
script.textContent = 'invalid json';
t.add_cleanup(() => script.remove());
const log = [];
script.addEventListener('error', () => log.push('element error'));
const globalError = new Promise(resolve => {
window.addEventListener('error', t.step_func(e => {
e.preventDefault();
assert_equals(e.constructor, ErrorEvent, 'global event should be an ErrorEvent');
assert_equals(e.error.constructor, TypeError, 'e.error should be a TypeError');
log.push('global error');
resolve();
}), { once: true });
});
document.head.appendChild(script);
await globalError;
await new Promise(resolve => t.step_timeout(resolve, 0));
assert_array_equals(log, ['global error']);
}, 'A script with invalid JSON should report an error on the window but not fire an error event on the element');
</script>