form-control-state.html (1780B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Writing out a document with form controls with values</title> 5 <link rel="author" href="mailto:bzbarsky@mit.edu"/> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 </head> 9 <body> 10 <div id="log"></div> 11 <script> 12 function asyncHop(t, arg) { 13 return new Promise(res => t.step_timeout(res.bind(null, arg), 0)); 14 } 15 16 function loadPromise(t, iframe) { 17 var p = new Promise(res => 18 iframe.addEventListener("load", res.bind(null, iframe), { once: true })); 19 // We need to do one trip through the event loop to make sure we're 20 // not still under the load event firing when we start doing our 21 // document.open bits. 22 return p.then(asyncHop.bind(null, t)); 23 } 24 25 async function createIframe(t) { 26 var i = document.createElement("iframe"); 27 t.add_cleanup(() => i.remove()); 28 var p = loadPromise(t, i); 29 document.body.appendChild(i); 30 return p; 31 } 32 33 async function replaceIframe(t, i, text) { 34 var p = loadPromise(t, i); 35 var doc = i.contentDocument; 36 doc.open(); 37 doc.write(text); 38 doc.close(); 39 return p; 40 } 41 42 promise_test(async function(t) { 43 var i = await createIframe(t); 44 var str = "<textarea>123</textarea>"; 45 await replaceIframe(t, i, str); 46 i.contentDocument.querySelector("textarea").value = "abc"; 47 await replaceIframe(t, i, str); 48 assert_equals(i.contentDocument.querySelector("textarea").value, "123"); 49 }, "textarea state"); 50 51 promise_test(async function(t) { 52 var i = await createIframe(t); 53 var str = "<input value='123'>"; 54 await replaceIframe(t, i, str); 55 i.contentDocument.querySelector("input").value = "abc"; 56 await replaceIframe(t, i, str); 57 assert_equals(i.contentDocument.querySelector("input").value, "123"); 58 }, "input state"); 59 </script> 60 </body> 61 </html>