Skip to content

Truly independent web browser

LibWeb: Import WPT coverage for picture dimension hints

Andreas Klingd98dc8f9960b
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index e18c78c6a2b..161af90467d 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -4,6 +4,8 @@ Text/input/wpt-import/html/browsers/origin/origin-keyed-agent-clusters/
; These custom-element fixtures fetch a detached document by XHR.
Text/input/wpt-import/custom-elements/adopted-callback.html
Text/input/wpt-import/custom-elements/connected-callbacks.html
+; Picture aspect ratios depend on images loaded by root-relative srcset URLs.
+Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.html
Text/input/HTML/xml-document-encoding.html
Text/input/HTML/createImageBitmap-video.html
Text/input/HTML/HTMLVideoElement-capture-before-seek-keeps-frame.html
diff --git a/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt b/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt
new file mode 100644
index 00000000000..1432687e542
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/picture-dimension-hints-before-image-selection.txt
@@ -0,0 +1,11 @@
+inserted: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+invalid srcset: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+nonmatching media: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+unsupported type: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+matching source: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+width only: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+no dimensions: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+dimensions restored: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+source follows image: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+source precedes image: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
+source removed: 10px, 30px, auto 10 / 30; currentSrc unchanged: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt
new file mode 100644
index 00000000000..97bffb76320
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.txt
@@ -0,0 +1,21 @@
+Harness status: OK
+
+Found 15 tests
+
+6 Pass
+9 Fail
+Pass source width and height attributes are used to infer aspect-ratio in <picture>
+Fail Computed style for width/height/aspect-ratio
+Fail Source width/height should take precedence over img attributes.
+Pass Make sure style gets invalidated correctly when the source gets removed.
+Fail If the <source> has only one of width/height, we don't get an aspect ratio, even if the <img> has both.
+Pass If we don't have width/height on the source, we fall back to width/height on the <img>.
+Fail If we only have one width attribute, we should get width mapped but no aspect ratio, even if <img> has attributes.
+Fail If we only have height attribute, we should get height mapped but no aspect ratio, even if <img> has attributes.
+Fail Dynamically changing width/height should change computed style
+Fail Changing which <source> matches should change computed style
+Fail Percentages on source should be ignored for aspect-ratio but used for width/height.
+Fail Trailing garbage should be ignored but not make the attribute invalid
+Pass Loaded picture test: Both <source> and <img> are without width and height attributes
+Pass Loaded picture test: <source> with width and height attributes, <img> without width and height attributes
+Pass Loaded picture test: Both <source> and <img> are with width and height attributes
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html b/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html
new file mode 100644
index 00000000000..49328776e50
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/picture-dimension-hints-before-image-selection.html
@@ -0,0 +1,50 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const url = `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="2" height="1"/>')}`;
+ const picture = document.createElement("picture");
+ const source = document.createElement("source");
+ source.srcset = url;
+ source.width = 80;
+ source.height = 40;
+ picture.append(source);
+ const image = document.createElement("img");
+ image.width = 10;
+ image.height = 30;
+ image.style.display = "none";
+ picture.append(image);
+ const initialCurrentSrc = image.currentSrc;
+ document.body.append(picture);
+
+ function printHints(label) {
+ const style = getComputedStyle(image);
+ println(`${label}: ${style.width}, ${style.height}, ${style.aspectRatio}; currentSrc unchanged: ${image.currentSrc === initialCurrentSrc}`);
+ }
+
+ printHints("inserted");
+ source.srcset = `${url} 1x 1x`;
+ printHints("invalid srcset");
+ source.srcset = url;
+ source.media = "not all";
+ printHints("nonmatching media");
+ source.media = "all";
+ source.type = "unsupported/image";
+ printHints("unsupported type");
+ source.type = "image/svg+xml";
+ printHints("matching source");
+ source.removeAttribute("height");
+ printHints("width only");
+ source.removeAttribute("width");
+ printHints("no dimensions");
+ source.width = 60;
+ source.height = 20;
+ printHints("dimensions restored");
+ picture.append(source);
+ printHints("source follows image");
+ picture.prepend(source);
+ printHints("source precedes image");
+ source.remove();
+ printHints("source removed");
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.html b/Tests/LibWeb/Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.html
new file mode 100644
index 00000000000..ba182c0affc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.html
@@ -0,0 +1,242 @@
+<!doctype html>
+<title>Image width and height attributes are used to infer aspect-ratio</title>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<style>
+ img:not([nowidth]) {
+ width: 100%;
+ max-width: 100px;
+ height: auto;
+ }
+</style>
+<picture>
+ <source srcset="/images/green-100x50.png">
+ <img>
+</picture>
+
+<picture>
+ <source srcset="/images/green-100x50.png" width="100" height="100">
+ <img>
+</picture>
+
+<picture>
+ <source srcset="/images/green-100x50.png" width="100" height="100">
+ <img width="50" height="100">
+</picture>
+
+<picture>
+ <source srcset="/images/green-100x50.png" width="100" height="100" id="twosource-s1">
+ <source srcset="/images/green-100x50.png" width="300" height="150">
+ <img id="twosource-img">
+</picture>
+
+<div style="width: 100px;">
+ <picture>
+ <source srcset="/images/green-100x50.png" width="100%" height="50%" id="percent-src">
+ <img style="contain:size;" id="percent-img" nowidth="true">
+ </picture>
+</div>
+
+<picture>
+ <source srcset="/images/green-100x50.png" width="100abc" height="50abc" id="trailing-src">
+ <img style="contain:size;" id="trailing-img" nowidth="true">
+</picture>
+
+<script>
+let guard = async_test("source width and height attributes are used to infer aspect-ratio in <picture>");
+function assert_ratio(img, expected, description) {
+ let epsilon = 0.001;
+ assert_approx_equals(parseFloat(getComputedStyle(img).width, 10) / parseFloat(getComputedStyle(img).height, 10),
+ expected, epsilon, description);
+}
+
+function createPicture(width, height) {
+ var picture = document.createElement("picture");
+ var source = document.createElement("source");
+ if (width !== undefined)
+ source.setAttribute("width", width);
+ if (height !== undefined)
+ source.setAttribute("height", height);
+ source.setAttribute("srcset", "../../../../images/green.png");
+ picture.appendChild(source);
+ var img = document.createElement("img");
+ picture.appendChild(img);
+ document.body.appendChild(picture);
+ return img;
+}
+
+function assert_cs(img, val) {
+ assert_equals(getComputedStyle(img).aspectRatio, val);
+}
+
+// Create and append a new image and immediately check the ratio.
+// This is not racy because the spec requires the user agent to queue a task:
+// https://html.spec.whatwg.org/multipage/images.html#updating-the-image-data
+test(function() {
+ var img = createPicture(100, 100);
+ assert_ratio(img, 1.0);
+ assert_cs(img, "auto 100 / 100");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "100px");
+ assert_equals(getComputedStyle(img).height, "100px");
+ var source = img.previousSibling;
+ assert_equals(getComputedStyle(source).width, "auto");
+ assert_equals(getComputedStyle(source).height, "auto");
+}, "Computed style for width/height/aspect-ratio");
+
+test(function() {
+ img = createPicture(200, 100);
+ img.setAttribute("width", 250);
+ img.setAttribute("height", 50);
+ assert_ratio(img, 2.0);
+ assert_cs(img, "auto 200 / 100");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "200px");
+ assert_equals(getComputedStyle(img).height, "100px");
+ source = img.previousSibling;
+ assert_equals(getComputedStyle(source).width, "auto");
+ assert_equals(getComputedStyle(source).height, "auto");
+}, "Source width/height should take precedence over img attributes.");
+
+test(function() {
+ img.parentNode.removeChild(img.previousSibling);
+ assert_cs(img, "auto 250 / 50");
+ img.src = "../../../../images/green.png";
+ assert_ratio(img, 5.0);
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "250px");
+ assert_equals(getComputedStyle(img).height, "50px");
+}, "Make sure style gets invalidated correctly when the source gets removed.");
+
+test(function() {
+ img = createPicture(100, undefined);
+ img.setAttribute("width", 200);
+ img.setAttribute("height", 100);
+ assert_cs(img, "auto");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "100px");
+ assert_equals(getComputedStyle(img).height, "auto");
+}, "If the <source> has only one of width/height, we don't get an aspect ratio, even if the <img> has both.");
+
+test(function() {
+ img = createPicture(undefined, undefined);
+ img.setAttribute("width", 200);
+ img.setAttribute("height", 100);
+ assert_cs(img, "auto 200 / 100");
+}, "If we don't have width/height on the source, we fall back to width/height on the <img>.");
+
+test(function() {
+ img = createPicture(100, undefined);
+ img.parentNode.style.display = "none";
+ img.setAttribute("width", "200");
+ img.setAttribute("height", "300");
+ img.setAttribute("nowidth", "true");
+ assert_cs(img, "auto");
+ assert_equals(getComputedStyle(img).width, "100px");
+ assert_equals(getComputedStyle(img).height, "auto");
+}, "If we only have one width attribute, we should get width mapped but no aspect ratio, even if <img> has attributes.");
+
+test(function() {
+ img = createPicture(undefined, 100);
+ img.parentNode.style.display = "none";
+ img.setAttribute("width", "200");
+ img.setAttribute("height", "300");
+ img.setAttribute("nowidth", "true");
+ assert_cs(img, "auto");
+ assert_equals(getComputedStyle(img).width, "auto");
+ assert_equals(getComputedStyle(img).height, "100px");
+}, "If we only have height attribute, we should get height mapped but no aspect ratio, even if <img> has attributes.");
+
+test(function() {
+ img = createPicture(100, 100);
+ assert_cs(img, "auto 100 / 100");
+ img.previousSibling.setAttribute("height", "300");
+ assert_cs(img, "auto 100 / 300");
+ img.previousSibling.setAttribute("width", "10");
+ assert_cs(img, "auto 10 / 300");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "10px");
+ assert_equals(getComputedStyle(img).height, "300px");
+}, "Dynamically changing width/height should change computed style");
+
+test(function() {
+ img = document.getElementById("twosource-img");
+ assert_cs(img, "auto 100 / 100");
+ source = document.getElementById("twosource-s1");
+ source.setAttribute("type", "x-foo/x-bar");
+ // We should now match the second source
+ assert_cs(img, "auto 300 / 150");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "300px");
+ assert_equals(getComputedStyle(img).height, "150px");
+}, "Changing which <source> matches should change computed style");
+
+test(function() {
+ img = document.getElementById("percent-img");
+ assert_equals(img.offsetWidth, 100);
+ assert_equals(img.offsetHeight, 0);
+ assert_cs(img, "auto");
+ source = document.getElementById("percent-src");
+ assert_equals(source.width, 100);
+ assert_equals(source.height, 50);
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "100%");
+ assert_equals(getComputedStyle(img).height, "50%");
+}, "Percentages on source should be ignored for aspect-ratio but used for width/height.");
+
+test(function() {
+ img = document.getElementById("trailing-img");
+ assert_equals(img.offsetWidth, 100);
+ assert_equals(img.offsetHeight, 50);
+ assert_cs(img, "auto 100 / 50");
+ source = document.getElementById("trailing-src");
+ assert_equals(source.width, 100);
+ assert_equals(source.height, 50);
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "100px");
+ assert_equals(getComputedStyle(img).height, "50px");
+}, "Trailing garbage should be ignored but not make the attribute invalid");
+
+onload = function() {
+ let images = document.querySelectorAll("img");
+ test(function() {
+ var img = images[0];
+ assert_ratio(img, 2.0, "2.0 is the original aspect ratio of green-100x50.png");
+ assert_cs(img, "auto");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "auto");
+ assert_equals(getComputedStyle(img).height, "auto");
+ }, "Loaded picture test: Both <source> and <img> are without width and height attributes");
+
+ test(function () {
+ img = images[1];
+ assert_ratio(img, 2.0, "Loaded image's aspect ratio, at least by default, overrides width / height ratio.");
+ assert_cs(img, "auto 100 / 100");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "100px");
+ assert_equals(getComputedStyle(img).height, "100px");
+ }, "Loaded picture test: <source> with width and height attributes, <img> without width and height attributes");
+
+ test(function () {
+ img = images[2];
+ assert_ratio(img, 2.0, "Loaded image's aspect ratio, at least by default, overrides width / height ratio (2).");
+ assert_cs(img, "auto 100 / 100");
+ img.style.display = "none";
+ img.setAttribute("nowidth", "true");
+ assert_equals(getComputedStyle(img).width, "100px");
+ assert_equals(getComputedStyle(img).height, "100px");
+ }, "Loaded picture test: Both <source> and <img> are with width and height attributes");
+
+ guard.done();
+};
+</script>