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