layout/generic/test/test_selection_preventDefault.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 <!DOCTYPE>
michael@0 2 <html>
michael@0 3 <head>
michael@0 4 <title>selection preventDefault test</title>
michael@0 5 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 6 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
michael@0 7 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
michael@0 8
michael@0 9 <style type="text/css">
michael@0 10 #fixedDiv1 {
michael@0 11 position: fixed;
michael@0 12 right: 0;
michael@0 13 overflow: scroll;
michael@0 14 width: 200px;
michael@0 15 top: 0;
michael@0 16 }
michael@0 17 input {
michael@0 18 font-size: 16px;
michael@0 19 height: 16px;
michael@0 20 width: 80px;
michael@0 21 margin: 0;
michael@0 22 padding: 0;
michael@0 23 }
michael@0 24 </style>
michael@0 25
michael@0 26 </head>
michael@0 27 <body>
michael@0 28 <input id="input" type="text" value="iiiiiiiii iiiiiiiii iiiiiiiii">
michael@0 29 <div id="fixedDiv1" class="testingDiv">
michael@0 30 dddddd dddddd dddddd
michael@0 31 </div>
michael@0 32 <pre id="test">
michael@0 33 <script class="testbody" type="text/javascript">
michael@0 34
michael@0 35 var fixedDiv1 = document.getElementById("fixedDiv1");
michael@0 36 var input = document.getElementById("input");
michael@0 37
michael@0 38 function test()
michael@0 39 {
michael@0 40 function getSelectionForEditor(aEditorElement)
michael@0 41 {
michael@0 42 const nsIDOMNSEditableElement =
michael@0 43 Components.interfaces.nsIDOMNSEditableElement;
michael@0 44 return aEditorElement.QueryInterface(nsIDOMNSEditableElement).editor.selection;
michael@0 45 }
michael@0 46
michael@0 47 function clear()
michael@0 48 {
michael@0 49 var sel = window.getSelection();
michael@0 50 if (sel.rangeCount > 0)
michael@0 51 sel.collapseToEnd();
michael@0 52 sel = getSelectionForEditor(input);
michael@0 53 if (sel.rangeCount > 0)
michael@0 54 sel.collapseToEnd();
michael@0 55 }
michael@0 56
michael@0 57 const kFalse = 0;
michael@0 58 const kTrue = 1;
michael@0 59 const kToDo = 2;
michael@0 60
michael@0 61 function check(aFixedDiv1ShouldBeSelected,
michael@0 62 aInputShouldBeSelected,
michael@0 63 aTestingDescription)
michael@0 64 {
michael@0 65 function checkCharacter(aSelectedText,
michael@0 66 aShouldBeIncludedCharacter,
michael@0 67 aSouldBeSelected,
michael@0 68 aElementName)
michael@0 69 {
michael@0 70 var boolvalue = aSouldBeSelected & kTrue;
michael@0 71 var f = aSouldBeSelected & kToDo ? todo : ok;
michael@0 72 var str = aSelectedText.replace('\n', '\\n');
michael@0 73 if (boolvalue) {
michael@0 74 f(aSelectedText.indexOf(aShouldBeIncludedCharacter) >= 0,
michael@0 75 "The contents of " + aElementName +
michael@0 76 " aren't selected (" + aTestingDescription +
michael@0 77 "): Selected String: \"" + str + "\"");
michael@0 78 } else {
michael@0 79 f(aSelectedText.indexOf(aShouldBeIncludedCharacter) < 0,
michael@0 80 "The contents of " + aElementName +
michael@0 81 " are selected (" + aTestingDescription +
michael@0 82 "): Selected String: \"" + str + "\"");
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 var sel = window.getSelection().toString();
michael@0 87 checkCharacter(sel, "d", aFixedDiv1ShouldBeSelected, "fixedDiv1");
michael@0 88
michael@0 89 // input contents must not be included on the parent
michael@0 90 // selection.
michael@0 91 checkCharacter(sel, "i", false, "input (checking on parent)");
michael@0 92
michael@0 93 var selInput = getSelectionForEditor(input).toString();
michael@0 94 checkCharacter(selInput, "i", aInputShouldBeSelected, "input");
michael@0 95 }
michael@0 96
michael@0 97 function eventHandler(evt) {
michael@0 98 evt.preventDefault();
michael@0 99 }
michael@0 100
michael@0 101 // prevent default action on mousedown should prevent selection
michael@0 102 fixedDiv1.addEventListener("mousedown", eventHandler);
michael@0 103 synthesizeMouse(fixedDiv1, 30, 5, { type: "mousedown" });
michael@0 104 synthesizeMouse(fixedDiv1, 40, 5, { type: "mousemove" });
michael@0 105 synthesizeMouse(fixedDiv1, 40, 5, { type: "mouseup" });
michael@0 106 check(kFalse, kFalse, "fixedDiv1-fixedDiv1-mousedown");
michael@0 107 clear();
michael@0 108
michael@0 109 input.addEventListener("mousedown", eventHandler);
michael@0 110 synthesizeMouse(input, 20, 5, { type: "mousedown" });
michael@0 111 synthesizeMouse(input, 40, 5, { type: "mousemove" });
michael@0 112 synthesizeMouse(input, 40, 5, { type: "mouseup" });
michael@0 113 check(kFalse, kFalse, "input-input-mousedown");
michael@0 114 clear();
michael@0 115
michael@0 116 // clean up mousedown listener
michael@0 117 [fixedDiv1, input].forEach(function(element) {
michael@0 118 element.removeEventListener("mousedown", eventHandler);
michael@0 119 });
michael@0 120
michael@0 121 // prevent default action on mouseup should not affect the selection state
michael@0 122 fixedDiv1.addEventListener("mouseup", eventHandler);
michael@0 123 synthesizeMouse(fixedDiv1, 30, 5, { type: "mousedown" });
michael@0 124 synthesizeMouse(fixedDiv1, 40, 5, { type: "mousemove" });
michael@0 125 synthesizeMouse(fixedDiv1, 40, 5, { type: "mouseup" });
michael@0 126 check(kTrue, kFalse, "fixedDiv1-fixedDiv1-mouseup");
michael@0 127 clear();
michael@0 128
michael@0 129 input.addEventListener("mouseup", eventHandler);
michael@0 130 synthesizeMouse(input, 20, 5, { type: "mousedown" });
michael@0 131 synthesizeMouse(input, 40, 5, { type: "mousemove" });
michael@0 132 synthesizeMouse(input, 40, 5, { type: "mouseup" });
michael@0 133 check(kFalse, kTrue, "input-input-mouseup");
michael@0 134 clear();
michael@0 135
michael@0 136 [fixedDiv1, input].forEach(function(element) {
michael@0 137 element.removeEventListener("mouseup", eventHandler);
michael@0 138 });
michael@0 139
michael@0 140 // touchmove event should not affect the selection state
michael@0 141 synthesizeTouch(fixedDiv1, 30, 5, { type: "touchstart" });
michael@0 142 synthesizeTouch(fixedDiv1, 40, 5, { type: "touchmove" });
michael@0 143 check(kFalse, kFalse, "fixedDiv1-fixedDiv1-touchmove");
michael@0 144 synthesizeTouch(fixedDiv1, 40, 5, { type: "touchend" });
michael@0 145 clear();
michael@0 146
michael@0 147 synthesizeTouch(input, 20, 5, { type: "touchstart" });
michael@0 148 synthesizeTouch(input, 40, 5, { type: "touchmove" });
michael@0 149 check(kFalse, kFalse, "input-input-touchmove");
michael@0 150 synthesizeTouch(input, 40, 5, { type: "touchend" });
michael@0 151 clear();
michael@0 152
michael@0 153 fixedDiv1.addEventListener("touchmove", eventHandler);
michael@0 154 synthesizeTouch(fixedDiv1, 30, 5, { type: "touchstart" });
michael@0 155 synthesizeTouch(fixedDiv1, 40, 5, { type: "touchmove" });
michael@0 156 check(kFalse, kFalse, "fixedDiv1-fixedDiv1-touchmove-preventDefault");
michael@0 157 synthesizeTouch(fixedDiv1, 40, 5, { type: "touchend" });
michael@0 158 clear();
michael@0 159
michael@0 160 input.addEventListener("touchmove", eventHandler);
michael@0 161 synthesizeTouch(input, 20, 5, { type: "touchstart" });
michael@0 162 synthesizeTouch(input, 40, 5, { type: "touchmove" });
michael@0 163 check(kFalse, kFalse, "input-input-touchmove-preventDefault");
michael@0 164 synthesizeTouch(input, 40, 5, { type: "touchend" });
michael@0 165 clear();
michael@0 166
michael@0 167 SimpleTest.finish();
michael@0 168 }
michael@0 169 window.onload = function() { setTimeout(test, 0); };
michael@0 170 SimpleTest.waitForExplicitFinish();
michael@0 171 </script>
michael@0 172 </pre>
michael@0 173 </body>
michael@0 174 </html>

mercurial