123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- 'use strict';
- exports.__esModule = true;
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- var _declaration = require('./declaration');
- var _declaration2 = _interopRequireDefault(_declaration);
- var _warnOnce = require('./warn-once');
- var _warnOnce2 = _interopRequireDefault(_warnOnce);
- var _comment = require('./comment');
- var _comment2 = _interopRequireDefault(_comment);
- var _node = require('./node');
- var _node2 = _interopRequireDefault(_node);
- 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; }
- function cleanSource(nodes) {
- return nodes.map(function (i) {
- if (i.nodes) i.nodes = cleanSource(i.nodes);
- delete i.source;
- return i;
- });
- }
- var Container = function (_Node) {
- _inherits(Container, _Node);
- function Container() {
- _classCallCheck(this, Container);
- return _possibleConstructorReturn(this, _Node.apply(this, arguments));
- }
- Container.prototype.push = function push(child) {
- child.parent = this;
- this.nodes.push(child);
- return this;
- };
-
- Container.prototype.each = function each(callback) {
- if (!this.lastEach) this.lastEach = 0;
- if (!this.indexes) this.indexes = {};
- this.lastEach += 1;
- var id = this.lastEach;
- this.indexes[id] = 0;
- if (!this.nodes) return undefined;
- var index = void 0,
- result = void 0;
- while (this.indexes[id] < this.nodes.length) {
- index = this.indexes[id];
- result = callback(this.nodes[index], index);
- if (result === false) break;
- this.indexes[id] += 1;
- }
- delete this.indexes[id];
- return result;
- };
-
- Container.prototype.walk = function walk(callback) {
- return this.each(function (child, i) {
- var result = callback(child, i);
- if (result !== false && child.walk) {
- result = child.walk(callback);
- }
- return result;
- });
- };
-
- Container.prototype.walkDecls = function walkDecls(prop, callback) {
- if (!callback) {
- callback = prop;
- return this.walk(function (child, i) {
- if (child.type === 'decl') {
- return callback(child, i);
- }
- });
- } else if (prop instanceof RegExp) {
- return this.walk(function (child, i) {
- if (child.type === 'decl' && prop.test(child.prop)) {
- return callback(child, i);
- }
- });
- } else {
- return this.walk(function (child, i) {
- if (child.type === 'decl' && child.prop === prop) {
- return callback(child, i);
- }
- });
- }
- };
-
- Container.prototype.walkRules = function walkRules(selector, callback) {
- if (!callback) {
- callback = selector;
- return this.walk(function (child, i) {
- if (child.type === 'rule') {
- return callback(child, i);
- }
- });
- } else if (selector instanceof RegExp) {
- return this.walk(function (child, i) {
- if (child.type === 'rule' && selector.test(child.selector)) {
- return callback(child, i);
- }
- });
- } else {
- return this.walk(function (child, i) {
- if (child.type === 'rule' && child.selector === selector) {
- return callback(child, i);
- }
- });
- }
- };
-
- Container.prototype.walkAtRules = function walkAtRules(name, callback) {
- if (!callback) {
- callback = name;
- return this.walk(function (child, i) {
- if (child.type === 'atrule') {
- return callback(child, i);
- }
- });
- } else if (name instanceof RegExp) {
- return this.walk(function (child, i) {
- if (child.type === 'atrule' && name.test(child.name)) {
- return callback(child, i);
- }
- });
- } else {
- return this.walk(function (child, i) {
- if (child.type === 'atrule' && child.name === name) {
- return callback(child, i);
- }
- });
- }
- };
-
- Container.prototype.walkComments = function walkComments(callback) {
- return this.walk(function (child, i) {
- if (child.type === 'comment') {
- return callback(child, i);
- }
- });
- };
-
- Container.prototype.append = function append() {
- for (var _len = arguments.length, children = Array(_len), _key = 0; _key < _len; _key++) {
- children[_key] = arguments[_key];
- }
- for (var _iterator = children, _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 child = _ref;
- var nodes = this.normalize(child, this.last);
- for (var _iterator2 = nodes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
- var _ref2;
- if (_isArray2) {
- if (_i2 >= _iterator2.length) break;
- _ref2 = _iterator2[_i2++];
- } else {
- _i2 = _iterator2.next();
- if (_i2.done) break;
- _ref2 = _i2.value;
- }
- var node = _ref2;
- this.nodes.push(node);
- }
- }
- return this;
- };
-
- Container.prototype.prepend = function prepend() {
- for (var _len2 = arguments.length, children = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- children[_key2] = arguments[_key2];
- }
- children = children.reverse();
- for (var _iterator3 = children, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
- var _ref3;
- if (_isArray3) {
- if (_i3 >= _iterator3.length) break;
- _ref3 = _iterator3[_i3++];
- } else {
- _i3 = _iterator3.next();
- if (_i3.done) break;
- _ref3 = _i3.value;
- }
- var child = _ref3;
- var nodes = this.normalize(child, this.first, 'prepend').reverse();
- for (var _iterator4 = nodes, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
- var _ref4;
- if (_isArray4) {
- if (_i4 >= _iterator4.length) break;
- _ref4 = _iterator4[_i4++];
- } else {
- _i4 = _iterator4.next();
- if (_i4.done) break;
- _ref4 = _i4.value;
- }
- var node = _ref4;
- this.nodes.unshift(node);
- }for (var id in this.indexes) {
- this.indexes[id] = this.indexes[id] + nodes.length;
- }
- }
- return this;
- };
- Container.prototype.cleanRaws = function cleanRaws(keepBetween) {
- _Node.prototype.cleanRaws.call(this, keepBetween);
- if (this.nodes) {
- for (var _iterator5 = this.nodes, _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
- var _ref5;
- if (_isArray5) {
- if (_i5 >= _iterator5.length) break;
- _ref5 = _iterator5[_i5++];
- } else {
- _i5 = _iterator5.next();
- if (_i5.done) break;
- _ref5 = _i5.value;
- }
- var node = _ref5;
- node.cleanRaws(keepBetween);
- }
- }
- };
-
- Container.prototype.insertBefore = function insertBefore(exist, add) {
- exist = this.index(exist);
- var type = exist === 0 ? 'prepend' : false;
- var nodes = this.normalize(add, this.nodes[exist], type).reverse();
- for (var _iterator6 = nodes, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
- var _ref6;
- if (_isArray6) {
- if (_i6 >= _iterator6.length) break;
- _ref6 = _iterator6[_i6++];
- } else {
- _i6 = _iterator6.next();
- if (_i6.done) break;
- _ref6 = _i6.value;
- }
- var node = _ref6;
- this.nodes.splice(exist, 0, node);
- }var index = void 0;
- for (var id in this.indexes) {
- index = this.indexes[id];
- if (exist <= index) {
- this.indexes[id] = index + nodes.length;
- }
- }
- return this;
- };
-
- Container.prototype.insertAfter = function insertAfter(exist, add) {
- exist = this.index(exist);
- var nodes = this.normalize(add, this.nodes[exist]).reverse();
- for (var _iterator7 = nodes, _isArray7 = Array.isArray(_iterator7), _i7 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
- var _ref7;
- if (_isArray7) {
- if (_i7 >= _iterator7.length) break;
- _ref7 = _iterator7[_i7++];
- } else {
- _i7 = _iterator7.next();
- if (_i7.done) break;
- _ref7 = _i7.value;
- }
- var node = _ref7;
- this.nodes.splice(exist + 1, 0, node);
- }var index = void 0;
- for (var id in this.indexes) {
- index = this.indexes[id];
- if (exist < index) {
- this.indexes[id] = index + nodes.length;
- }
- }
- return this;
- };
- Container.prototype.remove = function remove(child) {
- if (typeof child !== 'undefined') {
- (0, _warnOnce2.default)('Container#remove is deprecated. ' + 'Use Container#removeChild');
- this.removeChild(child);
- } else {
- _Node.prototype.remove.call(this);
- }
- return this;
- };
-
- Container.prototype.removeChild = function removeChild(child) {
- child = this.index(child);
- this.nodes[child].parent = undefined;
- this.nodes.splice(child, 1);
- var index = void 0;
- for (var id in this.indexes) {
- index = this.indexes[id];
- if (index >= child) {
- this.indexes[id] = index - 1;
- }
- }
- return this;
- };
-
- Container.prototype.removeAll = function removeAll() {
- for (var _iterator8 = this.nodes, _isArray8 = Array.isArray(_iterator8), _i8 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
- var _ref8;
- if (_isArray8) {
- if (_i8 >= _iterator8.length) break;
- _ref8 = _iterator8[_i8++];
- } else {
- _i8 = _iterator8.next();
- if (_i8.done) break;
- _ref8 = _i8.value;
- }
- var node = _ref8;
- node.parent = undefined;
- }this.nodes = [];
- return this;
- };
-
- Container.prototype.replaceValues = function replaceValues(pattern, opts, callback) {
- if (!callback) {
- callback = opts;
- opts = {};
- }
- this.walkDecls(function (decl) {
- if (opts.props && opts.props.indexOf(decl.prop) === -1) return;
- if (opts.fast && decl.value.indexOf(opts.fast) === -1) return;
- decl.value = decl.value.replace(pattern, callback);
- });
- return this;
- };
-
- Container.prototype.every = function every(condition) {
- return this.nodes.every(condition);
- };
-
- Container.prototype.some = function some(condition) {
- return this.nodes.some(condition);
- };
-
- Container.prototype.index = function index(child) {
- if (typeof child === 'number') {
- return child;
- } else {
- return this.nodes.indexOf(child);
- }
- };
-
- Container.prototype.normalize = function normalize(nodes, sample) {
- var _this2 = this;
- if (typeof nodes === 'string') {
- var parse = require('./parse');
- nodes = cleanSource(parse(nodes).nodes);
- } else if (!Array.isArray(nodes)) {
- if (nodes.type === 'root') {
- nodes = nodes.nodes;
- } else if (nodes.type) {
- nodes = [nodes];
- } else if (nodes.prop) {
- if (typeof nodes.value === 'undefined') {
- throw new Error('Value field is missed in node creation');
- } else if (typeof nodes.value !== 'string') {
- nodes.value = String(nodes.value);
- }
- nodes = [new _declaration2.default(nodes)];
- } else if (nodes.selector) {
- var Rule = require('./rule');
- nodes = [new Rule(nodes)];
- } else if (nodes.name) {
- var AtRule = require('./at-rule');
- nodes = [new AtRule(nodes)];
- } else if (nodes.text) {
- nodes = [new _comment2.default(nodes)];
- } else {
- throw new Error('Unknown node type in node creation');
- }
- }
- var processed = nodes.map(function (i) {
- if (typeof i.raws === 'undefined') i = _this2.rebuild(i);
- if (i.parent) i = i.clone();
- if (typeof i.raws.before === 'undefined') {
- if (sample && typeof sample.raws.before !== 'undefined') {
- i.raws.before = sample.raws.before.replace(/[^\s]/g, '');
- }
- }
- i.parent = _this2;
- return i;
- });
- return processed;
- };
- Container.prototype.rebuild = function rebuild(node, parent) {
- var _this3 = this;
- var fix = void 0;
- if (node.type === 'root') {
- var Root = require('./root');
- fix = new Root();
- } else if (node.type === 'atrule') {
- var AtRule = require('./at-rule');
- fix = new AtRule();
- } else if (node.type === 'rule') {
- var Rule = require('./rule');
- fix = new Rule();
- } else if (node.type === 'decl') {
- fix = new _declaration2.default();
- } else if (node.type === 'comment') {
- fix = new _comment2.default();
- }
- for (var i in node) {
- if (i === 'nodes') {
- fix.nodes = node.nodes.map(function (j) {
- return _this3.rebuild(j, fix);
- });
- } else if (i === 'parent' && parent) {
- fix.parent = parent;
- } else if (node.hasOwnProperty(i)) {
- fix[i] = node[i];
- }
- }
- return fix;
- };
- Container.prototype.eachInside = function eachInside(callback) {
- (0, _warnOnce2.default)('Container#eachInside is deprecated. ' + 'Use Container#walk instead.');
- return this.walk(callback);
- };
- Container.prototype.eachDecl = function eachDecl(prop, callback) {
- (0, _warnOnce2.default)('Container#eachDecl is deprecated. ' + 'Use Container#walkDecls instead.');
- return this.walkDecls(prop, callback);
- };
- Container.prototype.eachRule = function eachRule(selector, callback) {
- (0, _warnOnce2.default)('Container#eachRule is deprecated. ' + 'Use Container#walkRules instead.');
- return this.walkRules(selector, callback);
- };
- Container.prototype.eachAtRule = function eachAtRule(name, callback) {
- (0, _warnOnce2.default)('Container#eachAtRule is deprecated. ' + 'Use Container#walkAtRules instead.');
- return this.walkAtRules(name, callback);
- };
- Container.prototype.eachComment = function eachComment(callback) {
- (0, _warnOnce2.default)('Container#eachComment is deprecated. ' + 'Use Container#walkComments instead.');
- return this.walkComments(callback);
- };
- _createClass(Container, [{
- key: 'first',
- get: function get() {
- if (!this.nodes) return undefined;
- return this.nodes[0];
- }
-
- }, {
- key: 'last',
- get: function get() {
- if (!this.nodes) return undefined;
- return this.nodes[this.nodes.length - 1];
- }
- }, {
- key: 'semicolon',
- get: function get() {
- (0, _warnOnce2.default)('Node#semicolon is deprecated. Use Node#raws.semicolon');
- return this.raws.semicolon;
- },
- set: function set(val) {
- (0, _warnOnce2.default)('Node#semicolon is deprecated. Use Node#raws.semicolon');
- this.raws.semicolon = val;
- }
- }, {
- key: 'after',
- get: function get() {
- (0, _warnOnce2.default)('Node#after is deprecated. Use Node#raws.after');
- return this.raws.after;
- },
- set: function set(val) {
- (0, _warnOnce2.default)('Node#after is deprecated. Use Node#raws.after');
- this.raws.after = val;
- }
-
- }]);
- return Container;
- }(_node2.default);
- exports.default = Container;
- module.exports = exports['default'];
|