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.

148 lines
3.9 KiB

4 years ago
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _postcss = require('postcss');
  6. var _postcssValueParser = require('postcss-value-parser');
  7. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  8. var _cssnanoUtilGetArguments = require('cssnano-util-get-arguments');
  9. var _cssnanoUtilGetArguments2 = _interopRequireDefault(_cssnanoUtilGetArguments);
  10. var _has = require('has');
  11. var _has2 = _interopRequireDefault(_has);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. const directions = ['top', 'right', 'bottom', 'left', 'center'];
  14. const center = '50%';
  15. const horizontal = {
  16. right: '100%',
  17. left: '0'
  18. };
  19. const vertical = {
  20. bottom: '100%',
  21. top: '0'
  22. };
  23. function transform(value) {
  24. const parsed = (0, _postcssValueParser2.default)(value);
  25. const args = (0, _cssnanoUtilGetArguments2.default)(parsed);
  26. const relevant = [];
  27. args.forEach(arg => {
  28. relevant.push({
  29. start: null,
  30. end: null
  31. });
  32. arg.forEach((part, index) => {
  33. const isPosition = ~directions.indexOf(part.value.toLowerCase()) || (0, _postcssValueParser.unit)(part.value);
  34. const len = relevant.length - 1;
  35. if (relevant[len].start === null && isPosition) {
  36. relevant[len].start = index;
  37. relevant[len].end = index;
  38. return;
  39. }
  40. if (relevant[len].start !== null) {
  41. if (part.type === 'space') {
  42. return;
  43. } else if (isPosition) {
  44. relevant[len].end = index;
  45. return;
  46. }
  47. return;
  48. }
  49. });
  50. });
  51. relevant.forEach((range, index) => {
  52. if (range.start === null) {
  53. return;
  54. }
  55. const position = args[index].slice(range.start, range.end + 1);
  56. if (position.length > 3) {
  57. return;
  58. }
  59. const firstValue = position[0].value.toLowerCase();
  60. const secondValue = position[2] && position[2].value ? position[2].value.toLowerCase() : null;
  61. if (position.length === 1 || secondValue === 'center') {
  62. if (secondValue) {
  63. position[2].value = position[1].value = '';
  64. }
  65. const map = Object.assign({}, horizontal, {
  66. center
  67. });
  68. if ((0, _has2.default)(map, firstValue)) {
  69. position[0].value = map[firstValue];
  70. }
  71. return;
  72. }
  73. if (firstValue === 'center' && ~directions.indexOf(secondValue)) {
  74. position[0].value = position[1].value = '';
  75. if ((0, _has2.default)(horizontal, secondValue)) {
  76. position[2].value = horizontal[secondValue];
  77. }
  78. return;
  79. }
  80. if ((0, _has2.default)(horizontal, firstValue) && (0, _has2.default)(vertical, secondValue)) {
  81. position[0].value = horizontal[firstValue];
  82. position[2].value = vertical[secondValue];
  83. return;
  84. } else if ((0, _has2.default)(vertical, firstValue) && (0, _has2.default)(horizontal, secondValue)) {
  85. position[0].value = horizontal[secondValue];
  86. position[2].value = vertical[firstValue];
  87. return;
  88. }
  89. });
  90. return parsed.toString();
  91. }
  92. exports.default = (0, _postcss.plugin)('postcss-normalize-positions', () => {
  93. return css => {
  94. const cache = {};
  95. css.walkDecls(/^(background(-position)?|(-webkit-)?perspective-origin)$/i, decl => {
  96. const value = decl.value;
  97. if (cache[value]) {
  98. decl.value = cache[value];
  99. return;
  100. }
  101. const result = transform(value);
  102. decl.value = result;
  103. cache[value] = result;
  104. });
  105. };
  106. });
  107. module.exports = exports['default'];