1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
<!doctype html>
<title>Editing event tests</title>
<style>body { font-family: serif }</style>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=test></div>
<div id=log></div>
<script>
"use strict";
var div = document.querySelector("#test");
add_completion_callback(function() { div.parentNode.removeChild(div) });
function copyEvent(e) {
var ret = {};
ret.original = e;
["type", "target", "currentTarget", "eventPhase", "bubbles", "cancelable",
"defaultPrevented", "isTrusted", "command", "value"].forEach(function(k) {
ret[k] = e[k];
});
return ret;
}
var tests = [
{
name: "Simple editable div",
html: "<div contenteditable>foo<b>bar</b>baz</div>",
initRange: function(range) {
range.setStart(div.querySelector("b").firstChild, 0);
range.setEnd(div.querySelector("b"), 1);
},
target: function() { return div.firstChild },
command: "bold",
value: "",
},
{
name: "Editable b",
html: "foo<b contenteditable>bar</b>baz",
initRange: function(range) {
range.setStart(div.querySelector("b").firstChild, 0);
range.setEnd(div.querySelector("b"), 1);
},
target: function() { return div.querySelector("b") },
command: "bold",
value: "",
},
{
name: "No editable content",
html: "foo<b>bar</b>baz",
initRange: function(range) {
range.setStart(div.querySelector("b").firstChild, 0);
range.setEnd(div.querySelector("b"), 1);
},
target: function() { return null },
command: "bold",
value: "",
},
{
name: "Partially-selected editable content",
html: "foo<b contenteditable>bar</b>baz",
initRange: function(range) {
range.setStart(div.querySelector("b").firstChild, 0);
range.setEnd(div, 3);
},
target: function() { return null },
command: "bold",
value: "",
},
{
name: "Selection spans two editing hosts",
html: "<div contenteditable>foo</div><div contenteditable>bar</div>",
initRange: function(range) {
range.setStart(div.querySelector("div").firstChild, 2);
range.setEnd(div.querySelector("div + div").firstChild, 1);
},
target: function() { return null },
command: "bold",
value: "",
},
{
name: "Selection includes two editing hosts",
html: "foo<div contenteditable>bar</div>baz<div contenteditable>quz</div>qoz",
initRange: function(range) {
range.setStart(div.firstChild, 2);
range.setEnd(div.lastChild, 1);
},
target: function() { return null },
command: "bold",
value: "",
},
{
name: "Changing selection from handler",
html: "<div contenteditable>foo</div><div contenteditable>bar</div>",
initRange: function(range) {
range.setStart(div.querySelector("div").firstChild, 0);
range.setEnd(div.querySelector("div").firstChild, 3);
},
target: function() { return div.firstChild },
finalTarget: function() { return div.lastChild },
command: "bold",
value: "",
},
];
var commandTests = {
backColor: ["green"],
createLink: ["http://www.w3.org/community/editing/"],
fontName: ["serif", "Helvetica"],
fontSize: ["6", "15px"],
foreColor: ["green"],
hiliteColor: ["green"],
italic: [],
removeFormat: [],
strikeThrough: [],
subscript: [],
superscript: [],
underline: [],
unlink: [],
delete: [],
formatBlock: ["p"],
forwardDelete: [],
indent: [],
insertHorizontalRule: ["id"],
insertHTML: ["<b>hi</b>"],
insertImage: ["../images/green.png"],
insertLineBreak: [],
insertOrderedList: [],
insertParagraph: [],
insertText: ["abc"],
insertUnorderedList: [],
justifyCenter: [],
justifyFull: [],
justifyLeft: [],
justifyRight: [],
outdent: [],
redo: [],
selectAll: [],
styleWithCSS: [],
undo: [],
useCSS: [],
};
Object.keys(commandTests).forEach(function(command) {
commandTests[command] = ["", "quasit"].concat(commandTests[command]);
commandTests[command].forEach(function(value) {
tests.push({
name: "Command " + command + ", value " + format_value(value),
html: "<div contenteditable>foo<b>bar</b>baz</div>",
initRange: function(range) {
range.setStart(div.querySelector("b").firstChild, 0);
range.setEnd(div.querySelector("b"), 1);
},
target: function() {
return ["redo", "selectAll", "styleWithCSS", "undo", "useCSS"]
.indexOf(command) == -1 ? div.firstChild : null;
},
command: command,
value: value,
});
});
});
tests.forEach(function(obj) {
// Kill all event handlers first
var newDiv = div.cloneNode(false);
div.parentNode.insertBefore(newDiv, div);
div.parentNode.removeChild(div);
div = newDiv;
div.innerHTML = obj.html;
var originalContents = div.cloneNode(true);
getSelection().removeAllRanges();
var range = document.createRange();
obj.initRange(range);
getSelection().addRange(range);
var target = obj.target();
var finalTarget = "finalTarget" in obj ? obj.finalTarget() : target;
var command = obj.command;
var value = obj.value;
var inputEvents = [];
div.addEventListener("input", function(e) { inputEvents.push(copyEvent(e)) });
var exception = null;
try {
document.execCommand(command, false, value);
} catch(e) {
exception = e;
}
test(function() {
assert_equals(exception, null, "Unexpected exception");
}, obj.name + ": execCommand() must not throw");
test(function() {
assert_equals(inputEvents.length, target ? 1 : 0,
"number of input events fired");
if (!target) {
assert_true(originalContents.isEqualNode(div),
"div contents must not be changed");
return;
}
var e = inputEvents[0];
assert_equals(e.type, "input", "event.type");
assert_equals(e.target, finalTarget, "event.target");
assert_equals(e.currentTarget, div, "event.currentTarget");
assert_equals(e.eventPhase, Event.BUBBLING_PHASE, "event.eventPhase");
assert_equals(e.bubbles, true, "event.bubbles");
assert_equals(e.cancelable, false, "event.cancelable");
assert_equals(e.defaultPrevented, false, "event.defaultPrevented");
assert_own_property(window, "InputEvent",
"window.InputEvent must exist");
assert_equals(Object.getPrototypeOf(e.original), InputEvent.prototype,
"event prototype");
assert_equals(e.isTrusted, true, "event.isTrusted");
}, obj.name + ": input event");
});
// Thanks, Gecko.
document.body.bgColor = "";
</script>
|