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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Test HTML serializer with entities</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>
<div id="content" style="display: none">
<iframe id="testframe" src="file_htmlserializer_2.html">
</iframe>
</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 test, 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
//------------ OutputEncodeBasicEntities
encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeBasicEntities);
out = encoder.encodeToString();
expected = loadFileContent("file_htmlserializer_2_basic.html");
isRoughly(out, expected, "test OutputEncodeBasicEntities");
// tests on the serialization of selections
var node = document.getElementById('draggable');
var select = window.getSelection();
select.selectAllChildren(node);
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = 'This is a <em>draggable</em> bit of text.';
is(out, expected, "test selection");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(null);
encoder.setContainerNode(node);
out = encoder.encodeToString();
expected = 'This is a <em>draggable</em> bit of text.';
is(out, expected, "test container node");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setNode(node);
out = encoder.encodeToString();
expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>";
is(out, expected, "test node");
node = document.getElementById('aList');
var select = window.getSelection();
select.selectAllChildren(node);
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n';
is(out, expected, "test list selection");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(null);
encoder.setContainerNode(node);
out = encoder.encodeToString();
expected = '\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n';
is(out, expected, "test list container node");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setNode(node);
out = encoder.encodeToString();
expected = "<ol id=\"aList\">\n <li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>";
is(out, expected, "test list node");
var liList = node.getElementsByTagName("li");
var range = document.createRange();
// selection start at the first child of the ol, and end after the element ol
range.setStart(node, 1);
range.setEnd(node.parentNode, 2);
select.removeAllRanges();
select.addRange(range);
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '<ol id="aList"><li>Lorem ipsum dolor</li>\n <li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>';
is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol");
// selection start at the third child of the ol, and end after the element ol
range.setStart(node, 3);
range.setEnd(node.parentNode, 2);
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '<ol id="aList"><li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>';
is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol");
// selection start at the third child of the ol, and end after the element ol + ol start at the value 5
range.setStart(node, 3);
range.setEnd(node.parentNode, 2);
node.setAttribute("start","5");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '<ol id="aList" start="5"><li>sit amet, <strong>consectetuer</strong> </li>\n <li>adipiscing elit</li>\n <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li>aptent taciti</li>\n</ol>';
is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol + ol start at the value 5");
// selection contains only some child of the ol
node.removeAttribute("start");
range.setStart(node, 3);
range.setEnd(node, 5);
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n ';
is(out, expected, "test list selection with range: selection contains only some child of the ol");
// selection contains only some child of the ol + ol start at the value 5
node.setAttribute("start","5");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n ';
is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5");
// selection contains only some child of the ol + a value is set on the first li
node.removeAttribute("start");
liList[0].setAttribute("value","8");
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly);
encoder.setSelection(select);
out = encoder.encodeToString();
expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n ';
is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5");
// test on short attributes
node = document.getElementById('shortattr1');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<input id="shortattr1" checked="checked" value="" disabled="disabled" ismap="ismap" readonly="readonly" foo="">';
is(out, expected, "test short attr #1");
node = document.getElementById('shortattr2');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<ol id="shortattr2" compact="compact"><li></li></ol>';
is(out, expected, "test short attr #2");
node = document.getElementById('shortattr3');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<object id="shortattr3" declare="declare"></object>';
is(out, expected, "test short attr #3");
node = document.getElementById('shortattr4');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<script id="shortattr4" defer="defer"></'+ 'script>';
is(out, expected, "test short attr #4");
node = document.getElementById('shortattr5');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<select id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>';
is(out, expected, "test short attr #5");
node = document.getElementById('shortattr6');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<hr noshade="noshade" id="shortattr6">';
is(out, expected, "test short attr #6");
node = document.getElementById('shortattr7');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></foo></div>';
is(out, expected, "test short attr #7");
// test on _moz and -moz attr
node = document.getElementById('mozattr');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<div id="mozattr" __moz_b="b"> lorem ipsum</div>';
is(out, expected, "test -moz/_moz attr");
node.setAttribute('_moz_c','barc');
node.setAttribute('_-moz_d','bard');
node.setAttribute('__moz_e','bare');
encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
encoder.setNode(node);
out = encoder.encodeToString();
expected = '<div id="mozattr" __moz_b="b" _-moz_d="bard" __moz_e="bare"> lorem ipsum</div>';
is(out, expected, "test -moz/_moz attr #2");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(testHtmlSerializer_1);
</script>
</pre>
<div style="display: none">
<div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div>
</div>
<div style="display: none">
<ol id="aList">
<li>Lorem ipsum dolor</li>
<li>sit amet, <strong>consectetuer</strong> </li>
<li>adipiscing elit</li>
<li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>
<li>aptent taciti</li>
</ol>
<!-- test for some short attr -->
<div id="shortattr">
<input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo="">
<ol id="shortattr2" compact=""><li></li></ol>
<object id="shortattr3" declare=""></object>
<script id="shortattr4" defer=""></script>
<select id="shortattr5" multiple=""><option selected="">aaa</option></select>
<hr noshade="" id="shortattr6">
<div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></div>
<div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div>
</div>
</div>
</body>
</html>
|