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.

118 lines
3.3 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _postcss2 = _interopRequireDefault(_postcss);
  7. var _postcssValueParser = require('postcss-value-parser');
  8. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  9. var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
  10. var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
  11. var _cssnanoUtilGetMatch = require('cssnano-util-get-match');
  12. var _cssnanoUtilGetMatch2 = _interopRequireDefault(_cssnanoUtilGetMatch);
  13. var _map = require('./lib/map');
  14. var _map2 = _interopRequireDefault(_map);
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function evenValues(list, index) {
  17. return index % 2 === 0;
  18. }
  19. const repeatKeywords = _map2.default.map(mapping => mapping[0]);
  20. const getMatch = (0, _cssnanoUtilGetMatch2.default)(_map2.default);
  21. exports.default = _postcss2.default.plugin('postcss-normalize-repeat-style', () => {
  22. return css => {
  23. const cache = {};
  24. css.walkDecls(/background(-repeat)?|(-webkit-)?mask-repeat/i, decl => {
  25. const value = decl.value;
  26. if (cache[value]) {
  27. decl.value = cache[value];
  28. return;
  29. }
  30. const parsed = (0, _postcssValueParser2.default)(value);
  31. if (parsed.nodes.length === 1) {
  32. cache[value] = value;
  33. return;
  34. }
  35. const args = (0, _cssnanoUtilGetArguments2.default)(parsed);
  36. const relevant = [];
  37. args.forEach(arg => {
  38. relevant.push({
  39. start: null,
  40. end: null
  41. });
  42. arg.forEach((part, index) => {
  43. const isRepeat = ~repeatKeywords.indexOf(part.value.toLowerCase());
  44. const len = relevant.length - 1;
  45. if (relevant[len].start === null && isRepeat) {
  46. relevant[len].start = index;
  47. relevant[len].end = index;
  48. return;
  49. }
  50. if (relevant[len].start !== null) {
  51. if (part.type === 'space') {
  52. return;
  53. } else if (isRepeat) {
  54. relevant[len].end = index;
  55. return;
  56. }
  57. return;
  58. }
  59. });
  60. });
  61. relevant.forEach((range, index) => {
  62. if (range.start === null) {
  63. return;
  64. }
  65. const val = args[index].slice(range.start, range.end + 1);
  66. if (val.length !== 3) {
  67. return;
  68. }
  69. const match = getMatch(val.filter(evenValues).map(n => n.value.toLowerCase()));
  70. if (match) {
  71. args[index][range.start].value = match;
  72. args[index][range.start + 1].value = '';
  73. args[index][range.end].value = '';
  74. }
  75. });
  76. const result = parsed.toString();
  77. decl.value = result;
  78. cache[value] = result;
  79. });
  80. };
  81. });
  82. module.exports = exports['default'];