Skip to content

Commit 3ccf9cf

Browse files
committed
Bug 1580000 [wpt PR 18945] - Compute img aspect ratio from width and height HTML attributes, a=testonly
Automatic update from web-platform-tests Compute img aspect ratio from width and height HTML attributes As per this intent to implement: https://groups.google.com/a/chromium.org/forum/?fromgroups#!topic/blink-dev/hbhKRuBzZ4o Spec: WICG/intrinsicsize-attribute#16 Bug: 979891 Change-Id: I0f9ffa1584fa12a41393ef05daffb7238c97e990 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1790472 Commit-Queue: Christian Biesinger <cbiesingerchromium.org> Reviewed-by: Emilio Cobos Álvarez <emiliochromium.org> Auto-Submit: Christian Biesinger <cbiesingerchromium.org> Cr-Commit-Position: refs/heads/master{#699465} -- wpt-commits: 86ff86de45a67eccc781b84f50ab261c18aad5cf wpt-pr: 18945 UltraBlame original commit: e8962724f6cc584847fedccd8c0912b6bd00dab6
1 parent 2486f25 commit 3ccf9cf

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!doctype html>
2+
<title>Canvas width and height attributes are used to infer aspect-ratio</title>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<style>
6+
canvas {
7+
width: 100%;
8+
max-width: 100px;
9+
height: auto;
10+
}
11+
</style>
12+
<body>
13+
<script>
14+
let t = async_test("Canvas width and height attributes are used to infer aspect-ratio");
15+
function assert_ratio(img, expected) {
16+
let epsilon = 0.001;
17+
assert_approx_equals(parseInt(getComputedStyle(img).width, 10) / parseInt(getComputedStyle(img).height, 10), expected, epsilon);
18+
}
19+
// Create and append a new canvas and immediately check the ratio.
20+
t.step(function() {
21+
var canvas = document.createElement("canvas");
22+
canvas.setAttribute("width", "250");
23+
canvas.setAttribute("height", "100");
24+
document.body.appendChild(canvas);
25+
// Canvases always use the aspect ratio from their surface size.
26+
assert_ratio(canvas, 2.5);
27+
28+
t.done();
29+
});
30+
</script>

testing/web-platform/tests/html/rendering/replaced-elements/attributes-for-embedded-content-and-images/img-aspect-ratio.tentative.html

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
height: auto;
1010
}
1111
</style>
12-
<img src=broken width=100 height=125>
1312
<img src="/images/green.png">
1413
<img src="/images/green.png" width=100 height=125>
1514
<script>
@@ -18,10 +17,37 @@
1817
let epsilon = 0.001;
1918
assert_approx_equals(parseInt(getComputedStyle(img).width, 10) / parseInt(getComputedStyle(img).height, 10), expected, epsilon);
2019
}
20+
// Create and append a new image and immediately check the ratio.
21+
// This is not racy because the spec requires the user agent to queue a task:
22+
// https://html.spec.whatwg.org/multipage/images.html#updating-the-image-data
23+
t.step(function() {
24+
var img = new Image();
25+
img.width = 250;
26+
img.height = 100;
27+
img.src = "/images/blue.png";
28+
document.body.appendChild(img);
29+
assert_ratio(img, 2.5);
30+
31+
img = new Image();
32+
img.setAttribute("width", "0.8");
33+
img.setAttribute("height", "0.2");
34+
img.src = "/images/blue.png";
35+
document.body.appendChild(img);
36+
assert_ratio(img, 4);
37+
38+
img = new Image();
39+
img.setAttribute("width", "50%");
40+
img.setAttribute("height", "25%");
41+
img.src = "/images/blue.png";
42+
document.body.appendChild(img);
43+
// Percentages should be ignored.
44+
assert_equals(getComputedStyle(img).height, "0px");
45+
});
46+
2147
onload = t.step_func_done(function() {
2248
let images = document.querySelectorAll("img");
23-
assert_ratio(images[0], 0.8);
24-
assert_ratio(images[2], 2.0); // 2.0 is the original aspect ratio of green.png
25-
assert_ratio(images[1], 2.0); // Loaded image's aspect ratio, at least by default, overrides width / height ratio.
49+
assert_ratio(images[2], 1.266); // 1.266 is the original aspect ratio of blue.png
50+
assert_ratio(images[1], 2.0); // 2.0 is the original aspect ratio of green.png
51+
assert_ratio(images[0], 2.0); // Loaded image's aspect ratio, at least by default, overrides width / height ratio.
2652
});
2753
</script>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!doctype html>
2+
<title>Video width and height attributes are not used to infer aspect-ratio</title>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="/common/media.js"></script>
6+
<style>
7+
video {
8+
width: 100%;
9+
max-width: 100px;
10+
height: auto;
11+
}
12+
</style>
13+
<body>
14+
<script>
15+
let t = async_test("Video width and height attributes are not used to infer aspect-ratio");
16+
function assert_ratio(img, expected) {
17+
let epsilon = 0.001;
18+
assert_approx_equals(parseInt(getComputedStyle(img).width, 10) / parseInt(getComputedStyle(img).height, 10), expected, epsilon);
19+
}
20+
// Create and append a new video and immediately check the ratio.
21+
// This is not racy because the spec requires the user agent to queue a task:
22+
// https://html.spec.whatwg.org/multipage/media.html#concept-media-load-algorithm
23+
t.step(function() {
24+
var video = document.createElement("video");
25+
video.setAttribute("width", "250");
26+
video.setAttribute("height", "100");
27+
video.src = getVideoURI('/media/2x2-green');
28+
document.body.appendChild(video);
29+
// Videos default to a size of 300x150px and calculate their aspect ratio
30+
// based on that before the video is loaded. So this should be 2, ignoring
31+
// the 2.5 that it would be based on the attributes.
32+
assert_ratio(video, 2);
33+
34+
video.onloadeddata = t.step_func_done(function() {
35+
// When loaded this video is square.
36+
assert_ratio(video, 1);
37+
});
38+
});
39+
</script>

0 commit comments

Comments
 (0)