|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 --> |
|
5 <head> |
|
6 <title>Test HTML serializer with entities</title> |
|
7 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
|
9 </head> |
|
10 <body> |
|
11 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=424359">Mozilla Bug </a> |
|
12 <p id="display"></p> |
|
13 <div id="content" style="display: none"> |
|
14 <iframe id="testframe" src="file_htmlserializer_2.html"> |
|
15 </iframe> |
|
16 </div> |
|
17 <pre id="test"> |
|
18 <script class="testbody" type="text/javascript"> |
|
19 |
|
20 |
|
21 function loadFileContent(aFile, aCharset) { |
|
22 //if(aAsIso == undefined) aAsIso = false; |
|
23 if(aCharset == undefined) |
|
24 aCharset = 'UTF-8'; |
|
25 |
|
26 var baseUri = SpecialPowers.Cc['@mozilla.org/network/standard-url;1'] |
|
27 .createInstance(SpecialPowers.Ci.nsIURI); |
|
28 baseUri.spec = window.location.href; |
|
29 |
|
30 var ios = SpecialPowers.Cc['@mozilla.org/network/io-service;1'] |
|
31 .getService(SpecialPowers.Ci.nsIIOService); |
|
32 var chann = ios.newChannel(aFile, aCharset, baseUri); |
|
33 |
|
34 var cis = SpecialPowers.Ci.nsIConverterInputStream; |
|
35 |
|
36 var inputStream = SpecialPowers.Cc["@mozilla.org/intl/converter-input-stream;1"] |
|
37 .createInstance(cis); |
|
38 inputStream.init(chann.open(), aCharset, 1024, cis.DEFAULT_REPLACEMENT_CHARACTER); |
|
39 var str = {}, content = ''; |
|
40 while (inputStream.readString(4096, str) != 0) { |
|
41 content += str.value; |
|
42 } |
|
43 return content; |
|
44 } |
|
45 |
|
46 function isRoughly(actual, expected, message) { |
|
47 return is(actual.replace("<!DOCTYPE HTML", "<!DOCTYPE html"), |
|
48 expected, |
|
49 message); |
|
50 } |
|
51 |
|
52 function testHtmlSerializer_1 () { |
|
53 const de = SpecialPowers.Ci.nsIDocumentEncoder; |
|
54 var encoder = SpecialPowers.Cc["@mozilla.org/layout/documentEncoder;1?type=text/html"] |
|
55 .createInstance(SpecialPowers.Ci.nsIDocumentEncoder); |
|
56 |
|
57 var doc = $("testframe").contentDocument; |
|
58 var out, expected; |
|
59 |
|
60 // in the following tests, we must use the OutputLFLineBreak flag, to avoid |
|
61 // to have the default line break of the platform in the result, so the test |
|
62 // can pass on all platform |
|
63 |
|
64 //------------ OutputEncodeW3CEntities |
|
65 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeW3CEntities); |
|
66 out = encoder.encodeToString(); |
|
67 expected = loadFileContent("file_htmlserializer_2_entw3c.html"); |
|
68 isRoughly(out, expected, "test OutputEncodeW3CEntities"); |
|
69 |
|
70 //------------ OutputEncodeBasicEntities |
|
71 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeBasicEntities); |
|
72 out = encoder.encodeToString(); |
|
73 expected = loadFileContent("file_htmlserializer_2_basic.html"); |
|
74 isRoughly(out, expected, "test OutputEncodeBasicEntities"); |
|
75 |
|
76 //------------ OutputEncodeLatin1Entities |
|
77 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeLatin1Entities); |
|
78 out = encoder.encodeToString(); |
|
79 expected = loadFileContent("file_htmlserializer_2_latin1.html"); |
|
80 isRoughly(out, expected, "test OutputEncodeLatin1Entities"); |
|
81 |
|
82 //------------ OutputEncodeHTMLEntities |
|
83 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeHTMLEntities); |
|
84 out = encoder.encodeToString(); |
|
85 expected = loadFileContent("file_htmlserializer_2_enthtml.html"); |
|
86 isRoughly(out, expected, "test OutputEncodeHTMLEntities"); |
|
87 |
|
88 |
|
89 // tests on the serialization of selections |
|
90 |
|
91 var node = document.getElementById('draggable'); |
|
92 |
|
93 var select = window.getSelection(); |
|
94 select.selectAllChildren(node); |
|
95 |
|
96 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
97 encoder.setSelection(select); |
|
98 out = encoder.encodeToString(); |
|
99 expected = 'This is a <em>draggable</em> bit of text.'; |
|
100 is(out, expected, "test selection"); |
|
101 |
|
102 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
103 encoder.setSelection(null); |
|
104 encoder.setContainerNode(node); |
|
105 out = encoder.encodeToString(); |
|
106 expected = 'This is a <em>draggable</em> bit of text.'; |
|
107 is(out, expected, "test container node"); |
|
108 |
|
109 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
110 encoder.setNode(node); |
|
111 out = encoder.encodeToString(); |
|
112 expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>"; |
|
113 is(out, expected, "test node"); |
|
114 |
|
115 node = document.getElementById('aList'); |
|
116 |
|
117 var select = window.getSelection(); |
|
118 select.selectAllChildren(node); |
|
119 |
|
120 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
121 encoder.setSelection(select); |
|
122 out = encoder.encodeToString(); |
|
123 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'; |
|
124 is(out, expected, "test list selection"); |
|
125 |
|
126 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
127 encoder.setSelection(null); |
|
128 encoder.setContainerNode(node); |
|
129 out = encoder.encodeToString(); |
|
130 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'; |
|
131 is(out, expected, "test list container node"); |
|
132 |
|
133 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
134 encoder.setNode(node); |
|
135 out = encoder.encodeToString(); |
|
136 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>"; |
|
137 is(out, expected, "test list node"); |
|
138 |
|
139 var liList = node.getElementsByTagName("li"); |
|
140 var range = document.createRange(); |
|
141 |
|
142 // selection start at the first child of the ol, and end after the element ol |
|
143 range.setStart(node, 1); |
|
144 range.setEnd(node.parentNode, 2); |
|
145 select.removeAllRanges(); |
|
146 select.addRange(range); |
|
147 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
148 encoder.setSelection(select); |
|
149 out = encoder.encodeToString(); |
|
150 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>'; |
|
151 is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); |
|
152 |
|
153 // selection start at the third child of the ol, and end after the element ol |
|
154 range.setStart(node, 3); |
|
155 range.setEnd(node.parentNode, 2); |
|
156 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
157 encoder.setSelection(select); |
|
158 out = encoder.encodeToString(); |
|
159 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>'; |
|
160 is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); |
|
161 |
|
162 |
|
163 // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 |
|
164 range.setStart(node, 3); |
|
165 range.setEnd(node.parentNode, 2); |
|
166 node.setAttribute("start","5"); |
|
167 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
168 encoder.setSelection(select); |
|
169 out = encoder.encodeToString(); |
|
170 expected = '<ol start="5" 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>'; |
|
171 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"); |
|
172 |
|
173 |
|
174 // selection contains only some child of the ol |
|
175 node.removeAttribute("start"); |
|
176 range.setStart(node, 3); |
|
177 range.setEnd(node, 5); |
|
178 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
179 encoder.setSelection(select); |
|
180 out = encoder.encodeToString(); |
|
181 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
|
182 is(out, expected, "test list selection with range: selection contains only some child of the ol"); |
|
183 |
|
184 // selection contains only some child of the ol + ol start at the value 5 |
|
185 node.setAttribute("start","5"); |
|
186 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
187 encoder.setSelection(select); |
|
188 out = encoder.encodeToString(); |
|
189 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
|
190 is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); |
|
191 |
|
192 // selection contains only some child of the ol + a value is set on the first li |
|
193 node.removeAttribute("start"); |
|
194 liList[0].setAttribute("value","8"); |
|
195 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
|
196 encoder.setSelection(select); |
|
197 out = encoder.encodeToString(); |
|
198 expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
|
199 is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); |
|
200 |
|
201 |
|
202 |
|
203 // test on short attributes |
|
204 node = document.getElementById('shortattr1'); |
|
205 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
206 encoder.setNode(node); |
|
207 out = encoder.encodeToString(); |
|
208 expected = '<input id="shortattr1" checked="checked" value="" disabled="disabled" ismap="ismap" readonly="readonly" foo="">'; |
|
209 is(out, expected, "test short attr #1"); |
|
210 |
|
211 node = document.getElementById('shortattr2'); |
|
212 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
213 encoder.setNode(node); |
|
214 out = encoder.encodeToString(); |
|
215 expected = '<ol id="shortattr2" compact="compact"><li></li></ol>'; |
|
216 is(out, expected, "test short attr #2"); |
|
217 |
|
218 node = document.getElementById('shortattr3'); |
|
219 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
220 encoder.setNode(node); |
|
221 out = encoder.encodeToString(); |
|
222 expected = '<object id="shortattr3" declare="declare"></object>'; |
|
223 is(out, expected, "test short attr #3"); |
|
224 |
|
225 node = document.getElementById('shortattr4'); |
|
226 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
227 encoder.setNode(node); |
|
228 out = encoder.encodeToString(); |
|
229 expected = '<script id="shortattr4" defer="defer"></'+ 'script>'; |
|
230 is(out, expected, "test short attr #4"); |
|
231 |
|
232 node = document.getElementById('shortattr5'); |
|
233 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
234 encoder.setNode(node); |
|
235 out = encoder.encodeToString(); |
|
236 expected = '<select id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>'; |
|
237 is(out, expected, "test short attr #5"); |
|
238 |
|
239 node = document.getElementById('shortattr6'); |
|
240 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
241 encoder.setNode(node); |
|
242 out = encoder.encodeToString(); |
|
243 expected = '<hr id="shortattr6" noshade="noshade">'; |
|
244 is(out, expected, "test short attr #6"); |
|
245 |
|
246 node = document.getElementById('shortattr7'); |
|
247 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
248 encoder.setNode(node); |
|
249 out = encoder.encodeToString(); |
|
250 expected = '<div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></foo></div>'; |
|
251 is(out, expected, "test short attr #7"); |
|
252 |
|
253 // test on _moz and -moz attr |
|
254 node = document.getElementById('mozattr'); |
|
255 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
256 encoder.setNode(node); |
|
257 out = encoder.encodeToString(); |
|
258 expected = '<div id="mozattr" __moz_b="b"> lorem ipsum</div>'; |
|
259 is(out, expected, "test -moz/_moz attr"); |
|
260 |
|
261 node.setAttribute('_moz_c','barc'); |
|
262 node.setAttribute('_-moz_d','bard'); |
|
263 node.setAttribute('__moz_e','bare'); |
|
264 |
|
265 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
|
266 encoder.setNode(node); |
|
267 out = encoder.encodeToString(); |
|
268 expected = '<div __moz_e="bare" _-moz_d="bard" id="mozattr" __moz_b="b"> lorem ipsum</div>'; |
|
269 is(out, expected, "test -moz/_moz attr #2"); |
|
270 |
|
271 SimpleTest.finish(); |
|
272 } |
|
273 |
|
274 |
|
275 SimpleTest.waitForExplicitFinish(); |
|
276 |
|
277 addLoadEvent(testHtmlSerializer_1); |
|
278 |
|
279 </script> |
|
280 </pre> |
|
281 <div style="display: none"> |
|
282 |
|
283 <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div> |
|
284 |
|
285 </div> |
|
286 <div style="display: none"> |
|
287 |
|
288 <ol id="aList"> |
|
289 <li>Lorem ipsum dolor</li> |
|
290 <li>sit amet, <strong>consectetuer</strong> </li> |
|
291 <li>adipiscing elit</li> |
|
292 <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> |
|
293 <li>aptent taciti</li> |
|
294 </ol> |
|
295 |
|
296 |
|
297 <!-- test for some short attr --> |
|
298 <div id="shortattr"> |
|
299 <input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo=""> |
|
300 <ol id="shortattr2" compact=""><li></li></ol> |
|
301 <object id="shortattr3" declare=""></object> |
|
302 <script id="shortattr4" defer=""></script> |
|
303 <select id="shortattr5" multiple=""><option selected="">aaa</option></select> |
|
304 <hr noshade="" id="shortattr6"> |
|
305 <div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></div> |
|
306 <div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div> |
|
307 </div> |
|
308 |
|
309 |
|
310 |
|
311 </div> |
|
312 </body> |
|
313 </html> |