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.

71 lines
2.7 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
  7. var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
  8. var _parser = require("../parser");
  9. var _reducer = _interopRequireDefault(require("./reducer"));
  10. var _stringifier = _interopRequireDefault(require("./stringifier"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. // eslint-disable-next-line import/no-unresolved
  13. var MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
  14. function transformValue(value, options, result, item) {
  15. return (0, _postcssValueParser.default)(value).walk(function (node) {
  16. // skip anything which isn't a calc() function
  17. if (node.type !== 'function' || !MATCH_CALC.test(node.value)) return node; // stringify calc expression and produce an AST
  18. var contents = _postcssValueParser.default.stringify(node.nodes);
  19. var ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
  20. // or a simplified calc expression
  21. var reducedAst = (0, _reducer.default)(ast, options.precision, item); // stringify AST and write it back
  22. node.type = 'word';
  23. node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
  24. }, true).toString();
  25. }
  26. function transformSelector(value, options, result, item) {
  27. return (0, _postcssSelectorParser.default)(function (selectors) {
  28. selectors.walk(function (node) {
  29. // attribute value
  30. // e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
  31. if (node.type === 'attribute' && node.value) {
  32. node.setValue(transformValue(node.value, options, result, item));
  33. } // tag value
  34. // e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
  35. if (node.type === 'tag') node.value = transformValue(node.value, options, result, item);
  36. return;
  37. });
  38. }).processSync(value);
  39. }
  40. var _default = function _default(node, property, options, result) {
  41. var value = property === "selector" ? transformSelector(node[property], options, result, node) : transformValue(node[property], options, result, node); // if the preserve option is enabled and the value has changed, write the
  42. // transformed value into a cloned node which is inserted before the current
  43. // node, preserving the original value. Otherwise, overwrite the original
  44. // value.
  45. if (options.preserve && node[property] !== value) {
  46. var clone = node.clone();
  47. clone[property] = value;
  48. node.parent.insertBefore(node, clone);
  49. } else node[property] = value;
  50. };
  51. exports.default = _default;
  52. module.exports = exports.default;