test_bug422403-2.xhtml (14432B)
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 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[ 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-mutator;1'] 27 .createInstance(SpecialPowers.Ci.nsIURIMutator) 28 .setSpec(window.location.href) 29 .finalize(); 30 31 var ios = SpecialPowers.Services.io; 32 var chann = ios.newChannel(aFile, 33 aCharset, 34 baseUri, 35 null, // aLoadingNode 36 SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal(), 37 null, // aTriggeringPrincipal 38 SpecialPowers.Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, 39 SpecialPowers.Ci.nsIContentPolicy.TYPE_OTHER); 40 41 var cis = SpecialPowers.Ci.nsIConverterInputStream; 42 43 var inputStream = SpecialPowers.Cc["@mozilla.org/intl/converter-input-stream;1"] 44 .createInstance(cis); 45 inputStream.init(chann.open(), aCharset, 1024, cis.DEFAULT_REPLACEMENT_CHARACTER); 46 var str = {}, content = ''; 47 while (inputStream.readString(4096, str) != 0) { 48 content += str.value; 49 } 50 return content; 51 } 52 53 54 function testHtmlSerializer_1 () { 55 const de = SpecialPowers.Ci.nsIDocumentEncoder 56 var encoder = SpecialPowers.Cu.createDocumentEncoder("application/xhtml+xml"); 57 58 var doc = $("testframe").contentDocument; 59 var out, expected; 60 61 // in the following tests, we must use the OutputLFLineBreak flag, to avoid 62 // to have the default line break of the platform in the result, so the test 63 // can pass on all platform 64 65 //------------ OutputEncodeW3CEntities 66 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeW3CEntities); 67 out = encoder.encodeToString(); 68 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 69 is(out, expected, "test OutputEncodeW3CEntities"); 70 71 //------------ OutputEncodeBasicEntities 72 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeBasicEntities); 73 out = encoder.encodeToString(); 74 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 75 is(out, expected, "test OutputEncodeBasicEntities"); 76 77 //------------ OutputEncodeLatin1Entities 78 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeLatin1Entities); 79 out = encoder.encodeToString(); 80 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 81 is(out, expected, "test OutputEncodeLatin1Entities"); 82 83 //------------ OutputEncodeHTMLEntities 84 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeHTMLEntities); 85 out = encoder.encodeToString(); 86 expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 87 is(out, expected, "test OutputEncodeHTMLEntities"); 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, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 97 encoder.setSelection(select); 98 out = encoder.encodeToString(); 99 expected = 'This is a <em xmlns=\"http://www.w3.org/1999/xhtml\">draggable</em> bit of text.'; 100 is(out, expected, "test selection"); 101 102 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 103 encoder.setSelection(null); 104 encoder.setContainerNode(node); 105 out = encoder.encodeToString(); 106 expected = 'This is a <em xmlns=\"http://www.w3.org/1999/xhtml\">draggable</em> bit of text.'; 107 is(out, expected, "test container node"); 108 109 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 110 encoder.setNode(node); 111 out = encoder.encodeToString(); 112 expected = "<div xmlns=\"http://www.w3.org/1999/xhtml\" 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 select = window.getSelection(); 118 select.selectAllChildren(node); 119 120 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 121 encoder.setSelection(select); 122 out = encoder.encodeToString(); 123 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'; 124 is(out, expected, "test list selection"); 125 126 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 127 encoder.setSelection(null); 128 encoder.setContainerNode(node); 129 out = encoder.encodeToString(); 130 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'; 131 is(out, expected, "test list container node"); 132 133 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 134 encoder.setNode(node); 135 out = encoder.encodeToString(); 136 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>"; 137 is(out, expected, "test list node"); 138 139 var range = document.createRange(); 140 141 // selection start at the first child of the ol, and end after the element ol 142 range.setStart(node, 1); 143 range.setEnd(node.parentNode, 2); 144 select.removeAllRanges(); 145 select.addRange(range); 146 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 147 encoder.setSelection(select); 148 out = encoder.encodeToString(); 149 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>'; 150 is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); 151 152 // selection start at the third child of the ol, and end after the element ol 153 range.setStart(node, 3); 154 range.setEnd(node.parentNode, 2); 155 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 156 encoder.setSelection(select); 157 out = encoder.encodeToString(); 158 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>'; 159 is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); 160 161 162 // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 163 range.setStart(node, 3); 164 range.setEnd(node.parentNode, 2); 165 node.setAttribute("start","5"); 166 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 167 encoder.setSelection(select); 168 out = encoder.encodeToString(); 169 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>'; 170 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"); 171 172 173 // selection contains only some child of the ol 174 node.removeAttribute("start"); 175 range.setStart(node, 3); 176 range.setEnd(node, 5); 177 encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 178 encoder.setSelection(select); 179 out = encoder.encodeToString(); 180 expected = '<li xmlns=\"http://www.w3.org/1999/xhtml\">sit amet, <strong>consectetuer</strong> </li>\n '; 181 is(out, expected, "test list selection with range: selection contains only some child of the ol"); 182 183 184 // test on short attributes 185 186 node = document.getElementById('shortattr1'); 187 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 188 encoder.setNode(node); 189 out = encoder.encodeToString(); 190 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="" />'; 191 is(out, expected, "test short attr #1"); 192 193 node = document.getElementById('shortattr2'); 194 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 195 encoder.setNode(node); 196 out = encoder.encodeToString(); 197 expected = '<ol xmlns="http://www.w3.org/1999/xhtml" id="shortattr2" compact="compact"><li></li></ol>'; 198 is(out, expected, "test short attr #2"); 199 200 node = document.getElementById('shortattr3'); 201 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 202 encoder.setNode(node); 203 out = encoder.encodeToString(); 204 expected = '<object xmlns="http://www.w3.org/1999/xhtml" id="shortattr3" declare="declare"></object>'; 205 is(out, expected, "test short attr #3"); 206 207 node = document.getElementById('shortattr4'); 208 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 209 encoder.setNode(node); 210 out = encoder.encodeToString(); 211 expected = '<script xmlns="http://www.w3.org/1999/xhtml" id="shortattr4" defer="defer"></script>'; 212 is(out, expected, "test short attr #4"); 213 214 node = document.getElementById('shortattr5'); 215 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 216 encoder.setNode(node); 217 out = encoder.encodeToString(); 218 expected = '<select xmlns="http://www.w3.org/1999/xhtml" id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>'; 219 is(out, expected, "test short attr #5"); 220 221 node = document.getElementById('shortattr6'); 222 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 223 encoder.setNode(node); 224 out = encoder.encodeToString(); 225 expected = '<hr xmlns="http://www.w3.org/1999/xhtml" id="shortattr6" noshade="noshade" />'; 226 is(out, expected, "test short attr #6"); 227 228 node = document.getElementById('shortattr7'); 229 encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 230 encoder.setNode(node); 231 out = encoder.encodeToString(); 232 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>'; 233 is(out, expected, "test short attr #7"); 234 235 // test on _moz and -moz attr 236 node = document.getElementById('mozattr'); 237 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 238 encoder.setNode(node); 239 out = encoder.encodeToString(); 240 expected = '<div id="mozattr" __moz_b="b"> lorem ipsum</div>'; 241 is(out, expected, "test -moz/_moz attr"); 242 243 node.setAttribute('_moz_c','barc'); 244 node.setAttribute('_-moz_d','bard'); 245 node.setAttribute('__moz_e','bare'); 246 247 encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 248 encoder.setNode(node); 249 out = encoder.encodeToString(); 250 expected = '<div id="mozattr" __moz_b="b" _-moz_d="bard" __moz_e="bare"> lorem ipsum</div>'; 251 is(out, expected, "test -moz/_moz attr #2"); 252 253 SimpleTest.finish(); 254 } 255 256 257 SimpleTest.waitForExplicitFinish(); 258 259 addLoadEvent(testHtmlSerializer_1); 260 //]]> 261 </script> 262 </pre> 263 <div style="display: none"> 264 265 <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div> 266 267 </div> 268 <div style="display: none"> 269 270 <ol id="aList"> 271 <li>Lorem ipsum dolor</li> 272 <li>sit amet, <strong>consectetuer</strong> </li> 273 <li>adipiscing elit</li> 274 <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> 275 <li>aptent taciti</li> 276 </ol> 277 278 <!-- test for some short attr --> 279 <div id="shortattr" xmlns:foo="http://mozilla.org/ns/any"> 280 <input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo:checked="" foo:disabled=""/> 281 <ol id="shortattr2" compact=""><li></li></ol> 282 <object id="shortattr3" declare="" /> 283 <script id="shortattr4" defer="" /> 284 <select id="shortattr5" multiple=""><option selected="">aaa</option></select> 285 <hr id="shortattr6" noshade=""/> 286 <div id="shortattr7"><foo:bar checked="" value="" disabled="" ismap="" readonly="" /></div> 287 <div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div> 288 </div> 289 290 </div> 291 </body> 292 </html>