Skip to content

Commit 1270092

Browse files
author
rofrischmann
committed
#89 fixed array prefixing with fallback values
closes #89
1 parent 456cdfc commit 1270092

7 files changed

Lines changed: 36 additions & 54 deletions

File tree

modules/static/plugins/calc.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import joinPrefixedRules from '../../utils/joinPrefixedRules'
1+
import joinPrefixedValue from '../../utils/joinPrefixedValue'
22
import isPrefixedValue from '../../utils/isPrefixedValue'
33

44
export default function calc(property, value) {
5-
if (typeof value === 'string' && value.indexOf('calc(') > -1) {
6-
if (isPrefixedValue(value)) {
7-
return
8-
}
9-
10-
return joinPrefixedRules(property, value, (prefix, value) => value.replace(/calc\(/g, prefix + 'calc('))
5+
if (typeof value === 'string' && !isPrefixedValue(value) && value.indexOf('calc(') > -1) {
6+
return joinPrefixedValue(property, value, (prefix, value) => value.replace(/calc\(/g, prefix + 'calc('))
117
}
128
}

modules/static/plugins/cursor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import joinPrefixedRules from '../../utils/joinPrefixedRules'
1+
import joinPrefixedValue from '../../utils/joinPrefixedValue'
22

33
const values = {
44
'zoom-in': true,
@@ -9,6 +9,6 @@ const values = {
99

1010
export default function cursor(property, value) {
1111
if (property === 'cursor' && values[value]) {
12-
return joinPrefixedRules(property, value)
12+
return joinPrefixedValue(property, value)
1313
}
1414
}

modules/static/plugins/gradient.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import joinPrefixedRules from '../../utils/joinPrefixedRules'
1+
import joinPrefixedValue from '../../utils/joinPrefixedValue'
22
import isPrefixedValue from '../../utils/isPrefixedValue'
33

44
const values = /linear-gradient|radial-gradient|repeating-linear-gradient|repeating-radial-gradient/
55

66
export default function gradient(property, value) {
7-
if (typeof value === 'string' && value.match(values) !== null) {
8-
if (isPrefixedValue(value)) {
9-
return
10-
}
11-
12-
return joinPrefixedRules(property, value)
7+
if (typeof value === 'string' && !isPrefixedValue(value) && value.match(values) !== null) {
8+
return joinPrefixedValue(property, value)
139
}
1410
}

modules/static/plugins/sizing.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import joinPrefixedRules from '../../utils/joinPrefixedRules'
1+
import joinPrefixedValue from '../../utils/joinPrefixedValue'
22

33
const properties = {
44
maxHeight: true,
@@ -19,6 +19,6 @@ const values = {
1919

2020
export default function sizing(property, value) {
2121
if (properties[property] && values[value]) {
22-
return joinPrefixedRules(property, value)
22+
return joinPrefixedValue(property, value)
2323
}
2424
}

modules/static/prefixAll.js

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import prefixProperties from './prefixProps'
22
import capitalizeString from '../utils/capitalizeString'
3-
import assign from '../utils/assign'
43

54
import calc from './plugins/calc'
65
import cursor from './plugins/cursor'
@@ -34,9 +33,6 @@ export default function prefixAll(styles) {
3433
if (value instanceof Object && !Array.isArray(value)) {
3534
// recurse through nested style objects
3635
styles[property] = prefixAll(value)
37-
} else if (Array.isArray(value)) {
38-
// prefix fallback arrays
39-
assign(styles, prefixArray(property, value))
4036
} else {
4137
Object.keys(prefixProperties).forEach(prefix => {
4238
const properties = prefixProperties[prefix]
@@ -49,42 +45,28 @@ export default function prefixAll(styles) {
4945
})
5046

5147
Object.keys(styles).forEach(property => {
52-
const value = styles[property]
53-
// resolve every special plugins
54-
plugins.forEach(plugin => assign(styles, plugin(property, value)))
48+
[ ].concat(styles[property]).forEach((value, index) => {
49+
// resolve every special plugins
50+
plugins.forEach(plugin => assignStyles(styles, plugin(property, value)))
51+
})
5552
})
5653

5754
return styles
5855
}
5956

60-
function prefixArray(property, valueArray) {
61-
let result = { }
62-
valueArray.forEach(value => {
63-
plugins.forEach(plugin => {
64-
let prefixed = plugin(property, value)
65-
if (prefixed) {
66-
Object.keys(prefixed).forEach(prop => {
67-
const entry = prefixed[prop]
68-
result[prop] = result[prop] ? mergeValues(result[prop], entry) : entry
69-
})
70-
}
71-
})
72-
if (!result[property]) {
73-
result[property] = value
74-
}
75-
})
76-
return result
77-
}
78-
79-
function mergeValues(existing, toMerge) {
80-
let merged = existing
81-
let valuesToMerge = Array.isArray(toMerge) ? toMerge : [ toMerge ]
82-
valuesToMerge.forEach(value => {
83-
if (Array.isArray(merged) && merged.indexOf(value) === -1) {
84-
merged.push(value)
85-
} else if (merged !== value) {
86-
merged = [ merged, value ]
57+
function assignStyles(base, extend = { }) {
58+
Object.keys(extend).forEach(property => {
59+
const baseValue = base[property]
60+
if (Array.isArray(baseValue)) {
61+
[ ].concat(extend[property]).forEach(value => {
62+
const valueIndex = baseValue.indexOf(value)
63+
if (valueIndex > -1) {
64+
base[property].splice(valueIndex, 1)
65+
}
66+
base[property].push(value)
67+
})
68+
} else {
69+
base[property] = extend[property]
8770
}
8871
})
89-
return merged
9072
}

test/prefixAll-test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ describe('Resolving special plugins', () => {
226226
expect(prefixAll(input)).to.eql(output)
227227
})
228228

229+
it('should prefix special sizing values', () => {
230+
const input = { width: [ 'min-content', '100%' ] }
231+
const output = {
232+
width: [ '100%', '-webkit-min-content', '-moz-min-content', 'min-content' ]
233+
}
234+
expect(prefixAll(input)).to.eql(output)
235+
expect(prefixAll(input)).to.eql(output)
236+
})
237+
229238
it('should prefix special sizing values', () => {
230239
const input = { width: [ 'calc(100%)', 'min-content' ] }
231240
const output = {
@@ -252,5 +261,4 @@ describe('Resolving special plugins', () => {
252261
expect(prefixAll(input)).to.eql(output)
253262
expect(prefixAll(input)).to.eql(output)
254263
})
255-
256264
})

0 commit comments

Comments
 (0)