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.

129 lines
9.0 KiB

4 years ago
  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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  7. /**
  8. * Represents a CSS file and contains all its parsed nodes.
  9. *
  10. * @extends Container
  11. *
  12. * @example
  13. * const root = postcss.parse('a{color:black} b{z-index:2}')
  14. * root.type //=> 'root'
  15. * root.nodes.length //=> 2
  16. */
  17. var Root =
  18. /*#__PURE__*/
  19. function (_Container) {
  20. _inheritsLoose(Root, _Container);
  21. function Root(defaults) {
  22. var _this;
  23. _this = _Container.call(this, defaults) || this;
  24. _this.type = 'root';
  25. if (!_this.nodes) _this.nodes = [];
  26. return _this;
  27. }
  28. var _proto = Root.prototype;
  29. _proto.removeChild = function removeChild(child, ignore) {
  30. var index = this.index(child);
  31. if (!ignore && index === 0 && this.nodes.length > 1) {
  32. this.nodes[1].raws.before = this.nodes[index].raws.before;
  33. }
  34. return _Container.prototype.removeChild.call(this, child);
  35. };
  36. _proto.normalize = function normalize(child, sample, type) {
  37. var nodes = _Container.prototype.normalize.call(this, child);
  38. if (sample) {
  39. if (type === 'prepend') {
  40. if (this.nodes.length > 1) {
  41. sample.raws.before = this.nodes[1].raws.before;
  42. } else {
  43. delete sample.raws.before;
  44. }
  45. } else if (this.first !== sample) {
  46. for (var _iterator = nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  47. var _ref;
  48. if (_isArray) {
  49. if (_i >= _iterator.length) break;
  50. _ref = _iterator[_i++];
  51. } else {
  52. _i = _iterator.next();
  53. if (_i.done) break;
  54. _ref = _i.value;
  55. }
  56. var node = _ref;
  57. node.raws.before = sample.raws.before;
  58. }
  59. }
  60. }
  61. return nodes;
  62. }
  63. /**
  64. * Returns a {@link Result} instance representing the roots CSS.
  65. *
  66. * @param {processOptions} [opts] Options with only `to` and `map` keys.
  67. *
  68. * @return {Result} Result with current roots CSS.
  69. *
  70. * @example
  71. * const root1 = postcss.parse(css1, { from: 'a.css' })
  72. * const root2 = postcss.parse(css2, { from: 'b.css' })
  73. * root1.append(root2)
  74. * const result = root1.toResult({ to: 'all.css', map: true })
  75. */
  76. ;
  77. _proto.toResult = function toResult(opts) {
  78. if (opts === void 0) {
  79. opts = {};
  80. }
  81. var LazyResult = require('./lazy-result');
  82. var Processor = require('./processor');
  83. var lazy = new LazyResult(new Processor(), this, opts);
  84. return lazy.stringify();
  85. }
  86. /**
  87. * @memberof Root#
  88. * @member {object} raws Information to generate byte-to-byte equal
  89. * node string as it was in the origin input.
  90. *
  91. * Every parser saves its own properties,
  92. * but the default CSS parser uses:
  93. *
  94. * * `after`: the space symbols after the last child to the end of file.
  95. * * `semicolon`: is the last child has an (optional) semicolon.
  96. *
  97. * @example
  98. * postcss.parse('a {}\n').raws //=> { after: '\n' }
  99. * postcss.parse('a {}').raws //=> { after: '' }
  100. */
  101. ;
  102. return Root;
  103. }(_container.default);
  104. var _default = Root;
  105. exports.default = _default;
  106. module.exports = exports.default;
  107. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJvb3QuZXM2Il0sIm5hbWVzIjpbIlJvb3QiLCJkZWZhdWx0cyIsInR5cGUiLCJub2RlcyIsInJlbW92ZUNoaWxkIiwiY2hpbGQiLCJpZ25vcmUiLCJpbmRleCIsImxlbmd0aCIsInJhd3MiLCJiZWZvcmUiLCJub3JtYWxpemUiLCJzYW1wbGUiLCJmaXJzdCIsIm5vZGUiLCJ0b1Jlc3VsdCIsIm9wdHMiLCJMYXp5UmVzdWx0IiwicmVxdWlyZSIsIlByb2Nlc3NvciIsImxhenkiLCJzdHJpb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