root.js 10KB

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