Skip to content

Truly independent web browser

LibWeb: Set the initiator type of img requests to "img"

Shannon Booth16b306a85e2d
diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.cpp b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
index 53519d80575..4f433bdefbd 100644
--- a/Libraries/LibWeb/HTML/HTMLImageElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLImageElement.cpp
@@ -1091,6 +1091,10 @@ after_step_7:
// 20. Set request's client to the element's node document's relevant settings object.
request->set_client(&document().relevant_settings_object());
+ // AD-HOC: The spec doesn't set an initiator type for img requests, so they would never report resource timing.
+ // Spec PR: https://github.com/whatwg/html/pull/13101
+ request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::IMG);
+
// 21. If the element uses srcset or picture, set request's initiator to "imageset".
if (uses_srcset_or_picture())
request->set_initiator(Fetch::Infrastructure::Request::Initiator::ImageSet);
@@ -1363,6 +1367,10 @@ void HTMLImageElement::react_to_changes_in_the_environment()
request->set_client(&client);
request->set_initiator(Fetch::Infrastructure::Request::Initiator::ImageSet);
+ // AD-HOC: The spec doesn't set an initiator type for img requests, so they would never report resource timing.
+ // Spec PR: https://github.com/whatwg/html/pull/13101
+ request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::IMG);
+
// 3. Set request's referrer policy to the current state of the element's referrerpolicy attribute.
request->set_referrer_policy(ReferrerPolicy::from_string(attribute(HTML::AttributeNames::referrerpolicy).value_or({})).value_or(ReferrerPolicy::ReferrerPolicy::EmptyString));
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 6e44477cda2..883d2e810c4 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -570,6 +570,9 @@ Text/input/wpt-import/worklets/audio-worklet-import.https.html
Text/input/wpt-import/streams/transferable/audioworklet.https.html
Text/input/wpt-import/preload/prefetch-load-event.html
Text/input/wpt-import/preload/prefetch-events.html
+Text/input/wpt-import/resource-timing/initiator-type/img.html
+Text/input/wpt-import/resource-timing/initiator-type/img-srcset.html
+Text/input/wpt-import/resource-timing/initiator-type/picture.html
[Skipped]
; Needs vertical-rl inline geometry fidelity beyond the binary transpose
diff --git a/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/img-srcset.txt b/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/img-srcset.txt
new file mode 100644
index 00000000000..14ad879d83d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/img-srcset.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass The initiator type for <img srcset> must be 'img'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/img.txt b/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/img.txt
new file mode 100644
index 00000000000..b9f8e958ed7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/img.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass The initiator type for <img> must be 'img'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/picture.txt b/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/picture.txt
new file mode 100644
index 00000000000..c8008487053
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/resource-timing/initiator-type/picture.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass The initiator type for <source> in a <picture> must be 'img'
+Pass The initiator type for <img> in a <picture> must be 'img'
+Pass The initiator type for <img srcset> in a <picture> must be 'img'
+Pass The initiator type for <img src> in a <picture> must be 'img'
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/img-srcset.html b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/img-srcset.html
new file mode 100644
index 00000000000..a6d94fcf84f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/img-srcset.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8" />
+<title>Resource Timing initiator type: img with srcset attribute</title>
+<link rel="author" title="Google" href="http://www.google.com/" />
+<link rel="help" href="https://www.w3.org/TR/resource-timing-2/#dom-performanceresourcetiming-initiatortype"/>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resource-timing/resources/observe-entry.js"></script>
+<script src="resources/initiator-type-test.js"></script>
+</head>
+<body>
+<img src="../../resource-timing/resources/resource_timing_test0.png"
+ srcset="../../resource-timing/resources/resource_timing_test0.png?id=srcset 67w"
+ sizes="67px">
+<script>
+ initiator_type_test("resource_timing_test0.png?id=srcset", "img", "<img srcset>");
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/img.html b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/img.html
new file mode 100644
index 00000000000..3d6f873a381
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/img.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8" />
+<title>Resource Timing initiator type: img</title>
+<link rel="author" title="Google" href="http://www.google.com/" />
+<link rel="help" href="https://www.w3.org/TR/resource-timing-2/#dom-performanceresourcetiming-initiatortype"/>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resource-timing/resources/observe-entry.js"></script>
+<script src="resources/initiator-type-test.js"></script>
+</head>
+<body>
+<img src="../../resource-timing/resources/resource_timing_test0.png">
+<script>
+ initiator_type_test("resource_timing_test0.png", "img", "<img>");
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/picture.html b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/picture.html
new file mode 100644
index 00000000000..049d33784c1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/picture.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8" />
+<title>Resource Timing initiator type: picture</title>
+<link rel="author" title="Google" href="http://www.google.com/" />
+<link rel="help" href="https://www.w3.org/TR/resource-timing-2/#dom-performanceresourcetiming-initiatortype"/>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resource-timing/resources/observe-entry.js"></script>
+<script src="resources/initiator-type-test.js"></script>
+</head>
+<body>
+<picture>
+ <source srcset="blue.png?id=picture-source" type="image/png" />
+ <img src="blue.png?id=picture-img" />
+</picture>
+<picture>
+ <source srcset="blue.png?id=picture-notsupported-source" type="image/notsupported" />
+ <img src="blue.png?id=picture-notsupported-img" />
+</picture>
+<picture>
+ <img src="blue.png?id=picture-img-src"
+ srcset="blue.png?id=picture-img-srcset"
+ sizes="67px">
+</picture>
+<picture>
+ <img src="blue.png?id=picture-99x-img-src"
+ srcset="blue.png?id=picture-99x-img-srcset 99x"
+ sizes="67px">
+</picture>
+<script>
+ initiator_type_test("blue.png?id=picture-source", "img", "<source> in a <picture>");
+ initiator_type_test("blue.png?id=picture-notsupported-img", "img", "<img> in a <picture>");
+ initiator_type_test("blue.png?id=picture-img-srcset", "img", "<img srcset> in a <picture>");
+ initiator_type_test("blue.png?id=picture-99x-img-src", "img", "<img src> in a <picture>");
+</script>
+</body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/resources/initiator-type-test.js b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/resources/initiator-type-test.js
new file mode 100644
index 00000000000..2b1f844376c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/resource-timing/initiator-type/resources/initiator-type-test.js
@@ -0,0 +1,15 @@
+
+if (observe_entry === undefined) {
+ throw new Error("You must include resource-timing/resources/observe-entry.js "
+ + "before including this script.");
+}
+
+// Asserts that, for the given name, there is/will-be a
+// PerformanceResourceTiming entry that has the given 'initiatorType'. The test
+// is labeled according to the given descriptor.
+const initiator_type_test = (entry_name, expected_initiator, descriptor) => {
+ promise_test(async () => {
+ const entry = await observe_entry(entry_name);
+ assert_equals(entry.initiatorType, expected_initiator);
+ }, `The initiator type for ${descriptor} must be '${expected_initiator}'`);
+};
diff --git a/Tests/LibWeb/Text/input/wpt-import/resource-timing/resources/observe-entry.js b/Tests/LibWeb/Text/input/wpt-import/resource-timing/resources/observe-entry.js
new file mode 100644
index 00000000000..260b5929a5b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/resource-timing/resources/observe-entry.js
@@ -0,0 +1,25 @@
+// Given a resource name, returns a promise that will resolve to the
+// corresponding PerformanceResourceTiming entry. The promise will reject,
+// however, if the PerformanceResourceTiming entry isn't observed within ~2
+// seconds (scaled according to WPT timeout scaling).
+const observe_entry = entry_name => {
+ const entry = new Promise(resolve => {
+ new PerformanceObserver((entry_list, observer) => {
+ for (const entry of entry_list.getEntries()) {
+ if (entry.name.endsWith(entry_name)) {
+ resolve(entry);
+ observer.disconnect();
+ return;
+ }
+ }
+ }).observe({"type": "resource", "buffered": true});
+ });
+ const timeout = new Promise((resolve, reject) => {
+ step_timeout(() => {
+ reject(new Error("observe_entry: timeout"));
+ }, 2000);
+ });
+ // If the entry isn't observed within 2 seconds, assume it will never show
+ // up.
+ return Promise.race([entry, timeout]);
+};
diff --git a/Tests/LibWeb/Text/input/wpt-import/resource-timing/resources/resource_timing_test0.png b/Tests/LibWeb/Text/input/wpt-import/resource-timing/resources/resource_timing_test0.png
new file mode 100644
index 00000000000..be211bc3771
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/resource-timing/resources/resource_timing_test0.png differ