factor out parse-features-string.js

This commit is contained in:
Paul Frazee 2016-10-24 20:21:42 -05:00
parent e7962c7ba2
commit f35536bdc5
3 changed files with 35 additions and 29 deletions

View file

@ -2,6 +2,7 @@
const ipcMain = require('electron').ipcMain
const webContents = require('electron').webContents
const parseFeaturesString = require('../common/parse-features-string')
// Doesn't exist in early initialization.
let webViewManager = null
@ -187,27 +188,13 @@ const attachGuest = function (embedder, elementInstanceId, guestInstanceId, para
// 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') {
// split the attribute's value by ','
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
parseFeaturesString(params.webpreferences, function (key, value) {
if (value === undefined) {
// no value was specified, default it to true
value = true
}
webPreferences[name] = value
}
})
}
if (params.preload) {

View 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)
}
}

View file

@ -2,6 +2,7 @@
const ipcRenderer = require('electron').ipcRenderer
const remote = require('electron').remote
const parseFeaturesString = require('../common/parse-features-string')
// Helper function to resolve relative url.
var a = window.top.document.createElement('a')
@ -104,27 +105,21 @@ window.open = function (url, frameName, features) {
// Used to store additional features
additionalFeatures = []
// Make sure to get rid of excessive whitespace in the property name
ref1 = features.split(/,\s*/)
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
// Parse the features
parseFeaturesString(features, function (key, value) {
if (value === undefined) {
additionalFeatures.push(feature)
additionalFeatures.push(key)
} else {
if (webPreferences.includes(name)) {
if (webPreferences.includes(key)) {
if (options.webPreferences == null) {
options.webPreferences = {}
}
options.webPreferences[name] = value
options.webPreferences[key] = value
} else {
options[name] = value
}
options[key] = value
}
}
})
if (options.left) {
if (options.x == null) {
options.x = options.left