3 <div id=
"test"><span id=
"span1">foo
<span id=
"span2">bar
</span></span><span id=
"span3">baz
</span></div>
4 <div id=
"console"></div>
6 var s
= window
.getSelection();
7 var testDiv
= document
.getElementById("test");
8 var span1
= document
.getElementById("span1");
9 var span2
= document
.getElementById("span2");
10 var span3
= document
.getElementById("span3");
13 var li
= document
.createElement("li");
14 li
.appendChild(document
.createTextNode(str
));
15 document
.getElementById("console").appendChild(li
);
18 function shouldBe(expr
, expected
) {
19 var actual
= eval(expr
);
20 if (actual
!= expected
)
21 log("Failure: " + expr
+ " should be " + expected
+ ", was " + actual
+ ".");
23 log("Success: " + expr
+ " is " + expected
+ ".");
26 function shouldThrow(expr
) {
29 log("Failure: " + expr
+ " didn't raise an exception" + ".");
31 log("Success: " + expr
+ " raised " + ex
+ ".");
35 if (window
.testRunner
)
36 testRunner
.dumpAsText();
38 var r
= document
.createRange();
45 shouldThrow("s.containsNode(null, false);");
46 shouldThrow("s.containsNode(null, true);");
47 shouldBe('s.containsNode(testDiv, false)', false);
48 shouldBe('s.containsNode(testDiv, true)', true);
49 shouldBe('s.containsNode(span1, false)', false);
50 shouldBe('s.containsNode(span1, true)', true);
51 shouldBe('s.containsNode(span2.firstChild, false)', true);
52 shouldBe('s.containsNode(span2.firstChild, true)', true);
53 shouldBe('s.containsNode(span3.firstChild, false)', false);
54 shouldBe('s.containsNode(span3.firstChild, true)', false);
55 shouldBe('s.containsNode(document, false)', false);
56 shouldBe('s.containsNode(document.body, true)', true);
57 shouldBe('s.containsNode(document.getElementById("console"), false)', false);
58 shouldBe('s.containsNode(document.getElementById("console"), true)', false);
59 shouldBe('s.containsNode(document.implementation.createDocumentType("name", "", ""), true)', false);
61 // partially select a text node
62 r
.setStart(span2
.firstChild
, 1);
63 r
.setEnd(span2
.firstChild
, 2);
67 shouldBe('s.containsNode(span2.firstChild, false)', true);
68 shouldBe('s.containsNode(span2.firstChild, true)', true);
69 shouldBe('s.containsNode(span2, false)', false);
70 shouldBe('s.containsNode(span2, true)', true);
71 shouldBe('s.containsNode(span1, false)', false);
72 shouldBe('s.containsNode(span1, true)', true);
74 // select several nodes
75 r
.setStart(span1
.firstChild
, 1);
76 r
.setEnd(span3
.firstChild
, 2);
80 shouldBe('s.containsNode(span2, false)', true);
81 shouldBe('s.containsNode(span2.firstChild, false)', true);
82 shouldBe('s.containsNode(span3, false)', false);
83 shouldBe('s.containsNode(span3, true)', true);