You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7 KiB

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