refactor: improve feature string parsing (#23130)
* test: add pre-change snapshot of new-window event * move to .ts file for easier diff * refactor: improve feature string parsing logic * test: update snapshots * update type names per review * update comma-separated parse test * use for loop instead of reduce per review * tighten up types * avoid variable guest contents id returnValue in test snapshot
This commit is contained in:
parent
b3909f5600
commit
aca2e4f968
10 changed files with 589 additions and 108 deletions
|
@ -11,7 +11,7 @@ const { internalWindowOpen } = require('@electron/internal/browser/guest-window-
|
|||
const NavigationController = require('@electron/internal/browser/navigation-controller');
|
||||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal');
|
||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
|
||||
const { convertFeaturesString } = require('@electron/internal/common/parse-features-string');
|
||||
const { parseFeatures } = require('@electron/internal/common/parse-features-string');
|
||||
const { MessagePortMain } = require('@electron/internal/browser/message-port-main');
|
||||
|
||||
// session is not used here, the purpose is to make sure session is initalized
|
||||
|
@ -511,11 +511,13 @@ WebContents.prototype._init = function () {
|
|||
// Make new windows requested by links behave like "window.open".
|
||||
this.on('-new-window', (event, url, frameName, disposition,
|
||||
rawFeatures, referrer, postData) => {
|
||||
const { options, additionalFeatures } = convertFeaturesString(rawFeatures, frameName);
|
||||
const { options, webPreferences, additionalFeatures } = parseFeatures(rawFeatures);
|
||||
const mergedOptions = {
|
||||
show: true,
|
||||
width: 800,
|
||||
height: 600,
|
||||
title: frameName,
|
||||
webPreferences,
|
||||
...options
|
||||
};
|
||||
|
||||
|
@ -533,12 +535,14 @@ WebContents.prototype._init = function () {
|
|||
return;
|
||||
}
|
||||
|
||||
const { options, additionalFeatures } = convertFeaturesString(rawFeatures, frameName);
|
||||
const { options, webPreferences, additionalFeatures } = parseFeatures(rawFeatures);
|
||||
const mergedOptions = {
|
||||
show: true,
|
||||
width: 800,
|
||||
height: 600,
|
||||
webContents,
|
||||
title: frameName,
|
||||
webPreferences,
|
||||
...options
|
||||
};
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
const { webContents } = require('electron');
|
||||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal');
|
||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
|
||||
const { parseFeaturesString } = require('@electron/internal/common/parse-features-string');
|
||||
const { parseWebViewWebPreferences } = require('@electron/internal/common/parse-features-string');
|
||||
const { syncMethods, asyncMethods, properties } = require('@electron/internal/common/web-view-methods');
|
||||
const { serialize } = require('@electron/internal/common/type-utils');
|
||||
|
||||
|
@ -184,6 +184,13 @@ const attachGuest = function (event, embedderFrameId, elementInstanceId, guestIn
|
|||
}
|
||||
}
|
||||
|
||||
// parse the 'webpreferences' attribute string, if set
|
||||
// this uses the same parsing rules as window.open uses for its features
|
||||
const parsedWebPreferences =
|
||||
typeof params.webpreferences === 'string'
|
||||
? parseWebViewWebPreferences(params.webpreferences)
|
||||
: null;
|
||||
|
||||
const webPreferences = {
|
||||
guestInstanceId: guestInstanceId,
|
||||
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
|
||||
|
@ -194,21 +201,10 @@ const attachGuest = function (event, embedderFrameId, elementInstanceId, guestIn
|
|||
disablePopups: !params.allowpopups,
|
||||
webSecurity: !params.disablewebsecurity,
|
||||
enableBlinkFeatures: params.blinkfeatures,
|
||||
disableBlinkFeatures: params.disableblinkfeatures
|
||||
disableBlinkFeatures: params.disableblinkfeatures,
|
||||
...parsedWebPreferences
|
||||
};
|
||||
|
||||
// parse the 'webpreferences' attribute string, if set
|
||||
// this uses the same parsing rules as window.open uses for its features
|
||||
if (typeof params.webpreferences === 'string') {
|
||||
parseFeaturesString(params.webpreferences, function (key, value) {
|
||||
if (value === undefined) {
|
||||
// no value was specified, default it to true
|
||||
value = true;
|
||||
}
|
||||
webPreferences[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
if (params.preload) {
|
||||
webPreferences.preloadURL = params.preload;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ const { BrowserWindow } = electron;
|
|||
const { isSameOrigin } = process.electronBinding('v8_util');
|
||||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal');
|
||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils');
|
||||
const { convertFeaturesString } = require('@electron/internal/common/parse-features-string');
|
||||
const { parseFeatures } = require('@electron/internal/common/parse-features-string');
|
||||
|
||||
const hasProp = {}.hasOwnProperty;
|
||||
const frameToGuest = new Map();
|
||||
|
@ -224,7 +224,10 @@ ipcMainInternal.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, url, fra
|
|||
if (features == null) features = '';
|
||||
|
||||
const disposition = 'new-window';
|
||||
const { options, additionalFeatures } = convertFeaturesString(features, frameName);
|
||||
const { options, webPreferences, additionalFeatures } = parseFeatures(features);
|
||||
if (!options.title) options.title = frameName;
|
||||
options.webPreferences = webPreferences;
|
||||
|
||||
const referrer = { url: '', policy: 'default' };
|
||||
internalWindowOpen(event, url, referrer, frameName, disposition, options, additionalFeatures, null);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue