postcss-merge-longhand
Advanced tools
Comparing version 2.0.2 to 3.0.0
@@ -0,1 +1,13 @@ | ||
# 3.0.0 | ||
* Rewrote the module, with lots of bug fixes and improvements to longhand | ||
merging (thanks to @andyjansson). | ||
* Now supports merging `column-width` & `column-count` into `columns`. | ||
* Now supports merging `border-left-width` (plus the other 3) into | ||
`border-width`, where the previous behaviour would merge them into `border`. | ||
* Now supports merging single longhand properties into the shorthand, e.g. | ||
`padding: 2px;padding-top: 5px` becomes `padding: 5px 2px 2px`. | ||
* Better support for browser hacks; the module will not merge properties when | ||
they have browser hacks applied. | ||
# 2.0.2 | ||
@@ -2,0 +14,0 @@ |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _postcss = require('postcss'); | ||
@@ -13,84 +9,15 @@ | ||
var _libCanMerge = require('./lib/canMerge'); | ||
var _decl = require('./lib/decl'); | ||
var _libCanMerge2 = _interopRequireDefault(_libCanMerge); | ||
var _decl2 = _interopRequireDefault(_decl); | ||
var _libGetLastNode = require('./lib/getLastNode'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _libGetLastNode2 = _interopRequireDefault(_libGetLastNode); | ||
var _libHasAllProps = require('./lib/hasAllProps'); | ||
var _libHasAllProps2 = _interopRequireDefault(_libHasAllProps); | ||
var _libIdentical = require('./lib/identical'); | ||
var _libIdentical2 = _interopRequireDefault(_libIdentical); | ||
var _libMergeValues = require('./lib/mergeValues'); | ||
var _libMergeValues2 = _interopRequireDefault(_libMergeValues); | ||
var _libMinifyTrbl = require('./lib/minifyTrbl'); | ||
var _libMinifyTrbl2 = _interopRequireDefault(_libMinifyTrbl); | ||
var _libNumValues = require('./lib/numValues'); | ||
var _libNumValues2 = _interopRequireDefault(_libNumValues); | ||
var trbl = ['top', 'right', 'bottom', 'left']; | ||
var trblProps = ['margin', 'padding', 'border-color', 'border-width', 'border-style']; | ||
var trblMap = function trblMap(prop) { | ||
return trbl.map(function (direction) { | ||
return prop + '-' + direction; | ||
}); | ||
}; | ||
var remove = function remove(node) { | ||
return node.remove(); | ||
}; | ||
var mergeLonghand = function mergeLonghand(rule, prop) { | ||
var properties = trblMap(prop); | ||
if (_libHasAllProps2['default'].apply(undefined, [rule].concat(properties))) { | ||
var rules = properties.map(function (p) { | ||
return (0, _libGetLastNode2['default'])(rule, p); | ||
}); | ||
if (_libCanMerge2['default'].apply(undefined, rules)) { | ||
rules.slice(0, 3).forEach(remove); | ||
rules[3].value = (0, _libMinifyTrbl2['default'])(_libMergeValues2['default'].apply(undefined, rules)); | ||
rules[3].prop = prop; | ||
} | ||
} | ||
}; | ||
exports['default'] = _postcss2['default'].plugin('postcss-merge-longhand', function () { | ||
exports.default = _postcss2.default.plugin('postcss-merge-longhand', function () { | ||
return function (css) { | ||
css.walkRules(function (rule) { | ||
rule.nodes.filter(function (node) { | ||
return node.prop && ~trblProps.indexOf(node.prop); | ||
}).forEach(function (node) { | ||
node.value = (0, _libMinifyTrbl2['default'])(node.value); | ||
_decl2.default.forEach(function (p) { | ||
p.explode(rule); | ||
p.merge(rule); | ||
}); | ||
mergeLonghand(rule, 'margin'); | ||
mergeLonghand(rule, 'padding'); | ||
if ((0, _libHasAllProps2['default'])(rule, 'border-color', 'border-style', 'border-width')) { | ||
var rules = [(0, _libGetLastNode2['default'])(rule, 'border-width'), (0, _libGetLastNode2['default'])(rule, 'border-style'), (0, _libGetLastNode2['default'])(rule, 'border-color')]; | ||
if (_libCanMerge2['default'].apply(undefined, rules) && _libNumValues2['default'].apply(undefined, rules) === 3) { | ||
rules.slice(0, 2).forEach(remove); | ||
rules[2].prop = 'border'; | ||
rules[2].value = _libMergeValues2['default'].apply(undefined, rules); | ||
} | ||
} | ||
if (_libHasAllProps2['default'].apply(undefined, [rule].concat(trblMap('border')))) { | ||
var rules = [(0, _libGetLastNode2['default'])(rule, 'border-top'), (0, _libGetLastNode2['default'])(rule, 'border-right'), (0, _libGetLastNode2['default'])(rule, 'border-bottom'), (0, _libGetLastNode2['default'])(rule, 'border-left')]; | ||
if (_libCanMerge2['default'].apply(undefined, rules) && _libIdentical2['default'].apply(undefined, rules)) { | ||
rules.slice(0, 3).forEach(remove); | ||
rules[3].prop = 'border'; | ||
} | ||
} | ||
}); | ||
@@ -97,0 +24,0 @@ }; |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var important = function important(node) { | ||
@@ -13,3 +11,3 @@ return node.important; | ||
var hasInherit = function hasInherit(node) { | ||
return ~node.value.indexOf('inherit'); | ||
return node.value && ~node.value.indexOf('inherit'); | ||
}; | ||
@@ -20,3 +18,3 @@ var hasInitial = function hasInitial(node) { | ||
exports['default'] = function () { | ||
exports.default = function () { | ||
for (var _len = arguments.length, props = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -27,7 +25,7 @@ props[_key] = arguments[_key]; | ||
if (props.some(hasInherit) || props.some(hasInitial)) { | ||
return false; | ||
return props.every(hasInherit) || props.every(hasInitial); | ||
} | ||
return props.every(important) || props.every(unimportant); | ||
return props.every(unimportant) || props.every(important); | ||
}; | ||
module.exports = exports['default']; |
@@ -9,3 +9,3 @@ 'use strict'; | ||
function clone(obj, parent) { | ||
if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') { | ||
if (obj === null || (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') { | ||
return obj; | ||
@@ -12,0 +12,0 @@ } |
@@ -39,6 +39,6 @@ 'use strict'; | ||
function colorMerge(_ref) { | ||
var rule = _ref.rule; | ||
var properties = _ref.properties; | ||
var prop = _ref.prop; | ||
var value = _ref.value; | ||
var rule = _ref.rule, | ||
properties = _ref.properties, | ||
prop = _ref.prop, | ||
value = _ref.value; | ||
@@ -45,0 +45,0 @@ var decls = (0, _getDecls2.default)(rule, properties); |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _postcss = require('postcss'); | ||
@@ -22,6 +20,2 @@ | ||
var _genericMerge = require('../genericMerge'); | ||
var _genericMerge2 = _interopRequireDefault(_genericMerge); | ||
var _insertCloned = require('../insertCloned'); | ||
@@ -39,6 +33,2 @@ | ||
var _getLastNode = require('../getLastNode'); | ||
var _getLastNode2 = _interopRequireDefault(_getLastNode); | ||
var _getDecls = require('../getDecls'); | ||
@@ -56,2 +46,6 @@ | ||
var _mergeRules = require('../mergeRules'); | ||
var _mergeRules2 = _interopRequireDefault(_mergeRules); | ||
var _minifyTrbl = require('../minifyTrbl'); | ||
@@ -65,6 +59,2 @@ | ||
var _colorMerge = require('../colorMerge'); | ||
var _colorMerge2 = _interopRequireDefault(_colorMerge); | ||
var _remove = require('../remove'); | ||
@@ -83,17 +73,66 @@ | ||
var borderProperty = function borderProperty(property) { | ||
return 'border-' + property; | ||
}; | ||
var directions = _trbl2.default.map(borderProperty); | ||
var properties = wsc.map(borderProperty); | ||
function borderProperty() { | ||
for (var _len = arguments.length, parts = Array(_len), _key = 0; _key < _len; _key++) { | ||
parts[_key] = arguments[_key]; | ||
} | ||
return 'border-' + parts.join('-'); | ||
} | ||
function mapBorderProperty(value) { | ||
return borderProperty(value); | ||
} | ||
var directions = _trbl2.default.map(mapBorderProperty); | ||
var properties = wsc.map(mapBorderProperty); | ||
var directionalProperties = directions.reduce(function (prev, curr) { | ||
return prev.concat(wsc.map(function (prop) { | ||
return curr + '-' + prop; | ||
})); | ||
}, []); | ||
var precedence = [['border'], directions.concat(properties), directionalProperties]; | ||
var allProperties = precedence.reduce(function (a, b) { | ||
return a.concat(b); | ||
}); | ||
function getLevel(prop) { | ||
for (var i = 0; i < precedence.length; i++) { | ||
if (!!~precedence[i].indexOf(prop)) { | ||
return i; | ||
} | ||
} | ||
} | ||
function getColorValue(decl) { | ||
var values = _postcss.list.space(decl.value); | ||
if (decl.prop === 'border') { | ||
return values[2]; | ||
} | ||
if (!!~directions.indexOf(decl.prop)) { | ||
return values[2]; | ||
} | ||
if (decl.prop.substr(-5) === 'color') { | ||
return decl.value; | ||
} | ||
return null; | ||
} | ||
function mergeRedundant(_ref) { | ||
var values = _ref.values; | ||
var nextValues = _ref.nextValues; | ||
var decl = _ref.decl; | ||
var nextDecl = _ref.nextDecl; | ||
var index = _ref.index; | ||
var position = _ref.position; | ||
var prop = _ref.prop; | ||
var values = _ref.values, | ||
nextValues = _ref.nextValues, | ||
decl = _ref.decl, | ||
nextDecl = _ref.nextDecl, | ||
index = _ref.index, | ||
position = _ref.position, | ||
prop = _ref.prop; | ||
if ((0, _stylehacks.detect)(decl) || (0, _stylehacks.detect)(nextDecl)) { | ||
return; | ||
} | ||
var props = (0, _parseTrbl2.default)(values[position]); | ||
@@ -130,5 +169,2 @@ props[index] = nextValues[position]; | ||
function explode(rule) { | ||
if (rule.nodes.some(_stylehacks.detect)) { | ||
return false; | ||
} | ||
rule.walkDecls(/^border/, function (decl) { | ||
@@ -139,2 +175,7 @@ // Don't explode inherit values as they cannot be merged together | ||
} | ||
if ((0, _stylehacks.detect)(decl)) { | ||
return; | ||
} | ||
var prop = decl.prop; | ||
@@ -145,3 +186,3 @@ // border -> border-trbl | ||
directions.forEach(function (direction) { | ||
(0, _insertCloned2.default)(rule, decl, { prop: direction }); | ||
(0, _insertCloned2.default)(decl.parent, decl, { prop: direction }); | ||
}); | ||
@@ -154,25 +195,19 @@ return decl.remove(); | ||
})) { | ||
var _ret = function () { | ||
var values = _postcss.list.space(decl.value); | ||
wsc.forEach(function (d, i) { | ||
(0, _insertCloned2.default)(rule, decl, { | ||
prop: prop + '-' + d, | ||
value: values[i] || defaults[i] | ||
}); | ||
var values = _postcss.list.space(decl.value); | ||
wsc.forEach(function (d, i) { | ||
(0, _insertCloned2.default)(decl.parent, decl, { | ||
prop: prop + '-' + d, | ||
value: values[i] || defaults[i] | ||
}); | ||
return { | ||
v: decl.remove() | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
}); | ||
return decl.remove(); | ||
} | ||
// border-wsc -> border-trbl-wsc | ||
wsc.some(function (style) { | ||
if (prop !== 'border-' + style) { | ||
if (prop !== borderProperty(style)) { | ||
return false; | ||
} | ||
(0, _parseTrbl2.default)(decl.value).forEach(function (value, i) { | ||
(0, _insertCloned2.default)(rule, decl, { | ||
prop: 'border-' + _trbl2.default[i] + '-' + style, | ||
(0, _insertCloned2.default)(decl.parent, decl, { | ||
prop: borderProperty(_trbl2.default[i], style), | ||
value: value | ||
@@ -186,29 +221,16 @@ }); | ||
var borderProperties = _trbl2.default.reduce(function (props, direction) { | ||
return [].concat(props, wsc.map(function (style) { | ||
return 'border-' + direction + '-' + style; | ||
})); | ||
}, []); | ||
function merge(rule) { | ||
// Lift all inherit values from the rule, so that they don't | ||
// interfere with the merging logic. | ||
var inheritValues = (0, _getDecls2.default)(rule, borderProperties).reduce(function (values, decl) { | ||
if (decl.value === 'inherit') { | ||
decl.remove(); | ||
return [].concat(values, [decl]); | ||
} | ||
return values; | ||
}, []); | ||
// border-trbl-wsc -> border-trbl | ||
_trbl2.default.forEach(function (direction) { | ||
var prop = borderProperty(direction); | ||
(0, _genericMerge2.default)({ | ||
rule: rule, | ||
prop: prop, | ||
properties: wsc.map(function (style) { | ||
return prop + '-' + style; | ||
}), | ||
value: function value(rules) { | ||
return rules.map(_getValue2.default).join(' '); | ||
(0, _mergeRules2.default)(rule, wsc.map(function (style) { | ||
return borderProperty(direction, style); | ||
}), function (rules, lastNode) { | ||
if (_canMerge2.default.apply(undefined, rules) && !rules.some(_stylehacks.detect)) { | ||
(0, _insertCloned2.default)(lastNode.parent, lastNode, { | ||
prop: prop, | ||
value: rules.map(_getValue2.default).join(' ') | ||
}); | ||
rules.forEach(_remove2.default); | ||
return true; | ||
} | ||
@@ -221,24 +243,13 @@ }); | ||
var prop = borderProperty(style); | ||
if (style === 'color') { | ||
return (0, _colorMerge2.default)({ | ||
rule: rule, | ||
prop: prop, | ||
properties: _trbl2.default.map(function (direction) { | ||
return 'border-' + direction + '-' + style; | ||
}), | ||
value: function value(rules) { | ||
return (0, _minifyTrbl2.default)(rules.map(_getValue2.default).join(' ')); | ||
} | ||
}); | ||
} | ||
return (0, _genericMerge2.default)({ | ||
rule: rule, | ||
prop: prop, | ||
properties: _trbl2.default.map(function (direction) { | ||
return 'border-' + direction + '-' + style; | ||
}), | ||
value: function value(rules) { | ||
return (0, _minifyTrbl2.default)(rules.map(_getValue2.default).join(' ')); | ||
}, | ||
sanitize: false | ||
(0, _mergeRules2.default)(rule, _trbl2.default.map(function (direction) { | ||
return borderProperty(direction, style); | ||
}), function (rules, lastNode) { | ||
if (!rules.some(_stylehacks.detect)) { | ||
(0, _insertCloned2.default)(lastNode.parent, lastNode, { | ||
prop: prop, | ||
value: (0, _minifyTrbl2.default)(rules.map(_getValue2.default).join(' ')) | ||
}); | ||
rules.forEach(_remove2.default); | ||
return true; | ||
} | ||
}); | ||
@@ -248,99 +259,115 @@ }); | ||
// border-trbl -> border-wsc | ||
var decls = (0, _getDecls2.default)(rule, directions); | ||
var _loop = function _loop() { | ||
var lastNode = decls[decls.length - 1]; | ||
var props = decls.filter(function (node) { | ||
return node.important === lastNode.important; | ||
}); | ||
var rules = (0, _getRules2.default)(props, directions); | ||
if (_hasAllProps2.default.apply(undefined, [props].concat(directions))) { | ||
wsc.forEach(function (d, i) { | ||
(0, _insertCloned2.default)(rule, lastNode, { | ||
prop: borderProperty(d), | ||
value: (0, _minifyTrbl2.default)(rules.map(function (node) { | ||
return _postcss.list.space(node.value)[i]; | ||
})) | ||
}); | ||
(0, _mergeRules2.default)(rule, directions, function (rules, lastNode) { | ||
if (rules.some(_stylehacks.detect)) { | ||
return; | ||
} | ||
wsc.forEach(function (d, i) { | ||
(0, _insertCloned2.default)(lastNode.parent, lastNode, { | ||
prop: borderProperty(d), | ||
value: (0, _minifyTrbl2.default)(rules.map(function (node) { | ||
return _postcss.list.space(node.value)[i]; | ||
})) | ||
}); | ||
props.forEach(_remove2.default); | ||
} | ||
decls = decls.filter(function (node) { | ||
return !~rules.indexOf(node); | ||
}); | ||
}; | ||
rules.forEach(_remove2.default); | ||
return true; | ||
}); | ||
while (decls.length) { | ||
_loop(); | ||
} | ||
// border-wsc -> border | ||
// border-wsc -> border + border-color | ||
// border-wsc -> border + border-dir | ||
decls = (0, _getDecls2.default)(rule, properties); | ||
(0, _mergeRules2.default)(rule, properties, function (rules, lastNode) { | ||
if (rules.some(_stylehacks.detect)) { | ||
return; | ||
} | ||
var width = rules[0], | ||
style = rules[1], | ||
color = rules[2]; | ||
var _loop2 = function _loop2() { | ||
var lastNode = decls[decls.length - 1]; | ||
var props = decls.filter(function (node) { | ||
return node.important === lastNode.important; | ||
var values = rules.map(function (node) { | ||
return (0, _parseTrbl2.default)(node.value); | ||
}); | ||
if (_hasAllProps2.default.apply(undefined, [props].concat(properties))) { | ||
(function () { | ||
var rules = properties.map(function (prop) { | ||
return (0, _getLastNode2.default)(props, prop); | ||
}); | ||
var width = rules[0]; | ||
var style = rules[1]; | ||
var color = rules[2]; | ||
var mapped = [0, 1, 2, 3].map(function (i) { | ||
return [values[0][i], values[1][i], values[2][i]].join(' '); | ||
}); | ||
var reduced = getDistinctShorthands(mapped); | ||
var values = rules.map(function (node) { | ||
return (0, _parseTrbl2.default)(node.value); | ||
}); | ||
var mapped = [0, 1, 2, 3].map(function (i) { | ||
return [values[0][i], values[1][i], values[2][i]].join(' '); | ||
}); | ||
var reduced = getDistinctShorthands(mapped); | ||
if (isCloseEnough(mapped) && _canMerge2.default.apply(undefined, rules)) { | ||
var first = mapped.indexOf(reduced[0]) !== mapped.lastIndexOf(reduced[0]); | ||
if (isCloseEnough(mapped) && _canMerge2.default.apply(undefined, rules)) { | ||
var first = mapped.indexOf(reduced[0]) !== mapped.lastIndexOf(reduced[0]); | ||
var border = (0, _insertCloned2.default)(lastNode.parent, lastNode, { | ||
prop: 'border', | ||
value: first ? reduced[0] : reduced[1] | ||
}); | ||
var border = (0, _insertCloned2.default)(rule, lastNode, { | ||
prop: 'border', | ||
value: first ? reduced[0] : reduced[1] | ||
}); | ||
if (reduced[1]) { | ||
var value = first ? reduced[1] : reduced[0]; | ||
var prop = borderProperty(_trbl2.default[mapped.indexOf(value)]); | ||
if (reduced[1]) { | ||
var value = first ? reduced[1] : reduced[0]; | ||
var prop = 'border-' + _trbl2.default[mapped.indexOf(value)]; | ||
rule.insertAfter(border, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: prop, | ||
value: value | ||
})); | ||
} | ||
rules.forEach(_remove2.default); | ||
return true; | ||
} else if (reduced.length === 1) { | ||
rule.insertBefore(color, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: 'border', | ||
value: [width, style].map(_getValue2.default).join(' ') | ||
})); | ||
rules.filter(function (node) { | ||
return node.prop !== properties[2]; | ||
}).forEach(_remove2.default); | ||
return true; | ||
} | ||
}); | ||
rule.insertAfter(border, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: prop, | ||
value: value | ||
})); | ||
} | ||
props.forEach(_remove2.default); | ||
} else if (reduced.length === 1) { | ||
rule.insertBefore(color, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: 'border', | ||
value: [width, style].map(_getValue2.default).join(' ') | ||
// border-wsc -> border + border-trbl | ||
(0, _mergeRules2.default)(rule, properties, function (rules, lastNode) { | ||
if (rules.some(_stylehacks.detect)) { | ||
return; | ||
} | ||
var values = rules.map(function (node) { | ||
return (0, _parseTrbl2.default)(node.value); | ||
}); | ||
var mapped = [0, 1, 2, 3].map(function (i) { | ||
return [values[0][i], values[1][i], values[2][i]].join(' '); | ||
}); | ||
var reduced = getDistinctShorthands(mapped); | ||
var none = 'none none currentColor'; | ||
if (reduced.length === 2 && reduced[0] === none || reduced[1] === none) { | ||
var noOfNones = mapped.filter(function (value) { | ||
return value === none; | ||
}).length; | ||
rule.insertBefore(lastNode, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: 'border', | ||
value: noOfNones > 2 ? 'none' : mapped.filter(function (value) { | ||
return value !== none; | ||
})[0] | ||
})); | ||
directions.forEach(function (dir, i) { | ||
if (noOfNones > 2 && mapped[i] !== none) { | ||
rule.insertBefore(lastNode, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: dir, | ||
value: mapped[i] | ||
})); | ||
props.filter(function (node) { | ||
return node.prop !== properties[2]; | ||
}).forEach(_remove2.default); | ||
} | ||
})(); | ||
if (noOfNones <= 2 && mapped[i] === none) { | ||
rule.insertBefore(lastNode, (0, _objectAssign2.default)((0, _clone2.default)(lastNode), { | ||
prop: dir, | ||
value: 'none' | ||
})); | ||
} | ||
}); | ||
rules.forEach(_remove2.default); | ||
return true; | ||
} | ||
decls = decls.filter(function (node) { | ||
return !~props.indexOf(node); | ||
}); | ||
}; | ||
}); | ||
while (decls.length) { | ||
_loop2(); | ||
} | ||
// optimize border-trbl | ||
decls = (0, _getDecls2.default)(rule, directions); | ||
var decls = (0, _getDecls2.default)(rule, directions); | ||
var _loop3 = function _loop3() { | ||
var _loop = function _loop() { | ||
var lastNode = decls[decls.length - 1]; | ||
@@ -356,6 +383,5 @@ wsc.forEach(function (d, i) { | ||
}); | ||
if (_hasAllProps2.default.apply(undefined, [props].concat(names))) { | ||
var values = directions.map(function (prop) { | ||
return (0, _getLastNode2.default)(props, prop + '-' + d); | ||
}).map(function (node) { | ||
var rules = (0, _getRules2.default)(props, names); | ||
if (_hasAllProps2.default.apply(undefined, [rules].concat(names)) && !rules.some(_stylehacks.detect)) { | ||
var values = rules.map(function (node) { | ||
return node ? node.value : null; | ||
@@ -377,7 +403,10 @@ }); | ||
} | ||
(0, _insertCloned2.default)(rule, refNode, { | ||
prop: 'border-' + d, | ||
(0, _insertCloned2.default)(refNode.parent, refNode, { | ||
prop: borderProperty(d), | ||
value: value | ||
}); | ||
props.forEach(_remove2.default); | ||
decls = decls.filter(function (node) { | ||
return !~rules.indexOf(node); | ||
}); | ||
rules.forEach(_remove2.default); | ||
} | ||
@@ -391,3 +420,3 @@ }); | ||
while (decls.length) { | ||
_loop3(); | ||
_loop(); | ||
} | ||
@@ -448,6 +477,42 @@ | ||
// Restore inherited values | ||
inheritValues.forEach(function (decl) { | ||
return rule.append(decl); | ||
}); | ||
// clean-up rules | ||
decls = (0, _getDecls2.default)(rule, allProperties); | ||
var _loop2 = function _loop2() { | ||
var lastNode = decls[decls.length - 1]; | ||
// remove properties of lower precedence | ||
var lesser = decls.filter(function (node) { | ||
return !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && getLevel(node.prop) > getLevel(lastNode.prop); | ||
}); | ||
lesser.forEach(_remove2.default); | ||
decls = decls.filter(function (node) { | ||
return !~lesser.indexOf(node); | ||
}); | ||
// get duplicate properties | ||
var duplicates = decls.filter(function (node) { | ||
return !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && node.prop === lastNode.prop; | ||
}); | ||
if (duplicates.length) { | ||
if (/hsla|rgba/.test(getColorValue(lastNode))) { | ||
var preserve = duplicates.filter(function (node) { | ||
return !/hsla|rgba/.test(getColorValue(node)); | ||
}).pop(); | ||
duplicates = duplicates.filter(function (node) { | ||
return node !== preserve; | ||
}); | ||
} | ||
duplicates.forEach(_remove2.default); | ||
} | ||
decls = decls.filter(function (node) { | ||
return node !== lastNode && !~duplicates.indexOf(node); | ||
}); | ||
}; | ||
while (decls.length) { | ||
_loop2(); | ||
} | ||
} | ||
@@ -454,0 +519,0 @@ |
@@ -7,4 +7,10 @@ 'use strict'; | ||
var _genericMerge = require('../genericMerge'); | ||
var _canMerge = require('../canMerge'); | ||
var _canMerge2 = _interopRequireDefault(_canMerge); | ||
var _getDecls = require('../getDecls'); | ||
var _getDecls2 = _interopRequireDefault(_getDecls); | ||
var _minifyTrbl = require('../minifyTrbl'); | ||
@@ -22,2 +28,6 @@ | ||
var _mergeRules = require('../mergeRules'); | ||
var _mergeRules2 = _interopRequireDefault(_mergeRules); | ||
var _mergeValues = require('../mergeValues'); | ||
@@ -27,2 +37,6 @@ | ||
var _remove = require('../remove'); | ||
var _remove2 = _interopRequireDefault(_remove); | ||
var _trbl = require('../trbl'); | ||
@@ -39,7 +53,36 @@ | ||
var cleanup = function cleanup(rule) { | ||
var decls = (0, _getDecls2.default)(rule, [prop].concat(properties)); | ||
var _loop = function _loop() { | ||
var lastNode = decls[decls.length - 1]; | ||
// remove properties of lower precedence | ||
var lesser = decls.filter(function (node) { | ||
return !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && lastNode.prop === prop && node.prop !== lastNode.prop; | ||
}); | ||
lesser.forEach(_remove2.default); | ||
decls = decls.filter(function (node) { | ||
return !~lesser.indexOf(node); | ||
}); | ||
// get duplicate properties | ||
var duplicates = decls.filter(function (node) { | ||
return !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && node.prop === lastNode.prop; | ||
}); | ||
duplicates.forEach(_remove2.default); | ||
decls = decls.filter(function (node) { | ||
return node !== lastNode && !~duplicates.indexOf(node); | ||
}); | ||
}; | ||
while (decls.length) { | ||
_loop(); | ||
} | ||
}; | ||
var processor = { | ||
explode: function explode(rule) { | ||
if (rule.nodes.some(_stylehacks.detect)) { | ||
return false; | ||
} | ||
rule.walkDecls(prop, function (decl) { | ||
@@ -49,5 +92,8 @@ if (~decl.value.indexOf('inherit')) { | ||
} | ||
if ((0, _stylehacks.detect)(decl)) { | ||
return; | ||
} | ||
var values = (0, _parseTrbl2.default)(decl.value); | ||
_trbl2.default.forEach(function (direction, index) { | ||
(0, _insertCloned2.default)(rule, decl, { | ||
(0, _insertCloned2.default)(decl.parent, decl, { | ||
prop: properties[index], | ||
@@ -60,9 +106,15 @@ value: values[index] | ||
}, | ||
merge: (0, _genericMerge.genericMergeFactory)({ | ||
prop: prop, | ||
properties: properties, | ||
value: function value(rules) { | ||
return (0, _minifyTrbl2.default)(_mergeValues2.default.apply(undefined, rules)); | ||
} | ||
}) | ||
merge: function merge(rule) { | ||
(0, _mergeRules2.default)(rule, properties, function (rules, lastNode) { | ||
if (_canMerge2.default.apply(undefined, rules) && !rules.some(_stylehacks.detect)) { | ||
(0, _insertCloned2.default)(lastNode.parent, lastNode, { | ||
prop: prop, | ||
value: (0, _minifyTrbl2.default)(_mergeValues2.default.apply(undefined, rules)) | ||
}); | ||
rules.forEach(_remove2.default); | ||
return true; | ||
} | ||
}); | ||
cleanup(rule); | ||
} | ||
}; | ||
@@ -69,0 +121,0 @@ |
@@ -11,4 +11,10 @@ 'use strict'; | ||
var _genericMerge = require('../genericMerge'); | ||
var _canMerge = require('../canMerge'); | ||
var _canMerge2 = _interopRequireDefault(_canMerge); | ||
var _getDecls = require('../getDecls'); | ||
var _getDecls2 = _interopRequireDefault(_getDecls); | ||
var _getValue = require('../getValue'); | ||
@@ -18,2 +24,6 @@ | ||
var _mergeRules = require('../mergeRules'); | ||
var _mergeRules2 = _interopRequireDefault(_mergeRules); | ||
var _insertCloned = require('../insertCloned'); | ||
@@ -23,2 +33,6 @@ | ||
var _remove = require('../remove'); | ||
var _remove2 = _interopRequireDefault(_remove); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -28,2 +42,3 @@ | ||
var auto = 'auto'; | ||
var inherit = 'inherit'; | ||
@@ -46,2 +61,5 @@ /** | ||
} | ||
if (values[0] === inherit && values[1] === inherit) { | ||
return inherit; | ||
} | ||
return values.join(' '); | ||
@@ -51,6 +69,6 @@ } | ||
function explode(rule) { | ||
if (rule.nodes.some(_stylehacks.detect)) { | ||
return false; | ||
} | ||
rule.walkDecls('columns', function (decl) { | ||
if ((0, _stylehacks.detect)(decl)) { | ||
return; | ||
} | ||
var values = _postcss.list.space(decl.value); | ||
@@ -70,3 +88,3 @@ if (values.length === 1) { | ||
(0, _insertCloned2.default)(rule, decl, { | ||
(0, _insertCloned2.default)(decl.parent, decl, { | ||
prop: prop, | ||
@@ -80,10 +98,48 @@ value: value | ||
var merge = (0, _genericMerge.genericMergeFactory)({ | ||
prop: 'columns', | ||
properties: properties, | ||
value: function value(rules) { | ||
return normalize(rules.map(_getValue2.default)); | ||
function cleanup(rule) { | ||
var decls = (0, _getDecls2.default)(rule, ['columns'].concat(properties)); | ||
var _loop = function _loop() { | ||
var lastNode = decls[decls.length - 1]; | ||
// remove properties of lower precedence | ||
var lesser = decls.filter(function (node) { | ||
return !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && lastNode.prop === 'columns' && node.prop !== lastNode.prop; | ||
}); | ||
lesser.forEach(_remove2.default); | ||
decls = decls.filter(function (node) { | ||
return !~lesser.indexOf(node); | ||
}); | ||
// get duplicate properties | ||
var duplicates = decls.filter(function (node) { | ||
return !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && node.prop === lastNode.prop; | ||
}); | ||
duplicates.forEach(_remove2.default); | ||
decls = decls.filter(function (node) { | ||
return node !== lastNode && !~duplicates.indexOf(node); | ||
}); | ||
}; | ||
while (decls.length) { | ||
_loop(); | ||
} | ||
}); | ||
} | ||
function merge(rule) { | ||
(0, _mergeRules2.default)(rule, properties, function (rules, lastNode) { | ||
if (_canMerge2.default.apply(undefined, rules) && !rules.some(_stylehacks.detect)) { | ||
(0, _insertCloned2.default)(lastNode.parent, lastNode, { | ||
prop: 'columns', | ||
value: normalize(rules.map(_getValue2.default)) | ||
}); | ||
rules.forEach(_remove2.default); | ||
return true; | ||
} | ||
}); | ||
cleanup(rule); | ||
} | ||
exports.default = { | ||
@@ -90,0 +146,0 @@ explode: explode, |
@@ -1,9 +0,7 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports['default'] = function (rule, prop) { | ||
return rule.nodes.filter(function (n) { | ||
exports.default = function (rule, prop) { | ||
return rule.filter(function (n) { | ||
return n.prop && ~n.prop.indexOf(prop); | ||
@@ -13,2 +11,2 @@ }).pop(); | ||
module.exports = exports['default']; | ||
module.exports = exports["default"]; |
@@ -1,8 +0,6 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports['default'] = function (rule) { | ||
exports.default = function (rule) { | ||
for (var _len = arguments.length, props = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
@@ -20,2 +18,2 @@ props[_key - 1] = arguments[_key]; | ||
module.exports = exports['default']; | ||
module.exports = exports["default"]; |
@@ -1,8 +0,6 @@ | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports['default'] = function () { | ||
exports.default = function () { | ||
for (var _len = arguments.length, rules = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -19,2 +17,2 @@ rules[_key] = arguments[_key]; | ||
module.exports = exports['default']; | ||
module.exports = exports["default"]; |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports['default'] = function () { | ||
var _getValue = require('./getValue'); | ||
var _getValue2 = _interopRequireDefault(_getValue); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = function () { | ||
for (var _len = arguments.length, rules = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -12,7 +16,5 @@ rules[_key] = arguments[_key]; | ||
return rules.map(function (r) { | ||
return r.value; | ||
}).join(' '); | ||
return rules.map(_getValue2.default).join(' '); | ||
}; | ||
module.exports = exports['default']; |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _postcss = require('postcss'); | ||
var _parseTrbl = require('./parseTrbl'); | ||
exports['default'] = function (v) { | ||
var s = typeof v === 'string' ? _postcss.list.space(v) : v; | ||
var value = [s[0], // top | ||
s[1] || s[0], // right | ||
s[2] || s[0], // bottom | ||
s[3] || s[1] || s[0] // left | ||
]; | ||
var _parseTrbl2 = _interopRequireDefault(_parseTrbl); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = function (v) { | ||
var value = (0, _parseTrbl2.default)(v); | ||
if (value[3] === value[1]) { | ||
@@ -17,0 +14,0 @@ value.pop(); |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -11,3 +9,3 @@ var _postcss = require('postcss'); | ||
exports['default'] = function () { | ||
exports.default = function () { | ||
for (var _len = arguments.length, rules = Array(_len), _key = 0; _key < _len; _key++) { | ||
@@ -18,4 +16,3 @@ rules[_key] = arguments[_key]; | ||
return rules.reduce(function (memo, rule) { | ||
memo += space(rule.value).length; | ||
return memo; | ||
return memo += space(rule.value).length; | ||
}, 0); | ||
@@ -22,0 +19,0 @@ }; |
{ | ||
"name": "postcss-merge-longhand", | ||
"version": "2.0.2", | ||
"version": "3.0.0", | ||
"description": "Merge longhand properties into shorthand with PostCSS.", | ||
@@ -11,4 +11,7 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"prepublish": "babel src --out-dir dist --ignore /__tests__/", | ||
"test": "babel-tape-runner \"src/**/__tests__/*.js\" | tap-spec" | ||
"pretest": "eslint src", | ||
"prepublish": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/", | ||
"report": "nyc report --reporter=html", | ||
"test": "cross-env BABEL_ENV=test nyc ava", | ||
"test-012": "cross-env BABEL_ENV=test nyc ava" | ||
}, | ||
@@ -24,6 +27,18 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel": "^5.8.23", | ||
"babel-tape-runner": "^1.2.0", | ||
"tap-spec": "^4.1.0", | ||
"tape": "^4.2.0" | ||
"ava": "^0.16.0", | ||
"babel-cli": "^6.3.17", | ||
"babel-core": "^6.3.26", | ||
"babel-plugin-add-module-exports": "^0.2.1", | ||
"babel-plugin-istanbul": "^2.0.0", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-es2015-loose": "^7.0.0", | ||
"babel-preset-stage-0": "^6.3.13", | ||
"babel-register": "^6.5.2", | ||
"cross-env": "^1.0.7", | ||
"del-cli": "^0.2.0", | ||
"eslint": "^3.0.0", | ||
"eslint-config-cssnano": "^3.0.0", | ||
"eslint-plugin-babel": "^3.3.0", | ||
"eslint-plugin-import": "^1.10.2", | ||
"nyc": "^8.3.1" | ||
}, | ||
@@ -38,4 +53,17 @@ "homepage": "https://github.com/ben-eb/postcss-merge-longhand", | ||
"dependencies": { | ||
"postcss": "^5.0.4" | ||
"object-assign": "^4.0.1", | ||
"postcss": "^5.0.5", | ||
"postcss-value-parser": "^3.3.0", | ||
"stylehacks": "^2.2.0" | ||
}, | ||
"eslintConfig": { | ||
"extends": "cssnano" | ||
}, | ||
"nyc": { | ||
"sourceMap": false, | ||
"instrument": false | ||
}, | ||
"ava": { | ||
"require": "babel-register" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42284
28
929
0
4
16
+ Addedobject-assign@^4.0.1
+ Addedpostcss-value-parser@^3.3.0
+ Addedstylehacks@^2.2.0
+ Addedbrowserslist@1.7.7(transitive)
+ Addedcaniuse-db@1.0.30001680(transitive)
+ Addedelectron-to-chromium@1.5.62(transitive)
+ Addedflatten@1.0.3(transitive)
+ Addedgather-stream@1.0.0(transitive)
+ Addedindexes-of@1.0.1(transitive)
+ Addedirregular-plurals@1.4.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlog-symbols@1.0.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedplur@2.1.2(transitive)
+ Addedpostcss-reporter@1.4.1(transitive)
+ Addedpostcss-selector-parser@2.2.3(transitive)
+ Addedpostcss-value-parser@3.3.1(transitive)
+ Addedread-file-stdin@0.2.1(transitive)
+ Addedstylehacks@2.3.2(transitive)
+ Addedtext-table@0.2.0(transitive)
+ Addeduniq@1.0.1(transitive)
+ Addedwrite-file-stdout@0.0.2(transitive)
Updatedpostcss@^5.0.5