Tue, 06 Jan 2015 21:39:09 +0100
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 extend() tests</title> |
michael@0 | 3 | <meta charset=utf-8> |
michael@0 | 4 | <body> |
michael@0 | 5 | <script src=/resources/testharness.js></script> |
michael@0 | 6 | <script src=/resources/testharnessreport.js></script> |
michael@0 | 7 | <script src=common.js></script> |
michael@0 | 8 | <div id=log></div> |
michael@0 | 9 | <script> |
michael@0 | 10 | "use strict"; |
michael@0 | 11 | |
michael@0 | 12 | // Also test a selection with no ranges |
michael@0 | 13 | testRanges.unshift("[]"); |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * We test Selections that go both forwards and backwards here. In the latter |
michael@0 | 17 | * case we need to use extend() to force it to go backwards, which is fair |
michael@0 | 18 | * enough, since that's what we're testing. We test collapsed selections only |
michael@0 | 19 | * once. |
michael@0 | 20 | */ |
michael@0 | 21 | for (var i = 0; i < testRanges.length; i++) { |
michael@0 | 22 | var endpoints = eval(testRanges[i]); |
michael@0 | 23 | for (var j = 0; j < testPoints.length; j++) { |
michael@0 | 24 | if (endpoints[0] == endpoints[2] |
michael@0 | 25 | && endpoints[1] == endpoints[3]) { |
michael@0 | 26 | // Test collapsed selections only once |
michael@0 | 27 | test(function() { |
michael@0 | 28 | setSelectionForwards(endpoints); |
michael@0 | 29 | testExtend(endpoints, eval(testPoints[j])); |
michael@0 | 30 | }, "extend() with range " + i + " " + testRanges[i] |
michael@0 | 31 | + " and point " + j + " " + testPoints[j]); |
michael@0 | 32 | } else { |
michael@0 | 33 | test(function() { |
michael@0 | 34 | setSelectionForwards(endpoints); |
michael@0 | 35 | testExtend(endpoints, eval(testPoints[j])); |
michael@0 | 36 | }, "extend() forwards with range " + i + " " + testRanges[i] |
michael@0 | 37 | + " and point " + j + " " + testPoints[j]); |
michael@0 | 38 | |
michael@0 | 39 | test(function() { |
michael@0 | 40 | setSelectionBackwards(endpoints); |
michael@0 | 41 | testExtend(endpoints, eval(testPoints[j])); |
michael@0 | 42 | }, "extend() backwards with range " + i + " " + testRanges[i] |
michael@0 | 43 | + " and point " + j + " " + testPoints[j]); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function testExtend(endpoints, target) { |
michael@0 | 49 | assert_equals(getSelection().rangeCount, endpoints.length/4, |
michael@0 | 50 | "Sanity check: rangeCount must be correct"); |
michael@0 | 51 | |
michael@0 | 52 | var node = target[0]; |
michael@0 | 53 | var offset = target[1]; |
michael@0 | 54 | |
michael@0 | 55 | // "If the context object's range is null, throw an InvalidStateError |
michael@0 | 56 | // exception and abort these steps." |
michael@0 | 57 | if (getSelection().rangeCount == 0) { |
michael@0 | 58 | assert_throws("INVALID_STATE_ERR", function() { |
michael@0 | 59 | selection.extend(node, offset); |
michael@0 | 60 | }, "extend() when rangeCount is 0 must throw InvalidStateError"); |
michael@0 | 61 | return; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | assert_equals(getSelection().getRangeAt(0).startContainer, endpoints[0], |
michael@0 | 65 | "Sanity check: startContainer must be correct"); |
michael@0 | 66 | assert_equals(getSelection().getRangeAt(0).startOffset, endpoints[1], |
michael@0 | 67 | "Sanity check: startOffset must be correct"); |
michael@0 | 68 | assert_equals(getSelection().getRangeAt(0).endContainer, endpoints[2], |
michael@0 | 69 | "Sanity check: endContainer must be correct"); |
michael@0 | 70 | assert_equals(getSelection().getRangeAt(0).endOffset, endpoints[3], |
michael@0 | 71 | "Sanity check: endOffset must be correct"); |
michael@0 | 72 | |
michael@0 | 73 | // "Let anchor and focus be the context object's anchor and focus, and let |
michael@0 | 74 | // new focus be the boundary point (node, offset)." |
michael@0 | 75 | var anchorNode = getSelection().anchorNode; |
michael@0 | 76 | var anchorOffset = getSelection().anchorOffset; |
michael@0 | 77 | var focusNode = getSelection().focusNode; |
michael@0 | 78 | var focusOffset = getSelection().focusOffset; |
michael@0 | 79 | |
michael@0 | 80 | // "Let new range be a new range." |
michael@0 | 81 | // |
michael@0 | 82 | // We'll always be setting either new range's start or its end to new |
michael@0 | 83 | // focus, so we'll always throw at some point. Test that now. |
michael@0 | 84 | // |
michael@0 | 85 | // From DOM4's "set the start or end of a range": "If node is a doctype, |
michael@0 | 86 | // throw an "InvalidNodeTypeError" exception and terminate these steps." |
michael@0 | 87 | if (node.nodeType == Node.DOCUMENT_TYPE_NODE) { |
michael@0 | 88 | assert_throws("INVALID_NODE_TYPE_ERR", function() { |
michael@0 | 89 | selection.extend(node, offset); |
michael@0 | 90 | }, "extend() to a doctype must throw InvalidNodeTypeError"); |
michael@0 | 91 | return; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // From DOM4's "set the start or end of a range": "If offset is greater |
michael@0 | 95 | // than node's length, throw an "IndexSizeError" exception and terminate |
michael@0 | 96 | // these steps." |
michael@0 | 97 | // |
michael@0 | 98 | // FIXME: We should be casting offset to an unsigned int per WebIDL. Until |
michael@0 | 99 | // we do, we need the offset < 0 check too. |
michael@0 | 100 | if (offset < 0 || offset > getNodeLength(node)) { |
michael@0 | 101 | assert_throws("INDEX_SIZE_ERR", function() { |
michael@0 | 102 | selection.extend(node, offset); |
michael@0 | 103 | }, "extend() to an offset that's greater than node length (" + getNodeLength(node) + ") must throw IndexSizeError"); |
michael@0 | 104 | return; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // Now back to the editing spec. |
michael@0 | 108 | var originalRange = getSelection().getRangeAt(0); |
michael@0 | 109 | |
michael@0 | 110 | // "If node's root is not the same as the context object's range's root, |
michael@0 | 111 | // set new range's start and end to (node, offset)." |
michael@0 | 112 | // |
michael@0 | 113 | // "Otherwise, if anchor is before or equal to new focus, set new range's |
michael@0 | 114 | // start to anchor, then set its end to new focus." |
michael@0 | 115 | // |
michael@0 | 116 | // "Otherwise, set new range's start to new focus, then set its end to |
michael@0 | 117 | // anchor." |
michael@0 | 118 | // |
michael@0 | 119 | // "Set the context object's range to new range." |
michael@0 | 120 | // |
michael@0 | 121 | // "If new focus is before anchor, set the context object's direction to |
michael@0 | 122 | // backwards. Otherwise, set it to forwards." |
michael@0 | 123 | // |
michael@0 | 124 | // The upshot of all these is summed up by just testing the anchor and |
michael@0 | 125 | // offset. |
michael@0 | 126 | getSelection().extend(node, offset); |
michael@0 | 127 | |
michael@0 | 128 | if (furthestAncestor(anchorNode) == furthestAncestor(node)) { |
michael@0 | 129 | assert_equals(getSelection().anchorNode, anchorNode, |
michael@0 | 130 | "anchorNode must not change if the node passed to extend() has the same root as the original range"); |
michael@0 | 131 | assert_equals(getSelection().anchorOffset, anchorOffset, |
michael@0 | 132 | "anchorOffset must not change if the node passed to extend() has the same root as the original range"); |
michael@0 | 133 | } else { |
michael@0 | 134 | assert_equals(getSelection().anchorNode, node, |
michael@0 | 135 | "anchorNode must be the node passed to extend() if it has a different root from the original range"); |
michael@0 | 136 | assert_equals(getSelection().anchorOffset, offset, |
michael@0 | 137 | "anchorOffset must be the offset passed to extend() if the node has a different root from the original range"); |
michael@0 | 138 | } |
michael@0 | 139 | assert_equals(getSelection().focusNode, node, |
michael@0 | 140 | "focusNode must be the node passed to extend()"); |
michael@0 | 141 | assert_equals(getSelection().focusOffset, offset, |
michael@0 | 142 | "focusOffset must be the offset passed to extend()"); |
michael@0 | 143 | assert_not_equals(getSelection().getRangeAt(0), originalRange, |
michael@0 | 144 | "extend() must replace any existing range with a new one, not mutate the existing one"); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | testDiv.style.display = "none"; |
michael@0 | 148 | </script> |