Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 <!DOCTYPE HTML>
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <!--
4 -->
5 <head>
6 <title>Test XHTML serializer with entities and selection</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=422043">Mozilla Bug </a>
12 <p id="display"></p>
13 <div id="content" style="display: none">
14 <iframe id="testframe" src="file_xhtmlserializer_2.xhtml">
15 </iframe>
16 </div>
17 <pre id="test">
18 <script class="testbody" type="text/javascript">
19 //<![CDATA[
21 function loadFileContent(aFile, aCharset) {
22 //if(aAsIso == undefined) aAsIso = false;
23 if(aCharset == undefined)
24 aCharset = 'UTF-8';
26 var baseUri = SpecialPowers.Cc['@mozilla.org/network/standard-url;1']
27 .createInstance(SpecialPowers.Ci.nsIURI);
28 baseUri.spec = window.location.href;
30 var ios = SpecialPowers.Cc['@mozilla.org/network/io-service;1']
31 .getService(SpecialPowers.Ci.nsIIOService);
32 var chann = ios.newChannel(aFile, aCharset, baseUri);
34 var cis = SpecialPowers.Ci.nsIConverterInputStream;
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 }
47 function testHtmlSerializer_1 () {
48 const de = SpecialPowers.Ci.nsIDocumentEncoder
49 var encoder = SpecialPowers.Cc["@mozilla.org/layout/documentEncoder;1?type=application/xhtml+xml"]
50 .createInstance(SpecialPowers.Ci.nsIDocumentEncoder);
52 var doc = $("testframe").contentDocument;
53 var out, expected;
55 // in the following tests, we must use the OutputLFLineBreak flag, to avoid
56 // to have the default line break of the platform in the result, so the test
57 // can pass on all platform
59 //------------ OutputEncodeW3CEntities
60 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeW3CEntities);
61 out = encoder.encodeToString();
62 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml");
63 is(out, expected, "test OutputEncodeW3CEntities");
65 //------------ OutputEncodeBasicEntities
66 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeBasicEntities);
67 out = encoder.encodeToString();
68 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml");
69 is(out, expected, "test OutputEncodeBasicEntities");
71 //------------ OutputEncodeLatin1Entities
72 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeLatin1Entities);
73 out = encoder.encodeToString();
74 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml");
75 is(out, expected, "test OutputEncodeLatin1Entities");
77 //------------ OutputEncodeHTMLEntities
78 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeHTMLEntities);
79 out = encoder.encodeToString();
80 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml");
81 is(out, expected, "test OutputEncodeHTMLEntities");
83 // tests on the serialization of selections
85 var node = document.getElementById('draggable');
87 var select = window.getSelection();
88 select.selectAllChildren(node);
90 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
91 encoder.setSelection(select);
92 out = encoder.encodeToString();
93 expected = 'This is a <em xmlns=\"http://www.w3.org/1999/xhtml\">draggable</em> bit of text.';
94 is(out, expected, "test selection");
96 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
97 encoder.setSelection(null);
98 encoder.setContainerNode(node);
99 out = encoder.encodeToString();
100 expected = 'This is a <em xmlns=\"http://www.w3.org/1999/xhtml\">draggable</em> bit of text.';
101 is(out, expected, "test container node");
103 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
104 encoder.setNode(node);
105 out = encoder.encodeToString();
106 expected = "<div xmlns=\"http://www.w3.org/1999/xhtml\" id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>";
107 is(out, expected, "test node");
109 node = document.getElementById('aList');
111 var select = window.getSelection();
112 select.selectAllChildren(node);
114 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
115 encoder.setSelection(select);
116 out = encoder.encodeToString();
117 expected = '\n <li xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum dolor</li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">sit amet, <strong>consectetuer</strong> </li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">adipiscing elit</li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">aptent taciti</li>\n';
118 is(out, expected, "test list selection");
120 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
121 encoder.setSelection(null);
122 encoder.setContainerNode(node);
123 out = encoder.encodeToString();
124 expected = '\n <li xmlns=\"http://www.w3.org/1999/xhtml\">Lorem ipsum dolor</li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">sit amet, <strong>consectetuer</strong> </li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">adipiscing elit</li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>\n <li xmlns=\"http://www.w3.org/1999/xhtml\">aptent taciti</li>\n';
125 is(out, expected, "test list container node");
127 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
128 encoder.setNode(node);
129 out = encoder.encodeToString();
130 expected = "<ol xmlns=\"http://www.w3.org/1999/xhtml\" 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>";
131 is(out, expected, "test list node");
133 var liList = node.getElementsByTagName("li");
134 var range = document.createRange();
136 // selection start at the first child of the ol, and end after the element ol
137 range.setStart(node, 1);
138 range.setEnd(node.parentNode, 2);
139 select.removeAllRanges();
140 select.addRange(range);
141 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
142 encoder.setSelection(select);
143 out = encoder.encodeToString();
144 expected = '<ol xmlns=\"http://www.w3.org/1999/xhtml\" 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>';
145 is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol");
147 // selection start at the third child of the ol, and end after the element ol
148 range.setStart(node, 3);
149 range.setEnd(node.parentNode, 2);
150 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
151 encoder.setSelection(select);
152 out = encoder.encodeToString();
153 expected = '<ol xmlns=\"http://www.w3.org/1999/xhtml\" 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>';
154 is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol");
157 // selection start at the third child of the ol, and end after the element ol + ol start at the value 5
158 range.setStart(node, 3);
159 range.setEnd(node.parentNode, 2);
160 node.setAttribute("start","5");
161 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
162 encoder.setSelection(select);
163 out = encoder.encodeToString();
164 expected = '<ol xmlns=\"http://www.w3.org/1999/xhtml\" 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>';
165 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");
168 // selection contains only some child of the ol
169 node.removeAttribute("start");
170 range.setStart(node, 3);
171 range.setEnd(node, 5);
172 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly);
173 encoder.setSelection(select);
174 out = encoder.encodeToString();
175 expected = '<li xmlns=\"http://www.w3.org/1999/xhtml\">sit amet, <strong>consectetuer</strong> </li>\n ';
176 is(out, expected, "test list selection with range: selection contains only some child of the ol");
179 // test on short attributes
181 node = document.getElementById('shortattr1');
182 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
183 encoder.setNode(node);
184 out = encoder.encodeToString();
185 expected = '<input xmlns="http://www.w3.org/1999/xhtml" id="shortattr1" checked="checked" value="" disabled="disabled" ismap="ismap" readonly="readonly" foo:checked="" xmlns:foo="http://mozilla.org/ns/any" foo:disabled="" />';
186 is(out, expected, "test short attr #1");
188 node = document.getElementById('shortattr2');
189 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
190 encoder.setNode(node);
191 out = encoder.encodeToString();
192 expected = '<ol xmlns="http://www.w3.org/1999/xhtml" id="shortattr2" compact="compact"><li></li></ol>';
193 is(out, expected, "test short attr #2");
195 node = document.getElementById('shortattr3');
196 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
197 encoder.setNode(node);
198 out = encoder.encodeToString();
199 expected = '<object xmlns="http://www.w3.org/1999/xhtml" id="shortattr3" declare="declare"></object>';
200 is(out, expected, "test short attr #3");
202 node = document.getElementById('shortattr4');
203 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
204 encoder.setNode(node);
205 out = encoder.encodeToString();
206 expected = '<script xmlns="http://www.w3.org/1999/xhtml" id="shortattr4" defer="defer"></script>';
207 is(out, expected, "test short attr #4");
209 node = document.getElementById('shortattr5');
210 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
211 encoder.setNode(node);
212 out = encoder.encodeToString();
213 expected = '<select xmlns="http://www.w3.org/1999/xhtml" id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>';
214 is(out, expected, "test short attr #5");
216 node = document.getElementById('shortattr6');
217 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
218 encoder.setNode(node);
219 out = encoder.encodeToString();
220 expected = '<hr xmlns="http://www.w3.org/1999/xhtml" noshade="noshade" id="shortattr6" />';
221 is(out, expected, "test short attr #6");
223 node = document.getElementById('shortattr7');
224 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw);
225 encoder.setNode(node);
226 out = encoder.encodeToString();
227 expected = '<div xmlns="http://www.w3.org/1999/xhtml" id="shortattr7"><foo:bar xmlns:foo="http://mozilla.org/ns/any" checked="" value="" disabled="" ismap="" readonly=""/></div>';
228 is(out, expected, "test short attr #7");
230 // test on _moz and -moz attr
231 node = document.getElementById('mozattr');
232 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
233 encoder.setNode(node);
234 out = encoder.encodeToString();
235 expected = '<div __moz_b="b" id="mozattr"> lorem ipsum</div>';
236 is(out, expected, "test -moz/_moz attr");
238 node.setAttribute('_moz_c','barc');
239 node.setAttribute('_-moz_d','bard');
240 node.setAttribute('__moz_e','bare');
242 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw);
243 encoder.setNode(node);
244 out = encoder.encodeToString();
245 expected = '<div __moz_e="bare" _-moz_d="bard" __moz_b="b" id="mozattr"> lorem ipsum</div>';
246 is(out, expected, "test -moz/_moz attr #2");
248 SimpleTest.finish();
249 }
252 SimpleTest.waitForExplicitFinish();
254 addLoadEvent(testHtmlSerializer_1);
255 //]]>
256 </script>
257 </pre>
258 <div style="display: none">
260 <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div>
262 </div>
263 <div style="display: none">
265 <ol id="aList">
266 <li>Lorem ipsum dolor</li>
267 <li>sit amet, <strong>consectetuer</strong> </li>
268 <li>adipiscing elit</li>
269 <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li>
270 <li>aptent taciti</li>
271 </ol>
273 <!-- test for some short attr -->
274 <div id="shortattr" xmlns:foo="http://mozilla.org/ns/any">
275 <input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo:checked="" foo:disabled=""/>
276 <ol id="shortattr2" compact=""><li></li></ol>
277 <object id="shortattr3" declare="" />
278 <script id="shortattr4" defer="" />
279 <select id="shortattr5" multiple=""><option selected="">aaa</option></select>
280 <hr noshade="" id="shortattr6"/>
281 <div id="shortattr7"><foo:bar checked="" value="" disabled="" ismap="" readonly="" /></div>
282 <div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div>
283 </div>
285 </div>
286 </body>
287 </html>