Skip to content

Instantly share code, notes, and snippets.

@futurist
Created September 29, 2016 10:24
Show Gist options
  • Select an option

  • Save futurist/e962c28fa7c11118735952cbe2c53a61 to your computer and use it in GitHub Desktop.

Select an option

Save futurist/e962c28fa7c11118735952cbe2c53a61 to your computer and use it in GitHub Desktop.
// map key/value into newValue, each array elem holds: [key, value, newValue]
function cssobj_plugin_map (option) {
option = option || {}
var replaceMap = option.maps || [
[
'display',
'flex',
[
{
display: '-webkit-box'
}, {
display: '-moz-box'
}, {
display: '-ms-flexbox',
'-ms-flex-preferred-size': 'initial' // IE10
}, {
display: '-webkit-flex'
}, {
display: 'flex'
}
]
],
[
'display',
'inline-flex',
[
{
display: '-ms-inline-flexbox'
}, {
display: '-webkit-inline-flex'
}, {
display: 'inline-flex'
}
]
],
[
'flex-direction',
'row',
{
'-ms-flex-direction': 'row',
'-webkit-flex-direction': 'row',
'flex-direction': 'row'
}
],
[null,null, function(key, value){return key=='color'? {[key]:value, zoom:1} : {}}]
]
return {
value: function (value, key, node, result, propKey) {
if(propKey!==undefined) return value
// retVal hold all maps result
var retVal = []
// replace from prop+value, to mapped value
for(var map, val, i = 0, len = replaceMap.length; i < len; i++) {
map = replaceMap[i]
val = map[2]
// map[0] : prop, null as placeholder
// map[1] : value, null as placeholder
// map[2] : replacement, function(key, value){} || any
if((map[0]===null || map[0]==key)
&& (map[1]===null || map[1]==value))
retVal.push(
typeof val=='function'
? val(key, value, node, result)
: val
)
}
// only accept replaced value, return original value as fallback
return retVal.length ? retVal : value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment