Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 <!doctype html>
2 <title>Selection.collapse() tests</title>
3 <div id=log></div>
4 <script src=/resources/testharness.js></script>
5 <script src=/resources/testharnessreport.js></script>
6 <script src=common.js></script>
7 <script>
8 "use strict";
10 function testCollapse(range, point) {
11 selection.removeAllRanges();
12 var addedRange;
13 if (range) {
14 addedRange = range.cloneRange();
15 selection.addRange(addedRange);
16 }
18 if (point[0].nodeType == Node.DOCUMENT_TYPE_NODE) {
19 assert_throws("INVALID_NODE_TYPE_ERR", function() {
20 selection.collapse(point[0], point[1]);
21 }, "Must throw INVALID_NODE_TYPE_ERR when collapse()ing if the node is a DocumentType");
22 return;
23 }
25 if (point[1] < 0 || point[1] > getNodeLength(point[0])) {
26 assert_throws("INDEX_SIZE_ERR", function() {
27 selection.collapse(point[0], point[1]);
28 }, "Must throw INDEX_SIZE_ERR when collapse()ing if the offset is negative or greater than the node's length");
29 return;
30 }
32 selection.collapse(point[0], point[1]);
34 assert_equals(selection.rangeCount, 1,
35 "selection.rangeCount must equal 1 after collapse()");
36 assert_equals(selection.focusNode, point[0],
37 "focusNode must equal the node we collapse()d to");
38 assert_equals(selection.focusOffset, point[1],
39 "focusOffset must equal the offset we collapse()d to");
40 assert_equals(selection.focusNode, selection.anchorNode,
41 "focusNode and anchorNode must be equal after collapse()");
42 assert_equals(selection.focusOffset, selection.anchorOffset,
43 "focusOffset and anchorOffset must be equal after collapse()");
44 if (range) {
45 assert_equals(addedRange.startContainer, range.startContainer,
46 "collapse() must not change the startContainer of a preexisting Range");
47 assert_equals(addedRange.endContainer, range.endContainer,
48 "collapse() must not change the endContainer of a preexisting Range");
49 assert_equals(addedRange.startOffset, range.startOffset,
50 "collapse() must not change the startOffset of a preexisting Range");
51 assert_equals(addedRange.endOffset, range.endOffset,
52 "collapse() must not change the endOffset of a preexisting Range");
53 }
54 }
56 // Also test a selection with no ranges
57 testRanges.unshift("[]");
59 // Don't want to eval() each point a bazillion times
60 var testPointsCached = [];
61 for (var i = 0; i < testPoints.length; i++) {
62 testPointsCached.push(eval(testPoints[i]));
63 }
65 var tests = [];
66 for (var i = 0; i < testRanges.length; i++) {
67 var endpoints = eval(testRanges[i]);
68 var range;
69 test(function() {
70 if (endpoints.length) {
71 range = ownerDocument(endpoints[0]).createRange();
72 range.setStart(endpoints[0], endpoints[1]);
73 range.setEnd(endpoints[2], endpoints[3]);
74 } else {
75 // Empty selection
76 range = null;
77 }
78 }, "Set up range " + i + " " + testRanges[i]);
79 for (var j = 0; j < testPoints.length; j++) {
80 tests.push(["Range " + i + " " + testRanges[i] + ", point " + j + " " + testPoints[j], range, testPointsCached[j]]);
81 }
82 }
84 generate_tests(testCollapse, tests);
86 testDiv.style.display = "none";
87 </script>