summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug454326.html
blob: d477db428861d4908dc4c3348b22d10595af94dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=454326
-->
<head>
  <title>Test for Bug 454326</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=454326">Mozilla Bug 454326</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>

<div id="partial-text-selection">Hello Hello <div></div>World!</div>
<div id="partial-element-selection"><div id="begin">Hello Hello </div><div></div><div id="end">World!</div></div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 454326 **/

  function reinitPartialTextSelection() {
    var pts = document.getElementById("partial-text-selection");
    pts.textContent = null;
    pts.appendChild(document.createTextNode("Hello Hello "));
    pts.appendChild(document.createElement("div"));
    pts.appendChild(document.createTextNode("World!"));
  }


  function doTest() {
    var pts = document.getElementById("partial-text-selection");
    var ex = null;
    try {
      var r1 = document.createRange();
      r1.setStart(pts.firstChild, 6);
      r1.setEnd(pts.lastChild, 6);
      is(r1.toString(), "Hello World!", "Wrong range!");
      r1.surroundContents(document.createElement("div"));
      is(r1.toString(), "Hello World!", "Wrong range!");
    } catch(e) {
      ex = e;
    }
    is(ex, null, "Unexpected exception!");

    reinitPartialTextSelection();
    ex = null;
    try {
      var r2 = document.createRange();
      r2.setStart(pts.firstChild, 6);
      r2.setEnd(pts, 2);
      is(r2.toString(), "Hello ", "Wrong range!");
      r2.surroundContents(document.createElement("div"));
      is(r2.toString(), "Hello ", "Wrong range!");
    } catch(e) {
      ex = e;
    }
    is(ex, null, "Unexpected exception!");

    reinitPartialTextSelection();
    ex = null;
    try {
      var r3 = document.createRange();
      r3.setStart(pts, 1);
      r3.setEnd(pts.lastChild, 6);
      is(r3.toString(), "World!", "Wrong range!");
      r3.surroundContents(document.createElement("div"));
      is(r3.toString(), "World!", "Wrong range!");
    } catch(e) {
      ex = e;
    }
    is(ex, null, "Unexpected exception!");

    reinitPartialTextSelection();
    ex = null;
    try {
      var r3 = document.createRange();
      r3.setStart(pts.firstChild, 6);
      r3.setEnd(pts.firstChild.nextSibling, 0);
      is(r3.toString(), "Hello ", "Wrong range!");
      r3.surroundContents(document.createElement("div"));
      is(r3.toString(), "Hello ", "Wrong range!");
    } catch(e) {
      ex = e;
      is(e.name, "InvalidStateError", "Didn't get InvalidStateError exception!");
      is(Object.getPrototypeOf(e), DOMException.prototype, "Didn't get DOMException!");
      is(e.code, 11, "Didn't get INVALID_STATE_ERR exception!");
    }
    ok(ex, "There should have been an exception!");

    reinitPartialTextSelection();
    ex = null;
    try {
      var r3 = document.createRange();
      r3.setStart(pts.firstChild.nextSibling, 0);
      r3.setEnd(pts.lastChild, 6);
      is(r3.toString(), "World!", "Wrong range!");
      r3.surroundContents(document.createElement("div"));
      is(r3.toString(), "World!", "Wrong range!");
    } catch(e) {
      ex = e;
      is(e.name, "InvalidStateError", "Didn't get InvalidStateError exception!");
      is(Object.getPrototypeOf(e), DOMException.prototype, "Didn't get DOMException!");
      is(e.code, 11, "Didn't get INVALID_STATE_ERR exception!");
    }
    ok(ex, "There should have been an exception!");

    ex = null;
    try {
      var pes = document.getElementById("partial-element-selection");
      var r4 = document.createRange();
      r4.setStart(pes.firstChild.firstChild, 6);
      r4.setEnd(pes.lastChild.firstChild, 6);
      is(r4.toString(), "Hello World!", "Wrong range!");
      r4.surroundContents(document.createElement("div"));
      is(r4.toString(), "Hello World!", "Wrong range!");
    } catch(e) {
      ex = e;
      is(e.name, "InvalidStateError", "Didn't get InvalidStateError exception!");
      is(Object.getPrototypeOf(e), DOMException.prototype, "Didn't get DOMException!");
      is(e.code, 11, "Didn't get INVALID_STATE_ERR exception!");
    }
    ok(ex, "There should have been an exception!");
  }

  SimpleTest.waitForExplicitFinish();
  addLoadEvent(doTest);
  addLoadEvent(SimpleTest.finish);
</script>
</pre>
</body>
</html>