Bug 1770047 [wpt PR 34117] - [Clipboard API] Clipboard Web Custom Formats implementat...
[gecko.git] / testing / web-platform / tests / dom / ranges / Range-compareBoundaryPoints.html
blob9d150ae0ab895599b7aca34c53ee6eb99c71a3b1
1 <!doctype html>
2 <title>Range.compareBoundaryPoints() tests</title>
3 <link rel="author" title="Aryeh Gregor" href=ayg@aryeh.name>
4 <meta name=timeout content=long>
6 <div id=log></div>
7 <script src=/resources/testharness.js></script>
8 <script src=/resources/testharnessreport.js></script>
9 <script src=../common.js></script>
10 <script>
11 "use strict";
13 var testRangesCached = [];
14 testRangesCached.push(document.createRange());
15 testRangesCached[0].detach();
16 for (var i = 0; i < testRangesShort.length; i++) {
17 try {
18 testRangesCached.push(rangeFromEndpoints(eval(testRangesShort[i])));
19 } catch(e) {
20 testRangesCached.push(null);
24 var testRangesCachedClones = [];
25 testRangesCachedClones.push(document.createRange());
26 testRangesCachedClones[0].detach();
27 for (var i = 1; i < testRangesCached.length; i++) {
28 if (testRangesCached[i]) {
29 testRangesCachedClones.push(testRangesCached[i].cloneRange());
30 } else {
31 testRangesCachedClones.push(null);
35 // We want to run a whole bunch of extra tests with invalid "how" values (not
36 // 0-3), but it's excessive to run them for every single pair of ranges --
37 // there are too many of them. So just run them for a handful of the tests.
38 var extraTests = [0, // detached
39 1 + testRanges.indexOf("[paras[0].firstChild, 2, paras[0].firstChild, 8]"),
40 1 + testRanges.indexOf("[paras[0].firstChild, 3, paras[3], 1]"),
41 1 + testRanges.indexOf("[testDiv, 0, comment, 5]"),
42 1 + testRanges.indexOf("[foreignDoc.documentElement, 0, foreignDoc.documentElement, 1]")];
44 for (var i = 0; i < testRangesCached.length; i++) {
45 var range1 = testRangesCached[i];
46 var range1Desc = i + " " + (i == 0 ? "[detached]" : testRanges[i - 1]);
47 for (var j = 0; j <= testRangesCachedClones.length; j++) {
48 var range2;
49 var range2Desc;
50 if (j == testRangesCachedClones.length) {
51 range2 = range1;
52 range2Desc = "same as first range";
53 } else {
54 range2 = testRangesCachedClones[j];
55 range2Desc = j + " " + (j == 0 ? "[detached]" : testRanges[j - 1]);
58 var hows = [Range.START_TO_START, Range.START_TO_END, Range.END_TO_END,
59 Range.END_TO_START];
60 if (extraTests.indexOf(i) != -1 && extraTests.indexOf(j) != -1) {
61 // TODO: Make some type of reusable utility function to do this
62 // work.
63 hows.push(-1, 4, 5, NaN, -0, +Infinity, -Infinity);
64 [65536, -65536, 65536*65536, 0.5, -0.5, -72.5].forEach(function(addend) {
65 hows.push(-1 + addend, 0 + addend, 1 + addend,
66 2 + addend, 3 + addend, 4 + addend);
67 });
68 hows.forEach(function(how) { hows.push(String(how)) });
69 hows.push("6.5536e4", null, undefined, true, false, "", "quasit");
72 for (var k = 0; k < hows.length; k++) {
73 var how = hows[k];
74 test(function() {
75 assert_not_equals(range1, null,
76 "Creating context range threw an exception");
77 assert_not_equals(range2, null,
78 "Creating argument range threw an exception");
80 // Convert how per WebIDL. TODO: Make some type of reusable
81 // utility function to do this work.
82 // "Let number be the result of calling ToNumber on the input
83 // argument."
84 var convertedHow = Number(how);
86 // "If number is NaN, +0, −0, +∞, or −∞, return +0."
87 if (isNaN(convertedHow)
88 || convertedHow == 0
89 || convertedHow == Infinity
90 || convertedHow == -Infinity) {
91 convertedHow = 0;
92 } else {
93 // "Let posInt be sign(number) * floor(abs(number))."
94 var posInt = (convertedHow < 0 ? -1 : 1) * Math.floor(Math.abs(convertedHow));
96 // "Let int16bit be posInt modulo 2^16; that is, a finite
97 // integer value k of Number type with positive sign and
98 // less than 2^16 in magnitude such that the mathematical
99 // difference of posInt and k is mathematically an integer
100 // multiple of 2^16."
102 // "Return int16bit."
103 convertedHow = posInt % 65536;
104 if (convertedHow < 0) {
105 convertedHow += 65536;
109 // Now to the actual algorithm.
110 // "If how is not one of
111 // START_TO_START,
112 // START_TO_END,
113 // END_TO_END, and
114 // END_TO_START,
115 // throw a "NotSupportedError" exception and terminate these
116 // steps."
117 if (convertedHow != Range.START_TO_START
118 && convertedHow != Range.START_TO_END
119 && convertedHow != Range.END_TO_END
120 && convertedHow != Range.END_TO_START) {
121 assert_throws_dom("NOT_SUPPORTED_ERR", function() {
122 range1.compareBoundaryPoints(how, range2);
123 }, "NotSupportedError required if first parameter doesn't convert to 0-3 per WebIDL");
124 return;
127 // "If context object's root is not the same as sourceRange's
128 // root, throw a "WrongDocumentError" exception and terminate
129 // these steps."
130 if (furthestAncestor(range1.startContainer) != furthestAncestor(range2.startContainer)) {
131 assert_throws_dom("WRONG_DOCUMENT_ERR", function() {
132 range1.compareBoundaryPoints(how, range2);
133 }, "WrongDocumentError required if the ranges don't share a root");
134 return;
137 // "If how is:
138 // START_TO_START:
139 // Let this point be the context object's start.
140 // Let other point be sourceRange's start.
141 // START_TO_END:
142 // Let this point be the context object's end.
143 // Let other point be sourceRange's start.
144 // END_TO_END:
145 // Let this point be the context object's end.
146 // Let other point be sourceRange's end.
147 // END_TO_START:
148 // Let this point be the context object's start.
149 // Let other point be sourceRange's end."
150 var thisPoint = convertedHow == Range.START_TO_START || convertedHow == Range.END_TO_START
151 ? [range1.startContainer, range1.startOffset]
152 : [range1.endContainer, range1.endOffset];
153 var otherPoint = convertedHow == Range.START_TO_START || convertedHow == Range.START_TO_END
154 ? [range2.startContainer, range2.startOffset]
155 : [range2.endContainer, range2.endOffset];
157 // "If the position of this point relative to other point is
158 // before
159 // Return −1.
160 // equal
161 // Return 0.
162 // after
163 // Return 1."
164 var position = getPosition(thisPoint[0], thisPoint[1], otherPoint[0], otherPoint[1]);
165 var expected;
166 if (position == "before") {
167 expected = -1;
168 } else if (position == "equal") {
169 expected = 0;
170 } else if (position == "after") {
171 expected = 1;
174 assert_equals(range1.compareBoundaryPoints(how, range2), expected,
175 "Wrong return value");
176 }, i + "," + j + "," + k + ": context range " + range1Desc + ", argument range " + range2Desc + ", how " + format_value(how));
181 testDiv.style.display = "none";
182 </script>