reduceDisplayValues.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _postcss = require('postcss');
  4. var _postcss2 = _interopRequireDefault(_postcss);
  5. var _postcssValueParser = require('postcss-value-parser');
  6. var _postcssValueParser2 = _interopRequireDefault(_postcssValueParser);
  7. var _evenValues = require('./evenValues');
  8. var _evenValues2 = _interopRequireDefault(_evenValues);
  9. var _getMatch = require('./getMatch');
  10. var _getMatch2 = _interopRequireDefault(_getMatch);
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Specification: https://drafts.csswg.org/css-display/#the-display-properties
  14. */
  15. var mappings = [['block', ['block', 'flow']], ['flow-root', ['block', 'flow-root']], ['inline', ['inline', 'flow']], ['inline-block', ['inline', 'flow-root']], ['run-in', ['run-in', 'flow']], ['list-item', ['list-item', 'block', 'flow']], ['inline-list-item', ['list-item', 'inline', 'flow']], ['flex', ['block', 'flex']], ['inline-flex', ['inline', 'flex']], ['grid', ['block', 'grid']], ['inline-grid', ['inline', 'grid']], ['ruby', ['inline', 'ruby']], ['table', ['block', 'table']], ['inline-table', ['inline', 'table']], ['table-cell', ['table-cell', 'flow']], ['table-caption', ['table-caption', 'flow']], ['ruby-base', ['ruby-base', 'flow']], ['ruby-text', ['ruby-text', 'flow']]];
  16. var getMatch = (0, _getMatch2.default)(mappings);
  17. function transform(node) {
  18. var _valueParser = (0, _postcssValueParser2.default)(node.value),
  19. nodes = _valueParser.nodes;
  20. if (nodes.length === 1) {
  21. return;
  22. }
  23. var match = getMatch(nodes.filter(_evenValues2.default).map(function (n) {
  24. return n.value;
  25. }));
  26. if (match.length) {
  27. node.value = match[0][0];
  28. }
  29. }
  30. var plugin = _postcss2.default.plugin('cssnano-reduce-display-values', function () {
  31. return function (css) {
  32. return css.walkDecls('display', transform);
  33. };
  34. });
  35. plugin.mappings = mappings;
  36. exports.default = plugin;
  37. module.exports = exports['default'];