-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
img-aspect-ratio.tentative.html
53 lines (50 loc) · 1.84 KB
/
img-aspect-ratio.tentative.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!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 {
width: 100%;
max-width: 100px;
height: auto;
}
</style>
<img src="/images/green.png">
<img src="/images/green.png" width=100 height=125>
<script>
let t = async_test("Image width and height attributes are used to infer aspect-ratio");
function assert_ratio(img, expected) {
let epsilon = 0.001;
assert_approx_equals(parseInt(getComputedStyle(img).width, 10) / parseInt(getComputedStyle(img).height, 10), expected, epsilon);
}
// 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
t.step(function() {
var img = new Image();
img.width = 250;
img.height = 100;
img.src = "/images/blue.png";
document.body.appendChild(img);
assert_ratio(img, 2.5);
img = new Image();
img.setAttribute("width", "0.8");
img.setAttribute("height", "0.2");
img.src = "/images/blue.png";
document.body.appendChild(img);
assert_ratio(img, 4);
img = new Image();
img.setAttribute("width", "50%");
img.setAttribute("height", "25%");
img.src = "/images/blue.png";
document.body.appendChild(img);
// Percentages should be ignored.
assert_equals(getComputedStyle(img).height, "0px");
});
onload = t.step_func_done(function() {
let images = document.querySelectorAll("img");
assert_ratio(images[2], 1.266); // 1.266 is the original aspect ratio of blue.png
assert_ratio(images[1], 2.0); // 2.0 is the original aspect ratio of green.png
assert_ratio(images[0], 2.0); // Loaded image's aspect ratio, at least by default, overrides width / height ratio.
});
</script>