dom/imptests/editing/selecttest/test_collapse.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial