test_bug422403-1.html (7957B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 --> 5 <head> 6 <title>Test for XHTML 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=422043">Mozilla Bug </a> 12 <p id="display"></p> 13 <div id="content" style="display: none"> 14 </div> 15 <!-- IMPORTANT: This iframe needs to actually be displayed, so the serializer 16 sees the relevant styles for <pre> elements. --> 17 <iframe id="testframe" src="file_xhtmlserializer_1.xhtml"></iframe> 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 55 function testHtmlSerializer_1 () { 56 const de = SpecialPowers.Ci.nsIDocumentEncoder 57 var encoder = SpecialPowers.Cu.createDocumentEncoder("application/xhtml+xml"); 58 59 var doc = SpecialPowers.wrap($("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 //------------ no flags 67 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak); 68 encoder.setCharset("UTF-8"); 69 out = encoder.encodeToString(); 70 expected = loadFileContent("file_xhtmlserializer_1_noflag.xhtml"); 71 is(out, expected, "test no flags"); 72 73 //------------- unsupported flags 74 // since the following flags are not supported, we should 75 // have a result like the one without flag 76 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputPreformatted); 77 out = encoder.encodeToString(); 78 is(out, expected, "test OutputPreformatted"); 79 80 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputFormatFlowed); 81 out = encoder.encodeToString(); 82 is(out, expected, "test OutputFormatFlowed"); 83 84 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputNoScriptContent); 85 out = encoder.encodeToString(); 86 is(out, expected, "test OutputNoScriptContent"); 87 88 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputNoFramesContent); 89 out = encoder.encodeToString(); 90 is(out, expected, "test OutputNoFramesContent"); 91 92 93 //------------ OutputWrap 94 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputWrap); 95 out = encoder.encodeToString(); 96 expected = loadFileContent("file_xhtmlserializer_1_wrap.xhtml"); 97 is(out, expected, "test OutputWrap"); 98 99 //------------ OutputFormatted 100 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputFormatted); 101 out = encoder.encodeToString(); 102 expected = loadFileContent("file_xhtmlserializer_1_format.xhtml"); 103 is(out, expected, "test OutputFormatted"); 104 105 //------------ OutputRaw 106 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputRaw); 107 out = encoder.encodeToString(); 108 expected = loadFileContent("file_xhtmlserializer_1_raw.xhtml"); 109 is(out, expected, "test OutputRaw"); 110 111 //------------ OutputBodyOnly 112 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputBodyOnly); 113 out = encoder.encodeToString(); 114 expected = loadFileContent("file_xhtmlserializer_1_bodyonly.xhtml"); 115 is(out, expected, "test OutputBodyOnly"); 116 117 118 //------------ OutputAbsoluteLinks 119 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputAbsoluteLinks); 120 out = encoder.encodeToString(); 121 expected = loadFileContent("file_xhtmlserializer_1_links.xhtml").trim('\n'); 122 is(out, expected, "test OutputAbsoluteLinks"); 123 124 //------------ OutputLFLineBreak 125 encoder.init(doc, "application/xhtml+xml",de.OutputLFLineBreak); 126 out = encoder.encodeToString(); 127 expected = loadFileContent("file_xhtmlserializer_1_linebreak.xhtml"); 128 is(out, expected, "test OutputLFLineBreak"); 129 130 //------------ OutputCRLineBreak 131 encoder.init(doc, "application/xhtml+xml",de.OutputCRLineBreak); 132 out = encoder.encodeToString(); 133 expected = expected.replace(/\n/mg, "\r"); 134 is(out, expected, "test OutputCRLineBreak"); 135 136 //------------ OutputLFLineBreak + OutputCRLineBreak 137 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputCRLineBreak); 138 out = encoder.encodeToString(); 139 expected = expected.replace(/\r/mg, "\r\n"); 140 is(out, expected, "test OutputLFLineBreak + OutputCRLineBreak"); 141 142 //------------ OutputNoFormattingInPre 143 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputNoFormattingInPre); 144 out = encoder.encodeToString(); 145 expected = loadFileContent("file_xhtmlserializer_1_noformatpre.xhtml"); 146 is(out, expected, "test OutputNoFormattingInPre"); 147 148 // ------------- nested body elements 149 var body2 = doc.createElement('body'); 150 var p = doc.createElement('p'); 151 p.appendChild(doc.createTextNode("this is an other body element")); 152 body2.appendChild(p); 153 var body = doc.getElementsByTagName('body')[0]; 154 body.appendChild(body2); 155 156 is(doc.getElementsByTagName('body').length, 2); // to be sure we have two body elements 157 158 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak); 159 encoder.setCharset("UTF-8"); 160 out = encoder.encodeToString(); 161 expected = loadFileContent("file_xhtmlserializer_1_nested_body.xhtml"); 162 is(out, expected, "test with two nested body elements"); 163 164 // ------------- two body elements 165 body.parentNode.insertBefore(body2, body); 166 167 is(doc.getElementsByTagName('body').length, 2); // to be sure we have two body elements 168 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak); 169 encoder.setCharset("UTF-8"); 170 out = encoder.encodeToString(); 171 expected = loadFileContent("file_xhtmlserializer_1_sibling_body.xhtml"); 172 is(out, expected, "test with two body elements"); 173 174 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak | de.OutputBodyOnly); 175 encoder.setCharset("UTF-8"); 176 out = encoder.encodeToString(); 177 expected = loadFileContent("file_xhtmlserializer_1_sibling_body_only_body.xhtml"); 178 is(out, expected, "test with two body elements, and output body only"); 179 180 // --------------- no body element 181 doc.documentElement.removeChild(body); 182 doc.documentElement.removeChild(body2); 183 184 encoder.init(doc, "application/xhtml+xml", de.OutputLFLineBreak); 185 encoder.setCharset("UTF-8"); 186 out = encoder.encodeToString(); 187 expected = loadFileContent("file_xhtmlserializer_1_no_body.xhtml"); 188 is(out, expected, "test with no body element"); 189 190 SimpleTest.finish(); 191 } 192 193 194 SimpleTest.waitForExplicitFinish(); 195 196 addLoadEvent(testHtmlSerializer_1); 197 198 </script> 199 </pre> 200 </body> 201 </html>