Repository navigation
Expand file tree
/
Copy pathxml.js
More file actions
259 lines (219 loc) · 7.59 KB
/
Copy pathxml.js
File metadata and controls
259 lines (219 loc) · 7.59 KB
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
"use strict";
const assert = require("chai").assert;
const describe = require("mocha-sugar-free").describe;
const specify = require("mocha-sugar-free").specify;
const readTestFixture = require("../util").readTestFixture;
const http = require("http");
const path = require("path");
const jsdom = require("../..");
const xmlStringPromise = readTestFixture("jsdom/files/xml.xml");
function isParsedAsXml(document) {
return document.getElementsByTagName("CUSTOMTAG")[0].innerHTML.trim() === "";
}
describe("jsdom/xml", () => {
specify("should not throw if no parser is given", () => {
jsdom.jsdom("<!DOCTYPE html><html></html>");
});
specify("should not throw if invalid html document is given", () => {
jsdom.jsdom("<!DOCTYPE html><html</html>");
});
specify("should ignore self-closing of tags in html docs", () => {
return xmlStringPromise.then(xmlString => {
const document = jsdom.jsdom(xmlString, { parsingMode: "html" });
const window = document.defaultView;
assert.ok(!isParsedAsXml(window.document));
});
});
specify("should handle self-closing tags properly in xml docs (in .jsdom)", () => {
return xmlStringPromise.then(xmlString => {
const document = jsdom.jsdom(xmlString, {parsingMode: "xml"});
const window = document.defaultView;
assert.ok(isParsedAsXml(window.document));
});
});
specify("should handle self-closing tags properly in xml docs (in .env)", { async: true }, t => {
return xmlStringPromise.then(xmlString => {
jsdom.env({
html: xmlString,
parsingMode: "xml",
done(err, window) {
assert.ifError(err);
assert.ok(isParsedAsXml(window.document));
t.done();
}
});
});
});
specify("parsing XML keeps tag casing (GH-393)", { async: true }, t => {
jsdom.env({
html: "<foo><bar/></foo>",
parsingMode: "xml",
done(err, window) {
assert.ifError(err);
const elem = window.document.getElementsByTagName("foo")[0];
assert.strictEqual(elem.tagName, "foo");
t.done();
}
});
});
specify("attributes are case-sensitive in XML mode (GH-651)", { async: true }, t => {
jsdom.env({
html: "<foo caseSensitive='abc' casesensitive='def'><bar/></foo>",
parsingMode: "xml",
done(err, window) {
assert.ifError(err);
const elem = window.document.getElementsByTagName("foo")[0];
assert.strictEqual(elem.getAttribute("caseSensitive"), "abc");
assert.strictEqual(elem.getAttribute("casesensitive"), "def");
t.done();
}
});
});
specify("XML mode makes directives accessible (GH-415)", { async: true }, t => {
jsdom.env({
html: "<?xml-stylesheet version='1.0'?><foo caseSensitive='abc' casesensitive='def'><bar/></foo>",
parsingMode: "xml",
done(err, window) {
assert.ifError(err);
assert.strictEqual(window.document.firstChild.nodeName, "xml-stylesheet");
assert.strictEqual(window.document.firstChild.data, "version='1.0'");
t.done();
}
});
});
specify("parse5 can somewhat serialize XML docs", { async: true }, t => {
const source = `<foo xmlns:foo="http://example.org/bar"><foo:bar></foo:bar></foo>`;
jsdom.env({
html: source,
parsingMode: "xml",
done(err, window) {
assert.ifError(err);
assert.strictEqual(jsdom.serializeDocument(window.document), source);
t.done();
}
});
});
specify("xml parser recognizes built-in schemas (GH-1276)", { async: true }, t => {
const doc = jsdom.jsdom("<element xml:lang='uk'></element>", {
parsingMode: "xml"
});
const xmlns = "http://www.w3.org/XML/1998/namespace";
const lang = doc.documentElement.getAttributeNS(xmlns, "lang");
assert.strictEqual(lang, &qu
B49D
ot;uk");
t.done();
});
describe("node specific tests", { skipIfBrowser: true }, () => {
specify("should auto-detect XML documents based on .xml extension", { async: true }, t => {
jsdom.env({
file: path.resolve(__dirname, "files/xml.xml"),
done(err, window) {
assert.ifError(err);
assert.ok(isParsedAsXml(window.document));
t.done();
}
});
});
specify("parsingMode option should take precendence over .xml extension detection", { async: true }, t => {
jsdom.env({
file: path.resolve(__dirname, "files/xml.xml"),
parsingMode: "html",
done(err, window) {
assert.ifError(err);
assert.ok(!isParsedAsXml(window.document));
t.done();
}
});
});
specify("should auto-detect HTML documents based on header", { async: true }, t => {
const server = http.createServer((req, res) => {
xmlStringPromise.then(xmlString => {
res.setHeader("Content-Type", "text/html");
res.end(xmlString);
});
});
server.listen(0, () => {
jsdom.env({
url: "http://127.0.0.1:" + server.address().port + "/",
done(err, window) {
assert.ifError(err);
assert.ok(!isParsedAsXml(window.document));
t.done();
}
});
});
});
specify("should auto-detect XML documents based on application/xml header", { async: true }, t => {
const server = http.createServer((req, res) => {
xmlStringPromise.then(xmlString => {
res.setHeader("Content-Type", "application/xml");
res.end(xmlString);
});
});
server.listen(0, () => {
jsdom.env({
url: "http://127.0.0.1:" + server.address().port + "/",
done(err, window) {
assert.ifError(err);
assert.ok(isParsedAsXml(window.document));
t.done();
}
});
});
});
specify("should auto-detect XML documents based on text/xml header", { async: true }, t => {
const server = http.createServer((req, res) => {
xmlStringPromise.then(xmlString => {
res.setHeader("Content-Type", "text/xml");
res.end(xmlString);
});
});
server.listen(0, () => {
jsdom.env({
url: "http://127.0.0.1:" + server.address().port + "/",
done(err, window) {
assert.ifError(err);
assert.ok(isParsedAsXml(window.document));
t.done();
}
});
});
});
specify("should auto-detect XML documents based on application/xhtml+xml header", { async: true }, t => {
const server = http.createServer((req, res) => {
xmlStringPromise.then(xmlString => {
res.setHeader("Content-Type", "application/xhtml+xml");
res.end(xmlString);
});
});
server.listen(0, () => {
jsdom.env({
url: "http://127.0.0.1:" + server.address().port + "/",
done(err, window) {
assert.ifError(err);
assert.ok(isParsedAsXml(window.document));
t.done();
}
});
});
});
specify("parsingMode should take precedence over text/xml header", { async: true }, t => {
const server = http.createServer((req, res) => {
xmlStringPromise.then(xmlString => {
res.setHeader("Content-Type", "text/xml");
res.end(xmlString);
});
});
server.listen(0, () => {
jsdom.env({
url: "http://127.0.0.1:" + server.address().port + "/",
parsingMode: "html",
done(err, window) {
assert.ifError(err);
assert.ok(!isParsedAsXml(window.document));
t.done();
}
});
});
});
});
});