layout/generic/test/test_backspace_delete.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
michael@0 3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
michael@0 4 <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 5 xmlns:html="http://www.w3.org/1999/xhtml"
michael@0 6 title="Test BackSpace/Delete Keys">
michael@0 7 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 8
michael@0 9 <script class="testbody" type="application/javascript">
michael@0 10 <![CDATA[
michael@0 11
michael@0 12 function execTests() {
michael@0 13 var e = document.getElementById("edit");
michael@0 14 var doc = e.contentDocument;
michael@0 15 var win = e.contentWindow;
michael@0 16 var root = doc.documentElement;
michael@0 17 var editor = doc.body;
michael@0 18 var sel = win.getSelection();
michael@0 19 win.focus();
michael@0 20
michael@0 21 function setupTest(html, firstChildOffsetForCaret, node) {
michael@0 22 // Work around bug 474255 --- we need to have nonempty content before we turn on
michael@0 23 // editing, or the tests below break because the editor doesn't notice when we
michael@0 24 // insert non-empty content using innerHTML.
michael@0 25 doc.designMode = 'off';
michael@0 26 editor.innerHTML = html;
michael@0 27 doc.designMode = 'on';
michael@0 28 var n = editor.firstChild;
michael@0 29 if (node) {
michael@0 30 n = node();
michael@0 31 }
michael@0 32 sel.collapse(n, firstChildOffsetForCaret);
michael@0 33 }
michael@0 34
michael@0 35 var eatSpace;
michael@0 36
michael@0 37 function getPrefs() {
michael@0 38 const prefSvcContractID = "@mozilla.org/preferences-service;1";
michael@0 39 const prefSvcIID = Components.interfaces.nsIPrefService;
michael@0 40 return Components.classes[prefSvcContractID].getService(prefSvcIID)
michael@0 41 .getBranch("layout.word_select.");
michael@0 42 }
michael@0 43
michael@0 44 function setEatSpace(newValue) {
michael@0 45 getPrefs().setBoolPref("eat_space_to_next_word", newValue);
michael@0 46 eatSpace = newValue;
michael@0 47 }
michael@0 48
michael@0 49 function restoreEatSpace() {
michael@0 50 try {
michael@0 51 getPrefs().clearUserPref("eat_space_to_next_word");
michael@0 52 } catch(ex) {}
michael@0 53 }
michael@0 54
michael@0 55 function doCommand(cmd) {
michael@0 56 var controller = document.commandDispatcher.getControllerForCommand(cmd);
michael@0 57 if (controller) {
michael@0 58 try {
michael@0 59 controller.doCommand(cmd);
michael@0 60 ok(true, 'doCommand(' + cmd + ') succeeded');
michael@0 61 } catch(ex) {
michael@0 62 ok(false, 'exception in doCommand(' + cmd + '): ', ex.message);
michael@0 63 }
michael@0 64 }
michael@0 65 }
michael@0 66
michael@0 67 function testRight(node, offset) {
michael@0 68 doCommand("cmd_charNext");
michael@0 69 var msg = "Right movement broken in \"" + editor.innerHTML + "\", offset " + offset;
michael@0 70 is(sel.anchorNode, node, msg);
michael@0 71 is(sel.anchorOffset, offset, msg);
michael@0 72 }
michael@0 73
michael@0 74 function selErrString(dir) {
michael@0 75 return dir + " selection broken with eatSpace=" + eatSpace + " in \"" + editor.innerHTML + "\"";
michael@0 76 }
michael@0 77
michael@0 78 function testWordSelRight(startNode, startOffset, endNode, endOffset) {
michael@0 79 doCommand("cmd_selectWordNext");
michael@0 80 var selRange = sel.getRangeAt(0);
michael@0 81 is(selRange.startContainer, startNode, selErrString("Word right"));
michael@0 82 is(selRange.startOffset, startOffset, selErrString("Word right"));
michael@0 83 is(selRange.endContainer, endNode, selErrString("Word right"));
michael@0 84 is(selRange.endOffset, endOffset, selErrString("Word right"));
michael@0 85 }
michael@0 86
michael@0 87 function testDelete(node, offset, text, richtext) {
michael@0 88 doCommand("cmd_deleteCharForward");
michael@0 89 var msg = "Delete broken in \"" + editor.innerHTML + "\", offset " + offset;
michael@0 90 if(typeof node == 'function'){
michael@0 91 node = node();
michael@0 92 }
michael@0 93 is(sel.anchorNode, node, msg);
michael@0 94
michael@0 95 is(sel.anchorOffset, offset, msg);
michael@0 96 let text_result = richtext ? editor.innerHTML : editor.textContent;
michael@0 97 is(text_result, text, msg);
michael@0 98 }
michael@0 99
michael@0 100 function testBackspace(node, offset, text) {
michael@0 101 doCommand("cmd_deleteCharBackward");
michael@0 102 var msg = "Backspace broken in \"" + editor.innerHTML + "\", offset " + offset;
michael@0 103 is(sel.anchorNode, node, msg);
michael@0 104
michael@0 105 is(sel.anchorOffset, offset, msg);
michael@0 106 is(editor.textContent, text, msg);
michael@0 107 }
michael@0 108
michael@0 109 function testDeletePrevWord(node, offset, text) {
michael@0 110 doCommand("cmd_deleteWordBackward");
michael@0 111 var msg = "Delete previous word broken in \"" + editor.innerHTML + "\", offset " + offset;
michael@0 112 is(sel.anchorNode, node, msg);
michael@0 113 is(sel.anchorOffset, offset, msg);
michael@0 114 is(editor.textContent, text, msg);
michael@0 115 }
michael@0 116
michael@0 117 function testDeleteNextWord(node, offset, text) {
michael@0 118 doCommand("cmd_deleteWordForward");
michael@0 119 var msg = "Delete next word broken in \"" + editor.innerHTML + "\", offset " + offset;
michael@0 120 is(sel.anchorNode, node, msg);
michael@0 121 is(sel.anchorOffset, offset, msg);
michael@0 122 todo_is(editor.textContent, text, msg);
michael@0 123 }
michael@0 124
michael@0 125 // Test cell-wise deletion of Delete
michael@0 126 setupTest("สวัสดีพ่อแม่พี่น้อง", 0);
michael@0 127 testRight(editor.firstChild, 1);
michael@0 128 testDelete(editor.firstChild, 1, "สสดีพ่อแม่พี่น้อง");
michael@0 129 testRight(editor.firstChild, 2);
michael@0 130 testDelete(editor.firstChild, 2, "สสพ่อแม่พี่น้อง");
michael@0 131 testRight(editor.firstChild, 4);
michael@0 132 testDelete(editor.firstChild, 4, "สสพ่แม่พี่น้อง");
michael@0 133 testRight(editor.firstChild, 5);
michael@0 134 testDelete(editor.firstChild, 5, "สสพ่แพี่น้อง", false);
michael@0 135 testRight(editor.firstChild, 8);
michael@0 136 testDelete(editor.firstChild, 8, "สสพ่แพี่อง", false);
michael@0 137 testRight(editor.firstChild, 9);
michael@0 138 testDelete(editor.firstChild, 9, "สสพ่แพี่อ", false);
michael@0 139
michael@0 140 // Test character-wise deletion of Backspace
michael@0 141 setupTest("สวัสดีพ่อแม่พี่น้อง", 0);
michael@0 142 testRight(editor.firstChild, 1);
michael@0 143 testBackspace(editor.firstChild, 0, "วัสดีพ่อแม่พี่น้อง");
michael@0 144 testRight(editor.firstChild, 2);
michael@0 145 testBackspace(editor.firstChild, 1, "วสดีพ่อแม่พี่น้อง");
michael@0 146 testRight(editor.firstChild, 2);
michael@0 147 testBackspace(editor.firstChild, 1, "วดีพ่อแม่พี่น้อง");
michael@0 148 testRight(editor.firstChild, 3);
michael@0 149 testBackspace(editor.firstChild, 2, "วดพ่อแม่พี่น้อง");
michael@0 150 testRight(editor.firstChild, 4);
michael@0 151 testBackspace(editor.firstChild, 3, "วดพอแม่พี่น้อง");
michael@0 152 testRight(editor.firstChild, 4);
michael@0 153 testBackspace(editor.firstChild, 3, "วดพแม่พี่น้อง");
michael@0 154 testRight(editor.firstChild, 4);
michael@0 155 testBackspace(editor.firstChild, 3, "วดพม่พี่น้อง");
michael@0 156 testRight(editor.firstChild, 5);
michael@0 157 testBackspace(editor.firstChild, 4, "วดพมพี่น้อง");
michael@0 158 testRight(editor.firstChild, 7);
michael@0 159 testBackspace(editor.firstChild, 6, "วดพมพีน้อง");
michael@0 160 testRight(editor.firstChild, 8);
michael@0 161 testBackspace(editor.firstChild, 7, "วดพมพีนอง");
michael@0 162 testRight(editor.firstChild, 8);
michael@0 163 testBackspace(editor.firstChild, 7, "วดพมพีนง");
michael@0 164 testRight(editor.firstChild, 8);
michael@0 165 testBackspace(editor.firstChild, 7, "วดพมพีน");
michael@0 166
michael@0 167 // Tests for Bug 417745
michael@0 168
michael@0 169 setEatSpace(true);
michael@0 170
michael@0 171 setupTest("Quick yellow fox", 0);
michael@0 172 testWordSelRight(editor.firstChild, 0, editor.firstChild, 6);
michael@0 173 testDelete(editor.firstChild, 0, "yellow fox");
michael@0 174 testWordSelRight(editor.firstChild, 0, editor.firstChild, 7);
michael@0 175 testDelete(editor.firstChild, 0, "fox");
michael@0 176
michael@0 177 setEatSpace(false);
michael@0 178
michael@0 179 setupTest("Quick yellow fox", 0);
michael@0 180 testWordSelRight(editor.firstChild, 0, editor.firstChild, 5);
michael@0 181 // editor converts the leading space to an &nbsp;, otherwise it
michael@0 182 // wouldn't show up which would confuse users
michael@0 183 testDelete(editor.firstChild, 0, "\u00A0yellow fox");
michael@0 184 testWordSelRight(editor.firstChild, 0, editor.firstChild, 7);
michael@0 185 testDelete(editor.firstChild, 0, "\u00A0fox");
michael@0 186 testWordSelRight(editor.firstChild, 0, editor.firstChild, 4);
michael@0 187 testDelete(editor, 0, "");
michael@0 188
michael@0 189 restoreEatSpace();
michael@0 190
michael@0 191 // Tests for Bug 419217
michael@0 192
michael@0 193 setupTest("foo<div>bar</div>", 3);
michael@0 194 testDelete(function(){return editor.firstChild;}, 3, "foobar", true);
michael@0 195
michael@0 196 // Tests for Bug 419406
michael@0 197 var s = "helloשלום";
michael@0 198 setupTest(s, 4);
michael@0 199 testRight(editor.firstChild, 5);
michael@0 200 testDelete(editor.firstChild, 5, "helloשלום");
michael@0 201
michael@0 202 // Tests for Bug 462188
michael@0 203 setupTest("You should not see this text.", 29);
michael@0 204 testDeletePrevWord(editor.firstChild, 24, "You should not see this ");
michael@0 205 testDeletePrevWord(editor.firstChild, 19, "You should not see ");
michael@0 206 testDeletePrevWord(editor.firstChild, 15, "You should not ");
michael@0 207 testDeletePrevWord(editor.firstChild, 11, "You should ");
michael@0 208 testDeletePrevWord(editor.firstChild, 4, "You ");
michael@0 209 testDeletePrevWord(editor, 0, "");
michael@0 210
michael@0 211 setupTest("You should not see this text.", 0);
michael@0 212 testDeleteNextWord(editor.firstChild, 0, "\u00A0should not see this text.");
michael@0 213 testDeleteNextWord(editor.firstChild, 0, "\u00A0not see this text.");
michael@0 214 testDeleteNextWord(editor.firstChild, 0, "\u00A0see this text.");
michael@0 215 testDeleteNextWord(editor.firstChild, 0, "\u00A0this text.");
michael@0 216 testDeleteNextWord(editor.firstChild, 0, "\u00A0text.");
michael@0 217 // testDeleteNextWord(editor, 0, "");
michael@0 218
michael@0 219 // Tests for Bug 502259
michael@0 220 setupTest("<p>Bug</p>\n<p>502259</p>", 1);
michael@0 221 testDelete(function(){return editor.firstChild.firstChild;}, 3, "<p>Bug502259</p>", true);
michael@0 222
michael@0 223 // Tests for Bug 507936
michael@0 224 var nodecallback = function(){return editor.firstChild.firstChild.lastChild.firstChild.lastChild;};
michael@0 225 setupTest("<ol><li>one<ol><li>two</li></ol></li></ol>\n<p>three</p>", 3, nodecallback);
michael@0 226 testDelete(nodecallback, 0, "<ol><li>one<ol><li>twothree</li></ol></li></ol>", true);
michael@0 227
michael@0 228 setupTest("<ol><li>one<ol><li>two</li></ol></li></ol>\n<hr>\n<p>three</p>", 3, nodecallback);
michael@0 229 testDelete(nodecallback, 3,
michael@0 230 "<ol><li>one<ol><li>two</li></ol></li></ol><p>three</p>", true);
michael@0 231
michael@0 232 // Tests for Bug 519751
michael@0 233 var nodecallback = function(){return editor.firstChild.lastChild;};
michael@0 234 setupTest("<p>one</p><ol><li>two</li><li>three</li></ol>", 3, nodecallback);
michael@0 235 testDelete(nodecallback, 0, "<p>onetwo</p><ol><li>three</li></ol>", true);
michael@0 236
michael@0 237 nodecallback = function(){return editor.firstChild.childNodes[1].firstChild;};
michael@0 238 setupTest("<ol><li>one</li><li>two</li></ol><ol><li>three</li><li>four</li></ol>", 3, nodecallback);
michael@0 239 testDelete(function(){return editor.firstChild.childNodes[2].firstChild;},
michael@0 240 0, "<ol><li>one</li><li>two</li><li>three</li><li>four</li></ol>", true);
michael@0 241 /*todo_is(false, true, 'The above testDelete should use the same nodecallback' +
michael@0 242 'as in the proceeding setupTest: the cursor should stay at the end of "two", while currently it is at the beginning of "three" after delete');*/
michael@0 243
michael@0 244 // More Tests for Bug 507936
michael@0 245 nodecallback = function(){return editor.firstChild.firstChild.firstChild;}
michael@0 246 setupTest("<div><div>abcdef</div><div>bar</div><div>ghi</div></div>", 5, nodecallback);
michael@0 247 sel.extend(editor.lastChild.lastChild.lastChild, 1);
michael@0 248 testDelete(editor.lastChild.lastChild.lastChild, 5, "<div><div>abcdehi</div></div>", true);
michael@0 249
michael@0 250 setupTest("<div><div>abcdef</div><div>ghi</div></div>", 5, nodecallback);
michael@0 251 sel.extend(editor.lastChild.lastChild.lastChild, 1);
michael@0 252 testDelete(editor.lastChild.lastChild.lastChild, 5, "<div><div>abcdehi</div></div>", true);
michael@0 253
michael@0 254 nodecallback = function(){return editor.firstChild.firstChild;}
michael@0 255 setupTest("<div>abcdef<div><div>bar</div>ghi</div></div>", 5, nodecallback);
michael@0 256 sel.extend(editor.lastChild.lastChild.lastChild, 1);
michael@0 257 expectednodecallback = function(){return editor.lastChild.lastChild;}
michael@0 258 testDelete(expectednodecallback, 0, "<div>abcdehi</div>", true);
michael@0 259
michael@0 260 setupTest("<div>abcdef<div>ghi</div></div>", 5, nodecallback);
michael@0 261 sel.extend(editor.lastChild.lastChild.lastChild, 1);
michael@0 262 testDelete(expectednodecallback, 0, "<div>abcdehi</div>", true);
michael@0 263
michael@0 264 SimpleTest.finish();
michael@0 265 }
michael@0 266
michael@0 267 SimpleTest.waitForExplicitFinish();
michael@0 268 addLoadEvent(execTests);
michael@0 269 ]]>
michael@0 270 </script>
michael@0 271
michael@0 272 <body id="html_body" xmlns="http://www.w3.org/1999/xhtml">
michael@0 273 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=462188">Mozilla Bug 462188</a>
michael@0 274 <p id="display"></p>
michael@0 275
michael@0 276 <pre id="test">
michael@0 277 </pre>
michael@0 278 <iframe id="edit" width="200" height="100" src="about:blank"/>
michael@0 279 </body>
michael@0 280 </window>

mercurial