summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_bug541937.html
blob: c311b16157cd657e3ad23845bc4866e9d34f1f96 (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
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
  <title>Test for XHTML serializer, bug 541937</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=541937">Mozilla Bug </a>
<p id="display"></p>
<div id="content" style="display: none">
  <iframe id="testframe" src="file_bug541937.html">
  </iframe>
  <iframe id="testframe2" src="file_bug541937.xhtml">
  </iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">

function testSerializer () {
  const de = SpecialPowers.Ci.nsIDocumentEncoder;
  var encoder = SpecialPowers.Cc["@mozilla.org/layout/documentEncoder;1?type=text/html"]
                   .createInstance(SpecialPowers.Ci.nsIDocumentEncoder);

  var parser = new DOMParser();
  var serializer = new XMLSerializer();

  // with content
  var str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"><!-- child nodes -->  \n<content xmlns=""/></link>\n</doc>';
  var expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"><!-- child nodes -->  \n<content xmlns=""/></link>\n</doc>';

  var doc = parser.parseFromString(str,"application/xml");
  var result = serializer.serializeToString(doc);
  result = result.replace(/\r\n/mg, "\n");
  is(result, expected, "serialization of a link element inside an xml document with children");

  // with only whitespaces
  str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml">       \n    </link>\n</doc>';
  expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml">       \n    </link>\n</doc>';  

  doc = parser.parseFromString(str,"application/xml");
  result = serializer.serializeToString(doc);
  result = result.replace(/\r\n/mg, "\n");
  is(result, expected, "serialization of a link element with only whitespaces as content, inside an xml document");
  
  // with only one space as content
  str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> </link>\n</doc>';
  expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> </link>\n</doc>';  

  doc = parser.parseFromString(str,"application/xml");
  result = serializer.serializeToString(doc);
  result = result.replace(/\r\n/mg, "\n");
  is(result, expected, "serialization of a link element with only one space as content, inside an xml document");
  
  // let's remove the content
  str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"> <!-- child nodes -->  \ndeleted content<content xmlns=""/> </link>\n</doc>';
  expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>';  

  doc = parser.parseFromString(str,"application/xml");
  doc.documentElement.firstElementChild.textContent = '';
  result = serializer.serializeToString(doc);
  result = result.replace(/\r\n/mg, "\n");
  is(result, expected, "serialization of a link element on which we removed dynamically the content, inside an xml document");
  
  // with no content but an ended tag
  str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"></link>\n</doc>';
  expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>';  

  doc = parser.parseFromString(str,"application/xml");
  result = serializer.serializeToString(doc);
  result = result.replace(/\r\n/mg, "\n");
  is(result, expected, "serialization of a link element with no content but with an ended tag, inside an xml document");
  
  // with no content
  str = '<?xml version="1.0"?><doc>\n<link xmlns="http://www.w3.org/1999/xhtml"/>\n</doc>';
  expected = '<?xml version="1.0" encoding="UTF-8"?>\n<doc>\n<link xmlns="http://www.w3.org/1999/xhtml" />\n</doc>';  

  doc = parser.parseFromString(str,"application/xml");
  result = serializer.serializeToString(doc);
  result = result.replace(/\r\n/mg, "\n");
  is(result, expected, "serialization of a link element with no content, inside an xml document");
  
  
  doc = $("testframe").contentDocument;
  encoder.init(doc, "text/html", de.OutputLFLineBreak);
  encoder.setCharset("UTF-8");
  result = encoder.encodeToString();
  expected = '<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n  <title>Test</title>\n';
  expected += '   <link rel=\"Top\" href=\"\"> ';
  expected += ' </head><body>foo \n\n\n  <p>Hello world</p>\n</body></html>';
  is(result, expected, "serialization of a link element with content, inside an html document");

  doc = $("testframe2").contentDocument;
  encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak);
  encoder.setCharset("UTF-8");
  result = encoder.encodeToString();
  expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n';
  expected += '<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n  <title>Test</title>\n';
  expected += '   <link rel="Top" href="">  foo </link>\n';
  expected += '\n</head>\n<body>\n  <p>Hello world</p>\n</body>\n</html>';
  is(result, expected, "serialization of a link element with content, inside an xhtml document");

  SimpleTest.finish();
}


SimpleTest.waitForExplicitFinish();

addLoadEvent(testSerializer);

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