Skip to content

Commit

Permalink
fix: make feature strings more robust to whitespace (#15602)
Browse files Browse the repository at this point in the history
Fixes #15594
  • Loading branch information
nornagon committed Nov 6, 2018
1 parent c52cf01 commit 10969b8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/common/parse-features-string.js
Expand Up @@ -4,12 +4,12 @@
// - `features` input string
// - `emit` function(key, value) - called for each parsed KV
module.exports = function parseFeaturesString (features, emit) {
features = `${features}`
features = `${features}`.trim()
// split the string by ','
features.split(/,\s*/).forEach((feature) => {
features.split(/\s*,\s*/).forEach((feature) => {
// expected form is either a key by itself or a key/value pair in the form of
// 'key=value'
let [key, value] = feature.split(/\s*=/)
let [key, value] = feature.split(/\s*=\s*/)
if (!key) return

// interpret the value as a boolean, if possible
Expand Down
23 changes: 23 additions & 0 deletions spec/internal-spec.js
@@ -0,0 +1,23 @@
const chai = require('chai')
const { expect } = chai

describe('feature-string parsing', () => {
it('is indifferent to whitespace around keys and values', () => {
const parseFeaturesString = require('@electron/internal/common/parse-features-string')
const checkParse = (string, parsed) => {
const features = {}
parseFeaturesString(string, (k, v) => { features[k] = v })
expect(features).to.deep.equal(parsed)
}
checkParse('a=yes,c=d', { a: true, c: 'd' })
checkParse('a=yes ,c=d', { a: true, c: 'd' })
checkParse('a=yes, c=d', { a: true, c: 'd' })
checkParse('a=yes , c=d', { a: true, c: 'd' })
checkParse(' a=yes , c=d', { a: true, c: 'd' })
checkParse(' a= yes , c=d', { a: true, c: 'd' })
checkParse(' a = yes , c=d', { a: true, c: 'd' })
checkParse(' a = yes , c =d', { a: true, c: 'd' })
checkParse(' a = yes , c = d', { a: true, c: 'd' })
checkParse(' a = yes , c = d ', { a: true, c: 'd' })
})
})

0 comments on commit 10969b8

Please sign in to comment.