summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug454325.html
blob: 724cd4b549d4f2ad65c8bcf3b0ecd575d94c7439 (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
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=454325
-->
<head>
  <title>Test for Bug 454325</title>
  <script type="text/javascript" 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=454325">Mozilla Bug 454325</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 454325 **/

function testDocument1() {
  var doc = document.implementation.createDocument("", "", null);
  var html = doc.createElement('html');
  doc.appendChild(html);
  var body = doc.createElement('body');
  html.appendChild(body);
  var h1 = doc.createElement('h1');
  var t1 = doc.createTextNode('Hello ');
  h1.appendChild(t1);
  var em = doc.createElement('em');
  var t2 = doc.createTextNode('Wonderful');
  em.appendChild(t2);
  h1.appendChild(em);
  var t3 = doc.createTextNode(' Kitty');
  h1.appendChild(t3);
  body.appendChild(h1);
  var p = doc.createElement('p');
  var t4 = doc.createTextNode(' How are you?');
  p.appendChild(t4);
  body.appendChild(p);
  var r = doc.createRange();
  r.selectNodeContents(doc);
  is(r.toString(), "Hello Wonderful Kitty How are you?",
     "toString() on range selecting Document gave wrong output");
  r.setStart(h1, 3);
  r.setEnd(p, 0);
  // <html><body><h1>Hello <em>Wonder ful<\em> Kitty<\h1><p>How are you?<\p><\body></html>
  //                                                ^ -----^
  is(r.toString(), "", "toString() on range crossing text nodes gave wrong output");
  var c1 = r.cloneContents();
  is(c1.childNodes.length, 2, "Wrong child nodes");
  try {
    is(c1.childNodes[0].localName, "h1", "Wrong child node");
    is(c1.childNodes[1].localName, "p", "Wrong child node");
  } catch(ex) {
    ok(!ex, ex);
  }

  r.setStart(t2, 6);
  r.setEnd(p, 0);
  // <html><body><h1>Hello <em>Wonder ful<\em> Kitty<\h1><p>How are you?<\p><\body></html>
  //                                 ^----------------------^
  is(r.toString(), "ful Kitty", "toString() on range crossing text nodes gave wrong output");
  var c2 = r.cloneContents();
  is(c2.childNodes.length, 2, "Wrong child nodes");
  try {
    is(c1.childNodes[0].localName, "h1", "Wrong child node");
    is(c1.childNodes[1].localName, "p", "Wrong child node");
  } catch(ex) {
    ok(!ex, ex);
  }

  var e1 = r.extractContents();
  is(e1.childNodes.length, 2, "Wrong child nodes");
  try {
    is(e1.childNodes[0].localName, "h1", "Wrong child node");
    is(e1.childNodes[1].localName, "p", "Wrong child node");
  } catch(ex) {
    ok(!ex, ex);
  }
}

function testDocument2() {
  var doc = document.implementation.createDocument("", "", null);
  var html = doc.createElement('html');
  doc.appendChild(html);
  var head = doc.createElement('head');
  html.appendChild(head);
  var foohead = doc.createElement('foohead');
  html.appendChild(foohead);
  var body = doc.createElement('body');
  html.appendChild(body);
  var d1 = doc.createElement('div');
  head.appendChild(d1);
  var t1 = doc.createTextNode("|||");
  d1.appendChild(t1);
  var d2 = doc.createElement("div");
  body.appendChild(d2);
  var d3 = doc.createElement("div");
  d2.appendChild(d3);
  var d4 = doc.createElement("div");
  d2.appendChild(d4);
  var r = doc.createRange();
  r.setStart(t1, 1);
  r.setEnd(d2, 2);
  is(r.toString(), "||", "Wrong range");
  var c1 = r.cloneContents();
  var e1 = r.extractContents();
  ok(c1.isEqualNode(e1), "Wrong cloning or extracting!");
}

function testSurroundContents() {
  var div = document.createElement('div');
  document.body.appendChild(div);
  div.innerHTML = '<div>hello</div>world';
  var innerDiv = div.firstChild;
  var hello = innerDiv.firstChild;
  var range = document.createRange();
  range.setStart(hello, 0);
  range.setEnd(hello, 5);
  range.surroundContents(document.createElement('code'));
  is(innerDiv.childNodes.length, 3, "Wrong childNodes count");
  is(innerDiv.childNodes[0].nodeName, "#text", "Wrong node name (1)");
  is(innerDiv.childNodes[0].textContent, "", "Wrong textContent (1)");
  is(innerDiv.childNodes[1].nodeName, "CODE", "Wrong node name (2)");
  is(innerDiv.childNodes[1].textContent, "hello", "Wrong textContent (2)");
  is(innerDiv.childNodes[2].nodeName, "#text", "Wrong node name (3)");
  is(innerDiv.childNodes[2].textContent, "", "Wrong textContent (3)");
}

function runTest() {
  testDocument1();
  testDocument2();
  testSurroundContents();
  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(runTest);


</script>
</pre>
</body>
</html>