print-manual.html (2762B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Printing</title> 4 <link rel=help href="https://html.spec.whatwg.org/#printing"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script> 8 setup({explicit_timeout: true}) 9 </script> 10 11 <p>Click on the button below.</p> 12 <p>If the browser offers to print the current page, dismiss the prompt to 13 return to this page. The following should then appear in the box marked as 14 "Output":</p> 15 <pre> 16 before calling print() 17 beforeprint 18 afterprint 19 after calling print() 20 </pre> 21 <p>If no user dialog appears, either the above or the following should be 22 printed in the box marked as "Output":</p> 23 <pre> 24 before calling print() 25 after calling print() 26 </pre> 27 <p>The test passes if the above criteria are satisfied and "PASS" appears in a 28 table below this paragraph. The test fails otherwise.</p> 29 30 <button onclick="testBtn(this)">Click here!</button> 31 <h3>Output</h3> 32 <pre id=output></pre> 33 34 <script> 35 "use strict"; 36 // This test is actually synchronous, but we use async_test()'s nice 37 // infrastructure around t.step() to capture errors in event listeners. This is 38 // necessary since it seems like in Chrome, exceptions in beforeprint/afterprint 39 // event listeners are not propagated to error events. See 40 // https://crbug.com/977828. 41 const t = async_test(); 42 const log = []; 43 function out(str) { 44 log.push(str); 45 output.appendChild(document.createTextNode(str + "\n")); 46 } 47 function testBtn(btn) { 48 btn.remove(); 49 window.addEventListener("beforeprint", function (ev) { 50 // t.step_func() does not preserve `this`, which we test below. 51 t.step(() => { 52 out("beforeprint"); 53 assert_equals(Object.getPrototypeOf(ev), Event.prototype); 54 assert_equals(this, window); 55 assert_equals(ev.target, window); 56 assert_equals(ev.type, "beforeprint"); 57 }); 58 }); 59 window.addEventListener("afterprint", function (ev) { 60 // t.step_func() does not preserve `this`, which we test below. 61 t.step(() => { 62 out("afterprint"); 63 assert_equals(Object.getPrototypeOf(ev), Event.prototype); 64 assert_equals(this, window); 65 assert_equals(ev.target, window); 66 assert_equals(ev.type, "afterprint"); 67 }); 68 }); 69 out("before calling print()"); 70 print(); 71 out("after calling print()"); 72 t.step(() => { 73 try { 74 assert_array_equals(log, [ 75 "before calling print()", 76 "beforeprint", 77 "afterprint", 78 "after calling print()", 79 ]); 80 } catch (err) { 81 try { 82 assert_array_equals(log, [ 83 "before calling print()", 84 "after calling print()", 85 ]); 86 } catch (err) { 87 assert_unreached("Output does not match either possibility"); 88 } 89 } 90 }); 91 t.done(); 92 } 93 </script>