1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/test_bug422403-2.xhtml Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,289 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html xmlns="http://www.w3.org/1999/xhtml"> 1.6 +<!-- 1.7 +--> 1.8 +<head> 1.9 + <title>Test XHTML serializer with entities and selection</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=422043">Mozilla Bug </a> 1.15 +<p id="display"></p> 1.16 +<div id="content" style="display: none"> 1.17 + <iframe id="testframe" src="file_xhtmlserializer_2.xhtml"> 1.18 + </iframe> 1.19 +</div> 1.20 +<pre id="test"> 1.21 +<script class="testbody" type="text/javascript"> 1.22 +//<![CDATA[ 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 + 1.50 +function testHtmlSerializer_1 () { 1.51 + const de = SpecialPowers.Ci.nsIDocumentEncoder 1.52 + var encoder = SpecialPowers.Cc["@mozilla.org/layout/documentEncoder;1?type=application/xhtml+xml"] 1.53 + .createInstance(SpecialPowers.Ci.nsIDocumentEncoder); 1.54 + 1.55 + var doc = $("testframe").contentDocument; 1.56 + var out, expected; 1.57 + 1.58 + // in the following tests, we must use the OutputLFLineBreak flag, to avoid 1.59 + // to have the default line break of the platform in the result, so the test 1.60 + // can pass on all platform 1.61 + 1.62 + //------------ OutputEncodeW3CEntities 1.63 + encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeW3CEntities); 1.64 + out = encoder.encodeToString(); 1.65 + expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 1.66 + is(out, expected, "test OutputEncodeW3CEntities"); 1.67 + 1.68 + //------------ OutputEncodeBasicEntities 1.69 + encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeBasicEntities); 1.70 + out = encoder.encodeToString(); 1.71 + expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 1.72 + is(out, expected, "test OutputEncodeBasicEntities"); 1.73 + 1.74 + //------------ OutputEncodeLatin1Entities 1.75 + encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeLatin1Entities); 1.76 + out = encoder.encodeToString(); 1.77 + expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 1.78 + is(out, expected, "test OutputEncodeLatin1Entities"); 1.79 + 1.80 + //------------ OutputEncodeHTMLEntities 1.81 + encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputEncodeHTMLEntities); 1.82 + out = encoder.encodeToString(); 1.83 + expected = loadFileContent("file_xhtmlserializer_2_basic.xhtml"); 1.84 + is(out, expected, "test OutputEncodeHTMLEntities"); 1.85 + 1.86 + // tests on the serialization of selections 1.87 + 1.88 + var node = document.getElementById('draggable'); 1.89 + 1.90 + var select = window.getSelection(); 1.91 + select.selectAllChildren(node); 1.92 + 1.93 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.94 + encoder.setSelection(select); 1.95 + out = encoder.encodeToString(); 1.96 + expected = 'This is a <em xmlns=\"http://www.w3.org/1999/xhtml\">draggable</em> bit of text.'; 1.97 + is(out, expected, "test selection"); 1.98 + 1.99 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.100 + encoder.setSelection(null); 1.101 + encoder.setContainerNode(node); 1.102 + out = encoder.encodeToString(); 1.103 + expected = 'This is a <em xmlns=\"http://www.w3.org/1999/xhtml\">draggable</em> bit of text.'; 1.104 + is(out, expected, "test container node"); 1.105 + 1.106 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.107 + encoder.setNode(node); 1.108 + out = encoder.encodeToString(); 1.109 + expected = "<div xmlns=\"http://www.w3.org/1999/xhtml\" id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>"; 1.110 + is(out, expected, "test node"); 1.111 + 1.112 + node = document.getElementById('aList'); 1.113 + 1.114 + var select = window.getSelection(); 1.115 + select.selectAllChildren(node); 1.116 + 1.117 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.118 + encoder.setSelection(select); 1.119 + out = encoder.encodeToString(); 1.120 + 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'; 1.121 + is(out, expected, "test list selection"); 1.122 + 1.123 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.124 + encoder.setSelection(null); 1.125 + encoder.setContainerNode(node); 1.126 + out = encoder.encodeToString(); 1.127 + 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'; 1.128 + is(out, expected, "test list container node"); 1.129 + 1.130 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.131 + encoder.setNode(node); 1.132 + out = encoder.encodeToString(); 1.133 + 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>"; 1.134 + is(out, expected, "test list node"); 1.135 + 1.136 + var liList = node.getElementsByTagName("li"); 1.137 + var range = document.createRange(); 1.138 + 1.139 + // selection start at the first child of the ol, and end after the element ol 1.140 + range.setStart(node, 1); 1.141 + range.setEnd(node.parentNode, 2); 1.142 + select.removeAllRanges(); 1.143 + select.addRange(range); 1.144 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.145 + encoder.setSelection(select); 1.146 + out = encoder.encodeToString(); 1.147 + 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>'; 1.148 + is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); 1.149 + 1.150 + // selection start at the third child of the ol, and end after the element ol 1.151 + range.setStart(node, 3); 1.152 + range.setEnd(node.parentNode, 2); 1.153 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.154 + encoder.setSelection(select); 1.155 + out = encoder.encodeToString(); 1.156 + 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>'; 1.157 + is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); 1.158 + 1.159 + 1.160 + // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 1.161 + range.setStart(node, 3); 1.162 + range.setEnd(node.parentNode, 2); 1.163 + node.setAttribute("start","5"); 1.164 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.165 + encoder.setSelection(select); 1.166 + out = encoder.encodeToString(); 1.167 + 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>'; 1.168 + 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.169 + 1.170 + 1.171 + // selection contains only some child of the ol 1.172 + node.removeAttribute("start"); 1.173 + range.setStart(node, 3); 1.174 + range.setEnd(node, 5); 1.175 + encoder.init(document, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputSelectionOnly); 1.176 + encoder.setSelection(select); 1.177 + out = encoder.encodeToString(); 1.178 + expected = '<li xmlns=\"http://www.w3.org/1999/xhtml\">sit amet, <strong>consectetuer</strong> </li>\n '; 1.179 + is(out, expected, "test list selection with range: selection contains only some child of the ol"); 1.180 + 1.181 + 1.182 + // test on short attributes 1.183 + 1.184 + node = document.getElementById('shortattr1'); 1.185 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.186 + encoder.setNode(node); 1.187 + out = encoder.encodeToString(); 1.188 + 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="" />'; 1.189 + is(out, expected, "test short attr #1"); 1.190 + 1.191 + node = document.getElementById('shortattr2'); 1.192 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.193 + encoder.setNode(node); 1.194 + out = encoder.encodeToString(); 1.195 + expected = '<ol xmlns="http://www.w3.org/1999/xhtml" id="shortattr2" compact="compact"><li></li></ol>'; 1.196 + is(out, expected, "test short attr #2"); 1.197 + 1.198 + node = document.getElementById('shortattr3'); 1.199 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.200 + encoder.setNode(node); 1.201 + out = encoder.encodeToString(); 1.202 + expected = '<object xmlns="http://www.w3.org/1999/xhtml" id="shortattr3" declare="declare"></object>'; 1.203 + is(out, expected, "test short attr #3"); 1.204 + 1.205 + node = document.getElementById('shortattr4'); 1.206 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.207 + encoder.setNode(node); 1.208 + out = encoder.encodeToString(); 1.209 + expected = '<script xmlns="http://www.w3.org/1999/xhtml" id="shortattr4" defer="defer"></script>'; 1.210 + is(out, expected, "test short attr #4"); 1.211 + 1.212 + node = document.getElementById('shortattr5'); 1.213 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.214 + encoder.setNode(node); 1.215 + out = encoder.encodeToString(); 1.216 + expected = '<select xmlns="http://www.w3.org/1999/xhtml" id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>'; 1.217 + is(out, expected, "test short attr #5"); 1.218 + 1.219 + node = document.getElementById('shortattr6'); 1.220 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.221 + encoder.setNode(node); 1.222 + out = encoder.encodeToString(); 1.223 + expected = '<hr xmlns="http://www.w3.org/1999/xhtml" noshade="noshade" id="shortattr6" />'; 1.224 + is(out, expected, "test short attr #6"); 1.225 + 1.226 + node = document.getElementById('shortattr7'); 1.227 + encoder.init(document, "application/xhtml+xml",de.OutputSelectionOnly | de.OutputRaw); 1.228 + encoder.setNode(node); 1.229 + out = encoder.encodeToString(); 1.230 + 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>'; 1.231 + is(out, expected, "test short attr #7"); 1.232 + 1.233 + // test on _moz and -moz attr 1.234 + node = document.getElementById('mozattr'); 1.235 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.236 + encoder.setNode(node); 1.237 + out = encoder.encodeToString(); 1.238 + expected = '<div __moz_b="b" id="mozattr"> lorem ipsum</div>'; 1.239 + is(out, expected, "test -moz/_moz attr"); 1.240 + 1.241 + node.setAttribute('_moz_c','barc'); 1.242 + node.setAttribute('_-moz_d','bard'); 1.243 + node.setAttribute('__moz_e','bare'); 1.244 + 1.245 + encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); 1.246 + encoder.setNode(node); 1.247 + out = encoder.encodeToString(); 1.248 + expected = '<div __moz_e="bare" _-moz_d="bard" __moz_b="b" id="mozattr"> lorem ipsum</div>'; 1.249 + is(out, expected, "test -moz/_moz attr #2"); 1.250 + 1.251 + SimpleTest.finish(); 1.252 +} 1.253 + 1.254 + 1.255 +SimpleTest.waitForExplicitFinish(); 1.256 + 1.257 +addLoadEvent(testHtmlSerializer_1); 1.258 +//]]> 1.259 +</script> 1.260 +</pre> 1.261 +<div style="display: none"> 1.262 + 1.263 +<div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div> 1.264 + 1.265 +</div> 1.266 +<div style="display: none"> 1.267 + 1.268 +<ol id="aList"> 1.269 + <li>Lorem ipsum dolor</li> 1.270 + <li>sit amet, <strong>consectetuer</strong> </li> 1.271 + <li>adipiscing elit</li> 1.272 + <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> 1.273 + <li>aptent taciti</li> 1.274 +</ol> 1.275 + 1.276 +<!-- test for some short attr --> 1.277 +<div id="shortattr" xmlns:foo="http://mozilla.org/ns/any"> 1.278 + <input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo:checked="" foo:disabled=""/> 1.279 + <ol id="shortattr2" compact=""><li></li></ol> 1.280 + <object id="shortattr3" declare="" /> 1.281 + <script id="shortattr4" defer="" /> 1.282 + <select id="shortattr5" multiple=""><option selected="">aaa</option></select> 1.283 + <hr noshade="" id="shortattr6"/> 1.284 + <div id="shortattr7"><foo:bar checked="" value="" disabled="" ismap="" readonly="" /></div> 1.285 + <div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div> 1.286 +</div> 1.287 + 1.288 +</div> 1.289 +</body> 1.290 +</html> 1.291 + 1.292 +