Innovenergy_trunk/frontend/node_modules/testcafe-hammerhead/lib/processing/script/header.js

103 lines
6.4 KiB
JavaScript
Raw Normal View History

"use strict";
// -------------------------------------------------------------
// WARNING: this file is used by both the client and the server.
// Do not use any browser or node-specific API!
// -------------------------------------------------------------
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.add = exports.remove = exports.SCRIPT_PROCESSING_END_HEADER_COMMENT = exports.SCRIPT_PROCESSING_END_COMMENT = exports.SCRIPT_PROCESSING_START_COMMENT = void 0;
const regexp_escape_1 = __importDefault(require("../../utils/regexp-escape"));
const internal_properties_1 = __importDefault(require("../../processing/dom/internal-properties"));
const instruction_1 = __importDefault(require("./instruction"));
const service_routes_1 = __importDefault(require("../../proxy/service-routes"));
const json_1 = require("../../utils/json");
exports.SCRIPT_PROCESSING_START_COMMENT = '/*hammerhead|script|start*/';
exports.SCRIPT_PROCESSING_END_COMMENT = '/*hammerhead|script|end*/';
exports.SCRIPT_PROCESSING_END_HEADER_COMMENT = '/*hammerhead|script|processing-header-end*/';
const STRICT_MODE_PLACEHOLDER = '{strict-placeholder}';
const SW_SCOPE_HEADER_VALUE = '{sw-scope-header-value}';
const WORKER_SETTINGS_PLACEHOLDER = '{worker-settings}';
const IMPORT_WORKER_HAMMERHEAD = `
if (typeof importScripts !== "undefined" && /\\[native code]/g.test(importScripts.toString())) {
var ${instruction_1.default.getWorkerSettings} = function () {return ${WORKER_SETTINGS_PLACEHOLDER}};
importScripts((location.origin || (location.protocol + "//" + location.host)) + "${service_routes_1.default.workerHammerhead}");
}
`;
const PROCESS_DOM_METHOD = `window['${internal_properties_1.default.processDomMethodName}'] && window['${internal_properties_1.default.processDomMethodName}']();`;
function trim(val) {
return val.replace(/\n(?!$)\s*/g, '');
}
const PROXYLESS_HEADER = trim(`
${exports.SCRIPT_PROCESSING_START_COMMENT}
${STRICT_MODE_PLACEHOLDER}
if (typeof window !== 'undefined' && window) {
${PROCESS_DOM_METHOD}
}
${exports.SCRIPT_PROCESSING_END_HEADER_COMMENT}
`);
const HEADER = trim(`
${exports.SCRIPT_PROCESSING_START_COMMENT}
${STRICT_MODE_PLACEHOLDER}
${SW_SCOPE_HEADER_VALUE}
if (typeof window !== 'undefined' && window){
${PROCESS_DOM_METHOD}
if (window.${instruction_1.default.getProperty} && typeof ${instruction_1.default.getProperty} === 'undefined')
var ${instruction_1.default.getLocation} = window.${instruction_1.default.getLocation},
${instruction_1.default.setLocation} = window.${instruction_1.default.setLocation},
${instruction_1.default.setProperty} = window.${instruction_1.default.setProperty},
${instruction_1.default.getProperty} = window.${instruction_1.default.getProperty},
${instruction_1.default.callMethod} = window.${instruction_1.default.callMethod},
${instruction_1.default.getEval} = window.${instruction_1.default.getEval},
${instruction_1.default.processScript} = window.${instruction_1.default.processScript},
${instruction_1.default.processHtml} = window.${instruction_1.default.processHtml},
${instruction_1.default.getPostMessage} = window.${instruction_1.default.getPostMessage},
${instruction_1.default.getProxyUrl} = window.${instruction_1.default.getProxyUrl},
${instruction_1.default.restArray} = window.${instruction_1.default.restArray},
${instruction_1.default.restObject} = window.${instruction_1.default.restObject},
${instruction_1.default.arrayFrom} = window.${instruction_1.default.arrayFrom};
} else {
if (typeof ${instruction_1.default.getProperty} === 'undefined')
var ${instruction_1.default.getLocation} = function(l){return l},
${instruction_1.default.setLocation} = function(l,v){return l = v},
${instruction_1.default.setProperty} = function(o,p,v){return o[p] = v},
${instruction_1.default.getProperty} = function(o,p){return o[p]},
${instruction_1.default.callMethod} = function(o,p,a){return o[p].apply(o,a)},
${instruction_1.default.getEval} = function(e){return e},
${instruction_1.default.processScript} = function(s){return s},
${instruction_1.default.processHtml} = function(h){return h},
${instruction_1.default.getPostMessage} = function(w,p){return arguments.length===1?w.postMessage:p},
${instruction_1.default.getProxyUrl} = function(u,d){return u},
${instruction_1.default.restArray} = function(a,i){return Array.prototype.slice.call(a, i)},
${instruction_1.default.restObject} = function(o,p){var k=Object.keys(o),n={};for(var i=0;i<k.length;++i)if(p.indexOf(k[i])<0)n[k[i]]=o[k[i]];return n},
${instruction_1.default.arrayFrom} = function(r){if(!r)return r;return!Array.isArray(r)&&"function"==typeof r[Symbol.iterator]?Array.from(r):r};
${IMPORT_WORKER_HAMMERHEAD}
}
${exports.SCRIPT_PROCESSING_END_HEADER_COMMENT}
`);
// NOTE: IE removes trailing newlines in script.textContent,
// so a trailing newline in RegExp is optional
const HEADER_RE = new RegExp(`${(0, regexp_escape_1.default)(exports.SCRIPT_PROCESSING_START_COMMENT)}[\\S\\s]+?${(0, regexp_escape_1.default)(exports.SCRIPT_PROCESSING_END_HEADER_COMMENT)}\n?`, 'gi');
const PROCESSING_END_COMMENT_RE = new RegExp(`\n?${(0, regexp_escape_1.default)(exports.SCRIPT_PROCESSING_END_COMMENT)}\\s*`, 'gi');
function remove(code) {
return code
.replace(HEADER_RE, '')
.replace(PROCESSING_END_COMMENT_RE, '');
}
exports.remove = remove;
function add(code, isStrictMode, swScopeHeaderValue, proxyless, workerSettings) {
const targetHeader = proxyless ? PROXYLESS_HEADER : HEADER;
const header = targetHeader
.replace(STRICT_MODE_PLACEHOLDER, isStrictMode ? '"use strict";' : '')
.replace(SW_SCOPE_HEADER_VALUE, swScopeHeaderValue ? `var ${instruction_1.default.swScopeHeaderValue} = ${(0, json_1.stringify)(swScopeHeaderValue)};` : '')
.replace(WORKER_SETTINGS_PLACEHOLDER, workerSettings ? JSON.stringify(workerSettings) : 'null');
return header + code + '\n' + exports.SCRIPT_PROCESSING_END_COMMENT;
}
exports.add = add;