144 lines
11 KiB
JavaScript
144 lines
11 KiB
JavaScript
|
'use strict';
|
|||
|
|
|||
|
exports.__esModule = true;
|
|||
|
|
|||
|
var _container = require('./container');
|
|||
|
|
|||
|
var _container2 = _interopRequireDefault(_container);
|
|||
|
|
|||
|
var _warnOnce = require('./warn-once');
|
|||
|
|
|||
|
var _warnOnce2 = _interopRequireDefault(_warnOnce);
|
|||
|
|
|||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|||
|
|
|||
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|||
|
|
|||
|
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; }
|
|||
|
|
|||
|
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; }
|
|||
|
|
|||
|
/**
|
|||
|
* Represents a CSS file and contains all its parsed nodes.
|
|||
|
*
|
|||
|
* @extends Container
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const root = postcss.parse('a{color:black} b{z-index:2}');
|
|||
|
* root.type //=> 'root'
|
|||
|
* root.nodes.length //=> 2
|
|||
|
*/
|
|||
|
var Root = function (_Container) {
|
|||
|
_inherits(Root, _Container);
|
|||
|
|
|||
|
function Root(defaults) {
|
|||
|
_classCallCheck(this, Root);
|
|||
|
|
|||
|
var _this = _possibleConstructorReturn(this, _Container.call(this, defaults));
|
|||
|
|
|||
|
_this.type = 'root';
|
|||
|
if (!_this.nodes) _this.nodes = [];
|
|||
|
return _this;
|
|||
|
}
|
|||
|
|
|||
|
Root.prototype.removeChild = function removeChild(child) {
|
|||
|
child = this.index(child);
|
|||
|
|
|||
|
if (child === 0 && this.nodes.length > 1) {
|
|||
|
this.nodes[1].raws.before = this.nodes[child].raws.before;
|
|||
|
}
|
|||
|
|
|||
|
return _Container.prototype.removeChild.call(this, child);
|
|||
|
};
|
|||
|
|
|||
|
Root.prototype.normalize = function normalize(child, sample, type) {
|
|||
|
var nodes = _Container.prototype.normalize.call(this, child);
|
|||
|
|
|||
|
if (sample) {
|
|||
|
if (type === 'prepend') {
|
|||
|
if (this.nodes.length > 1) {
|
|||
|
sample.raws.before = this.nodes[1].raws.before;
|
|||
|
} else {
|
|||
|
delete sample.raws.before;
|
|||
|
}
|
|||
|
} else if (this.first !== sample) {
|
|||
|
for (var _iterator = nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|||
|
var _ref;
|
|||
|
|
|||
|
if (_isArray) {
|
|||
|
if (_i >= _iterator.length) break;
|
|||
|
_ref = _iterator[_i++];
|
|||
|
} else {
|
|||
|
_i = _iterator.next();
|
|||
|
if (_i.done) break;
|
|||
|
_ref = _i.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref;
|
|||
|
|
|||
|
node.raws.before = sample.raws.before;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return nodes;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* Returns a {@link Result} instance representing the root’s CSS.
|
|||
|
*
|
|||
|
* @param {processOptions} [opts] - options with only `to` and `map` keys
|
|||
|
*
|
|||
|
* @return {Result} result with current root’s CSS
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const root1 = postcss.parse(css1, { from: 'a.css' });
|
|||
|
* const root2 = postcss.parse(css2, { from: 'b.css' });
|
|||
|
* root1.append(root2);
|
|||
|
* const result = root1.toResult({ to: 'all.css', map: true });
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
Root.prototype.toResult = function toResult() {
|
|||
|
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|||
|
|
|||
|
var LazyResult = require('./lazy-result');
|
|||
|
var Processor = require('./processor');
|
|||
|
|
|||
|
var lazy = new LazyResult(new Processor(), this, opts);
|
|||
|
return lazy.stringify();
|
|||
|
};
|
|||
|
|
|||
|
Root.prototype.remove = function remove(child) {
|
|||
|
(0, _warnOnce2.default)('Root#remove is deprecated. Use Root#removeChild');
|
|||
|
this.removeChild(child);
|
|||
|
};
|
|||
|
|
|||
|
Root.prototype.prevMap = function prevMap() {
|
|||
|
(0, _warnOnce2.default)('Root#prevMap is deprecated. Use Root#source.input.map');
|
|||
|
return this.source.input.map;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @memberof Root#
|
|||
|
* @member {object} raws - Information to generate byte-to-byte equal
|
|||
|
* node string as it was in the origin input.
|
|||
|
*
|
|||
|
* Every parser saves its own properties,
|
|||
|
* but the default CSS parser uses:
|
|||
|
*
|
|||
|
* * `after`: the space symbols after the last child to the end of file.
|
|||
|
* * `semicolon`: is the last child has an (optional) semicolon.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* postcss.parse('a {}\n').raws //=> { after: '\n' }
|
|||
|
* postcss.parse('a {}').raws //=> { after: '' }
|
|||
|
*/
|
|||
|
|
|||
|
return Root;
|
|||
|
}(_container2.default);
|
|||
|
|
|||
|
exports.default = Root;
|
|||
|
module.exports = exports['default'];
|
|||
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJvb3QuZXM2Il0sIm5hbWVzIjpbIlJvb3QiLCJkZWZhdWx0cyIsInR5cGUiLCJub2RlcyIsInJlbW92ZUNoaWxkIiwiY2hpbGQiLCJpbmRleCIsImxlbmd0aCIsInJhd3MiLCJiZWZvcmUiLCJub3JtYWxpemUiLCJzYW1wbGUiLCJmaXJzdCIsIm5vZGUiLCJ0b1Jlc3VsdCIsIm9wdHMiLCJMYXp5UmVzdWx0IiwicmVx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LCBzYW1wbGUsIHR5cGUpIHtcbiAgICAgICAgbGV0IG5vZGVzID0gc3VwZXIubm9ybWFsaXplKGNoaWxkKTtcblxuICAgICAgICBpZiAoIHNhbXBsZSApIHtcbiAgICAgICAgICAgIGlmICggdHlwZSA9PT0gJ3ByZXBlbmQnICkge1xuICAgICAgICAgICAgICAgIGlmICggdGhpcy5ub2Rlcy5sZW5ndGggPiAxICkge1xuICAgICAgICAgICAgICAgICAgICBzYW1wbGUucmF3cy5iZWZvcmUgPSB0aGlzLm5vZGVzWzFdLnJhd3MuYmVmb3JlO1xuICAgICAgICAgICAgICAgIH0gZWxzZSB7XG4gICAgICAgICAgICAgICAgICAgIGRlbGV0ZSBzYW1wbGUucmF3cy5iZWZvcmU7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfSBlbHNlIGlmICggdGhpcy5maXJzdCAhPT0gc2FtcGxlICkge1xuICAgICAgICAgICAgICAgIGZvciAoIGxldCBub2Rl
|