Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML> |
michael@0 | 2 | <html> |
michael@0 | 3 | <!-- |
michael@0 | 4 | --> |
michael@0 | 5 | <head> |
michael@0 | 6 | <title>Test HTML serializer with entities</title> |
michael@0 | 7 | <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 8 | <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> |
michael@0 | 9 | </head> |
michael@0 | 10 | <body> |
michael@0 | 11 | <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=424359">Mozilla Bug </a> |
michael@0 | 12 | <p id="display"></p> |
michael@0 | 13 | <div id="content" style="display: none"> |
michael@0 | 14 | <iframe id="testframe" src="file_htmlserializer_2.html"> |
michael@0 | 15 | </iframe> |
michael@0 | 16 | </div> |
michael@0 | 17 | <pre id="test"> |
michael@0 | 18 | <script class="testbody" type="text/javascript"> |
michael@0 | 19 | |
michael@0 | 20 | |
michael@0 | 21 | function loadFileContent(aFile, aCharset) { |
michael@0 | 22 | //if(aAsIso == undefined) aAsIso = false; |
michael@0 | 23 | if(aCharset == undefined) |
michael@0 | 24 | aCharset = 'UTF-8'; |
michael@0 | 25 | |
michael@0 | 26 | var baseUri = SpecialPowers.Cc['@mozilla.org/network/standard-url;1'] |
michael@0 | 27 | .createInstance(SpecialPowers.Ci.nsIURI); |
michael@0 | 28 | baseUri.spec = window.location.href; |
michael@0 | 29 | |
michael@0 | 30 | var ios = SpecialPowers.Cc['@mozilla.org/network/io-service;1'] |
michael@0 | 31 | .getService(SpecialPowers.Ci.nsIIOService); |
michael@0 | 32 | var chann = ios.newChannel(aFile, aCharset, baseUri); |
michael@0 | 33 | |
michael@0 | 34 | var cis = SpecialPowers.Ci.nsIConverterInputStream; |
michael@0 | 35 | |
michael@0 | 36 | var inputStream = SpecialPowers.Cc["@mozilla.org/intl/converter-input-stream;1"] |
michael@0 | 37 | .createInstance(cis); |
michael@0 | 38 | inputStream.init(chann.open(), aCharset, 1024, cis.DEFAULT_REPLACEMENT_CHARACTER); |
michael@0 | 39 | var str = {}, content = ''; |
michael@0 | 40 | while (inputStream.readString(4096, str) != 0) { |
michael@0 | 41 | content += str.value; |
michael@0 | 42 | } |
michael@0 | 43 | return content; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | function isRoughly(actual, expected, message) { |
michael@0 | 47 | return is(actual.replace("<!DOCTYPE HTML", "<!DOCTYPE html"), |
michael@0 | 48 | expected, |
michael@0 | 49 | message); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | function testHtmlSerializer_1 () { |
michael@0 | 53 | const de = SpecialPowers.Ci.nsIDocumentEncoder; |
michael@0 | 54 | var encoder = SpecialPowers.Cc["@mozilla.org/layout/documentEncoder;1?type=text/html"] |
michael@0 | 55 | .createInstance(SpecialPowers.Ci.nsIDocumentEncoder); |
michael@0 | 56 | |
michael@0 | 57 | var doc = $("testframe").contentDocument; |
michael@0 | 58 | var out, expected; |
michael@0 | 59 | |
michael@0 | 60 | // in the following tests, we must use the OutputLFLineBreak flag, to avoid |
michael@0 | 61 | // to have the default line break of the platform in the result, so the test |
michael@0 | 62 | // can pass on all platform |
michael@0 | 63 | |
michael@0 | 64 | //------------ OutputEncodeW3CEntities |
michael@0 | 65 | encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeW3CEntities); |
michael@0 | 66 | out = encoder.encodeToString(); |
michael@0 | 67 | expected = loadFileContent("file_htmlserializer_2_entw3c.html"); |
michael@0 | 68 | isRoughly(out, expected, "test OutputEncodeW3CEntities"); |
michael@0 | 69 | |
michael@0 | 70 | //------------ OutputEncodeBasicEntities |
michael@0 | 71 | encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeBasicEntities); |
michael@0 | 72 | out = encoder.encodeToString(); |
michael@0 | 73 | expected = loadFileContent("file_htmlserializer_2_basic.html"); |
michael@0 | 74 | isRoughly(out, expected, "test OutputEncodeBasicEntities"); |
michael@0 | 75 | |
michael@0 | 76 | //------------ OutputEncodeLatin1Entities |
michael@0 | 77 | encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeLatin1Entities); |
michael@0 | 78 | out = encoder.encodeToString(); |
michael@0 | 79 | expected = loadFileContent("file_htmlserializer_2_latin1.html"); |
michael@0 | 80 | isRoughly(out, expected, "test OutputEncodeLatin1Entities"); |
michael@0 | 81 | |
michael@0 | 82 | //------------ OutputEncodeHTMLEntities |
michael@0 | 83 | encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputEncodeHTMLEntities); |
michael@0 | 84 | out = encoder.encodeToString(); |
michael@0 | 85 | expected = loadFileContent("file_htmlserializer_2_enthtml.html"); |
michael@0 | 86 | isRoughly(out, expected, "test OutputEncodeHTMLEntities"); |
michael@0 | 87 | |
michael@0 | 88 | |
michael@0 | 89 | // tests on the serialization of selections |
michael@0 | 90 | |
michael@0 | 91 | var node = document.getElementById('draggable'); |
michael@0 | 92 | |
michael@0 | 93 | var select = window.getSelection(); |
michael@0 | 94 | select.selectAllChildren(node); |
michael@0 | 95 | |
michael@0 | 96 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 97 | encoder.setSelection(select); |
michael@0 | 98 | out = encoder.encodeToString(); |
michael@0 | 99 | expected = 'This is a <em>draggable</em> bit of text.'; |
michael@0 | 100 | is(out, expected, "test selection"); |
michael@0 | 101 | |
michael@0 | 102 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 103 | encoder.setSelection(null); |
michael@0 | 104 | encoder.setContainerNode(node); |
michael@0 | 105 | out = encoder.encodeToString(); |
michael@0 | 106 | expected = 'This is a <em>draggable</em> bit of text.'; |
michael@0 | 107 | is(out, expected, "test container node"); |
michael@0 | 108 | |
michael@0 | 109 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 110 | encoder.setNode(node); |
michael@0 | 111 | out = encoder.encodeToString(); |
michael@0 | 112 | expected = "<div id=\"draggable\" ondragstart=\"doDragStartSelection(event)\">This is a <em>draggable</em> bit of text.</div>"; |
michael@0 | 113 | is(out, expected, "test node"); |
michael@0 | 114 | |
michael@0 | 115 | node = document.getElementById('aList'); |
michael@0 | 116 | |
michael@0 | 117 | var select = window.getSelection(); |
michael@0 | 118 | select.selectAllChildren(node); |
michael@0 | 119 | |
michael@0 | 120 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 121 | encoder.setSelection(select); |
michael@0 | 122 | out = encoder.encodeToString(); |
michael@0 | 123 | 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'; |
michael@0 | 124 | is(out, expected, "test list selection"); |
michael@0 | 125 | |
michael@0 | 126 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 127 | encoder.setSelection(null); |
michael@0 | 128 | encoder.setContainerNode(node); |
michael@0 | 129 | out = encoder.encodeToString(); |
michael@0 | 130 | 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'; |
michael@0 | 131 | is(out, expected, "test list container node"); |
michael@0 | 132 | |
michael@0 | 133 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 134 | encoder.setNode(node); |
michael@0 | 135 | out = encoder.encodeToString(); |
michael@0 | 136 | 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>"; |
michael@0 | 137 | is(out, expected, "test list node"); |
michael@0 | 138 | |
michael@0 | 139 | var liList = node.getElementsByTagName("li"); |
michael@0 | 140 | var range = document.createRange(); |
michael@0 | 141 | |
michael@0 | 142 | // selection start at the first child of the ol, and end after the element ol |
michael@0 | 143 | range.setStart(node, 1); |
michael@0 | 144 | range.setEnd(node.parentNode, 2); |
michael@0 | 145 | select.removeAllRanges(); |
michael@0 | 146 | select.addRange(range); |
michael@0 | 147 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 148 | encoder.setSelection(select); |
michael@0 | 149 | out = encoder.encodeToString(); |
michael@0 | 150 | 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>'; |
michael@0 | 151 | is(out, expected, "test list selection with range: selection start at the first child of the ol, and end after the element ol"); |
michael@0 | 152 | |
michael@0 | 153 | // selection start at the third child of the ol, and end after the element ol |
michael@0 | 154 | range.setStart(node, 3); |
michael@0 | 155 | range.setEnd(node.parentNode, 2); |
michael@0 | 156 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 157 | encoder.setSelection(select); |
michael@0 | 158 | out = encoder.encodeToString(); |
michael@0 | 159 | 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>'; |
michael@0 | 160 | is(out, expected, "test list selection with range: selection start at the third child of the ol, and end after the element ol"); |
michael@0 | 161 | |
michael@0 | 162 | |
michael@0 | 163 | // selection start at the third child of the ol, and end after the element ol + ol start at the value 5 |
michael@0 | 164 | range.setStart(node, 3); |
michael@0 | 165 | range.setEnd(node.parentNode, 2); |
michael@0 | 166 | node.setAttribute("start","5"); |
michael@0 | 167 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 168 | encoder.setSelection(select); |
michael@0 | 169 | out = encoder.encodeToString(); |
michael@0 | 170 | 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>'; |
michael@0 | 171 | 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"); |
michael@0 | 172 | |
michael@0 | 173 | |
michael@0 | 174 | // selection contains only some child of the ol |
michael@0 | 175 | node.removeAttribute("start"); |
michael@0 | 176 | range.setStart(node, 3); |
michael@0 | 177 | range.setEnd(node, 5); |
michael@0 | 178 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 179 | encoder.setSelection(select); |
michael@0 | 180 | out = encoder.encodeToString(); |
michael@0 | 181 | expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
michael@0 | 182 | is(out, expected, "test list selection with range: selection contains only some child of the ol"); |
michael@0 | 183 | |
michael@0 | 184 | // selection contains only some child of the ol + ol start at the value 5 |
michael@0 | 185 | node.setAttribute("start","5"); |
michael@0 | 186 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 187 | encoder.setSelection(select); |
michael@0 | 188 | out = encoder.encodeToString(); |
michael@0 | 189 | expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
michael@0 | 190 | is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); |
michael@0 | 191 | |
michael@0 | 192 | // selection contains only some child of the ol + a value is set on the first li |
michael@0 | 193 | node.removeAttribute("start"); |
michael@0 | 194 | liList[0].setAttribute("value","8"); |
michael@0 | 195 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly); |
michael@0 | 196 | encoder.setSelection(select); |
michael@0 | 197 | out = encoder.encodeToString(); |
michael@0 | 198 | expected = '<li>sit amet, <strong>consectetuer</strong> </li>\n '; |
michael@0 | 199 | is(out, expected, "test list selection with range: selection contains only some child of the ol + ol start at the value 5"); |
michael@0 | 200 | |
michael@0 | 201 | |
michael@0 | 202 | |
michael@0 | 203 | // test on short attributes |
michael@0 | 204 | node = document.getElementById('shortattr1'); |
michael@0 | 205 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 206 | encoder.setNode(node); |
michael@0 | 207 | out = encoder.encodeToString(); |
michael@0 | 208 | expected = '<input id="shortattr1" checked="checked" value="" disabled="disabled" ismap="ismap" readonly="readonly" foo="">'; |
michael@0 | 209 | is(out, expected, "test short attr #1"); |
michael@0 | 210 | |
michael@0 | 211 | node = document.getElementById('shortattr2'); |
michael@0 | 212 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 213 | encoder.setNode(node); |
michael@0 | 214 | out = encoder.encodeToString(); |
michael@0 | 215 | expected = '<ol id="shortattr2" compact="compact"><li></li></ol>'; |
michael@0 | 216 | is(out, expected, "test short attr #2"); |
michael@0 | 217 | |
michael@0 | 218 | node = document.getElementById('shortattr3'); |
michael@0 | 219 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 220 | encoder.setNode(node); |
michael@0 | 221 | out = encoder.encodeToString(); |
michael@0 | 222 | expected = '<object id="shortattr3" declare="declare"></object>'; |
michael@0 | 223 | is(out, expected, "test short attr #3"); |
michael@0 | 224 | |
michael@0 | 225 | node = document.getElementById('shortattr4'); |
michael@0 | 226 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 227 | encoder.setNode(node); |
michael@0 | 228 | out = encoder.encodeToString(); |
michael@0 | 229 | expected = '<script id="shortattr4" defer="defer"></'+ 'script>'; |
michael@0 | 230 | is(out, expected, "test short attr #4"); |
michael@0 | 231 | |
michael@0 | 232 | node = document.getElementById('shortattr5'); |
michael@0 | 233 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 234 | encoder.setNode(node); |
michael@0 | 235 | out = encoder.encodeToString(); |
michael@0 | 236 | expected = '<select id="shortattr5" multiple="multiple"><option selected="selected">aaa</option></select>'; |
michael@0 | 237 | is(out, expected, "test short attr #5"); |
michael@0 | 238 | |
michael@0 | 239 | node = document.getElementById('shortattr6'); |
michael@0 | 240 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 241 | encoder.setNode(node); |
michael@0 | 242 | out = encoder.encodeToString(); |
michael@0 | 243 | expected = '<hr id="shortattr6" noshade="noshade">'; |
michael@0 | 244 | is(out, expected, "test short attr #6"); |
michael@0 | 245 | |
michael@0 | 246 | node = document.getElementById('shortattr7'); |
michael@0 | 247 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 248 | encoder.setNode(node); |
michael@0 | 249 | out = encoder.encodeToString(); |
michael@0 | 250 | expected = '<div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></foo></div>'; |
michael@0 | 251 | is(out, expected, "test short attr #7"); |
michael@0 | 252 | |
michael@0 | 253 | // test on _moz and -moz attr |
michael@0 | 254 | node = document.getElementById('mozattr'); |
michael@0 | 255 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 256 | encoder.setNode(node); |
michael@0 | 257 | out = encoder.encodeToString(); |
michael@0 | 258 | expected = '<div id="mozattr" __moz_b="b"> lorem ipsum</div>'; |
michael@0 | 259 | is(out, expected, "test -moz/_moz attr"); |
michael@0 | 260 | |
michael@0 | 261 | node.setAttribute('_moz_c','barc'); |
michael@0 | 262 | node.setAttribute('_-moz_d','bard'); |
michael@0 | 263 | node.setAttribute('__moz_e','bare'); |
michael@0 | 264 | |
michael@0 | 265 | encoder.init(document, "text/html", de.OutputLFLineBreak | de.OutputSelectionOnly | de.OutputRaw); |
michael@0 | 266 | encoder.setNode(node); |
michael@0 | 267 | out = encoder.encodeToString(); |
michael@0 | 268 | expected = '<div __moz_e="bare" _-moz_d="bard" id="mozattr" __moz_b="b"> lorem ipsum</div>'; |
michael@0 | 269 | is(out, expected, "test -moz/_moz attr #2"); |
michael@0 | 270 | |
michael@0 | 271 | SimpleTest.finish(); |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | |
michael@0 | 275 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 276 | |
michael@0 | 277 | addLoadEvent(testHtmlSerializer_1); |
michael@0 | 278 | |
michael@0 | 279 | </script> |
michael@0 | 280 | </pre> |
michael@0 | 281 | <div style="display: none"> |
michael@0 | 282 | |
michael@0 | 283 | <div id="draggable" ondragstart="doDragStartSelection(event)">This is a <em>draggable</em> bit of text.</div> |
michael@0 | 284 | |
michael@0 | 285 | </div> |
michael@0 | 286 | <div style="display: none"> |
michael@0 | 287 | |
michael@0 | 288 | <ol id="aList"> |
michael@0 | 289 | <li>Lorem ipsum dolor</li> |
michael@0 | 290 | <li>sit amet, <strong>consectetuer</strong> </li> |
michael@0 | 291 | <li>adipiscing elit</li> |
michael@0 | 292 | <li>Nam eu sapien. Sed viverra lacus. Donec quis ipsum. Nunc cursus aliquet lectus. Nunc vitae eros. Class</li> |
michael@0 | 293 | <li>aptent taciti</li> |
michael@0 | 294 | </ol> |
michael@0 | 295 | |
michael@0 | 296 | |
michael@0 | 297 | <!-- test for some short attr --> |
michael@0 | 298 | <div id="shortattr"> |
michael@0 | 299 | <input id="shortattr1" checked="" value="" disabled="" ismap="" readonly="" foo=""> |
michael@0 | 300 | <ol id="shortattr2" compact=""><li></li></ol> |
michael@0 | 301 | <object id="shortattr3" declare=""></object> |
michael@0 | 302 | <script id="shortattr4" defer=""></script> |
michael@0 | 303 | <select id="shortattr5" multiple=""><option selected="">aaa</option></select> |
michael@0 | 304 | <hr noshade="" id="shortattr6"> |
michael@0 | 305 | <div id="shortattr7"><foo checked="" value="" disabled="" ismap="" readonly=""></div> |
michael@0 | 306 | <div id="mozattr" _moz_a="a" __moz_b="b"> lorem ipsum</div> |
michael@0 | 307 | </div> |
michael@0 | 308 | |
michael@0 | 309 | |
michael@0 | 310 | |
michael@0 | 311 | </div> |
michael@0 | 312 | </body> |
michael@0 | 313 | </html> |