wrapping-transformation.window.js (2681B)
1 test((t) => { 2 const form = document.createElement("form"); 3 const textarea = document.createElement("textarea"); 4 textarea.name = "linebreakTest"; 5 textarea.textContent = "a\nb\rc\r\nd\n\re"; 6 form.appendChild(textarea); 7 document.body.appendChild(form); 8 t.add_cleanup(() => { 9 document.body.removeChild(form); 10 }); 11 12 assert_equals(textarea.textContent, "a\nb\rc\r\nd\n\re"); 13 assert_equals(textarea.value, "a\nb\nc\nd\n\ne"); 14 15 const formData = new FormData(form); 16 assert_equals( 17 formData.get("linebreakTest"), 18 "a\nb\nc\nd\n\ne", 19 ); 20 }, "Textarea wrapping transformation: Newlines should be normalized to LF."); 21 22 test((t) => { 23 const form = document.createElement("form"); 24 const textarea = document.createElement("textarea"); 25 textarea.name = "wrapTest"; 26 textarea.cols = 10; 27 textarea.wrap = "hard"; 28 textarea.textContent = 29 "Some text that is too long for the specified character width."; 30 form.appendChild(textarea); 31 document.body.appendChild(form); 32 t.add_cleanup(() => { 33 document.body.removeChild(form); 34 }); 35 36 assert_true( 37 !textarea.textContent.includes("\n") && 38 !textarea.textContent.includes("\r"), 39 "textContent shouldn't contain any newlines", 40 ); 41 assert_true( 42 !textarea.textContent.includes("\n") && 43 !textarea.textContent.includes("\r"), 44 "The API value shouldn't be line wrapped.", 45 ); 46 47 const formData = new FormData(form); 48 const formDataValue = formData.get("wrapTest"); 49 50 assert_true( 51 !formDataValue.includes("\r"), 52 "The wrapping done on the value must be LF, not CRLF.", 53 ); 54 assert_true( 55 formDataValue.includes("\n"), 56 "The value must be wrapped.", 57 ); 58 }, "Textarea wrapping transformation: Wrapping happens with LF newlines."); 59 60 function assert_roundtrips(text) { 61 test((t) => { 62 const form = document.createElement("form"); 63 const textarea = document.createElement("textarea"); 64 textarea.name = "wrapTest"; 65 textarea.cols = 10; 66 textarea.wrap = "hard"; 67 form.appendChild(textarea); 68 document.body.appendChild(form); 69 t.add_cleanup(() => { 70 document.body.removeChild(form); 71 }); 72 textarea.value = text; 73 const formDataValue = new FormData(form).get("wrapTest"); 74 textarea.value = formDataValue; 75 const newFormDataValue = new FormData(form).get("wrapTest"); 76 assert_equals(formDataValue, newFormDataValue, "Value should round-trip"); 77 }, "Textarea wrapping transformation: wrapping round-trips: " + text); 78 } 79 80 assert_roundtrips("Some text that is too long for the specified character width."); 81 assert_roundtrips("Some text that is too long for the\n\n\nspecified character width."); 82 assert_roundtrips("exact len");