editor/libeditor/html/tests/test_contenteditable_focus.html

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 <html>
michael@0 2 <head>
michael@0 3 <title>Test for contenteditable focus</title>
michael@0 4 <script type="text/javascript"
michael@0 5 src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <link rel="stylesheet" type="text/css"
michael@0 7 href="/tests/SimpleTest/test.css" />
michael@0 8 </head>
michael@0 9 <body>
michael@0 10 <div id="display">
michael@0 11 First text in this document.<br>
michael@0 12 <input id="inputText" type="text"><br>
michael@0 13 <input id="inputTextReadonly" type="text" readonly><br>
michael@0 14 <input id="inputButton" type="button" value="input[type=button]"><br>
michael@0 15 <button id="button">button</button><br>
michael@0 16 <div id="editor" contenteditable="true">
michael@0 17 editable contents.<br>
michael@0 18 <input id="inputTextInEditor" type="text"><br>
michael@0 19 <input id="inputTextReadonlyInEditor" type="text" readonly><br>
michael@0 20 <input id="inputButtonInEditor" type="button" value="input[type=button]"><br>
michael@0 21 <button id="buttonInEditor">button</button><br>
michael@0 22 <div id="noeditableInEditor" contenteditable="false">
michael@0 23 <span id="spanInNoneditableInEditor">span element in noneditable in editor</span><br>
michael@0 24 <input id="inputTextInNoneditableInEditor" type="text"><br>
michael@0 25 <input id="inputTextReadonlyInNoneditableInEditor" type="text" readonly><br>
michael@0 26 <input id="inputButtonInNoneditableInEditor" type="button" value="input[type=button]"><br>
michael@0 27 <button id="buttonInNoneditableInEditor">button</button><br>
michael@0 28 </div>
michael@0 29 <span id="spanInEditor">span element in editor</span><br>
michael@0 30 </div>
michael@0 31 <div id="otherEditor" contenteditable="true">
michael@0 32 other editor.
michael@0 33 </div>
michael@0 34 </div>
michael@0 35 <div id="content" style="display: none">
michael@0 36
michael@0 37 </div>
michael@0 38 <pre id="test">
michael@0 39 </pre>
michael@0 40
michael@0 41 <script class="testbody" type="application/javascript">
michael@0 42
michael@0 43 SimpleTest.waitForExplicitFinish();
michael@0 44 SimpleTest.waitForFocus(runTests, window);
michael@0 45
michael@0 46 function runTests()
michael@0 47 {
michael@0 48 runTestsInternal();
michael@0 49 SimpleTest.finish();
michael@0 50 }
michael@0 51
michael@0 52 function runTestsInternal()
michael@0 53 {
michael@0 54 var fm = SpecialPowers.Cc["@mozilla.org/focus-manager;1"].
michael@0 55 getService(SpecialPowers.Ci.nsIFocusManager);
michael@0 56 // XXX using selCon for checking the visibility of the caret, however,
michael@0 57 // selCon is shared in document, cannot get the element of owner of the
michael@0 58 // caret from javascript?
michael@0 59 var selCon = SpecialPowers.wrap(window).
michael@0 60 QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
michael@0 61 getInterface(SpecialPowers.Ci.nsIWebNavigation).
michael@0 62 QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
michael@0 63 getInterface(SpecialPowers.Ci.nsISelectionDisplay).
michael@0 64 QueryInterface(SpecialPowers.Ci.nsISelectionController);
michael@0 65 var selection = window.getSelection();
michael@0 66
michael@0 67 var inputText = document.getElementById("inputText");
michael@0 68 var inputTextReadonly = document.getElementById("inputTextReadonly");
michael@0 69 var inputButton = document.getElementById("inputButton");
michael@0 70 var button = document.getElementById("button");
michael@0 71 var editor = document.getElementById("editor");
michael@0 72 var inputTextInEditor = document.getElementById("inputTextInEditor");
michael@0 73 var inputTextReadonlyInEditor = document.getElementById("inputTextReadonlyInEditor");
michael@0 74 var inputButtonInEditor = document.getElementById("inputButtonInEditor");
michael@0 75 var noeditableInEditor = document.getElementById("noeditableInEditor");
michael@0 76 var spanInNoneditableInEditor = document.getElementById("spanInNoneditableInEditor");
michael@0 77 var inputTextInNoneditableInEditor = document.getElementById("inputTextInNoneditableInEditor");
michael@0 78 var inputTextReadonlyInNoneditableInEditor = document.getElementById("inputTextReadonlyInNoneditableInEditor");
michael@0 79 var inputButtonInNoneditableInEditor = document.getElementById("inputButtonInNoneditableInEditor");
michael@0 80 var buttonInNoneditableInEditor = document.getElementById("buttonInNoneditableInEditor");
michael@0 81 var spanInEditor = document.getElementById("spanInEditor");
michael@0 82 var otherEditor = document.getElementById("otherEditor");
michael@0 83
michael@0 84 // XXX if there is a contenteditable element, HTML editor sets dom selection
michael@0 85 // to first editable node, but this makes inconsistency with normal document
michael@0 86 // behavior.
michael@0 87 todo_is(selection.rangeCount, 0, "unexpected selection range is there");
michael@0 88 ok(!selCon.caretVisible, "caret is visible in the document");
michael@0 89 // Move focus to inputTextInEditor
michael@0 90 inputTextInEditor.focus();
michael@0 91 is(SpecialPowers.unwrap(fm.focusedElement), inputTextInEditor,
michael@0 92 "inputTextInEditor didn't get focus");
michael@0 93 todo_is(selection.rangeCount, 0, "unexpected selection range is there");
michael@0 94 ok(selCon.caretVisible, "caret isn't visible in the inputTextInEditor");
michael@0 95 // Move focus to the editor
michael@0 96 editor.focus();
michael@0 97 is(SpecialPowers.unwrap(fm.focusedElement), editor,
michael@0 98 "editor didn't get focus");
michael@0 99 is(selection.rangeCount, 1,
michael@0 100 "there is no selection range when editor has focus");
michael@0 101 var range = selection.getRangeAt(0);
michael@0 102 ok(range.collapsed, "the selection range isn't collapsed");
michael@0 103 var startNode = range.startContainer;
michael@0 104 is(startNode.nodeType, 1, "the caret isn't set to the div node");
michael@0 105 is(startNode, editor, "the caret isn't set to the editor");
michael@0 106 ok(selCon.caretVisible, "caret isn't visible in the editor");
michael@0 107 // Move focus to other editor
michael@0 108 otherEditor.focus();
michael@0 109 is(SpecialPowers.unwrap(fm.focusedElement), otherEditor,
michael@0 110 "the other editor didn't get focus");
michael@0 111 is(selection.rangeCount, 1,
michael@0 112 "there is no selection range when the other editor has focus");
michael@0 113 range = selection.getRangeAt(0);
michael@0 114 ok(range.collapsed, "the selection range isn't collapsed");
michael@0 115 var startNode = range.startContainer;
michael@0 116 is(startNode.nodeType, 1, "the caret isn't set to the div node");
michael@0 117 is(startNode, otherEditor, "the caret isn't set to the other editor");
michael@0 118 ok(selCon.caretVisible, "caret isn't visible in the other editor");
michael@0 119 // Move focus to inputTextInEditor
michael@0 120 inputTextInEditor.focus();
michael@0 121 is(SpecialPowers.unwrap(fm.focusedElement), inputTextInEditor,
michael@0 122 "inputTextInEditor didn't get focus #2");
michael@0 123 is(selection.rangeCount, 1, "selection range is lost from the document");
michael@0 124 range = selection.getRangeAt(0);
michael@0 125 ok(range.collapsed, "the selection range isn't collapsed");
michael@0 126 var startNode = range.startContainer;
michael@0 127 is(startNode.nodeType, 1, "the caret isn't set to the div node");
michael@0 128 // XXX maybe, the caret can stay on the other editor if it's better.
michael@0 129 is(startNode, editor,
michael@0 130 "the caret should stay on the other editor");
michael@0 131 ok(selCon.caretVisible,
michael@0 132 "caret isn't visible in the inputTextInEditor");
michael@0 133 // Move focus to the other editor again
michael@0 134 otherEditor.focus();
michael@0 135 is(SpecialPowers.unwrap(fm.focusedElement), otherEditor,
michael@0 136 "the other editor didn't get focus #2");
michael@0 137 // Set selection to the span element in the editor (unfocused)
michael@0 138 range = document.createRange();
michael@0 139 range.setStart(spanInEditor.firstChild, 5);
michael@0 140 selection.removeAllRanges();
michael@0 141 selection.addRange(range);
michael@0 142 is(selection.rangeCount, 1, "selection range is lost from the document");
michael@0 143 is(SpecialPowers.unwrap(fm.focusedElement), otherEditor,
michael@0 144 "the other editor shouldn't lose focus by selection range change");
michael@0 145 ok(selCon.caretVisible, "caret isn't visible in inputTextInEditor");
michael@0 146 // Move focus to the editor
michael@0 147 editor.focus();
michael@0 148 is(SpecialPowers.unwrap(fm.focusedElement), editor,
michael@0 149 "the editor didn't get focus #2");
michael@0 150 is(selection.rangeCount, 1, "selection range is lost from the document");
michael@0 151 range = selection.getRangeAt(0);
michael@0 152 ok(range.collapsed, "the selection range isn't collapsed");
michael@0 153 is(range.startOffset, 5,
michael@0 154 "the caret is moved when the editor was focused (offset)");
michael@0 155 var startNode = range.startContainer;
michael@0 156 is(startNode.nodeType, 3, "the caret isn't in text node");
michael@0 157 is(startNode.parentNode, spanInEditor,
michael@0 158 "the caret is moved when the editor was focused (node)");
michael@0 159 ok(selCon.caretVisible, "caret isn't visible in the editor (spanInEditor)");
michael@0 160
michael@0 161 // Move focus to each focusable element in the editor.
michael@0 162 function testFocusMove(aSetFocusElementID, aFocusable, aCaretVisible)
michael@0 163 {
michael@0 164 editor.focus();
michael@0 165 is(SpecialPowers.unwrap(fm.focusedElement), editor,
michael@0 166 "testFocusMove: the editor didn't get focus at initializing (" +
michael@0 167 aSetFocusElementID + ")");
michael@0 168 var setFocusElement = document.getElementById(aSetFocusElementID);
michael@0 169 setFocusElement.focus();
michael@0 170 if (aFocusable) {
michael@0 171 is(SpecialPowers.unwrap(fm.focusedElement), setFocusElement,
michael@0 172 "testFocusMove: the " + aSetFocusElementID +
michael@0 173 " didn't get focus");
michael@0 174 } else {
michael@0 175 is(SpecialPowers.unwrap(fm.focusedElement), editor,
michael@0 176 "testFocusMove: the editor lost focus by focus() of the " +
michael@0 177 aSetFocusElementID);
michael@0 178 }
michael@0 179 if (aCaretVisible) {
michael@0 180 ok(selCon.caretVisible,
michael@0 181 "testFocusMove: caret isn't visible when the " +
michael@0 182 aSetFocusElementID + " has focus");
michael@0 183 } else {
michael@0 184 ok(!selCon.caretVisible,
michael@0 185 "testFocusMove: caret is visible when the " +
michael@0 186 aSetFocusElementID + " has focus");
michael@0 187 }
michael@0 188 }
michael@0 189 testFocusMove("inputTextInEditor", true, true);
michael@0 190 testFocusMove("inputTextReadonlyInEditor", true, true);
michael@0 191 // XXX shouldn't the caret become invisible?
michael@0 192 testFocusMove("inputButtonInEditor", true, true);
michael@0 193 testFocusMove("noeditableInEditor", false, true);
michael@0 194 testFocusMove("spanInNoneditableInEditor", false, true);
michael@0 195 testFocusMove("inputTextInNoneditableInEditor", true, true);
michael@0 196 testFocusMove("inputTextReadonlyInNoneditableInEditor", true, true);
michael@0 197 testFocusMove("inputButtonInNoneditableInEditor", true, false);
michael@0 198 testFocusMove("buttonInNoneditableInEditor", true, false);
michael@0 199 testFocusMove("spanInEditor", false, true);
michael@0 200 testFocusMove("inputText", true, true);
michael@0 201 testFocusMove("inputTextReadonly", true, true);
michael@0 202 testFocusMove("inputButton", true, false);
michael@0 203 testFocusMove("button", true, false);
michael@0 204 }
michael@0 205
michael@0 206 </script>
michael@0 207 </body>
michael@0 208
michael@0 209 </html>

mercurial