jsdom.js
11.8 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"use strict";
/* eslint-disable no-unused-expressions */
() => `jsdom 7.x onward only works on Node.js 4 or newer: https://github.com/tmpvar/jsdom#install`;
/* eslint-enable no-unused-expressions */
const fs = require("fs");
const path = require("path");
const CookieJar = require("tough-cookie").CookieJar;
const toFileUrl = require("./jsdom/utils").toFileUrl;
const defineGetter = require("./jsdom/utils").defineGetter;
const defineSetter = require("./jsdom/utils").defineSetter;
const documentFeatures = require("./jsdom/browser/documentfeatures");
const domToHtml = require("./jsdom/browser/domtohtml").domToHtml;
const Window = require("./jsdom/browser/Window");
const resourceLoader = require("./jsdom/browser/resource-loader");
const VirtualConsole = require("./jsdom/virtual-console");
const locationInfo = require("./jsdom/living/helpers/internal-constants").locationInfo;
require("./jsdom/living"); // Enable living standard features
/* eslint-disable no-restricted-modules */
// TODO: stop using the built-in URL in favor of the spec-compliant whatwg-url package
// This legacy usage is in the process of being purged.
const URL = require("url");
/* eslint-enable no-restricted-modules */
const canReadFilesFromFS = Boolean(fs.readFile); // in a browserify environment, this isn't present
exports.createVirtualConsole = function (options) {
return new VirtualConsole(options);
};
exports.getVirtualConsole = function (window) {
return window._virtualConsole;
};
exports.createCookieJar = function () {
return new CookieJar(null, { looseMode: true });
};
exports.nodeLocation = function (node) {
return node[locationInfo];
};
exports.reconfigureWindow = function (window, newProps) {
if ("top" in newProps) {
window._top = newProps.top;
}
};
exports.debugMode = false;
// Proxy feature functions to features module.
for (const propName of ["availableDocumentFeatures", "defaultDocumentFeatures", "applyDocumentFeatures"]) {
defineGetter(exports, propName, () => documentFeatures[propName]);
defineSetter(exports, propName, val => documentFeatures[propName] = val);
}
exports.jsdom = function (html, options) {
if (options === undefined) {
options = {};
}
if (options.parsingMode === undefined || options.parsingMode === "auto") {
options.parsingMode = "html";
}
if (options.parsingMode !== "html" && options.parsingMode !== "xml") {
throw new RangeError(`Invalid parsingMode option ${JSON.stringify(options.parsingMode)}; must be either "html", ` +
`"xml", "auto", or undefined`);
}
setGlobalDefaultConfig(options);
// Back-compat hack: we have previously suggested nesting these under document, for jsdom.env at least.
// So we need to support that.
if (options.document) {
if (options.document.cookie !== undefined) {
options.cookie = options.document.cookie;
}
if (options.document.referrer !== undefined) {
options.referrer = options.document.referrer;
}
}
// List options explicitly to be clear which are passed through
const window = new Window({
parsingMode: options.parsingMode,
contentType: options.contentType,
parser: options.parser,
url: options.url,
referrer: options.referrer,
cookieJar: options.cookieJar,
cookie: options.cookie,
resourceLoader: options.resourceLoader,
deferClose: options.deferClose,
concurrentNodeIterators: options.concurrentNodeIterators,
virtualConsole: options.virtualConsole,
pool: options.pool,
agentOptions: options.agentOptions,
userAgent: options.userAgent
});
documentFeatures.applyDocumentFeatures(window.document, options.features);
if (options.created) {
options.created(null, window.document.defaultView);
}
if (options.parsingMode === "html") {
if (html === undefined || html === "") {
html = "<html><head></head><body></body></html>";
}
window.document.write(html);
}
if (options.parsingMode === "xml") {
if (html !== undefined) {
window.document._htmlToDom.appendHtmlToDocument(html, window.document);
}
}
if (window.document.close && !options.deferClose) {
window.document.close();
}
return window.document;
};
exports.jQueryify = exports.jsdom.jQueryify = function (window, jqueryUrl, callback) {
if (!window || !window.document) {
return;
}
const features = window.document.implementation._features;
window.document.implementation._addFeature("FetchExternalResources", ["script"]);
window.document.implementation._addFeature("ProcessExternalResources", ["script"]);
window.document.implementation._addFeature("MutationEvents", ["2.0"]);
const scriptEl = window.document.createElement("script");
scriptEl.className = "jsdom";
scriptEl.src = jqueryUrl;
scriptEl.onload = scriptEl.onerror = () => {
window.document.implementation._features = features;
if (callback) {
callback(window, window.jQuery);
}
};
window.document.body.appendChild(scriptEl);
};
exports.env = exports.jsdom.env = function () {
const config = getConfigFromArguments(arguments);
let req = null;
if (config.file && canReadFilesFromFS) {
req = resourceLoader.readFile(config.file, (err, text) => {
if (err) {
reportInitError(err, config);
return;
}
setParsingModeFromExtension(config, config.file);
config.html = text;
processHTML(config);
});
} else if (config.html !== undefined) {
processHTML(config);
} else if (config.url) {
req = handleUrl(config);
} else if (config.somethingToAutodetect !== undefined) {
const url = URL.parse(config.somethingToAutodetect);
if (url.protocol && url.hostname) {
config.url = config.somethingToAutodetect;
req = handleUrl(config.somethingToAutodetect);
} else if (canReadFilesFromFS) {
req = resourceLoader.readFile(config.somethingToAutodetect, (err, text) => {
if (err) {
if (err.code === "ENOENT" || err.code === "ENAMETOOLONG") {
config.html = config.somethingToAutodetect;
processHTML(config);
} else {
reportInitError(err, config);
}
} else {
setParsingModeFromExtension(config, config.somethingToAutodetect);
config.html = text;
config.url = toFileUrl(config.somethingToAutodetect);
processHTML(config);
}
});
} else {
config.html = config.somethingToAutodetect;
processHTML(config);
}
}
function handleUrl() {
const options = {
encoding: config.encoding || "utf8",
headers: config.headers || {},
pool: config.pool,
agentOptions: config.agentOptions
};
if (config.proxy) {
options.proxy = config.proxy;
}
options.headers["User-Agent"] = config.userAgent;
config.cookieJar = config.cookieJar || exports.createCookieJar();
return resourceLoader.download(config.url, options, config.cookieJar, null, (err, responseText, res) => {
if (err) {
reportInitError(err, config);
return;
}
// The use of `res.request.uri.href` ensures that `window.location.href`
// is updated when `request` follows redirects.
config.html = responseText;
config.url = res.request.uri.href;
const contentType = res.headers["content-type"];
if (config.parsingMode === "auto" && (
contentType === "application/xml" ||
contentType === "text/xml" ||
contentType === "application/xhtml+xml")) {
config.parsingMode = "xml";
}
processHTML(config);
});
}
return req;
};
exports.serializeDocument = function (doc) {
return domToHtml([doc]);
};
function processHTML(config) {
const window = exports.jsdom(config.html, config).defaultView;
const features = JSON.parse(JSON.stringify(window.document.implementation._features));
let docsLoaded = 0;
const totalDocs = config.scripts.length + config.src.length;
if (!window || !window.document) {
reportInitError(new Error("JSDOM: a window object could not be created."), config);
return;
}
function scriptComplete() {
docsLoaded++;
if (docsLoaded >= totalDocs) {
window.document.implementation._features = features;
process.nextTick(() => {
if (config.onload) {
config.onload(window);
}
if (config.done) {
config.done(null, window);
}
});
}
}
function handleScriptError() {
// nextTick so that an exception within scriptComplete won't cause
// another script onerror (which would be an infinite loop)
process.nextTick(scriptComplete);
}
if (config.scripts.length > 0 || config.src.length > 0) {
window.document.implementation._addFeature("FetchExternalResources", ["script"]);
window.document.implementation._addFeature("ProcessExternalResources", ["script"]);
window.document.implementation._addFeature("MutationEvents", ["2.0"]);
for (const scriptSrc of config.scripts) {
const script = window.document.createElement("script");
script.className = "jsdom";
script.onload = scriptComplete;
script.onerror = handleScriptError;
script.src = scriptSrc;
window.document.body.appendChild(script);
}
for (const scriptText of config.src) {
const script = window.document.createElement("script");
script.onload = scriptComplete;
script.onerror = handleScriptError;
script.text = scriptText;
window.document.documentElement.appendChild(script);
window.document.documentElement.removeChild(script);
}
} else if (window.document.readyState === "complete") {
scriptComplete();
} else {
window.addEventListener("load", scriptComplete);
}
}
function setGlobalDefaultConfig(config) {
config.pool = config.pool !== undefined ? config.pool : {
maxSockets: 6
};
config.agentOptions = config.agentOptions !== undefined ? config.agentOptions : {
keepAlive: true,
keepAliveMsecs: 115 * 1000
};
config.userAgent = config.userAgent || "Node.js (" + process.platform + "; U; rv:" + process.version + ")";
}
function getConfigFromArguments(args) {
const config = {};
if (typeof args[0] === "object") {
Object.assign(config, args[0]);
} else {
for (const arg of args) {
switch (typeof arg) {
case "string":
config.somethingToAutodetect = arg;
break;
case "function":
config.done = arg;
break;
case "object":
if (Array.isArray(arg)) {
config.scripts = arg;
} else {
Object.assign(config, arg);
}
break;
}
}
}
if (!config.done && !config.created && !config.onload) {
throw new Error("Must pass a \"created\", \"onload\", or \"done\" option, or a callback, to jsdom.env");
}
if (config.somethingToAutodetect === undefined &&
config.html === undefined && !config.file && !config.url) {
throw new Error("Must pass a \"html\", \"file\", or \"url\" option, or a string, to jsdom.env");
}
config.scripts = ensureArray(config.scripts);
config.src = ensureArray(config.src);
config.parsingMode = config.parsingMode || "auto";
config.features = config.features || {
FetchExternalResources: false,
ProcessExternalResources: false,
SkipExternalResources: false
};
if (!config.url && config.file) {
config.url = toFileUrl(config.file);
}
setGlobalDefaultConfig(config);
return config;
}
function reportInitError(err, config) {
if (config.created) {
config.created(err);
}
if (config.done) {
config.done(err);
}
}
function ensureArray(value) {
let array = value || [];
if (typeof array === "string") {
array = [array];
}
return array;
}
function setParsingModeFromExtension(config, filename) {
if (config.parsingMode === "auto") {
const ext = path.extname(filename);
if (ext === ".xhtml" || ext === ".xml") {
config.parsingMode = "xml";
}
}
}