Skip to content

Commit b7abe3d

Browse files
domenicjcscottiii
authored andcommitted
Fire error events for invalid speculation rules
This CL introduces error handling for <script type=speculationrules>, mirroring the behavior of <script type=importmap>. Specifically, it adds error events for two cases: - Inline speculation rules with unparsable JSON. - External speculation rules, which are not yet supported. This is verified by new web platform tests. This follows the spec change at whatwg/html#11426. Change-Id: I9b0776b86059f6c8734d57c17f50ed26e89215da Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6758353 Reviewed-by: Hiroki Nakagawa <[email protected]> Commit-Queue: Domenic Denicola <[email protected]> Cr-Commit-Position: refs/heads/main@{#1488632}
1 parent f92ebdd commit b7abe3d

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script>
5+
window.markupErrored = false;
6+
window.markupLoaded = false;
7+
</script>
8+
<script src="resources/empty.json" type="speculationrules" onload="window.markupLoaded = true;" onerror="window.markupErrored = true;"></script>
9+
<script>
10+
promise_test(async () => {
11+
await new Promise((resolve, reject) => {
12+
const element = document.createElement("script");
13+
element.onload = () => { reject("Got an unexpected load event"); };
14+
element.onerror = () => { resolve("Got an error event"); };
15+
element.src = "resources/empty.json";
16+
element.type = "speculationrules";
17+
document.head.appendChild(element);
18+
})
19+
}, "Test that an external speculation rules script fires an error event");
20+
21+
promise_test(async () => {
22+
await new Promise((resolve, reject) => {
23+
const element = document.createElement("script");
24+
element.type = "speculationrules";
25+
element.onload = () => { reject("Got an unexpected load event"); };
26+
element.onerror = () => { resolve("Got an error event"); };
27+
element.src = "resources/empty.json";
28+
document.head.appendChild(element);
29+
})
30+
}, "Test that an external speculation rules script fires an error event, regardless of attribute order");
31+
32+
promise_test(async () => {
33+
assert_true(window.markupErrored, "error");
34+
assert_false(window.markupLoaded, "load");
35+
}, "Test that an external speculation rules script in markup fires an error event");
36+
</script>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<script>
5+
setup({ allow_uncaught_exception: true });
6+
7+
promise_test(async t => {
8+
const script = document.createElement('script');
9+
script.type = 'speculationrules';
10+
script.textContent = 'invalid json';
11+
12+
const log = [];
13+
const elementError = new Promise(resolve => {
14+
script.addEventListener('error', e => {
15+
assert_equals(e.constructor, Event, 'event should be a simple Event');
16+
log.push('element error');
17+
resolve();
18+
}, { once: true });
19+
});
20+
21+
const globalError = new Promise(resolve => {
22+
window.addEventListener('error', e => {
23+
e.preventDefault();
24+
assert_equals(e.constructor, ErrorEvent, 'global event should be an ErrorEvent');
25+
assert_equals(e.error.constructor, TypeError, 'e.error should be a TypeError');
26+
log.push('global error');
27+
resolve();
28+
}, { once: true });
29+
});
30+
31+
document.head.appendChild(script);
32+
33+
await Promise.all([elementError, globalError]);
34+
assert_array_equals(log, ['element error', 'global error']);
35+
}, 'A script with invalid JSON should fire error events on the element and window');
36+
</script>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)