Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / editing / selection / containsNode.html
blob4d50873f8f34df89ff41beeabcb132f7a5e62de3
1 <html>
2 <body>
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>
5 <script>
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");
12 function log(str) {
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 + ".");
22 else
23 log("Success: " + expr + " is " + expected + ".");
26 function shouldThrow(expr) {
27 try {
28 eval(expr);
29 log("Failure: " + expr + " didn't raise an exception" + ".");
30 } catch (ex) {
31 log("Success: " + expr + " raised " + ex + ".");
35 if (window.testRunner)
36 testRunner.dumpAsText();
38 var r = document.createRange();
40 // select span2
41 r.setStart(span1, 1);
42 r.setEnd(span1, 2);
43 s.addRange(r);
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);
64 s.removeAllRanges();
65 s.addRange(r);
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);
77 s.removeAllRanges();
78 s.addRange(r);
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);
85 </script>
86 </body>
87 </html>