root.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _container = _interopRequireDefault(require("./container"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
  7. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  8. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  9. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10. /**
  11. * Represents a CSS file and contains all its parsed nodes.
  12. *
  13. * @extends Container
  14. *
  15. * @example
  16. * const root = postcss.parse('a{color:black} b{z-index:2}')
  17. * root.type //=> 'root'
  18. * root.nodes.length //=> 2
  19. */
  20. var Root = /*#__PURE__*/function (_Container) {
  21. _inheritsLoose(Root, _Container);
  22. function Root(defaults) {
  23. var _this;
  24. _this = _Container.call(this, defaults) || this;
  25. _this.type = 'root';
  26. if (!_this.nodes) _this.nodes = [];
  27. return _this;
  28. }
  29. var _proto = Root.prototype;
  30. _proto.removeChild = function removeChild(child, ignore) {
  31. var index = this.index(child);
  32. if (!ignore && index === 0 && this.nodes.length > 1) {
  33. this.nodes[1].raws.before = this.nodes[index].raws.before;
  34. }
  35. return _Container.prototype.removeChild.call(this, child);
  36. };
  37. _proto.normalize = function normalize(child, sample, type) {
  38. var nodes = _Container.prototype.normalize.call(this, child);
  39. if (sample) {
  40. if (type === 'prepend') {
  41. if (this.nodes.length > 1) {
  42. sample.raws.before = this.nodes[1].raws.before;
  43. } else {
  44. delete sample.raws.before;
  45. }
  46. } else if (this.first !== sample) {
  47. for (var _iterator = _createForOfIteratorHelperLoose(nodes), _step; !(_step = _iterator()).done;) {
  48. var node = _step.value;
  49. node.raws.before = sample.raws.before;
  50. }
  51. }
  52. }
  53. return nodes;
  54. }
  55. /**
  56. * Returns a {@link Result} instance representing the root’s CSS.
  57. *
  58. * @param {processOptions} [opts] Options with only `to` and `map` keys.
  59. *
  60. * @return {Result} Result with current root’s CSS.
  61. *
  62. * @example
  63. * const root1 = postcss.parse(css1, { from: 'a.css' })
  64. * const root2 = postcss.parse(css2, { from: 'b.css' })
  65. * root1.append(root2)
  66. * const result = root1.toResult({ to: 'all.css', map: true })
  67. */
  68. ;
  69. _proto.toResult = function toResult(opts) {
  70. if (opts === void 0) {
  71. opts = {};
  72. }
  73. var LazyResult = require('./lazy-result');
  74. var Processor = require('./processor');
  75. var lazy = new LazyResult(new Processor(), this, opts);
  76. return lazy.stringify();
  77. }
  78. /**
  79. * @memberof Root#
  80. * @member {object} raws Information to generate byte-to-byte equal
  81. * node string as it was in the origin input.
  82. *
  83. * Every parser saves its own properties,
  84. * but the default CSS parser uses:
  85. *
  86. * * `after`: the space symbols after the last child to the end of file.
  87. * * `semicolon`: is the last child has an (optional) semicolon.
  88. *
  89. * @example
  90. * postcss.parse('a {}\n').raws //=> { after: '\n' }
  91. * postcss.parse('a {}').raws //=> { after: '' }
  92. */
  93. ;
  94. return Root;
  95. }(_container.default);
  96. var _default = Root;
  97. exports.default = _default;
  98. module.exports = exports.default;
  99. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJvb3QuZXM2Il0sIm5hbWVzIjpbIlJvb3QiLCJkZWZhdWx0cyIsInR5cGUiLCJub2RlcyIsInJlbW92ZUNoaWxkIiwiY2hpbGQiLCJpZ25vcmUiLCJpbmRleCIsImxlbmd0aCIsInJhd3MiLCJiZWZvcmUiLCJub3JtYWxpemUiLCJzYW1wbGUiLCJmaXJzdCIsIm5vZGUiLCJ0b1Jlc3VsdCIsIm9wdHMiLCJMYXp5UmVzdWx0IiwicmVxdWlyZSIsIlByb2Nlc3NvciIsImxhenkiLCJzdHJpbmdpZnkiLCJDb250YWluZXIiXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUE7Ozs7Ozs7Ozs7OztBQUVBOzs7Ozs7Ozs7O0lBVU1BLEk7OztBQUNKLGdCQUFhQyxRQUFiLEVBQXVCO0FBQUE7O0FBQ3JCLGtDQUFNQSxRQUFOO0FBQ0EsVUFBS0MsSUFBTCxHQUFZLE1BQVo7QUFDQSxRQUFJLENBQUMsTUFBS0MsS0FBVixFQUFpQixNQUFLQSxLQUFMLEdBQWEsRUFBYjtBQUhJO0FBSXRCOzs7O1NBRURDLFcsR0FBQSxxQkFBYUMsS0FBYixFQUFvQkMsTUFBcEIsRUFBNEI7QUFDMUIsUUFBSUMsS0FBSyxHQUFHLEtBQUtBLEtBQUwsQ0FBV0YsS0FBWCxDQUFaOztBQUVBLFFBQUksQ0FBQ0MsTUFBRCxJQUFXQyxLQUFLLEtBQUssQ0FBckIsSUFBMEIsS0FBS0osS0FBTCxDQUFXSyxNQUFYLEdBQW9CLENBQWxELEVBQXFEO0FBQ25ELFdBQUtMLEtBQUwsQ0FBVyxDQUFYLEVBQWNNLElBQWQ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