gecko-dev/dom/html/test/forms/test_set_range_text.html

246 строки
9.5 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=850364
-->
<head>
<title>Tests for Bug 850364 && Bug 918940</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=850364">Mozilla Bug 850364</a>
<p id="display"></p>
<div id="content">
<!-- "SetRangeText() supported types"-->
<input type="text" id="input_text"></input>
<input type="search" id="input_search"></input>
<input type="url" id="input_url"></input>
<input type="tel" id="input_tel"></input>
<input type="password" id="input_password"></input>
<input type="number" id="input_number"></input>
<textarea id="input_textarea"></textarea>
<!-- "SetRangeText() non-supported types" -->
<input type="button" id="input_button"></input>
<input type="submit" id="input_submit"></input>
<input type="image" id="input_image"></input>
<input type="reset" id="input_reset"></input>
<input type="radio" id="input_radio"></input>
<input type="checkbox" id="input_checkbox"></input>
<input type="range" id="input_range"></input>
<input type="file" id="input_file"></input>
<input type="email" id="input_email"></input>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Tests for Bug 850364 && Bug 918940**/
var SupportedTypes = ["text", "search", "url", "tel", "password", "textarea", "number"];
var NonSupportedTypes = ["button", "submit", "image", "reset", "radio",
"checkbox", "range", "file", "email"];
SimpleTest.waitForExplicitFinish();
function TestInputs() {
var opThrows, elem, i, msg;
//Non-supported types should throw
for (i = 0; i < NonSupportedTypes.length; ++i) {
opThrows = false;
msg = "input_" + NonSupportedTypes[i];
elem = document.getElementById(msg);
elem.focus();
try {
elem.setRangeText("abc");
} catch (ex) {
opThrows = true;
}
ok(opThrows, msg + " should throw NotSupportedError");
}
var numOfSelectCalls = 0, expectedNumOfSelectCalls = 0;
//Supported types should not throw
for (i = 0; i < SupportedTypes.length; ++i) {
opThrows = false;
msg = "input_" + SupportedTypes[i];
elem = document.getElementById(msg);
elem.focus();
try {
elem.setRangeText("abc");
expectedNumOfSelectCalls += 1;
} catch (ex) {
opThrows = true;
}
is(opThrows, false, msg + " should not throw NotSupportedError");
elem.addEventListener("select", function (aEvent) {
ok(true, "select event should be fired for " + aEvent.target.id);
if (++numOfSelectCalls == expectedNumOfSelectCalls) {
SimpleTest.finish();
} else if (numOfSelectCalls > expectedNumOfSelectCalls) {
ok(false, "Too many select events were fired");
}
}, false);
elem.addEventListener("input", function (aEvent) {
ok(false, "input event should NOT be fired for " + + aEvent.target.id);
}, false);
var test = " setRange(replacement), shrink";
elem.value = "0123456789123456";
elem.setSelectionRange(1, 6);
elem.setRangeText("999");
is(elem.value, "09996789123456", msg + test);
is(elem.selectionStart, 1, msg + test);
is(elem.selectionEnd, 4, msg + test);
elem.setRangeText("222");
is(elem.value, "02226789123456", msg + test);
expectedNumOfSelectCalls += 3;
test = " setRange(replacement), expand";
elem.value = "0123456789123456";
elem.setSelectionRange(1, 2);
elem.setRangeText("999");
is(elem.value, "099923456789123456", msg + test);
is(elem.selectionStart, 1, msg + test);
is(elem.selectionEnd, 4, msg + test);
elem.setRangeText("222");
is(elem.value, "022223456789123456", msg + test);
expectedNumOfSelectCalls += 3;
test = " setRange(replacement) pure insertion at start";
elem.value = "0123456789123456";
elem.setSelectionRange(0, 0);
elem.setRangeText("999");
is(elem.value, "9990123456789123456", msg + test);
is(elem.selectionStart, 0, msg + test);
is(elem.selectionEnd, 0, msg + test);
elem.setRangeText("222");
is(elem.value, "2229990123456789123456", msg + test);
expectedNumOfSelectCalls += 3;
test = " setRange(replacement) pure insertion in the middle";
elem.value = "0123456789123456";
elem.setSelectionRange(4, 4);
elem.setRangeText("999");
is(elem.value, "0123999456789123456", msg + test);
is(elem.selectionStart, 4, msg + test);
is(elem.selectionEnd, 4, msg + test);
elem.setRangeText("222");
is(elem.value, "0123222999456789123456", msg + test);
expectedNumOfSelectCalls += 3;
test = " setRange(replacement) pure insertion at the end";
elem.value = "1123456789123456";
elem.setSelectionRange(16, 16);
elem.setRangeText("999");
is(elem.value, "1123456789123456999", msg + test);
is(elem.selectionStart, 16, msg + test);
is(elem.selectionEnd, 16, msg + test);
elem.setRangeText("222");
is(elem.value, "1123456789123456222999", msg + test);
expectedNumOfSelectCalls += 3;
//test SetRange(replacement, start, end, mode) with start > end
try {
elem.setRangeText("abc", 20, 4);
} catch (ex) {
opThrows = (ex.name == "IndexSizeError" && ex.code == DOMException.INDEX_SIZE_ERR);
}
is(opThrows, true, msg + " should throw IndexSizeError");
//test SelectionMode 'select'
elem.value = "1023456789123456";
elem.setRangeText("999", 4, 9, "select");
is(elem.value, "10239999123456", msg + ".value == \"10239999123456\"");
is(elem.selectionStart, 4, msg + ".selectionStart == 4, with \"select\"");
is(elem.selectionEnd, 7, msg + ".selectionEnd == 7, with \"select\"");
expectedNumOfSelectCalls += 1;
elem.setRangeText("888", 6, 25, "select");
is(elem.value, "102399888", msg + ".value == \"102399888\"");
is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"select\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"select\"");
expectedNumOfSelectCalls += 1;
//test SelectionMode 'start'
elem.value = "0123456789123456";
elem.setRangeText("999", 4, 9, "start");
is(elem.value, "01239999123456", msg + ".value == \"01239999123456\"");
is(elem.selectionStart, 4, msg + ".selectionStart == 4, with \"start\"");
is(elem.selectionEnd, 4, msg + ".selectionEnd == 4, with \"start\"");
expectedNumOfSelectCalls += 1;
elem.setRangeText("888", 6, 25, "start");
is(elem.value, "012399888", msg + ".value == \"012399888\"");
is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"start\"");
is(elem.selectionEnd, 6, msg + ".selectionEnd == 6, with \"start\"");
expectedNumOfSelectCalls += 1;
//test SelectionMode 'end'
elem.value = "1023456789123456";
elem.setRangeText("999", 4, 9, "end");
is(elem.value, "10239999123456", msg + ".value == \"10239999123456\"");
is(elem.selectionStart, 7, msg + ".selectionStart == 7, with \"end\"");
is(elem.selectionEnd, 7, msg + ".selectionEnd == 7, with \"end\"");
expectedNumOfSelectCalls += 1;
elem.setRangeText("888", 6, 25, "end");
is(elem.value, "102399888", msg + ".value == \"102399888\"");
is(elem.selectionStart, 9, msg + ".selectionStart == 9, with \"end\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"end\"");
expectedNumOfSelectCalls += 1;
//test SelectionMode 'preserve' (default)
//subcase: selection{Start|End} > end
elem.value = "0123456789";
elem.setSelectionRange(6, 9);
elem.setRangeText("7", 1, 2, "preserve");
is(elem.value, "0723456789", msg + ".value == \"0723456789\"");
is(elem.selectionStart, 6, msg + ".selectionStart == 6, with \"preserve\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"preserve\"");
expectedNumOfSelectCalls += 2;
//subcase: selection{Start|End} < end
elem.value = "0123456789";
elem.setSelectionRange(4, 5);
elem.setRangeText("3456", 2, 9, "preserve");
is(elem.value, "0134569", msg + ".value == \"0134569\"");
is(elem.selectionStart, 2, msg + ".selectionStart == 2, with \"preserve\"");
is(elem.selectionEnd, 6, msg + ".selectionEnd == 6, with \"preserve\"");
expectedNumOfSelectCalls += 2;
//subcase: selectionStart > end, selectionEnd < end
elem.value = "0123456789";
elem.setSelectionRange(8, 4);
elem.setRangeText("3456", 1, 5);
is(elem.value, "0345656789", msg + ".value == \"0345656789\"");
is(elem.selectionStart, 1, msg + ".selectionStart == 1, with \"default\"");
is(elem.selectionEnd, 5, msg + ".selectionEnd == 5, with \"default\"");
expectedNumOfSelectCalls += 2;
//subcase: selectionStart < end, selectionEnd > end
elem.value = "0123456789";
elem.setSelectionRange(4, 9);
elem.setRangeText("3456", 2, 6);
is(elem.value, "0134566789", msg + ".value == \"0134566789\"");
is(elem.selectionStart, 2, msg + ".selectionStart == 2, with \"default\"");
is(elem.selectionEnd, 9, msg + ".selectionEnd == 9, with \"default\"");
expectedNumOfSelectCalls += 2;
}
}
addLoadEvent(TestInputs);
</script>
</pre>
</body>
</html>