mirror of
https://github.com/S2-/minifyfromhtml.git
synced 2025-08-03 04:10:04 +02:00
update node modules
This commit is contained in:
152
node_modules/jsdom/lib/api.js
generated
vendored
152
node_modules/jsdom/lib/api.js
generated
vendored
@@ -3,7 +3,6 @@ const path = require("path");
|
||||
const fs = require("pn/fs");
|
||||
const vm = require("vm");
|
||||
const toughCookie = require("tough-cookie");
|
||||
const request = require("request-promise-native");
|
||||
const sniffHTMLEncoding = require("html-encoding-sniffer");
|
||||
const whatwgURL = require("whatwg-url");
|
||||
const whatwgEncoding = require("whatwg-encoding");
|
||||
@@ -12,13 +11,9 @@ const MIMEType = require("whatwg-mimetype");
|
||||
const idlUtils = require("./jsdom/living/generated/utils.js");
|
||||
const VirtualConsole = require("./jsdom/virtual-console.js");
|
||||
const Window = require("./jsdom/browser/Window.js");
|
||||
const { domToHtml } = require("./jsdom/browser/domtohtml.js");
|
||||
const { applyDocumentFeatures } = require("./jsdom/browser/documentfeatures.js");
|
||||
const { wrapCookieJarForRequest } = require("./jsdom/browser/resource-loader.js");
|
||||
const { version: packageVersion } = require("../package.json");
|
||||
|
||||
const DEFAULT_USER_AGENT = `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 (KHTML, like Gecko) ` +
|
||||
`jsdom/${packageVersion}`;
|
||||
const { fragmentSerialization } = require("./jsdom/living/domparsing/serialization.js");
|
||||
const ResourceLoader = require("./jsdom/browser/resources/resource-loader.js");
|
||||
const NoOpResourceLoader = require("./jsdom/browser/resources/no-op-resource-loader.js");
|
||||
|
||||
// This symbol allows us to smuggle a non-public option through to the JSDOM constructor, for use by JSDOM.fromURL.
|
||||
const transportLayerEncodingLabelHiddenOption = Symbol("transportLayerEncodingLabel");
|
||||
@@ -35,31 +30,14 @@ let sharedFragmentDocument = null;
|
||||
|
||||
class JSDOM {
|
||||
constructor(input, options = {}) {
|
||||
const { html, encoding } = normalizeHTML(input, options[transportLayerEncodingLabelHiddenOption]);
|
||||
options = transformOptions(options, encoding);
|
||||
const mimeType = new MIMEType(options.contentType === undefined ? "text/html" : options.contentType);
|
||||
const { html, encoding } = normalizeHTML(input, options[transportLayerEncodingLabelHiddenOption], mimeType);
|
||||
|
||||
options = transformOptions(options, encoding, mimeType);
|
||||
|
||||
this[window] = new Window(options.windowOptions);
|
||||
|
||||
// TODO NEWAPI: the whole "features" infrastructure is horrible and should be re-built. When we switch to newapi
|
||||
// wholesale, or perhaps before, we should re-do it. For now, just adapt the new, nice, public API into the old,
|
||||
// ugly, internal API.
|
||||
const features = {
|
||||
FetchExternalResources: [],
|
||||
SkipExternalResources: false
|
||||
};
|
||||
|
||||
if (options.resources === "usable") {
|
||||
features.FetchExternalResources = ["link", "img", "frame", "iframe"];
|
||||
if (options.windowOptions.runScripts === "dangerously") {
|
||||
features.FetchExternalResources.push("script");
|
||||
}
|
||||
|
||||
// Note that "img" will be ignored by the code in HTMLImageElement-impl.js if canvas is not installed.
|
||||
// TODO NEWAPI: clean that up and centralize the logic here.
|
||||
}
|
||||
|
||||
const documentImpl = idlUtils.implForWrapper(this[window]._document);
|
||||
applyDocumentFeatures(documentImpl, features);
|
||||
|
||||
options.beforeParse(this[window]._globalProxy);
|
||||
|
||||
@@ -84,24 +62,24 @@ class JSDOM {
|
||||
}
|
||||
|
||||
serialize() {
|
||||
return domToHtml([idlUtils.implForWrapper(this[window]._document)]);
|
||||
return fragmentSerialization(idlUtils.implForWrapper(this[window]._document), { requireWellFormed: false });
|
||||
}
|
||||
|
||||
nodeLocation(node) {
|
||||
if (!idlUtils.implForWrapper(this[window]._document)._parseOptions.locationInfo) {
|
||||
if (!idlUtils.implForWrapper(this[window]._document)._parseOptions.sourceCodeLocationInfo) {
|
||||
throw new Error("Location information was not saved for this jsdom. Use includeNodeLocations during creation.");
|
||||
}
|
||||
|
||||
return idlUtils.implForWrapper(node).__location;
|
||||
return idlUtils.implForWrapper(node).sourceCodeLocation;
|
||||
}
|
||||
|
||||
runVMScript(script) {
|
||||
runVMScript(script, options) {
|
||||
if (!vm.isContext(this[window])) {
|
||||
throw new TypeError("This jsdom was not configured to allow script running. " +
|
||||
"Use the runScripts option during creation.");
|
||||
}
|
||||
|
||||
return script.runInContext(this[window]);
|
||||
return script.runInContext(this[window], options);
|
||||
}
|
||||
|
||||
reconfigure(settings) {
|
||||
@@ -118,11 +96,11 @@ class JSDOM {
|
||||
}
|
||||
|
||||
document._URL = url;
|
||||
document._origin = whatwgURL.serializeURLOrigin(document._URL);
|
||||
document.origin = whatwgURL.serializeURLOrigin(document._URL);
|
||||
}
|
||||
}
|
||||
|
||||
static fragment(string) {
|
||||
static fragment(string = "") {
|
||||
if (!sharedFragmentDocument) {
|
||||
sharedFragmentDocument = (new JSDOM()).window.document;
|
||||
}
|
||||
@@ -138,20 +116,20 @@ class JSDOM {
|
||||
url = parsedURL.href;
|
||||
options = normalizeFromURLOptions(options);
|
||||
|
||||
const requestOptions = {
|
||||
resolveWithFullResponse: true,
|
||||
encoding: null, // i.e., give me the raw Buffer
|
||||
gzip: true,
|
||||
headers: {
|
||||
"User-Agent": options.userAgent,
|
||||
Referer: options.referrer,
|
||||
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
"Accept-Language": "en"
|
||||
},
|
||||
jar: wrapCookieJarForRequest(options.cookieJar)
|
||||
};
|
||||
const resourceLoader = resourcesToResourceLoader(options.resources);
|
||||
const resourceLoaderForInitialRequest = resourceLoader.constructor === NoOpResourceLoader ?
|
||||
new ResourceLoader() :
|
||||
resourceLoader;
|
||||
|
||||
const req = resourceLoaderForInitialRequest.fetch(url, {
|
||||
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
||||
cookieJar: options.cookieJar,
|
||||
referrer: options.referrer
|
||||
});
|
||||
|
||||
return req.then(body => {
|
||||
const res = req.response;
|
||||
|
||||
return request(url, requestOptions).then(res => {
|
||||
let transportLayerEncodingLabel;
|
||||
if ("content-type" in res.headers) {
|
||||
const mimeType = new MIMEType(res.headers["content-type"]);
|
||||
@@ -159,13 +137,13 @@ class JSDOM {
|
||||
}
|
||||
|
||||
options = Object.assign(options, {
|
||||
url: res.request.href + parsedURL.hash,
|
||||
url: req.href + parsedURL.hash,
|
||||
contentType: res.headers["content-type"],
|
||||
referrer: res.request.getHeader("referer"),
|
||||
referrer: req.getHeader("referer"),
|
||||
[transportLayerEncodingLabelHiddenOption]: transportLayerEncodingLabel
|
||||
});
|
||||
|
||||
return new JSDOM(res.body, options);
|
||||
return new JSDOM(body, options);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -193,9 +171,6 @@ function normalizeFromURLOptions(options) {
|
||||
// Normalization of options which must be done before the rest of the fromURL code can use them, because they are
|
||||
// given to request()
|
||||
const normalized = Object.assign({}, options);
|
||||
if (options.userAgent === undefined) {
|
||||
normalized.userAgent = DEFAULT_USER_AGENT;
|
||||
}
|
||||
|
||||
if (options.referrer !== undefined) {
|
||||
normalized.referrer = (new URL(options.referrer)).href;
|
||||
@@ -228,7 +203,7 @@ function normalizeFromFileOptions(filename, options) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function transformOptions(options, encoding) {
|
||||
function transformOptions(options, encoding, mimeType) {
|
||||
const transformed = {
|
||||
windowOptions: {
|
||||
// Defaults
|
||||
@@ -236,33 +211,30 @@ function transformOptions(options, encoding) {
|
||||
referrer: "",
|
||||
contentType: "text/html",
|
||||
parsingMode: "html",
|
||||
userAgent: DEFAULT_USER_AGENT,
|
||||
parseOptions: { locationInfo: false },
|
||||
parseOptions: { sourceCodeLocationInfo: false },
|
||||
runScripts: undefined,
|
||||
encoding,
|
||||
pretendToBeVisual: false,
|
||||
storageQuota: 5000000,
|
||||
|
||||
// Defaults filled in later
|
||||
resourceLoader: undefined,
|
||||
virtualConsole: undefined,
|
||||
cookieJar: undefined
|
||||
},
|
||||
|
||||
// Defaults
|
||||
resources: undefined,
|
||||
beforeParse() { }
|
||||
};
|
||||
|
||||
if (options.contentType !== undefined) {
|
||||
const mimeType = new MIMEType(options.contentType);
|
||||
|
||||
if (!mimeType.isHTML() && !mimeType.isXML()) {
|
||||
throw new RangeError(`The given content type of "${options.contentType}" was not a HTML or XML content type`);
|
||||
}
|
||||
|
||||
transformed.windowOptions.contentType = mimeType.essence;
|
||||
transformed.windowOptions.parsingMode = mimeType.isHTML() ? "html" : "xml";
|
||||
// options.contentType was parsed into mimeType by the caller.
|
||||
if (!mimeType.isHTML() && !mimeType.isXML()) {
|
||||
throw new RangeError(`The given content type of "${options.contentType}" was not a HTML or XML content type`);
|
||||
}
|
||||
|
||||
transformed.windowOptions.contentType = mimeType.essence;
|
||||
transformed.windowOptions.parsingMode = mimeType.isHTML() ? "html" : "xml";
|
||||
|
||||
if (options.url !== undefined) {
|
||||
transformed.windowOptions.url = (new URL(options.url)).href;
|
||||
}
|
||||
@@ -271,16 +243,12 @@ function transformOptions(options, encoding) {
|
||||
transformed.windowOptions.referrer = (new URL(options.referrer)).href;
|
||||
}
|
||||
|
||||
if (options.userAgent !== undefined) {
|
||||
transformed.windowOptions.userAgent = String(options.userAgent);
|
||||
}
|
||||
|
||||
if (options.includeNodeLocations) {
|
||||
if (transformed.windowOptions.parsingMode === "xml") {
|
||||
throw new TypeError("Cannot set includeNodeLocations to true with an XML content type");
|
||||
}
|
||||
|
||||
transformed.windowOptions.parseOptions = { locationInfo: true };
|
||||
transformed.windowOptions.parseOptions = { sourceCodeLocationInfo: true };
|
||||
}
|
||||
|
||||
transformed.windowOptions.cookieJar = options.cookieJar === undefined ?
|
||||
@@ -291,13 +259,12 @@ function transformOptions(options, encoding) {
|
||||
(new VirtualConsole()).sendTo(console) :
|
||||
options.virtualConsole;
|
||||
|
||||
if (options.resources !== undefined) {
|
||||
transformed.resources = String(options.resources);
|
||||
if (transformed.resources !== "usable") {
|
||||
throw new RangeError(`resources must be undefined or "usable"`);
|
||||
}
|
||||
if (!(transformed.windowOptions.virtualConsole instanceof VirtualConsole)) {
|
||||
throw new TypeError("virtualConsole must be an instance of VirtualConsole");
|
||||
}
|
||||
|
||||
transformed.windowOptions.resourceLoader = resourcesToResourceLoader(options.resources);
|
||||
|
||||
if (options.runScripts !== undefined) {
|
||||
transformed.windowOptions.runScripts = String(options.runScripts);
|
||||
if (transformed.windowOptions.runScripts !== "dangerously" &&
|
||||
@@ -314,12 +281,16 @@ function transformOptions(options, encoding) {
|
||||
transformed.windowOptions.pretendToBeVisual = Boolean(options.pretendToBeVisual);
|
||||
}
|
||||
|
||||
if (options.storageQuota !== undefined) {
|
||||
transformed.windowOptions.storageQuota = Number(options.storageQuota);
|
||||
}
|
||||
|
||||
// concurrentNodeIterators??
|
||||
|
||||
return transformed;
|
||||
}
|
||||
|
||||
function normalizeHTML(html = "", transportLayerEncodingLabel) {
|
||||
function normalizeHTML(html = "", transportLayerEncodingLabel, mimeType) {
|
||||
let encoding = "UTF-8";
|
||||
|
||||
if (ArrayBuffer.isView(html)) {
|
||||
@@ -329,7 +300,10 @@ function normalizeHTML(html = "", transportLayerEncodingLabel) {
|
||||
}
|
||||
|
||||
if (Buffer.isBuffer(html)) {
|
||||
encoding = sniffHTMLEncoding(html, { defaultEncoding: "windows-1252", transportLayerEncodingLabel });
|
||||
encoding = sniffHTMLEncoding(html, {
|
||||
defaultEncoding: mimeType.isXML() ? "UTF-8" : "windows-1252",
|
||||
transportLayerEncodingLabel
|
||||
});
|
||||
html = whatwgEncoding.decode(html, encoding);
|
||||
} else {
|
||||
html = String(html);
|
||||
@@ -338,9 +312,27 @@ function normalizeHTML(html = "", transportLayerEncodingLabel) {
|
||||
return { html, encoding };
|
||||
}
|
||||
|
||||
function resourcesToResourceLoader(resources) {
|
||||
switch (resources) {
|
||||
case undefined: {
|
||||
return new NoOpResourceLoader();
|
||||
}
|
||||
case "usable": {
|
||||
return new ResourceLoader();
|
||||
}
|
||||
default: {
|
||||
if (!(resources instanceof ResourceLoader)) {
|
||||
throw new TypeError("resources must be an instance of ResourceLoader");
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.JSDOM = JSDOM;
|
||||
|
||||
exports.VirtualConsole = VirtualConsole;
|
||||
exports.CookieJar = CookieJar;
|
||||
exports.ResourceLoader = ResourceLoader;
|
||||
|
||||
exports.toughCookie = toughCookie;
|
||||
|
Reference in New Issue
Block a user