transform.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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)) {
  18. return node;
  19. } // stringify calc expression and produce an AST
  20. var contents = _postcssValueParser.default.stringify(node.nodes);
  21. var ast = _parser.parser.parse(contents); // reduce AST to its simplest form, that is, either to a single value
  22. // or a simplified calc expression
  23. var reducedAst = (0, _reducer.default)(ast, options.precision); // stringify AST and write it back
  24. node.type = 'word';
  25. node.value = (0, _stringifier.default)(node.value, reducedAst, value, options, result, item);
  26. return false;
  27. }).toString();
  28. }
  29. function transformSelector(value, options, result, item) {
  30. return (0, _postcssSelectorParser.default)(function (selectors) {
  31. selectors.walk(function (node) {
  32. // attribute value
  33. // e.g. the "calc(3*3)" part of "div[data-size="calc(3*3)"]"
  34. if (node.type === 'attribute' && node.value) {
  35. node.setValue(transformValue(node.value, options, result, item));
  36. } // tag value
  37. // e.g. the "calc(3*3)" part of "div:nth-child(2n + calc(3*3))"
  38. if (node.type === 'tag') {
  39. node.value = transformValue(node.value, options, result, item);
  40. }
  41. return;
  42. });
  43. }).processSync(value);
  44. }
  45. var _default = function _default(node, property, options, result) {
  46. 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
  47. // transformed value into a cloned node which is inserted before the current
  48. // node, preserving the original value. Otherwise, overwrite the original
  49. // value.
  50. if (options.preserve && node[property] !== value) {
  51. var clone = node.clone();
  52. clone[property] = value;
  53. node.parent.insertBefore(node, clone);
  54. } else {
  55. node[property] = value;
  56. }
  57. };
  58. exports.default = _default;
  59. module.exports = exports.default;