2 <title>Range.compareBoundaryPoints() tests
</title>
3 <link rel=
"author" title=
"Aryeh Gregor" href=ayg@aryeh.name
>
4 <meta name=timeout content=long
>
7 <script src=/resources/testharness.js
></script>
8 <script src=/resources/testharnessreport.js
></script>
9 <script src=../common.js
></script>
13 var testRangesCached
= [];
14 testRangesCached
.push(document
.createRange());
15 testRangesCached
[0].detach();
16 for (var i
= 0; i
< testRangesShort
.length
; i
++) {
18 testRangesCached
.push(rangeFromEndpoints(eval(testRangesShort
[i
])));
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());
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
++) {
50 if (j
== testRangesCachedClones
.length
) {
52 range2Desc
= "same as first range";
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
,
60 if (extraTests
.indexOf(i
) != -1 && extraTests
.indexOf(j
) != -1) {
61 // TODO: Make some type of reusable utility function to do this
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
);
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
++) {
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
84 var convertedHow
= Number(how
);
86 // "If number is NaN, +0, −0, +∞, or −∞, return +0."
87 if (isNaN(convertedHow
)
89 || convertedHow
== Infinity
90 || convertedHow
== -Infinity
) {
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
115 // throw a "NotSupportedError" exception and terminate these
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");
127 // "If context object's root is not the same as sourceRange's
128 // root, throw a "WrongDocumentError" exception and terminate
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");
139 // Let this point be the context object's start.
140 // Let other point be sourceRange's start.
142 // Let this point be the context object's end.
143 // Let other point be sourceRange's start.
145 // Let this point be the context object's end.
146 // Let other point be sourceRange's end.
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
164 var position
= getPosition(thisPoint
[0], thisPoint
[1], otherPoint
[0], otherPoint
[1]);
166 if (position
== "before") {
168 } else if (position
== "equal") {
170 } else if (position
== "after") {
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";