<!doctypehtml>
<title>Selection direction tests</title>
<meta charset=utf-8>
<div id=test>
<p>This is a manual test, since there's no way to synthesize keyboard or
mouse input. Click after the letter "c" in the following paragraph and
drag backwards so that both the "b" and the "c" are highlighted, then click
the "Test"button:
function testDirection() { var testDiv = document.getElementById("test"); var p = testDiv.getElementsByTagName("p")[1].firstChild; var selection = getSelection(); var range = selection.getRangeAt(0);
test(function() {
assert_equals(range.toString(), "bc");
}, "The expected range is selected");
test(function() {
assert_equals(selection.anchorNode, p);
assert_equals(selection.focusNode, p);
}, "Expected node is initially selected");
test(function() {
assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [1, 3]);
}, "Expected offsets are initially selected (maybe not in order)");
test(function() {
assert_equals(selection.anchorOffset, 3);
assert_equals(selection.focusOffset, 1);
}, "Offsets are backwards for initial selection"),
test(function() {
assert_equals(selection.anchorNode, range.endContainer);
assert_equals(selection.anchorOffset, range.endOffset);
assert_equals(selection.focusNode, range.startContainer);
assert_equals(selection.focusOffset, range.startOffset);
}, "Offsets match the range for initial selection");
// Per spec, the direction of the selection remains even if you zap a range
// and add a new one.
test(function() {
selection.removeRange(range);
range = document.createRange();
p = testDiv.getElementsByTagName("p")[0].firstChild;
range.setStart(p, 0);
range.setEnd(p, 4);
assert_equals(range.toString(), "This");
selection.addRange(range);
}, "removeRange()/addRange() successful");
test(function() {
assert_equals(selection.anchorNode, p);
assert_equals(selection.focusNode, p);
}, "Expected node is selected after remove/addRange()");
test(function() {
assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [0, 4]);
}, "Expected offsets are selected after remove/addRange() (maybe not in order)");
test(function() {
assert_equals(selection.anchorOffset, 4);
assert_equals(selection.focusOffset, 0);
}, "Offsets are backwards after remove/addRange()"),
test(function() {
assert_equals(selection.anchorNode, range.endContainer);
assert_equals(selection.anchorOffset, range.endOffset);
assert_equals(selection.focusNode, range.startContainer);
assert_equals(selection.focusOffset, range.startOffset);
}, "Offsets match the range after remove/addRange()");
// But if you call removeAllRanges(), the direction should reset to
// forwards.
test(function() {
selection.removeAllRanges();
range = document.createRange();
p = testDiv.getElementsByTagName("p")[2].firstChild;
range.setStart(p, 2);
range.setEnd(p, 5);
assert_equals(range.toString(), "ghi");
selection.addRange(range);
}, "removeAllRanges() successful");
test(function() {
assert_equals(selection.anchorNode, p);
assert_equals(selection.focusNode, p);
}, "Expected node is selected after removeAllRanges()");
test(function() {
assert_array_equals([selection.anchorOffset, selection.focusOffset].sort(), [2, 5]);
}, "Expected offsets are selected after removeAllRanges() (maybe not in order)");
test(function() {
assert_equals(selection.anchorOffset, 2);
assert_equals(selection.focusOffset, 5);
}, "Offsets are forwards after removeAllRanges()");
test(function() {
assert_equals(selection.anchorNode, range.startContainer);
assert_equals(selection.anchorOffset, range.startOffset);
assert_equals(selection.focusNode, range.endContainer);
assert_equals(selection.focusOffset, range.endOffset);
}, "Offsets match the range after removeAllRanges()");
done();
}
</script>
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-04-26)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.