video-aspect-ratio.html (3578B)
1 <!doctype html> 2 <title>Video width and height attributes are used to infer aspect-ratio</title> 3 <link rel="help" href="https://github.com/w3c/csswg-drafts/issues/12053"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/common/media.js"></script> 7 <script src="resources/aspect-ratio.js"></script> 8 <style> 9 video { 10 width: 100%; 11 max-width: 150px; 12 height: auto; 13 } 14 </style> 15 <body> 16 <video width="250" height="100" id="contained" style="contain: size;"></video> 17 <script> 18 function assert_ratio(element, expected, description) { 19 let epsilon = 0.001; 20 assert_approx_equals( 21 parseFloat(getComputedStyle(element).width) / parseFloat(getComputedStyle(element).height), 22 expected, 23 epsilon, 24 description, 25 ); 26 } 27 28 function test_computed_style(width, height, expected) { 29 test_computed_style_aspect_ratio("video", {width: width, height: height}, expected); 30 } 31 32 promise_test(async function() { 33 let video = document.getElementById("contained"); 34 video.src = getVideoURI('/media/2x2-green'); 35 assert_ratio(video, 2.5); 36 }, "Aspect ratio for video with contain:size"); 37 38 // Create and append a new video and immediately check the ratio. 39 // This is not racy because the spec requires the user agent to queue a task: 40 // https://html.spec.whatwg.org/multipage/media.html#concept-media-load-algorithm 41 promise_test(async function() { 42 let video = document.createElement("video"); 43 video.setAttribute("width", "250"); 44 video.setAttribute("height", "100"); 45 video.src = getVideoURI('/media/2x2-green'); 46 document.body.appendChild(video); 47 assert_ratio(video, 2.5, "aspect ratio before load"); 48 await new Promise(r => video.addEventListener("loadeddata", r, { once: true })); 49 // When loaded this video is square. 50 assert_ratio(video, 1, "aspect ratio after load"); 51 }, "Aspect ratio for regular video"); 52 53 // Same but with auto width. 54 promise_test(async function() { 55 let video = document.createElement("video"); 56 video.setAttribute("width", "250"); 57 video.setAttribute("height", "100"); 58 video.style.width = "auto"; 59 video.src = getVideoURI('/media/2x2-green'); 60 document.body.appendChild(video); 61 assert_ratio(video, 2.5, "aspect ratio before load"); 62 await new Promise(r => video.addEventListener("loadeddata", r, { once: true })); 63 assert_ratio(video, 1, "aspect ratio after load"); 64 }, "Aspect ratio for regular video with width:auto"); 65 66 // Same but with auto aspect-ratio. 67 promise_test(async function() { 68 let video = document.createElement("video"); 69 video.setAttribute("width", "250"); 70 video.setAttribute("height", "100"); 71 video.style.aspectRatio = "auto"; 72 video.src = getVideoURI('/media/2x2-green'); 73 document.body.appendChild(video); 74 // The size is 150x150 as there is no default aspect-ratio. 75 assert_ratio(video, 1, "aspect ratio before load"); 76 await new Promise(r => video.addEventListener("loadeddata", r, { once: true })); 77 // But now we can use the natural ratio from the video source. 78 assert_ratio(video, 1, "aspect ratio after load"); 79 }, "Aspect ratio for regular video with aspect-ratio:auto"); 80 81 test_computed_style("10", "20", "auto 10 / 20"); 82 test_computed_style("0.5", "1.5", "auto 0.5 / 1.5"); 83 test_computed_style("0", "1", "auto 0 / 1"); 84 test_computed_style("1", "0", "auto 1 / 0"); 85 test_computed_style("0", "0", "auto 0 / 0"); 86 test_computed_style(null, null, "auto"); 87 test_computed_style("10", null, "auto"); 88 test_computed_style(null, "20", "auto"); 89 test_computed_style("xx", "20", "auto"); 90 test_computed_style("10%", "20", "auto"); 91 </script>