Test removeAllRanges
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Mon, 24 Oct 2011 14:28:53 -0600
changeset 649 44db956023f9
parent 648 210a670d6227
child 650 56ef67010340
Test removeAllRanges
selecttest/common.js
selecttest/removeAllRanges.html
--- a/selecttest/common.js	Mon Oct 24 14:15:17 2011 -0600
+++ b/selecttest/common.js	Mon Oct 24 14:28:53 2011 -0600
@@ -912,8 +912,7 @@
 
 /**
  * Given an array of endpoint data [start container, start offset, end
- * container, end offset], returns a Range with those endpoints.  Uses
- * addRange, so the range will always be forwards.
+ * container, end offset], returns a Range with those endpoints.
  */
 function rangeFromEndpoints(endpoints) {
 	// If we just use document instead of the ownerDocument of endpoints[0],
@@ -924,3 +923,30 @@
 	range.setEnd(endpoints[2], endpoints[3]);
 	return range;
 }
+
+/**
+ * Given an array of endpoint data [start container, start offset, end
+ * container, end offset], sets the selection to have those endpoints.  Uses
+ * addRange, so the range will be forwards.  Accepts an empty array for
+ * endpoints, in which case the selection will just be emptied.
+ */
+function setSelectionForwards(endpoints) {
+	selection.removeAllRanges();
+	if (endpoints.length) {
+		selection.addRange(rangeFromEndpoints(endpoints));
+	}
+}
+
+/**
+ * Given an array of endpoint data [start container, start offset, end
+ * container, end offset], sets the selection to have those endpoints, with the
+ * direction backwards.  Uses extend, so it will throw in IE.  Accepts an empty
+ * array for endpoints, in which case the selection will just be emptied.
+ */
+function setSelectionBackwards(endpoints) {
+	selection.removeAllRanges();
+	if (endpoints.length) {
+		selection.collapse(endpoints[2], endpoints[3]);
+		selection.extend(endpoints[0], endpoints[1]);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/selecttest/removeAllRanges.html	Mon Oct 24 14:28:53 2011 -0600
@@ -0,0 +1,45 @@
+<!doctype html>
+<title>Selection.removeAllRanges() tests</title>
+<div id=log></div>
+<script src=http://w3c-test.org/resources/testharness.js></script>
+<script src=http://w3c-test.org/resources/testharnessreport.js></script>
+<script src=common.js></script>
+<script>
+"use strict";
+
+// Also test a selection with no ranges
+testRanges.unshift("[]");
+
+var range = rangeFromEndpoints([paras[0].firstChild, 0, paras[0].firstChild, 1]);
+
+for (var i = 0; i < testRanges.length; i++) {
+	test(function() {
+		setSelectionForwards(eval(testRanges[i]));
+		selection.removeAllRanges();
+		assert_equals(selection.rangeCount, 0,
+			"After removeAllRanges(), rangeCount must be 0");
+		// Test that it's forwards
+		selection.addRange(range);
+		assert_equals(selection.anchorOffset, selection.getRangeAt(0).startOffset,
+			"After removeAllRanges(), addRange() must be forwards, so anchorOffset must equal startOffset rather than endOffset");
+		assert_equals(selection.focusOffset, selection.getRangeAt(0).endOffset,
+			"After removeAllRanges(), addRange() must be forwards, so focusOffset must equal endOffset rather than startOffset");
+	}, "Range " + i + " " + testRanges[i] + " forwards");
+
+	// Copy-pasted from above
+	test(function() {
+		setSelectionBackwards(eval(testRanges[i]));
+		selection.removeAllRanges();
+		assert_equals(selection.rangeCount, 0,
+			"After removeAllRanges(), rangeCount must be 0");
+		// Test that it's forwards
+		selection.addRange(range);
+		assert_equals(selection.anchorOffset, selection.getRangeAt(0).startOffset,
+			"After removeAllRanges(), addRange() must be forwards, so anchorOffset must equal startOffset rather than endOffset");
+		assert_equals(selection.focusOffset, selection.getRangeAt(0).endOffset,
+			"After removeAllRanges(), addRange() must be forwards, so focusOffset must equal endOffset rather than startOffset");
+	}, "Range " + i + " " + testRanges[i] + " backwards");
+}
+
+testDiv.style.display = "none";
+</script>