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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Test for HTML serializer</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=424359">Mozilla Bug </a>
<p id="display"></p>
<!-- IMPORTANT: This iframe needs to actually be displayed, so the serializer
sees the relevant styles for <pre> elements. -->
<iframe id="testframe" src="file_htmlserializer_1.html"></iframe>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
function loadFileContent(aFile, aCharset) {
//if(aAsIso == undefined) aAsIso = false;
if(aCharset == undefined)
aCharset = 'UTF-8';
var baseUri = SpecialPowers.Cc['@mozilla.org/network/standard-url-mutator;1']
.createInstance(SpecialPowers.Ci.nsIURIMutator)
.setSpec(window.location.href)
.finalize();
var ios = SpecialPowers.Cc['@mozilla.org/network/io-service;1']
.getService(SpecialPowers.Ci.nsIIOService);
var chann = ios.newChannel(aFile,
aCharset,
baseUri,
null, // aLoadingNode
SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal(),
null, // aTriggeringPrincipal
SpecialPowers.Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
SpecialPowers.Ci.nsIContentPolicy.TYPE_OTHER);
var cis = SpecialPowers.Ci.nsIConverterInputStream;
var inputStream = SpecialPowers.Cc["@mozilla.org/intl/converter-input-stream;1"]
.createInstance(cis);
inputStream.init(chann.open(), aCharset, 1024, cis.DEFAULT_REPLACEMENT_CHARACTER);
var str = {}, content = '';
while (inputStream.readString(4096, str) != 0) {
content += str.value;
}
return content;
}
function isRoughly(actual, expected, message) {
return is(actual.replace("<!DOCTYPE HTML", "<!DOCTYPE html"),
expected,
message);
}
function testHtmlSerializer_1 () {
const de = SpecialPowers.Ci.nsIDocumentEncoder;
var encoder = SpecialPowers.Cu.createDocumentEncoder("text/html");
var doc = $("testframe").contentDocument;
var out, expected;
// in the following tests, we must use the OutputLFLineBreak flag, to avoid
// to have the default line break of the platform in the result, so the test
// can pass on all platform
//------------ no flags
encoder.init(doc, "text/html", de.OutputLFLineBreak);
encoder.setCharset("UTF-8");
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_noflag.html");
isRoughly(out, expected, "test no flags");
//------------- unsupported flags
// since the following flags are not supported, we should
// have a result like the one without flag
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputPreformatted);
out = encoder.encodeToString();
isRoughly(out, expected, "test OutputPreformatted");
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputFormatFlowed);
out = encoder.encodeToString();
isRoughly(out, expected, "test OutputFormatFlowed");
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputNoScriptContent);
out = encoder.encodeToString();
isRoughly(out, expected, "test OutputNoScriptContent");
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputNoFramesContent);
out = encoder.encodeToString();
isRoughly(out, expected, "test OutputNoFramesContent");
//------------ OutputWrap
encoder.init(doc, "text/html", de.OutputLFLineBreak |de.OutputWrap);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_wrap.html");
isRoughly(out, expected, "test OutputWrap");
//------------ OutputFormatted
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputFormatted);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_format.html");
isRoughly(out, expected, "test OutputFormatted");
//------------ OutputRaw
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputRaw);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_raw.html");
isRoughly(out, expected, "test OutputRaw");
//------------ OutputBodyOnly
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputBodyOnly);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_bodyonly.html");
isRoughly(out, expected, "test OutputBodyOnly");
//------------ OutputAbsoluteLinks
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputAbsoluteLinks);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_links.html").trim('\n');
isRoughly(out, expected, "test OutputAbsoluteLinks");
//------------ OutputLFLineBreak
encoder.init(doc, "text/html",de.OutputLFLineBreak);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_linebreak.html");
isRoughly(out, expected, "test OutputLFLineBreak");
//------------ OutputCRLineBreak
encoder.init(doc, "text/html",de.OutputCRLineBreak);
out = encoder.encodeToString();
expected = expected.replace(/\n/mg, "\r");
isRoughly(out, expected, "test OutputCRLineBreak");
//------------ OutputLFLineBreak + OutputCRLineBreak
encoder.init(doc, "text/html",de.OutputLFLineBreak | de.OutputCRLineBreak);
out = encoder.encodeToString();
expected = expected.replace(/\r/mg, "\r\n");
isRoughly(out, expected, "test OutputLFLineBreak + OutputCRLineBreak");
//------------ OutputNoFormattingInPre
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputNoFormattingInPre);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_noformatpre.html");
isRoughly(out, expected, "test OutputNoFormattingInPre");
// ------------- nested body elements
var body2 = doc.createElement('body');
var p = doc.createElement('p');
p.appendChild(doc.createTextNode("this is an other body element"));
body2.appendChild(p);
var body = doc.getElementsByTagName('body')[0];
body.appendChild(body2);
is(doc.getElementsByTagName('body').length, 2); // to be sure we have two body elements
encoder.init(doc, "text/html", de.OutputLFLineBreak);
encoder.setCharset("UTF-8");
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_nested_body.html");
isRoughly(out, expected, "test with two nested body elements");
// ------------- two body elements
body.parentNode.insertBefore(body2, body);
is(doc.getElementsByTagName('body').length, 2); // to be sure we have two body elements
encoder.init(doc, "text/html", de.OutputLFLineBreak);
encoder.setCharset("UTF-8");
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_sibling_body.html");
isRoughly(out, expected, "test with two body elements");
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputBodyOnly);
encoder.setCharset("UTF-8");
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_sibling_body_only_body.html");
isRoughly(out, expected, "test with two body elements, and output body only");
// --------------- no body element
doc.documentElement.removeChild(body);
doc.documentElement.removeChild(body2);
encoder.init(doc, "text/html", de.OutputLFLineBreak);
encoder.setCharset("UTF-8");
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_1_no_body.html");
isRoughly(out, expected, "test with no body element");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(testHtmlSerializer_1);
</script>
</pre>
<!--<h1>1</h1><h2>result</h2><textarea id="t1" cols="80" rows="20"></textarea>
<h2>expected</h2><textarea id="t1e" cols="80" rows="20"></textarea>-->
</body>
</html>
|