test_bug424359-1.html (8129B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 --> 5 <head> 6 <title>Test for HTML serializer</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=424359">Mozilla Bug </a> 12 <p id="display"></p> 13 <!-- IMPORTANT: This iframe needs to actually be displayed, so the serializer 14 sees the relevant styles for <pre> elements. --> 15 <iframe id="testframe" src="file_htmlserializer_1.html"></iframe> 16 <div id="content" style="display: none"> 17 </div> 18 <pre id="test"> 19 <script class="testbody" type="text/javascript"> 20 21 22 function loadFileContent(aFile, aCharset) { 23 //if(aAsIso == undefined) aAsIso = false; 24 if(aCharset == undefined) 25 aCharset = 'UTF-8'; 26 27 var baseUri = SpecialPowers.Cc['@mozilla.org/network/standard-url-mutator;1'] 28 .createInstance(SpecialPowers.Ci.nsIURIMutator) 29 .setSpec(window.location.href) 30 .finalize(); 31 32 var ios = SpecialPowers.Services.io; 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 function isRoughly(actual, expected, message) { 55 return is(actual.replace("<!DOCTYPE HTML", "<!DOCTYPE html"), 56 expected, 57 message); 58 } 59 60 function testHtmlSerializer_1 () { 61 const de = SpecialPowers.Ci.nsIDocumentEncoder; 62 var encoder = SpecialPowers.Cu.createDocumentEncoder("text/html"); 63 64 var doc = $("testframe").contentDocument; 65 var out, expected; 66 67 // in the following tests, we must use the OutputLFLineBreak flag, to avoid 68 // to have the default line break of the platform in the result, so the test 69 // can pass on all platform 70 71 //------------ no flags 72 encoder.init(doc, "text/html", de.OutputLFLineBreak); 73 encoder.setCharset("UTF-8"); 74 out = encoder.encodeToString(); 75 expected = loadFileContent("file_htmlserializer_1_noflag.html"); 76 isRoughly(out, expected, "test no flags"); 77 78 //------------- unsupported flags 79 // since the following flags are not supported, we should 80 // have a result like the one without flag 81 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputPreformatted); 82 out = encoder.encodeToString(); 83 isRoughly(out, expected, "test OutputPreformatted"); 84 85 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputFormatFlowed); 86 out = encoder.encodeToString(); 87 isRoughly(out, expected, "test OutputFormatFlowed"); 88 89 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputNoScriptContent); 90 out = encoder.encodeToString(); 91 isRoughly(out, expected, "test OutputNoScriptContent"); 92 93 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputNoFramesContent); 94 out = encoder.encodeToString(); 95 isRoughly(out, expected, "test OutputNoFramesContent"); 96 97 98 //------------ OutputWrap 99 encoder.init(doc, "text/html", de.OutputLFLineBreak |de.OutputWrap); 100 out = encoder.encodeToString(); 101 expected = loadFileContent("file_htmlserializer_1_wrap.html"); 102 isRoughly(out, expected, "test OutputWrap"); 103 104 //------------ OutputFormatted 105 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputFormatted); 106 out = encoder.encodeToString(); 107 expected = loadFileContent("file_htmlserializer_1_format.html"); 108 isRoughly(out, expected, "test OutputFormatted"); 109 110 //------------ OutputRaw 111 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputRaw); 112 out = encoder.encodeToString(); 113 expected = loadFileContent("file_htmlserializer_1_raw.html"); 114 isRoughly(out, expected, "test OutputRaw"); 115 116 //------------ OutputBodyOnly 117 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputBodyOnly); 118 out = encoder.encodeToString(); 119 expected = loadFileContent("file_htmlserializer_1_bodyonly.html"); 120 isRoughly(out, expected, "test OutputBodyOnly"); 121 122 123 124 //------------ OutputAbsoluteLinks 125 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputAbsoluteLinks); 126 out = encoder.encodeToString(); 127 expected = loadFileContent("file_htmlserializer_1_links.html").trim('\n'); 128 isRoughly(out, expected, "test OutputAbsoluteLinks"); 129 130 //------------ OutputLFLineBreak 131 encoder.init(doc, "text/html",de.OutputLFLineBreak); 132 out = encoder.encodeToString(); 133 expected = loadFileContent("file_htmlserializer_1_linebreak.html"); 134 isRoughly(out, expected, "test OutputLFLineBreak"); 135 136 //------------ OutputCRLineBreak 137 encoder.init(doc, "text/html",de.OutputCRLineBreak); 138 out = encoder.encodeToString(); 139 expected = expected.replace(/\n/mg, "\r"); 140 isRoughly(out, expected, "test OutputCRLineBreak"); 141 142 //------------ OutputLFLineBreak + OutputCRLineBreak 143 encoder.init(doc, "text/html",de.OutputLFLineBreak | de.OutputCRLineBreak); 144 out = encoder.encodeToString(); 145 expected = expected.replace(/\r/mg, "\r\n"); 146 isRoughly(out, expected, "test OutputLFLineBreak + OutputCRLineBreak"); 147 148 //------------ OutputNoFormattingInPre 149 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputNoFormattingInPre); 150 out = encoder.encodeToString(); 151 expected = loadFileContent("file_htmlserializer_1_noformatpre.html"); 152 isRoughly(out, expected, "test OutputNoFormattingInPre"); 153 154 // ------------- nested body elements 155 var body2 = doc.createElement('body'); 156 var p = doc.createElement('p'); 157 p.appendChild(doc.createTextNode("this is an other body element")); 158 body2.appendChild(p); 159 var body = doc.getElementsByTagName('body')[0]; 160 body.appendChild(body2); 161 162 is(doc.getElementsByTagName('body').length, 2); // to be sure we have two body elements 163 164 encoder.init(doc, "text/html", de.OutputLFLineBreak); 165 encoder.setCharset("UTF-8"); 166 out = encoder.encodeToString(); 167 expected = loadFileContent("file_htmlserializer_1_nested_body.html"); 168 isRoughly(out, expected, "test with two nested body elements"); 169 170 // ------------- two body elements 171 body.parentNode.insertBefore(body2, body); 172 173 is(doc.getElementsByTagName('body').length, 2); // to be sure we have two body elements 174 encoder.init(doc, "text/html", de.OutputLFLineBreak); 175 encoder.setCharset("UTF-8"); 176 out = encoder.encodeToString(); 177 expected = loadFileContent("file_htmlserializer_1_sibling_body.html"); 178 isRoughly(out, expected, "test with two body elements"); 179 180 encoder.init(doc, "text/html", de.OutputLFLineBreak | de.OutputBodyOnly); 181 encoder.setCharset("UTF-8"); 182 out = encoder.encodeToString(); 183 expected = loadFileContent("file_htmlserializer_1_sibling_body_only_body.html"); 184 isRoughly(out, expected, "test with two body elements, and output body only"); 185 186 // --------------- no body element 187 doc.documentElement.removeChild(body); 188 doc.documentElement.removeChild(body2); 189 190 encoder.init(doc, "text/html", de.OutputLFLineBreak); 191 encoder.setCharset("UTF-8"); 192 out = encoder.encodeToString(); 193 expected = loadFileContent("file_htmlserializer_1_no_body.html"); 194 isRoughly(out, expected, "test with no body element"); 195 196 SimpleTest.finish(); 197 } 198 199 200 SimpleTest.waitForExplicitFinish(); 201 202 addLoadEvent(testHtmlSerializer_1); 203 204 </script> 205 </pre> 206 <!--<h1>1</h1><h2>result</h2><textarea id="t1" cols="80" rows="20"></textarea> 207 <h2>expected</h2><textarea id="t1e" cols="80" rows="20"></textarea>--> 208 209 </body> 210 </html>