summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_xmlserializer.js
blob: 86f926def5de5dd4dbb3596a75c5bf3d1e19816e (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
/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

async function xmlEncode(aFile, aFlags, aCharset) {
  if (aFlags == undefined) {
    aFlags = 0;
  }
  if (aCharset == undefined) {
    aCharset = "UTF-8";
  }

  var doc = await do_parse_document(aFile, "text/xml");

  var encoder = Cu.createDocumentEncoder("text/xml");
  encoder.setCharset(aCharset);
  encoder.init(doc, "text/xml", aFlags);
  return encoder.encodeToString();
}

add_task(async function test_encoding() {
  var result, expected;
  const de = Ci.nsIDocumentEncoder;

  result = await xmlEncode("1_original.xml", de.OutputLFLineBreak);
  expected = loadContentFile("1_result.xml");
  Assert.equal(expected, result);

  result = await xmlEncode("2_original.xml", de.OutputLFLineBreak);
  expected = loadContentFile("2_result_1.xml");
  Assert.equal(expected, result);

  result = await xmlEncode("2_original.xml", de.OutputCRLineBreak);
  expected = expected.replace(/\n/g, "\r");
  Assert.equal(expected, result);

  result = await xmlEncode(
    "2_original.xml",
    de.OutputLFLineBreak | de.OutputCRLineBreak
  );
  expected = expected.replace(/\r/gm, "\r\n");
  Assert.equal(expected, result);

  result = await xmlEncode(
    "2_original.xml",
    de.OutputLFLineBreak | de.OutputFormatted
  );
  expected = loadContentFile("2_result_2.xml");
  Assert.equal(expected, result);

  result = await xmlEncode(
    "2_original.xml",
    de.OutputLFLineBreak | de.OutputFormatted | de.OutputWrap
  );
  expected = loadContentFile("2_result_3.xml");
  Assert.equal(expected, result);

  result = await xmlEncode(
    "2_original.xml",
    de.OutputLFLineBreak | de.OutputWrap
  );
  expected = loadContentFile("2_result_4.xml");
  Assert.equal(expected, result);

  result = await xmlEncode(
    "3_original.xml",
    de.OutputLFLineBreak | de.OutputFormatted | de.OutputWrap
  );
  expected = loadContentFile("3_result.xml");
  Assert.equal(expected, result);

  result = await xmlEncode(
    "3_original.xml",
    de.OutputLFLineBreak | de.OutputWrap
  );
  expected = loadContentFile("3_result_2.xml");
  Assert.equal(expected, result);

  // tests on namespaces
  var doc = await do_parse_document("4_original.xml", "text/xml");

  var encoder = Cu.createDocumentEncoder("text/xml");
  encoder.setCharset("UTF-8");
  encoder.init(doc, "text/xml", de.OutputLFLineBreak);

  result = encoder.encodeToString();
  expected = loadContentFile("4_result_1.xml");
  Assert.equal(expected, result);

  encoder.setNode(doc.documentElement.childNodes[9]);
  result = encoder.encodeToString();
  expected = loadContentFile("4_result_2.xml");
  Assert.equal(expected, result);

  encoder.setNode(doc.documentElement.childNodes[7].childNodes[1]);
  result = encoder.encodeToString();
  expected = loadContentFile("4_result_3.xml");
  Assert.equal(expected, result);

  encoder.setNode(doc.documentElement.childNodes[11].childNodes[1]);
  result = encoder.encodeToString();
  expected = loadContentFile("4_result_4.xml");
  Assert.equal(expected, result);

  encoder.setCharset("UTF-8");
  // it doesn't support this flags
  encoder.init(
    doc,
    "text/xml",
    de.OutputLFLineBreak | de.OutputFormatted | de.OutputWrap
  );
  encoder.setWrapColumn(40);
  result = encoder.encodeToString();
  expected = loadContentFile("4_result_5.xml");
  Assert.equal(expected, result);

  encoder.init(doc, "text/xml", de.OutputLFLineBreak | de.OutputWrap);
  encoder.setWrapColumn(40);
  result = encoder.encodeToString();
  expected = loadContentFile("4_result_6.xml");
  Assert.equal(expected, result);
});

// OutputRaw should cause OutputWrap and OutputFormatted to be ignored.
// Check by encoding each test file and making sure the result matches what
// was fed in.
add_task(async function test_outputRaw() {
  let result, expected;
  const de = Ci.nsIDocumentEncoder;

  expected = loadContentFile("2_original.xml");
  result = await xmlEncode(
    "2_original.xml",
    de.OutputRaw | de.OutputLFLineBreak | de.OutputWrap
  );
  Assert.equal(expected, result);

  result = await xmlEncode(
    "2_original.xml",
    de.OutputRaw | de.OutputLFLineBreak | de.OutputFormatted | de.OutputWrap
  );
  Assert.equal(expected, result);

  expected = loadContentFile("3_original.xml");
  result = await xmlEncode(
    "3_original.xml",
    de.OutputRaw | de.OutputLFLineBreak | de.OutputWrap
  );
  Assert.equal(expected, result);

  result = await xmlEncode(
    "3_original.xml",
    de.OutputRaw | de.OutputLFLineBreak | de.OutputFormatted | de.OutputWrap
  );
  Assert.equal(expected, result);

  expected = loadContentFile("4_original.xml");
  let doc = await do_parse_document("4_original.xml", "text/xml");
  let encoder = Cu.createDocumentEncoder("text/xml");
  encoder.setCharset("UTF-8");
  encoder.init(
    doc,
    "text/xml",
    de.OutputRaw | de.OutputLFLineBreak | de.OutputWrap
  );
  encoder.setWrapColumn(40);
  result = encoder.encodeToString();
  Assert.equal(expected, result);

  encoder.init(
    doc,
    "text/xml",
    de.OutputRaw | de.OutputLFLineBreak | de.OutputFormatted | de.OutputWrap
  );
  encoder.setWrapColumn(40);
  result = encoder.encodeToString();
  Assert.equal(expected, result);
});