root.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _container = require('./container');
  4. var _container2 = _interopRequireDefault(_container);
  5. var _warnOnce = require('./warn-once');
  6. var _warnOnce2 = _interopRequireDefault(_warnOnce);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. 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; }
  10. 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; }
  11. /**
  12. * Represents a CSS file and contains all its parsed nodes.
  13. *
  14. * @extends Container
  15. *
  16. * @example
  17. * const root = postcss.parse('a{color:black} b{z-index:2}');
  18. * root.type //=> 'root'
  19. * root.nodes.length //=> 2
  20. */
  21. var Root = function (_Container) {
  22. _inherits(Root, _Container);
  23. function Root(defaults) {
  24. _classCallCheck(this, Root);
  25. var _this = _possibleConstructorReturn(this, _Container.call(this, defaults));
  26. _this.type = 'root';
  27. if (!_this.nodes) _this.nodes = [];
  28. return _this;
  29. }
  30. Root.prototype.removeChild = function removeChild(child) {
  31. child = this.index(child);
  32. if (child === 0 && this.nodes.length > 1) {
  33. this.nodes[1].raws.before = this.nodes[child].raws.before;
  34. }
  35. return _Container.prototype.removeChild.call(this, child);
  36. };
  37. Root.prototype.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 = nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  48. var _ref;
  49. if (_isArray) {
  50. if (_i >= _iterator.length) break;
  51. _ref = _iterator[_i++];
  52. } else {
  53. _i = _iterator.next();
  54. if (_i.done) break;
  55. _ref = _i.value;
  56. }
  57. var node = _ref;
  58. node.raws.before = sample.raws.before;
  59. }
  60. }
  61. }
  62. return nodes;
  63. };
  64. /**
  65. * Returns a {@link Result} instance representing the root’s CSS.
  66. *
  67. * @param {processOptions} [opts] - options with only `to` and `map` keys
  68. *
  69. * @return {Result} result with current root’s CSS
  70. *
  71. * @example
  72. * const root1 = postcss.parse(css1, { from: 'a.css' });
  73. * const root2 = postcss.parse(css2, { from: 'b.css' });
  74. * root1.append(root2);
  75. * const result = root1.toResult({ to: 'all.css', map: true });
  76. */
  77. Root.prototype.toResult = function toResult() {
  78. var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  79. var LazyResult = require('./lazy-result');
  80. var Processor = require('./processor');
  81. var lazy = new LazyResult(new Processor(), this, opts);
  82. return lazy.stringify();
  83. };
  84. Root.prototype.remove = function remove(child) {
  85. (0, _warnOnce2.default)('Root#remove is deprecated. Use Root#removeChild');
  86. this.removeChild(child);
  87. };
  88. Root.prototype.prevMap = function prevMap() {
  89. (0, _warnOnce2.default)('Root#prevMap is deprecated. Use Root#source.input.map');
  90. return this.source.input.map;
  91. };
  92. /**
  93. * @memberof Root#
  94. * @member {object} raws - Information to generate byte-to-byte equal
  95. * node string as it was in the origin input.
  96. *
  97. * Every parser saves its own properties,
  98. * but the default CSS parser uses:
  99. *
  100. * * `after`: the space symbols after the last child to the end of file.
  101. * * `semicolon`: is the last child has an (optional) semicolon.
  102. *
  103. * @example
  104. * postcss.parse('a {}\n').raws //=> { after: '\n' }
  105. * postcss.parse('a {}').raws //=> { after: '' }
  106. */
  107. return Root;
  108. }(_container2.default);
  109. exports.default = Root;
  110. module.exports = exports['default'];
  111. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJvb3QuZXM2Il0sIm5hbWVzIjpbIlJvb3QiLCJkZWZhdWx0cyIsInR5cGUiLCJub2RlcyIsInJlbW92ZUNoaWxkIiwiY2hpbGQiLCJpbmRleCIsImxlbmd0aCIsInJhd3MiLCJiZWZvcmUiLCJub3JtYWxpemUiLCJzYW1wbGUiLCJmaXJzdCIsIm5vZGUiLCJ0b1Jlc3VsdCIsIm9wdHMiLCJMYXp5UmVzdWx0IiwicmVxdWlyZSIsIlByb2Nlc3NvciIsImxhenkiLCJzdHJpbmdpZnkiLCJyZW1vdmUiLCJwcmV2TWFwIiwic291cmNlIiwiaW5wdXQiLCJtYXAiXSwibWFwcGluZ3MiOiI7Ozs7QUFBQTs7OztBQUNBOzs7Ozs7Ozs7Ozs7QUFFQTs7Ozs7Ozs7OztJQVVNQSxJOzs7QUFFRixrQkFBWUMsUUFBWixFQUFzQjtBQUFBOztBQUFBLHFEQUNsQixzQkFBTUEsUUFBTixDQURrQjs7QUFFbEIsY0FBS0MsSUFBTCxHQUFZLE1BQVo7QUFDQSxZQUFLLENBQUMsTUFBS0MsS0FBWCxFQUFtQixNQUFLQSxLQUFMLEdBQWEsRUFBYjtBQUhEO0FBSXJCOzttQkFFREMsVyx3QkFBWUMsSyxFQUFPO0FBQ2ZBLGdCQUFRLEtBQUtDLEtBQUwsQ0FBV0QsS0FBWCxDQUFSOztBQUVBLFlBQUtBLFVBQVUsQ0FBVixJQUFlLEtBQUtGLEtBQUwsQ0FBV0ksTUFBWCxHQUFvQixDQUF4QyxFQUE0QztBQUN4QyxpQkFBS0osS0FBTCxDQUF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MpO1xuICAgICAgICB0aGlzLnR5cGUgPSAncm9vdCc7XG4gICAgICAgIGlmICggIXRoaXMubm9kZXMgKSB0aGlzLm5vZGVzID0gW107XG4gICAgfVxuXG4gICAgcmVtb3ZlQ2hpbGQoY2hpbGQpIHtcbiAgICAgICAgY2hpbGQgPSB0aGlzLmluZGV4KGNoaWxkKTtcblxuICAgICAgICBpZiAoIGNoaWxkID09PSAwICYmIHRoaXMubm9kZXMubGVuZ3RoID4gMSApIHtcbiAgICAgICAgICAgIHRoaXMubm9kZXNbMV0ucmF3cy5iZWZvcmUgPSB0aGlzLm5vZGVzW2NoaWxkXS5yYXdzLmJlZm9yZTtcbiAgICAgICAgfVxuXG4gICAgICAgIHJldHVybiBzdXBlci5yZW1vdmVDaGlsZChjaGlsZCk7XG4gICAgfVxuXG4gICAgbm9ybWFsaXplKGNoaWxkLCBzYW1wbGUsIHR5cGUpIHtcbiAgICAgICAgbGV0IG5vZGVzID0gc3VwZXIubm9ybWFsaXplKGNoaWxkKTtcblxuICAgICAgICBpZiAoIHNhbXBsZSApIHtcbiAgICAgICAgICAgIGlmICggdHlwZSA9PT0gJ3ByZXBlbmQnICkge1xuICAgICAgICAgICAgICAgIGlmICggdGhpcy5ub2Rlcy5sZW5ndGggPiAxICkge1xuICAgICAgICAgICAgICAgICAgICBzYW1wbGUucmF3cy5iZWZvcmUgPSB0aGlzLm5vZGVzWzFdLnJhd3MuYmVmb3JlO1xuICAgICAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgICAgICAgIGRlbGV0ZSBzYW1wbGUucmF3cy5iZWZvcmU7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSBlbHNlIGlmICggdGhpcy5maXJzdCAhPT0gc2FtcGxlICkge1xuICAgICAgICAgICAgICAgIGZvciAoIGxldCBub2RlIG9mIG5vZGVzICkge1xuICAgICAgICAgICAgICAgICAgICBub2RlLnJhd3MuYmVmb3JlID0gc2FtcGxlLnJhd3MuYmVmb3J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