6 <title>Test for HTML serializer
</title>
7 <script type=
"text/javascript" src=
"/MochiKit/MochiKit.js"></script>
8 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
12 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=424359">Mozilla Bug
</a>
14 <div id=
"content" style=
"display: none">
15 <iframe id=
"testframe" src=
"file_htmlserializer_1.html">
19 <script class=
"testbody" type=
"text/javascript">
22 function loadFileContent(aFile, aCharset) {
23 //if(aAsIso == undefined) aAsIso = false;
24 if(aCharset == undefined)
27 var baseUri = Components.classes['@mozilla.org/network/standard-url;
1']
28 .createInstance(Components.interfaces.nsIURI);
29 baseUri.spec = window.location.href;
31 var ios = Components.classes['@mozilla.org/network/io-service;
1']
32 .getService(Components.interfaces.nsIIOService);
33 var chann = ios.newChannel(aFile, aCharset, baseUri);
35 var cis = Components.interfaces.nsIConverterInputStream;
37 var inputStream = Components.classes[
"@mozilla.org/intl/converter-input-stream;1"]
39 inputStream.init(chann.open(), aCharset,
1024, cis.DEFAULT_REPLACEMENT_CHARACTER);
40 var str = {}, content = '';
41 while (inputStream.readString(
4096, str) !=
0) {
48 function testHtmlSerializer_1 () {
49 netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect");
50 const de = Components.interfaces.nsIDocumentEncoder
51 var encoder = Components.classes[
"@mozilla.org/layout/documentEncoder;1?type=text/html"]
52 .createInstance(Components.interfaces.nsIDocumentEncoder);
54 var doc = $(
"testframe").contentDocument;
57 // in the following tests, we must use the OutputLFLineBreak flag, to avoid
58 // to have the default line break of the platform in the result, so the test
59 // can pass on all platform
61 //------------ no flags
62 encoder.init(doc,
"text/html", de.OutputLFLineBreak);
63 encoder.setCharset(
"UTF-8");
64 out = encoder.encodeToString();
65 expected = loadFileContent(
"file_htmlserializer_1_noflag.html");
66 is(out, expected,
"test no flags");
68 //------------- unsupported flags
69 // since the following flags are not supported, we should
70 // have a result like the one without flag
71 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputPreformatted);
72 out = encoder.encodeToString();
73 is(out, expected,
"test OutputPreformatted");
75 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputWrap);
76 out = encoder.encodeToString();
77 is(out, expected,
"test OutputWrap");
79 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputFormatFlowed);
80 out = encoder.encodeToString();
81 is(out, expected,
"test OutputFormatFlowed");
83 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputNoScriptContent);
84 out = encoder.encodeToString();
85 is(out, expected,
"test OutputNoScriptContent");
87 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputNoFramesContent);
88 out = encoder.encodeToString();
89 is(out, expected,
"test OutputNoFramesContent");
92 //------------ OutputFormatted
93 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputFormatted);
94 out = encoder.encodeToString();
95 expected = loadFileContent(
"file_htmlserializer_1_format.html");
96 is(out, expected,
"test OutputFormatted");
98 //------------ OutputRaw
99 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputRaw);
100 out = encoder.encodeToString();
101 expected = loadFileContent(
"file_htmlserializer_1_raw.html");
102 is(out, expected,
"test OutputRaw");
104 //------------ OutputBodyOnly
105 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputBodyOnly);
106 out = encoder.encodeToString();
107 expected = loadFileContent(
"file_htmlserializer_1_bodyonly.html");
108 is(out, expected,
"test OutputBodyOnly");
112 //------------ OutputAbsoluteLinks
113 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputAbsoluteLinks);
114 out = encoder.encodeToString();
115 expected = loadFileContent(
"file_htmlserializer_1_links.html");
116 is(out, expected,
"test OutputAbsoluteLinks");
118 //------------ OutputLFLineBreak
119 encoder.init(doc,
"text/html",de.OutputLFLineBreak);
120 out = encoder.encodeToString();
121 expected = loadFileContent(
"file_htmlserializer_1_linebreak.html");
122 is(out, expected,
"test OutputLFLineBreak");
124 //------------ OutputCRLineBreak
125 encoder.init(doc,
"text/html",de.OutputCRLineBreak);
126 out = encoder.encodeToString();
127 expected = expected.replace(/\n/mg,
"\r");
128 is(out, expected,
"test OutputCRLineBreak");
130 //------------ OutputLFLineBreak + OutputCRLineBreak
131 encoder.init(doc,
"text/html",de.OutputLFLineBreak | de.OutputCRLineBreak);
132 out = encoder.encodeToString();
133 expected = expected.replace(/\r/mg,
"\r\n");
134 is(out, expected,
"test OutputLFLineBreak + OutputCRLineBreak");
136 //------------ OutputNoFormattingInPre
137 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputNoFormattingInPre);
138 out = encoder.encodeToString();
139 expected = loadFileContent(
"file_htmlserializer_1_noformatpre.html");
140 is(out, expected,
"test OutputNoFormattingInPre");
142 // ------------- nested body elements
143 var body2 = doc.createElement('body');
144 var p = doc.createElement('p');
145 p.appendChild(doc.createTextNode(
"this is an other body element"));
146 body2.appendChild(p);
147 var body = doc.getElementsByTagName('body')[
0];
148 body.appendChild(body2);
150 is(doc.getElementsByTagName('body').length,
2); // to be sure we have two body elements
152 encoder.init(doc,
"text/html", de.OutputLFLineBreak);
153 encoder.setCharset(
"UTF-8");
154 out = encoder.encodeToString();
155 expected = loadFileContent(
"file_htmlserializer_1_nested_body.html");
156 is(out, expected,
"test with two nested body elements");
158 // ------------- two body elements
159 body.parentNode.insertBefore(body2, body);
161 is(doc.getElementsByTagName('body').length,
2); // to be sure we have two body elements
162 encoder.init(doc,
"text/html", de.OutputLFLineBreak);
163 encoder.setCharset(
"UTF-8");
164 out = encoder.encodeToString();
165 expected = loadFileContent(
"file_htmlserializer_1_sibling_body.html");
166 is(out, expected,
"test with two body elements");
168 encoder.init(doc,
"text/html", de.OutputLFLineBreak | de.OutputBodyOnly);
169 encoder.setCharset(
"UTF-8");
170 out = encoder.encodeToString();
171 expected = loadFileContent(
"file_htmlserializer_1_sibling_body_only_body.html");
172 is(out, expected,
"test with two body elements, and output body only");
174 // --------------- no body element
175 doc.documentElement.removeChild(body);
176 doc.documentElement.removeChild(body2);
178 encoder.init(doc,
"text/html", de.OutputLFLineBreak);
179 encoder.setCharset(
"UTF-8");
180 out = encoder.encodeToString();
181 expected = loadFileContent(
"file_htmlserializer_1_no_body.html");
182 is(out, expected,
"test with no body element");
189 SimpleTest.waitForExplicitFinish();
191 addLoadEvent(testHtmlSerializer_1);
195 <!--<h1>1</h1><h2>result</h2><textarea id="t1" cols="80" rows="20"></textarea>
196 <h2>expected</h2><textarea id="t1e" cols="80" rows="20"></textarea>-->