[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = mergeRules;
|
---|
| 7 |
|
---|
| 8 | var _hasAllProps = _interopRequireDefault(require("./hasAllProps"));
|
---|
| 9 |
|
---|
| 10 | var _getDecls = _interopRequireDefault(require("./getDecls"));
|
---|
| 11 |
|
---|
| 12 | var _getRules = _interopRequireDefault(require("./getRules"));
|
---|
| 13 |
|
---|
| 14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 15 |
|
---|
| 16 | function isConflictingProp(propA, propB) {
|
---|
| 17 | if (!propB.prop || propB.important !== propA.important) {
|
---|
| 18 | return;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | const parts = propA.prop.split('-');
|
---|
| 22 | return parts.some(() => {
|
---|
| 23 | parts.pop();
|
---|
| 24 | return parts.join('-') === propB.prop;
|
---|
| 25 | });
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | function hasConflicts(match, nodes) {
|
---|
| 29 | const firstNode = Math.min.apply(null, match.map(n => nodes.indexOf(n)));
|
---|
| 30 | const lastNode = Math.max.apply(null, match.map(n => nodes.indexOf(n)));
|
---|
| 31 | const between = nodes.slice(firstNode + 1, lastNode);
|
---|
| 32 | return match.some(a => between.some(b => isConflictingProp(a, b)));
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | function mergeRules(rule, properties, callback) {
|
---|
| 36 | let decls = (0, _getDecls.default)(rule, properties);
|
---|
| 37 |
|
---|
| 38 | while (decls.length) {
|
---|
| 39 | const last = decls[decls.length - 1];
|
---|
| 40 | const props = decls.filter(node => node.important === last.important);
|
---|
| 41 | const rules = (0, _getRules.default)(props, properties);
|
---|
| 42 |
|
---|
| 43 | if ((0, _hasAllProps.default)(rules, ...properties) && !hasConflicts(rules, rule.nodes)) {
|
---|
| 44 | if (callback(rules, last, props)) {
|
---|
| 45 | decls = decls.filter(node => !~rules.indexOf(node));
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | decls = decls.filter(node => node !== last);
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | module.exports = exports.default; |
---|