Skip to content

Commit f35536b

Browse files
committed
factor out parse-features-string.js
1 parent e7962c7 commit f35536b

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

lib/browser/guest-view-manager.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const ipcMain = require('electron').ipcMain
44
const webContents = require('electron').webContents
5+
const parseFeaturesString = require('../common/parse-features-string')
56

67
// Doesn't exist in early initialization.
78
let webViewManager = null
@@ -187,27 +188,13 @@ const attachGuest = function (embedder, elementInstanceId, guestInstanceId, para
187188
// parse the 'webpreferences' attribute string, if set
188189
// this uses the same parsing rules as window.open uses for its features
189190
if (typeof params.webpreferences === 'string') {
190-
// split the attribute's value by ','
191-
let i, len
192-
let webpreferencesTokens = params.webpreferences.split(/,\s*/)
193-
for (i = 0, len = webpreferencesTokens.length; i < len; i++) {
194-
// expected form is either a name by itself (true boolean flag)
195-
// or a key/value, in the form of 'name=value'
196-
// split the tokens by '='
197-
let pref = webpreferencesTokens[i]
198-
let prefTokens = pref.split(/\s*=/)
199-
let name = prefTokens[0]
200-
let value = prefTokens[1]
201-
if (!name) continue
202-
203-
// interpret the value as a boolean, if possible
204-
value = (value === 'yes' || value === '1') ? true : (value === 'no' || value === '0') ? false : value
191+
parseFeaturesString(params.webpreferences, function (key, value) {
205192
if (value === undefined) {
206193
// no value was specified, default it to true
207194
value = true
208195
}
209196
webPreferences[name] = value
210-
}
197+
})
211198
}
212199

213200
if (params.preload) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// parses a feature string that has the format used in window.open()
2+
// - `str` input string
3+
// - `emit` function(key, value) - called for each parsed KV
4+
module.exports = function parseFeaturesString (str, emit) {
5+
// split the string by ','
6+
let i, len
7+
let strTokens = params.str.split(/,\s*/)
8+
for (i = 0, len = strTokens.length; i < len; i++) {
9+
// expected form is either a key by itself (true boolean flag)
10+
// or a key/value, in the form of 'key=value'
11+
// split the tokens by '='
12+
let kv = strTokens[i]
13+
let kvTokens = kv.split(/\s*=/)
14+
let key = kvTokens[0]
15+
let value = kvTokens[1]
16+
if (!key) continue
17+
18+
// interpret the value as a boolean, if possible
19+
value = (value === 'yes' || value === '1') ? true : (value === 'no' || value === '0') ? false : value
20+
21+
// emit the parsed pair
22+
emit(key, value)
23+
}
24+
}

lib/renderer/override.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const ipcRenderer = require('electron').ipcRenderer
44
const remote = require('electron').remote
5+
const parseFeaturesString = require('../common/parse-features-string')
56

67
// Helper function to resolve relative url.
78
var a = window.top.document.createElement('a')
@@ -104,27 +105,21 @@ window.open = function (url, frameName, features) {
104105
// Used to store additional features
105106
additionalFeatures = []
106107

107-
// Make sure to get rid of excessive whitespace in the property name
108-
ref1 = features.split(/,\s*/)
109-
for (i = 0, len = ref1.length; i < len; i++) {
110-
feature = ref1[i]
111-
ref2 = feature.split(/\s*=/)
112-
name = ref2[0]
113-
value = ref2[1]
114-
value = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value
108+
// Parse the features
109+
parseFeaturesString(features, function (key, value) {
115110
if (value === undefined) {
116-
additionalFeatures.push(feature)
111+
additionalFeatures.push(key)
117112
} else {
118-
if (webPreferences.includes(name)) {
113+
if (webPreferences.includes(key)) {
119114
if (options.webPreferences == null) {
120115
options.webPreferences = {}
121116
}
122-
options.webPreferences[name] = value
117+
options.webPreferences[key] = value
123118
} else {
124-
options[name] = value
119+
options[key] = value
125120
}
126121
}
127-
}
122+
})
128123
if (options.left) {
129124
if (options.x == null) {
130125
options.x = options.left

0 commit comments

Comments
 (0)