dom/imptests/editing/selecttest/test_collapseToStartEnd.html

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 <!doctype html>
michael@0 2 <title>Selection.collapseTo(Start|End)() 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 // Also test a selection with no ranges
michael@0 11 testRanges.unshift("[]");
michael@0 12
michael@0 13 for (var i = 0; i < testRanges.length; i++) {
michael@0 14 test(function() {
michael@0 15 selection.removeAllRanges();
michael@0 16 var endpoints = eval(testRanges[i]);
michael@0 17 if (!endpoints.length) {
michael@0 18 assert_throws("INVALID_STATE_ERR", function() {
michael@0 19 selection.collapseToStart();
michael@0 20 }, "Must throw InvalidStateErr if the selection's range is null");
michael@0 21 return;
michael@0 22 }
michael@0 23
michael@0 24 var addedRange = ownerDocument(endpoints[0]).createRange();
michael@0 25 addedRange.setStart(endpoints[0], endpoints[1]);
michael@0 26 addedRange.setEnd(endpoints[2], endpoints[3]);
michael@0 27 selection.addRange(addedRange);
michael@0 28
michael@0 29 // We don't penalize browsers here for mishandling addRange() and
michael@0 30 // adding a different range than we specified. They fail addRange()
michael@0 31 // tests for that, and don't have to fail collapseToStart/End() tests
michael@0 32 // too. They do fail if they throw unexpectedly, though. I also fail
michael@0 33 // them if there's no range at all, because otherwise they could pass
michael@0 34 // all tests if addRange() always does nothing and collapseToStart()
michael@0 35 // always throws.
michael@0 36 assert_equals(selection.rangeCount, 1,
michael@0 37 "Sanity check: rangeCount must equal 1 after addRange()");
michael@0 38
michael@0 39 var expectedEndpoint = [
michael@0 40 selection.getRangeAt(0).startContainer,
michael@0 41 selection.getRangeAt(0).startOffset
michael@0 42 ];
michael@0 43
michael@0 44 selection.collapseToStart();
michael@0 45
michael@0 46 assert_equals(selection.rangeCount, 1,
michael@0 47 "selection.rangeCount must equal 1");
michael@0 48 assert_equals(selection.focusNode, expectedEndpoint[0],
michael@0 49 "focusNode must equal the original start node");
michael@0 50 assert_equals(selection.focusOffset, expectedEndpoint[1],
michael@0 51 "focusOffset must equal the original start offset");
michael@0 52 assert_equals(selection.anchorNode, expectedEndpoint[0],
michael@0 53 "anchorNode must equal the original start node");
michael@0 54 assert_equals(selection.anchorOffset, expectedEndpoint[1],
michael@0 55 "anchorOffset must equal the original start offset");
michael@0 56 assert_equals(addedRange.startContainer, endpoints[0],
michael@0 57 "collapseToStart() must not change the startContainer of the selection's original range");
michael@0 58 assert_equals(addedRange.startOffset, endpoints[1],
michael@0 59 "collapseToStart() must not change the startOffset of the selection's original range");
michael@0 60 assert_equals(addedRange.endContainer, endpoints[2],
michael@0 61 "collapseToStart() must not change the endContainer of the selection's original range");
michael@0 62 assert_equals(addedRange.endOffset, endpoints[3],
michael@0 63 "collapseToStart() must not change the endOffset of the selection's original range");
michael@0 64 }, "Range " + i + " " + testRanges[i] + " collapseToStart()");
michael@0 65
michael@0 66 // Copy-paste of above
michael@0 67 test(function() {
michael@0 68 selection.removeAllRanges();
michael@0 69 var endpoints = eval(testRanges[i]);
michael@0 70 if (!endpoints.length) {
michael@0 71 assert_throws("INVALID_STATE_ERR", function() {
michael@0 72 selection.collapseToEnd();
michael@0 73 }, "Must throw InvalidStateErr if the selection's range is null");
michael@0 74 return;
michael@0 75 }
michael@0 76
michael@0 77 var addedRange = ownerDocument(endpoints[0]).createRange();
michael@0 78 addedRange.setStart(endpoints[0], endpoints[1]);
michael@0 79 addedRange.setEnd(endpoints[2], endpoints[3]);
michael@0 80 selection.addRange(addedRange);
michael@0 81
michael@0 82 // We don't penalize browsers here for mishandling addRange() and
michael@0 83 // adding a different range than we specified. They fail addRange()
michael@0 84 // tests for that, and don't have to fail collapseToStart/End() tests
michael@0 85 // too. They do fail if they throw unexpectedly, though. I also fail
michael@0 86 // them if there's no range at all, because otherwise they could pass
michael@0 87 // all tests if addRange() always does nothing and collapseToStart()
michael@0 88 // always throws.
michael@0 89 assert_equals(selection.rangeCount, 1,
michael@0 90 "Sanity check: rangeCount must equal 1 after addRange()");
michael@0 91
michael@0 92 var expectedEndpoint = [
michael@0 93 selection.getRangeAt(0).endContainer,
michael@0 94 selection.getRangeAt(0).endOffset
michael@0 95 ];
michael@0 96
michael@0 97 selection.collapseToEnd();
michael@0 98
michael@0 99 assert_equals(selection.rangeCount, 1,
michael@0 100 "selection.rangeCount must equal 1");
michael@0 101 assert_equals(selection.focusNode, expectedEndpoint[0],
michael@0 102 "focusNode must equal the original end node");
michael@0 103 assert_equals(selection.focusOffset, expectedEndpoint[1],
michael@0 104 "focusOffset must equal the original end offset");
michael@0 105 assert_equals(selection.anchorNode, expectedEndpoint[0],
michael@0 106 "anchorNode must equal the original end node");
michael@0 107 assert_equals(selection.anchorOffset, expectedEndpoint[1],
michael@0 108 "anchorOffset must equal the original end offset");
michael@0 109 assert_equals(addedRange.startContainer, endpoints[0],
michael@0 110 "collapseToEnd() must not change the startContainer of the selection's original range");
michael@0 111 assert_equals(addedRange.startOffset, endpoints[1],
michael@0 112 "collapseToEnd() must not change the startOffset of the selection's original range");
michael@0 113 assert_equals(addedRange.endContainer, endpoints[2],
michael@0 114 "collapseToEnd() must not change the endContainer of the selection's original range");
michael@0 115 assert_equals(addedRange.endOffset, endpoints[3],
michael@0 116 "collapseToEnd() must not change the endOffset of the selection's original range");
michael@0 117 }, "Range " + i + " " + testRanges[i] + " collapseToEnd()");
michael@0 118 }
michael@0 119
michael@0 120 testDiv.style.display = "none";
michael@0 121 </script>

mercurial