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.

120 lines
3.8 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _stylehacks = require('stylehacks');
  6. var _canMerge = require('../canMerge');
  7. var _canMerge2 = _interopRequireDefault(_canMerge);
  8. var _getDecls = require('../getDecls');
  9. var _getDecls2 = _interopRequireDefault(_getDecls);
  10. var _minifyTrbl = require('../minifyTrbl');
  11. var _minifyTrbl2 = _interopRequireDefault(_minifyTrbl);
  12. var _parseTrbl = require('../parseTrbl');
  13. var _parseTrbl2 = _interopRequireDefault(_parseTrbl);
  14. var _insertCloned = require('../insertCloned');
  15. var _insertCloned2 = _interopRequireDefault(_insertCloned);
  16. var _mergeRules = require('../mergeRules');
  17. var _mergeRules2 = _interopRequireDefault(_mergeRules);
  18. var _mergeValues = require('../mergeValues');
  19. var _mergeValues2 = _interopRequireDefault(_mergeValues);
  20. var _remove = require('../remove');
  21. var _remove2 = _interopRequireDefault(_remove);
  22. var _trbl = require('../trbl');
  23. var _trbl2 = _interopRequireDefault(_trbl);
  24. var _isCustomProp = require('../isCustomProp');
  25. var _isCustomProp2 = _interopRequireDefault(_isCustomProp);
  26. var _canExplode = require('../canExplode');
  27. var _canExplode2 = _interopRequireDefault(_canExplode);
  28. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  29. exports.default = prop => {
  30. const properties = _trbl2.default.map(direction => `${prop}-${direction}`);
  31. const cleanup = rule => {
  32. let decls = (0, _getDecls2.default)(rule, [prop].concat(properties));
  33. while (decls.length) {
  34. const lastNode = decls[decls.length - 1];
  35. // remove properties of lower precedence
  36. const lesser = decls.filter(node => !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && lastNode.prop === prop && node.prop !== lastNode.prop);
  37. lesser.forEach(_remove2.default);
  38. decls = decls.filter(node => !~lesser.indexOf(node));
  39. // get duplicate properties
  40. let duplicates = decls.filter(node => !(0, _stylehacks.detect)(lastNode) && !(0, _stylehacks.detect)(node) && node !== lastNode && node.important === lastNode.important && node.prop === lastNode.prop && !(!(0, _isCustomProp2.default)(node) && (0, _isCustomProp2.default)(lastNode)));
  41. duplicates.forEach(_remove2.default);
  42. decls = decls.filter(node => node !== lastNode && !~duplicates.indexOf(node));
  43. }
  44. };
  45. const processor = {
  46. explode: rule => {
  47. rule.walkDecls(new RegExp("^" + prop + "$", "i"), decl => {
  48. if (!(0, _canExplode2.default)(decl)) {
  49. return;
  50. }
  51. if ((0, _stylehacks.detect)(decl)) {
  52. return;
  53. }
  54. const values = (0, _parseTrbl2.default)(decl.value);
  55. _trbl2.default.forEach((direction, index) => {
  56. (0, _insertCloned2.default)(decl.parent, decl, {
  57. prop: properties[index],
  58. value: values[index]
  59. });
  60. });
  61. decl.remove();
  62. });
  63. },
  64. merge: rule => {
  65. (0, _mergeRules2.default)(rule, properties, (rules, lastNode) => {
  66. if ((0, _canMerge2.default)(rules) && !rules.some(_stylehacks.detect)) {
  67. (0, _insertCloned2.default)(lastNode.parent, lastNode, {
  68. prop,
  69. value: (0, _minifyTrbl2.default)((0, _mergeValues2.default)(...rules))
  70. });
  71. rules.forEach(_remove2.default);
  72. return true;
  73. }
  74. });
  75. cleanup(rule);
  76. }
  77. };
  78. return processor;
  79. };
  80. module.exports = exports['default'];