summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_xml_serializer.js
blob: c74d067a81151fdd65d741ec6b647ab0e340a720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

// The xml serializer uses the default line break of the plateform.
// So we need to know the value of this default line break, in order
// to build correctly the reference strings for tests.
// This variable will contain this value.
var LB;

function run_test() {

  if (mozinfo.os == "win") {
    LB = "\r\n";
  } else {
    LB = "\n";
  }

  for (var i = 0; i < tests.length && tests[i]; ++i) {
    tests[i].call();
  }
}

var tests = [
  test1,
  test2,
  test3,
  test4,
  test5,
  test6,
  test7,
  test8,
  test9,
  test10,
  null
];

function testString(str) {
  do_check_eq(roundtrip(str), str);
}

function test1() {
  // Basic round-tripping which we expect to hand back the same text
  // as we passed in (not strictly required for correctness in some of
  // those cases, but best for readability of serializer output)
  testString('<root/>');
  testString('<root><child/></root>');
  testString('<root xmlns=""/>');
  testString('<root xml:lang="en"/>');
  testString('<root xmlns="ns1"><child xmlns="ns2"/></root>')
  testString('<root xmlns="ns1"><child xmlns=""/></root>')
  testString('<a:root xmlns:a="ns1"><child/></a:root>')
  testString('<a:root xmlns:a="ns1"><a:child/></a:root>')
  testString('<a:root xmlns:a="ns1"><b:child xmlns:b="ns1"/></a:root>')
  testString('<a:root xmlns:a="ns1"><a:child xmlns:a="ns2"/></a:root>')
  testString('<a:root xmlns:a="ns1"><b:child xmlns:b="ns1" b:attr=""/></a:root>')
}

function test2() {
  // Test setting of "xmlns" attribute in the null namespace

  // XXXbz are these tests needed?  What should happen here?  These
  // may be bogus.

  // Setting random "xmlns" attribute
  var doc = ParseXML('<root xmlns="ns1"/>');
  doc.documentElement.setAttribute("xmlns", "ns2");
  do_check_serialize(doc);
}

function test3() {
  // Test basic appending of kids.  Again, we're making assumptions
  // about how our serializer will serialize simple DOMs.
  var doc = ParseXML('<root xmlns="ns1"/>');
  var root = doc.documentElement;
  var child = doc.createElementNS("ns2", "child");
  root.appendChild(child);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc), 
              '<root xmlns="ns1"><child xmlns="ns2"/></root>');
  
  doc = ParseXML('<root xmlns="ns1"/>');
  root = doc.documentElement;
  child = doc.createElementNS("ns2", "prefix:child");
  root.appendChild(child);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc), 
              '<root xmlns="ns1"><prefix:child xmlns:prefix="ns2"/></root>');
  
  doc = ParseXML('<prefix:root xmlns:prefix="ns1"/>');
  root = doc.documentElement;
  child = doc.createElementNS("ns2", "prefix:child");
  root.appendChild(child);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc), 
              '<prefix:root xmlns:prefix="ns1"><a0:child xmlns:a0="ns2"/>'+
              '</prefix:root>');
  
}

function test4() {
  // setAttributeNS tests

  var doc = ParseXML('<root xmlns="ns1"/>');
  var root = doc.documentElement;
  root.setAttributeNS("ns1", "prefix:local", "val");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<root xmlns="ns1" prefix:local="val" xmlns:prefix="ns1"/>');

  doc = ParseXML('<prefix:root xmlns:prefix="ns1"/>');
  root = doc.documentElement;
  root.setAttributeNS("ns1", "local", "val");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<prefix:root xmlns:prefix="ns1" prefix:local="val"/>');

  doc = ParseXML('<root xmlns="ns1"/>');
  root = doc.documentElement;
  root.setAttributeNS("ns2", "local", "val");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<root xmlns="ns1" a0:local="val" xmlns:a0="ns2"/>');

  // Handling of prefix-generation for non-null-namespace attributes
  // which have the same namespace as the current default namespace
  // (bug 301260).
  doc = ParseXML('<root xmlns="ns1"/>');
  root = doc.documentElement;
  root.setAttributeNS("ns1", "local", "val");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
                '<root xmlns="ns1" a0:local="val" xmlns:a0="ns1"/>');

  // Tree-walking test
  doc = ParseXML('<root xmlns="ns1" xmlns:a="ns2">'+
                 '<child xmlns:b="ns2" xmlns:a="ns3">'+
                 '<child2/></child></root>');
  root = doc.documentElement;
  // Have to QI here -- no classinfo flattening in xpcshell, apparently
  var node = root.firstChild.firstChild.QueryInterface(nsIDOMElement);
  node.setAttributeNS("ns4", "l1", "v1");
  node.setAttributeNS("ns4", "p2:l2", "v2");
  node.setAttributeNS("", "l3", "v3");
  node.setAttributeNS("ns3", "l4", "v4");
  node.setAttributeNS("ns3", "p5:l5", "v5");
  node.setAttributeNS("ns3", "a:l6", "v6");
  node.setAttributeNS("ns2", "l7", "v7");
  node.setAttributeNS("ns2", "p8:l8", "v8");
  node.setAttributeNS("ns2", "b:l9", "v9");
  node.setAttributeNS("ns2", "a:l10", "v10");
  node.setAttributeNS("ns1", "a:l11", "v11");
  node.setAttributeNS("ns1", "b:l12", "v12");
  node.setAttributeNS("ns1", "l13", "v13");
  do_check_serialize(doc);
  //  Note: we end up with "a2" as the prefix on "l11" and "l12" because we use
  //  "a1" earlier, and discard it in favor of something we get off the
  //  namespace stack, apparently
  do_check_eq(SerializeXML(doc),
              '<root xmlns="ns1" xmlns:a="ns2">'+
              '<child xmlns:b="ns2" xmlns:a="ns3">'+
              '<child2 a0:l1="v1" xmlns:a0="ns4"' +
              ' a0:l2="v2"' +
              ' l3="v3"' +
              ' a:l4="v4"' +
              ' a:l5="v5"' +
              ' a:l6="v6"' +
              ' b:l7="v7"' +
              ' b:l8="v8"' +
              ' b:l9="v9"' +
              ' b:l10="v10"' +
              ' a2:l11="v11" xmlns:a2="ns1"' +
              ' a2:l12="v12"' +
              ' a2:l13="v13"/></child></root>');
}

function test5() {
  // Handling of kids in the null namespace when the default is a
  // different namespace (bug 301260).
  var doc = ParseXML('<root xmlns="ns1"/>')
  var child = doc.createElement('child');
  doc.documentElement.appendChild(child);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<root xmlns="ns1"><child xmlns=""/></root>');
}

function test6() {
  // Handling of not using a namespace prefix (or default namespace!)
  // that's not bound to our namespace in our scope (bug 301260).
  var doc = ParseXML('<prefix:root xmlns:prefix="ns1"/>');
  var root = doc.documentElement;
  var child1 = doc.createElementNS("ns2", "prefix:child1");
  var child2 = doc.createElementNS("ns1", "prefix:child2");
  child1.appendChild(child2);
  root.appendChild(child1);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<prefix:root xmlns:prefix="ns1"><a0:child1 xmlns:a0="ns2">'+
              '<prefix:child2/></a0:child1></prefix:root>');

  doc = ParseXML('<root xmlns="ns1"><prefix:child1 xmlns:prefix="ns2"/></root>');
  root = doc.documentElement;
  child1 = root.firstChild;
  child2 = doc.createElementNS("ns1", "prefix:child2");
  child1.appendChild(child2);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<root xmlns="ns1"><prefix:child1 xmlns:prefix="ns2">'+
              '<child2/></prefix:child1></root>');

  doc = ParseXML('<prefix:root xmlns:prefix="ns1">'+
                 '<prefix:child1 xmlns:prefix="ns2"/></prefix:root>');
  root = doc.documentElement;
  child1 = root.firstChild;
  child2 = doc.createElementNS("ns1", "prefix:child2");
  child1.appendChild(child2);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<prefix:root xmlns:prefix="ns1"><prefix:child1 xmlns:prefix="ns2">'+
              '<a0:child2 xmlns:a0="ns1"/></prefix:child1></prefix:root>');
  

  doc = ParseXML('<root xmlns="ns1"/>');
  root = doc.documentElement;
  child1 = doc.createElementNS("ns2", "child1");
  child2 = doc.createElementNS("ns1", "child2");
  child1.appendChild(child2);
  root.appendChild(child1);
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
                '<root xmlns="ns1"><child1 xmlns="ns2"><child2 xmlns="ns1"/>'+
                '</child1></root>');
}

function test7() {
  // Handle xmlns attribute declaring a default namespace on a non-namespaced
  // element (bug 326994).
  var doc = ParseXML('<root xmlns=""/>')
  var root = doc.documentElement;
  root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
                      "http://www.w3.org/1999/xhtml");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc), '<root/>');

  doc = ParseXML('<root xmlns=""><child1/></root>')
  root = doc.documentElement;
  root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
                      "http://www.w3.org/1999/xhtml");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc), '<root><child1/></root>');

  doc = ParseXML('<root xmlns="http://www.w3.org/1999/xhtml">' +
                 '<child1 xmlns=""><child2/></child1></root>')
  root = doc.documentElement;

  // No interface flattening in xpcshell
  var child1 = root.firstChild.QueryInterface(nsIDOMElement);
  child1.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
                        "http://www.w3.org/1999/xhtml");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<root xmlns="http://www.w3.org/1999/xhtml"><child1 xmlns="">' +
              '<child2/></child1></root>');

  doc = ParseXML('<root xmlns="http://www.w3.org/1999/xhtml">' +
                 '<child1 xmlns="">' +
                 '<child2 xmlns="http://www.w3.org/1999/xhtml"></child2>' +
                 '</child1></root>')
  root = doc.documentElement;
  // No interface flattening in xpcshell
  child1 = root.firstChild.QueryInterface(nsIDOMElement);
  var child2 = child1.firstChild.QueryInterface(nsIDOMElement);
  child1.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
                        "http://www.w3.org/1999/xhtml");
  child2.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "");
  do_check_serialize(doc);
  do_check_eq(SerializeXML(doc),
              '<root xmlns="http://www.w3.org/1999/xhtml"><child1 xmlns="">' +
              '<a0:child2 xmlns:a0="http://www.w3.org/1999/xhtml" xmlns=""></a0:child2></child1></root>');
}

function test8() {
  // Test behavior of serializing with a given charset.
  var str1 = '<?xml version="1.0" encoding="ISO-8859-1"?>'+LB+'<root/>';
  var str2 = '<?xml version="1.0" encoding="UTF8"?>'+LB+'<root/>';
  var doc1 = ParseXML(str1);
  var doc2 = ParseXML(str2);

  var p = Pipe();
  DOMSerializer().serializeToStream(doc1, p.outputStream, "ISO-8859-1");
  p.outputStream.close();
  do_check_eq(ScriptableInput(p).read(-1), str1);

  p = Pipe();
  DOMSerializer().serializeToStream(doc2, p.outputStream, "ISO-8859-1");
  p.outputStream.close();
  do_check_eq(ScriptableInput(p).read(-1), str1);

  p = Pipe();
  DOMSerializer().serializeToStream(doc1, p.outputStream, "UTF8");
  p.outputStream.close();
  do_check_eq(ScriptableInput(p).read(-1), str2);

  p = Pipe();
  DOMSerializer().serializeToStream(doc2, p.outputStream, "UTF8");
  p.outputStream.close();
  do_check_eq(ScriptableInput(p).read(-1), str2);
}

function test9() {
  // Test behavior of serializing between given charsets, using
  // ISO-8859-1-representable text.
  var contents = '<root>' +
                   '\u00BD + \u00BE == \u00BD\u00B2 + \u00BC + \u00BE' +
                 '</root>';
  var str1 = '<?xml version="1.0" encoding="ISO-8859-1"?>'+ LB + contents;
  var str2 = '<?xml version="1.0" encoding="UTF8"?>'+ LB + contents;
  var str3 = '<?xml version="1.0" encoding="UTF-16"?>'+ LB + contents;
  var doc1 = ParseXML(str1);
  var doc2 = ParseXML(str2);
  var doc3 = ParseXML(str3);

  checkSerialization(doc1, "ISO-8859-1", str1);
  checkSerialization(doc2, "ISO-8859-1", str1);
  checkSerialization(doc3, "ISO-8859-1", str1);

  checkSerialization(doc1, "UTF8", str2);
  checkSerialization(doc2, "UTF8", str2);
  checkSerialization(doc3, "UTF8", str2);

  checkSerialization(doc1, "UTF-16", str3);
  checkSerialization(doc2, "UTF-16", str3);
  checkSerialization(doc3, "UTF-16", str3);
}

function test10() {
  // Test behavior of serializing between given charsets, using
  // Unicode characters (XXX but only BMP ones because I don't know
  // how to create one with non-BMP characters, either with JS strings
  // or using DOM APIs).
  var contents = '<root>' +
                   'AZaz09 \u007F ' +               // U+000000 to U+00007F
                   '\u0080 \u0398 \u03BB \u0725 ' + // U+000080 to U+0007FF
                   '\u0964 \u0F5F \u20AC \uFFFB' +  // U+000800 to U+00FFFF
                 '</root>';
  var str1 = '<?xml version="1.0" encoding="UTF8"?>'+ LB + contents;
  var str2 = '<?xml version="1.0" encoding="UTF-16"?>'+ LB + contents;
  var doc1 = ParseXML(str1);
  var doc2 = ParseXML(str2);

  checkSerialization(doc1, "UTF8", str1);
  checkSerialization(doc2, "UTF8", str1);

  checkSerialization(doc1, "UTF-16", str2);
  checkSerialization(doc2, "UTF-16", str2);
}

function checkSerialization(doc, toCharset, expectedString) {
  var p = Pipe();
  DOMSerializer().serializeToStream(doc, p.outputStream, toCharset);
  p.outputStream.close();

  var cin = C["@mozilla.org/intl/converter-input-stream;1"]
             .createInstance(I.nsIConverterInputStream);
  cin.init(p.inputStream, toCharset, 1024, 0x0);

  // compare the first expectedString.length characters for equality
  var outString = {};
  var count = cin.readString(expectedString.length, outString);
  do_check_true(count == expectedString.length);
  do_check_true(outString.value == expectedString);

  // if there's anything more in the stream, it's a bug
  do_check_eq(0, cin.readString(1, outString));
  do_check_eq(outString.value, "");
}