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