1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_bug424359-2.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,313 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +--> 1.8 +<head> 1.9 + <title>Test HTML serializer with entities</title> 1.10 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.11 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.12 +</head> 1.13 +<body> 1.14 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=424359">Mozilla Bug </a> 1.15 +<p id="display"></p> 1.16 +<div id="content" style="display: none"> 1.17 + <iframe id="testframe" src="file_htmlserializer_2.html"> 1.18 + </iframe> 1.19 +</div> 1.20 +<pre id="test"> 1.21 +<script class="testbody" type="text/javascript"> 1.22 + 1.23 + 1.24 +function loadFileContent(aFile, aCharset) { 1.25 + //if(aAsIso == undefined) aAsIso = false; 1.26 + if(aCharset == undefined) 1.27 + aCharset = 'UTF-8'; 1.28 + 1.29 + var baseUri = SpecialPowers.Cc['@mozilla.org/network/standard-url;1'] 1.30 + .createInstance(SpecialPowers.Ci.nsIURI); 1.31 + baseUri.spec = window.location.href; 1.32 + 1.33 + var ios = SpecialPowers.Cc['@mozilla.org/network/io-service;1'] 1.34 + .getService(SpecialPowers.Ci.nsIIOService); 1.35 + var chann = ios.newChannel(aFile, aCharset, baseUri); 1.36 + 1.37 + var cis = SpecialPowers.Ci.nsIConverterInputStream; 1.38 + 1.39 + var inputStream = SpecialPowers.Cc["@mozilla.org/intl/converter-input-stream;1"] 1.40 + .createInstance(cis); 1.41 + inputStream.init(chann.open(), aCharset, 1024, cis.DEFAULT_REPLACEMENT_CHARACTER); 1.42 + var str = {}, content = ''; 1.43 + while (inputStream.readString(4096, str) != 0) { 1.44 + content += str.value; 1.45 + } 1.46 + return content; 1.47 +} 1.48 + 1.49 +function isRoughly(actual, expected, message) { 1.50 + return is(actual.replace("<!DOCTYPE HTML", "<!DOCTYPE html"), 1.51 + expected, 1.52 + message); 1.53 +} 1.54 + 1.55 +function testHtmlSerializer_1 () { 1.56 + const de = SpecialPowers.Ci.nsIDocumentEncoder; 1.57 + var encoder = SpecialPowers.Cc["@mozilla.org/layout/documentEncoder;1?type=text/html"] 1.58 + .createInstance(SpecialPowers.Ci.nsIDocumentEncoder); 1.59 + 1.60 + var doc = $("testframe").contentDocument; 1.61 + var out, expected; 1.62 + 1.63 + // in the following tests, we must use the OutputLFLineBreak flag, to avoid 1.64 + // to have the default line break of the platform in the result, so the test 1.65 + // can pass on all platform 1.66 + 1.67 + //------------ OutputEncodeW3CEntities 1.68 + encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeW3CEntities); 1.69 + out = encoder.encodeToString(); 1.70 + expected = loadFileContent("file_htmlserializer_2_entw3c.html"); 1.71 + isRoughly(out, expected, "test OutputEncodeW3CEntities"); 1.72 + 1.73 + //------------ OutputEncodeBasicEntities 1.74 + encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeBasicEntities); 1.75 + out = encoder.encodeToString(); 1.76 + expected = loadFileContent("file_htmlserializer_2_basic.html"); 1.77 + isRoughly(out, expected, "test OutputEncodeBasicEntities"); 1.78 + 1.79 + //------------ OutputEncodeLatin1Entities 1.80 + encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeLatin1Entities); 1.81 + out = encoder.encodeToString(); 1.82 + expected = loadFileContent("file_htmlserializer_2_latin1.html"); 1.83 + isRoughly(out, expected, "test OutputEncodeLatin1Entities"); 1.84 + 1.85 + //------------ OutputEncodeHTMLEntities 1.86 + encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeHTMLEntities); 1.87 + out = encoder.encodeToString(); 1.88 + expected = loadFileContent("file_htmlserializer_2_enthtml.html"); 1.89 + isRoughly(out, expected, "test OutputEncodeHTMLEntities"); 1.90 + 1.91 + 1.92 + // tests on the serialization of selections 1.93 + 1.94 + var node = document.getElementById('draggable'); 1.95 + 1.96 + var select = window.getSelection(); 1.97 + select.selectAllChildren(node); 1.98 + 1.99 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.100 + encoder.setSelection(select); 1.101 + out = encoder.encodeToString(); 1.102 + expected = 'This is a <em>draggable</em> bit of text.'; 1.103 + is(out, expected, "test selection"); 1.104 + 1.105 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.106 + encoder.setSelection(null); 1.107 + encoder.setContainerNode(node); 1.108 + out = encoder.encodeToString(); 1.109 + expected = 'This is a <em>draggable</em> bit of text.'; 1.110 + is(out, expected, "test container node"); 1.111 + 1.112 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.113 + encoder.setNode(node); 1.114 + out = encoder.encodeToString(); 1.115 + expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>"; 1.116 + is(out, expected, "test node"); 1.117 + 1.118 + node = document.getElementById('aList'); 1.119 + 1.120 + var select = window.getSelection(); 1.121 + select.selectAllChildren(node); 1.122 + 1.123 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.124 + encoder.setSelection(select); 1.125 + out = encoder.encodeToString(); 1.126 + 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'; 1.127 + is(out, expected, "test list selection"); 1.128 + 1.129 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.130 + encoder.setSelection(null); 1.131 + encoder.setContainerNode(node); 1.132 + out = encoder.encodeToString(); 1.133 + 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'; 1.134 + is(out, expected, "test list container node"); 1.135 + 1.136 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.137 + encoder.setNode(node); 1.138 + out = encoder.encodeToString(); 1.139 + 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>"; 1.140 + is(out, expected, "test list node"); 1.141 + 1.142 + var liList = node.getElementsByTagName("li"); 1.143 + var range = document.createRange(); 1.144 + 1.145 + // selection start at the first child of the ol, and end after the element ol 1.146 + range.setStart(node, 1); 1.147 + range.setEnd(node.parentNode, 2); 1.148 + select.removeAllRanges(); 1.149 + select.addRange(range); 1.150 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.151 + encoder.setSelection(select); 1.152 + out = encoder.encodeToString(); 1.153 + 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>'; 1.154 + is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); 1.155 + 1.156 + // selection start at the third child of the ol, and end after the element ol 1.157 + range.setStart(node, 3); 1.158 + range.setEnd(node.parentNode, 2); 1.159 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.160 + encoder.setSelection(select); 1.161 + out = encoder.encodeToString(); 1.162 + 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>'; 1.163 + is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); 1.164 + 1.165 + 1.166 + // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 1.167 + range.setStart(node, 3); 1.168 + range.setEnd(node.parentNode, 2); 1.169 + node.setAttribute("start","5"); 1.170 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.171 + encoder.setSelection(select); 1.172 + out = encoder.encodeToString(); 1.173 + 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>'; 1.174 + 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"); 1.175 + 1.176 + 1.177 + // selection contains only some child of the ol 1.178 + node.removeAttribute("start"); 1.179 + range.setStart(node, 3); 1.180 + range.setEnd(node, 5); 1.181 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.182 + encoder.setSelection(select); 1.183 + out = encoder.encodeToString(); 1.184 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.185 + is(out, expected, "test list selection with range: selection contains only some child of the ol"); 1.186 + 1.187 + // selection contains only some child of the ol + ol start at the value 5 1.188 + node.setAttribute("start","5"); 1.189 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.190 + encoder.setSelection(select); 1.191 + out = encoder.encodeToString(); 1.192 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.193 + is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); 1.194 + 1.195 + // selection contains only some child of the ol + a value is set on the first li 1.196 + node.removeAttribute("start"); 1.197 + liList[0].setAttribute("value","8"); 1.198 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.199 + encoder.setSelection(select); 1.200 + out = encoder.encodeToString(); 1.201 + expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; 1.202 + is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); 1.203 + 1.204 + 1.205 + 1.206 + // test on short attributes 1.207 + node = document.getElementById('shortattr1'); 1.208 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.209 + encoder.setNode(node); 1.210 + out = encoder.encodeToString(); 1.211 + expected = '<input id="shortattr1" checked="checked" value="" disabled="disabled" ismap="ismap" readonly="readonly" foo="">'; 1.212 + is(out, expected, "test short attr #1"); 1.213 + 1.214 + node = document.getElementById('shortattr2'); 1.215 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.216 + encoder.setNode(node); 1.217 + out = encoder.encodeToString(); 1.218 + expected = '<ol id="shortattr2" compact="compact"><li></li></ol>'; 1.219 + is(out, expected, "test short attr #2"); 1.220 + 1.221 + node = document.getElementById('shortattr3'); 1.222 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.223 + encoder.setNode(node); 1.224 + out = encoder.encodeToString(); 1.225 + expected = '<object id="shortattr3" declare="declare"></object>'; 1.226 + is(out, expected, "test short attr #3"); 1.227 + 1.228 + node = document.getElementById('shortattr4'); 1.229 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.230 + encoder.setNode(node); 1.231 + out = encoder.encodeToString(); 1.232 + expected = '<script id="shortattr4" defer="defer"></'+ 'script>'; 1.233 + is(out, expected, "test short attr #4"); 1.234 + 1.235 + node = document.getElementById('shortattr5'); 1.236 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.237 + encoder.setNode(node); 1.238 + out = encoder.encodeToString(); 1.239 + expected = '<select id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>'; 1.240 + is(out, expected, "test short attr #5"); 1.241 + 1.242 + node = document.getElementById('shortattr6'); 1.243 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.244 + encoder.setNode(node); 1.245 + out = encoder.encodeToString(); 1.246 + expected = '<hr id="shortattr6" noshade="noshade">'; 1.247 + is(out, expected, "test short attr #6"); 1.248 + 1.249 + node = document.getElementById('shortattr7'); 1.250 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.251 + encoder.setNode(node); 1.252 + out = encoder.encodeToString(); 1.253 + expected = '<div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></foo></div>'; 1.254 + is(out, expected, "test short attr #7"); 1.255 + 1.256 + // test on _moz and -moz attr 1.257 + node = document.getElementById('mozattr'); 1.258 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.259 + encoder.setNode(node); 1.260 + out = encoder.encodeToString(); 1.261 + expected = '<div id="mozattr" __moz_b="b"> lorem ipsum</div>'; 1.262 + is(out, expected, "test -moz/_moz attr"); 1.263 + 1.264 + node.setAttribute('_moz_c','barc'); 1.265 + node.setAttribute('_-moz_d','bard'); 1.266 + node.setAttribute('__moz_e','bare'); 1.267 + 1.268 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.269 + encoder.setNode(node); 1.270 + out = encoder.encodeToString(); 1.271 + expected = '<div __moz_e="bare" _-moz_d="bard" id="mozattr" __moz_b="b"> lorem ipsum</div>'; 1.272 + is(out, expected, "test -moz/_moz attr #2"); 1.273 + 1.274 + SimpleTest.finish(); 1.275 +} 1.276 + 1.277 + 1.278 +SimpleTest.waitForExplicitFinish(); 1.279 + 1.280 +addLoadEvent(testHtmlSerializer_1); 1.281 + 1.282 +</script> 1.283 +</pre> 1.284 +<div style="display: none"> 1.285 + 1.286 +<div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div> 1.287 + 1.288 +</div> 1.289 +<div style="display: none"> 1.290 + 1.291 +<ol id="aList"> 1.292 + <li>Lorem ipsum dolor</li> 1.293 + <li>sit amet, <strong>consectetuer</strong> </li> 1.294 + <li>adipiscing elit</li> 1.295 + <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> 1.296 + <li>aptent taciti</li> 1.297 +</ol> 1.298 + 1.299 + 1.300 +<!-- test for some short attr --> 1.301 +<div id="shortattr"> 1.302 + <input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo=""> 1.303 + <ol id="shortattr2" compact=""><li></li></ol> 1.304 + <object id="shortattr3" declare=""></object> 1.305 + <script id="shortattr4" defer=""></script> 1.306 + <select id="shortattr5" multiple=""><option selected="">aaa</option></select> 1.307 + <hr noshade="" id="shortattr6"> 1.308 + <div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></div> 1.309 + <div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div> 1.310 +</div> 1.311 + 1.312 + 1.313 + 1.314 +</div> 1.315 +</body> 1.316 +</html> 1.317 \ No newline at end of file