selectall-in-editinghost.html (4690B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset=utf-8> 5 <title>Select All in focused editor</title> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script> 9 "use strict"; 10 11 addEventListener("DOMContentLoaded", () => { 12 const editingHost = document.querySelector("div[contenteditable]"); 13 test(() => { 14 editingHost.focus(); 15 document.execCommand("selectAll"); 16 assert_false( 17 getSelection().isCollapsed, 18 'Selection should not be collapsed after calling document.execCommand("selectAll")' 19 ); 20 const rangeText = getSelection().toString(); 21 assert_false( 22 rangeText.includes("preceding text"), 23 "Selection should not contain the preceding text of the editing host" 24 ); 25 assert_true( 26 rangeText.includes("editable text"), 27 "Selection should contain the editable text in the editing host" 28 ); 29 assert_false( 30 rangeText.includes("following text"), 31 "Selection should not contain the following text of the editing host" 32 ); 33 getSelection().removeAllRanges(); 34 }, "execCommand('selectAll') should select all content in the editing host"); 35 36 test(() => { 37 editingHost.focus(); 38 getSelection().removeAllRanges(); 39 document.execCommand("selectAll"); 40 assert_false( 41 getSelection().isCollapsed, 42 'Selection should not be collapsed after calling document.execCommand("selectAll")' 43 ); 44 const rangeText = getSelection().toString(); 45 assert_false( 46 rangeText.includes("preceding text"), 47 "Selection should not contain the preceding text of the editing host" 48 ); 49 assert_true( 50 rangeText.includes("editable text"), 51 "Selection should contain the editable text in the editing host" 52 ); 53 assert_false( 54 rangeText.includes("following text"), 55 "Selection should not contain the following text of the editing host" 56 ); 57 getSelection().removeAllRanges(); 58 }, "execCommand('selectAll') should select all content in the editing host when it has focus but no selection range"); 59 60 test(() => { 61 editingHost.focus(); 62 editingHost.innerHTML = "preceding editable text<input value='input value'>following editable text"; 63 getSelection().collapse(editingHost.querySelector("input"), 0); 64 document.execCommand("selectAll"); 65 assert_false( 66 getSelection().isCollapsed, 67 'Selection should not be collapsed after calling document.execCommand("selectAll")' 68 ); 69 const rangeText = getSelection().toString(); 70 assert_false( 71 rangeText.includes("preceding text"), 72 "Selection should not contain the preceding text of the editing host" 73 ); 74 assert_true( 75 rangeText.includes("preceding editable text"), 76 "Selection should contain the preceding editable text of <input> in the editing host" 77 ); 78 assert_true( 79 rangeText.includes("following editable text"), 80 "Selection should contain the following editable text of <input> in the editing host" 81 ); 82 assert_false( 83 rangeText.includes("following text"), 84 "Selection should not contain the following text of the editing host" 85 ); 86 getSelection().removeAllRanges(); 87 }, "execCommand('selectAll') should select all content in the editing host when selection collapsed in the <input>"); 88 89 test(() => { 90 editingHost.focus(); 91 editingHost.innerHTML = "preceding editable text<textarea>textarea value</textarea>following editable text"; 92 getSelection().collapse(editingHost.querySelector("textarea"), 0); 93 document.execCommand("selectAll"); 94 assert_false( 95 getSelection().isCollapsed, 96 'Selection should not be collapsed after calling document.execCommand("selectAll")' 97 ); 98 const rangeText = getSelection().toString(); 99 assert_false( 100 rangeText.includes("preceding text"), 101 "Selection should not contain the preceding text of the editing host" 102 ); 103 assert_true( 104 rangeText.includes("preceding editable text"), 105 "Selection should contain the preceding editable text of <textarea> in the editing host" 106 ); 107 assert_true( 108 rangeText.includes("following editable text"), 109 "Selection should contain the following editable text of <textarea> in the editing host" 110 ); 111 assert_false( 112 rangeText.includes("following text"), 113 "Selection should not contain the following text of the editing host" 114 ); 115 getSelection().removeAllRanges(); 116 }, "execCommand('selectAll') should select all content in the editing host when selection collapsed in the <textarea>"); 117 }); 118 </script> 119 </head> 120 <body> 121 <p>preceding text</p> 122 <div contenteditable>editable text</div> 123 <p>following text</p> 124 </body> 125 </html>