test_sendSelectionSetEvent_with_same_range.html (3852B)
1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Testing nsIDOMWindowUtils.sendSelectionSetEvent not update selection if result range is same in serialized text within the ContentEventHandler rules</title> 6 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> 8 <script> 9 "use strict"; 10 const { AppConstants } = ChromeUtils.importESModule( 11 "resource://gre/modules/AppConstants.sys.mjs" 12 ); 13 const kLineBreak = navigator.platform.indexOf("Win") == 0 && false ? "\r\n" : "\n"; 14 15 SimpleTest.waitForExplicitFinish(); 16 SimpleTest.waitForFocus(async () => { 17 const editingHost = document.querySelector("div[contenteditable]"); 18 19 function waitForFlushingIMEContentObserver() { 20 return new Promise(resolve => requestAnimationFrame( 21 () => requestAnimationFrame(resolve) 22 )); 23 } 24 25 await (async function test_setSelection_when_selection_collapsed_at_end_of_inline_element() { 26 editingHost.innerHTML = "<b>abc</b>def"; 27 getSelection().collapse(editingHost.querySelector("b").firstChild, "abc".length); 28 await waitForFlushingIMEContentObserver(); 29 const ret = windowUtils.sendSelectionSetEvent( 30 3, 31 0, 32 SpecialPowers.Ci.nsIDOMWindowUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK 33 ); 34 ok( 35 ret, 36 "test_setSelection_when_selection_collapsed_at_end_of_inline_element: eSetSelection event should be succeeded" 37 ); 38 is( 39 getSelection().focusNode, 40 editingHost.querySelector("b").firstChild, 41 "test_setSelection_when_selection_collapsed_at_end_of_inline_element: focus node should not be changed from the text node in the <b>" 42 ); 43 is( 44 getSelection().focusOffset, 45 "abc".length, 46 "test_setSelection_when_selection_collapsed_at_end_of_inline_element: focus offset should not be changed from end of the text node" 47 ); 48 })(); 49 50 await (async function test_setSelection_when_selection_collapsed_after_inline_element() { 51 editingHost.innerHTML = "<b>abc</b>def"; 52 getSelection().collapse(editingHost.querySelector("b").nextSibling, 0); 53 await waitForFlushingIMEContentObserver(); 54 const ret = windowUtils.sendSelectionSetEvent( 55 3, 56 0, 57 SpecialPowers.Ci.nsIDOMWindowUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK 58 ); 59 ok( 60 ret, 61 "test_setSelection_when_selection_collapsed_after_inline_element: eSetSelection event should be succeeded" 62 ); 63 is( 64 getSelection().focusNode, 65 editingHost.querySelector("b").nextSibling, 66 "test_setSelection_when_selection_collapsed_after_inline_element: focus node should not be changed from the text node after the <b>" 67 ); 68 is( 69 getSelection().focusOffset, 70 0, 71 "test_setSelection_when_selection_collapsed_after_inline_element: focus offset should not be changed from start of the text node" 72 ); 73 })(); 74 75 await (async function test_setSelection_when_selection_collapsed_in_empty_block_element() { 76 editingHost.innerHTML = "<p style=\"width:1em;height:1em;\"></p>\n"; 77 getSelection().collapse(editingHost.querySelector("p"), 0); 78 await waitForFlushingIMEContentObserver(); 79 const ret = windowUtils.sendSelectionSetEvent( 80 kLineBreak.length, 81 0, 82 SpecialPowers.Ci.nsIDOMWindowUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK 83 ); 84 ok( 85 ret, 86 "test_setSelection_when_selection_collapsed_in_empty_block_element: eSetSelection event should be succeeded" 87 ); 88 is( 89 getSelection().focusNode, 90 editingHost.querySelector("p"), 91 "test_setSelection_when_selection_collapsed_in_empty_block_element: focus node should not be changed from the empty <div>" 92 ); 93 })(); 94 95 SimpleTest.finish(); 96 }); 97 </script> 98 </head> 99 <body> 100 <div contenteditable></div> 101 </body> 102 </html>