factor out parse-features-string.js
This commit is contained in:
parent
e7962c7ba2
commit
f35536bdc5
3 changed files with 35 additions and 29 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const ipcMain = require('electron').ipcMain
|
const ipcMain = require('electron').ipcMain
|
||||||
const webContents = require('electron').webContents
|
const webContents = require('electron').webContents
|
||||||
|
const parseFeaturesString = require('../common/parse-features-string')
|
||||||
|
|
||||||
// Doesn't exist in early initialization.
|
// Doesn't exist in early initialization.
|
||||||
let webViewManager = null
|
let webViewManager = null
|
||||||
|
@ -187,27 +188,13 @@ const attachGuest = function (embedder, elementInstanceId, guestInstanceId, para
|
||||||
// parse the 'webpreferences' attribute string, if set
|
// parse the 'webpreferences' attribute string, if set
|
||||||
// this uses the same parsing rules as window.open uses for its features
|
// this uses the same parsing rules as window.open uses for its features
|
||||||
if (typeof params.webpreferences === 'string') {
|
if (typeof params.webpreferences === 'string') {
|
||||||
// split the attribute's value by ','
|
parseFeaturesString(params.webpreferences, function (key, value) {
|
||||||
let i, len
|
|
||||||
let webpreferencesTokens = params.webpreferences.split(/,\s*/)
|
|
||||||
for (i = 0, len = webpreferencesTokens.length; i < len; i++) {
|
|
||||||
// expected form is either a name by itself (true boolean flag)
|
|
||||||
// or a key/value, in the form of 'name=value'
|
|
||||||
// split the tokens by '='
|
|
||||||
let pref = webpreferencesTokens[i]
|
|
||||||
let prefTokens = pref.split(/\s*=/)
|
|
||||||
let name = prefTokens[0]
|
|
||||||
let value = prefTokens[1]
|
|
||||||
if (!name) continue
|
|
||||||
|
|
||||||
// interpret the value as a boolean, if possible
|
|
||||||
value = (value === 'yes' || value === '1') ? true : (value === 'no' || value === '0') ? false : value
|
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
// no value was specified, default it to true
|
// no value was specified, default it to true
|
||||||
value = true
|
value = true
|
||||||
}
|
}
|
||||||
webPreferences[name] = value
|
webPreferences[name] = value
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.preload) {
|
if (params.preload) {
|
||||||
|
|
24
lib/common/parse-features-string.js
Normal file
24
lib/common/parse-features-string.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
// parses a feature string that has the format used in window.open()
|
||||||
|
// - `str` input string
|
||||||
|
// - `emit` function(key, value) - called for each parsed KV
|
||||||
|
module.exports = function parseFeaturesString (str, emit) {
|
||||||
|
// split the string by ','
|
||||||
|
let i, len
|
||||||
|
let strTokens = params.str.split(/,\s*/)
|
||||||
|
for (i = 0, len = strTokens.length; i < len; i++) {
|
||||||
|
// expected form is either a key by itself (true boolean flag)
|
||||||
|
// or a key/value, in the form of 'key=value'
|
||||||
|
// split the tokens by '='
|
||||||
|
let kv = strTokens[i]
|
||||||
|
let kvTokens = kv.split(/\s*=/)
|
||||||
|
let key = kvTokens[0]
|
||||||
|
let value = kvTokens[1]
|
||||||
|
if (!key) continue
|
||||||
|
|
||||||
|
// interpret the value as a boolean, if possible
|
||||||
|
value = (value === 'yes' || value === '1') ? true : (value === 'no' || value === '0') ? false : value
|
||||||
|
|
||||||
|
// emit the parsed pair
|
||||||
|
emit(key, value)
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const ipcRenderer = require('electron').ipcRenderer
|
const ipcRenderer = require('electron').ipcRenderer
|
||||||
const remote = require('electron').remote
|
const remote = require('electron').remote
|
||||||
|
const parseFeaturesString = require('../common/parse-features-string')
|
||||||
|
|
||||||
// Helper function to resolve relative url.
|
// Helper function to resolve relative url.
|
||||||
var a = window.top.document.createElement('a')
|
var a = window.top.document.createElement('a')
|
||||||
|
@ -104,27 +105,21 @@ window.open = function (url, frameName, features) {
|
||||||
// Used to store additional features
|
// Used to store additional features
|
||||||
additionalFeatures = []
|
additionalFeatures = []
|
||||||
|
|
||||||
// Make sure to get rid of excessive whitespace in the property name
|
// Parse the features
|
||||||
ref1 = features.split(/,\s*/)
|
parseFeaturesString(features, function (key, value) {
|
||||||
for (i = 0, len = ref1.length; i < len; i++) {
|
|
||||||
feature = ref1[i]
|
|
||||||
ref2 = feature.split(/\s*=/)
|
|
||||||
name = ref2[0]
|
|
||||||
value = ref2[1]
|
|
||||||
value = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value
|
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
additionalFeatures.push(feature)
|
additionalFeatures.push(key)
|
||||||
} else {
|
} else {
|
||||||
if (webPreferences.includes(name)) {
|
if (webPreferences.includes(key)) {
|
||||||
if (options.webPreferences == null) {
|
if (options.webPreferences == null) {
|
||||||
options.webPreferences = {}
|
options.webPreferences = {}
|
||||||
}
|
}
|
||||||
options.webPreferences[name] = value
|
options.webPreferences[key] = value
|
||||||
} else {
|
} else {
|
||||||
options[name] = value
|
options[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
if (options.left) {
|
if (options.left) {
|
||||||
if (options.x == null) {
|
if (options.x == null) {
|
||||||
options.x = options.left
|
options.x = options.left
|
||||||
|
|
Loading…
Reference in a new issue