123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- "use strict";
- exports.__esModule = true;
- exports.default = void 0;
- var _declaration = _interopRequireDefault(require("./declaration"));
- var _comment = _interopRequireDefault(require("./comment"));
- var _node = _interopRequireDefault(require("./node"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
- 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); } }
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; 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) {
- _inheritsLoose(Container, _Node);
- function Container() {
- return _Node.apply(this, arguments) || this;
- }
- var _proto = Container.prototype;
- _proto.push = function push(child) {
- child.parent = this;
- this.nodes.push(child);
- return this;
- }
-
- ;
- _proto.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, result;
- 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;
- }
-
- ;
- _proto.walk = function walk(callback) {
- return this.each(function (child, i) {
- var result;
- try {
- result = callback(child, i);
- } catch (e) {
- e.postcssNode = child;
- if (e.stack && child.source && /\n\s{4}at /.test(e.stack)) {
- var s = child.source;
- e.stack = e.stack.replace(/\n\s{4}at /, "$&" + s.input.from + ":" + s.start.line + ":" + s.start.column + "$&");
- }
- throw e;
- }
- if (result !== false && child.walk) {
- result = child.walk(callback);
- }
- return result;
- });
- }
-
- ;
- _proto.walkDecls = function walkDecls(prop, callback) {
- if (!callback) {
- callback = prop;
- return this.walk(function (child, i) {
- if (child.type === 'decl') {
- return callback(child, i);
- }
- });
- }
- if (prop instanceof RegExp) {
- return this.walk(function (child, i) {
- if (child.type === 'decl' && prop.test(child.prop)) {
- return callback(child, i);
- }
- });
- }
- return this.walk(function (child, i) {
- if (child.type === 'decl' && child.prop === prop) {
- return callback(child, i);
- }
- });
- }
-
- ;
- _proto.walkRules = function walkRules(selector, callback) {
- if (!callback) {
- callback = selector;
- return this.walk(function (child, i) {
- if (child.type === 'rule') {
- return callback(child, i);
- }
- });
- }
- if (selector instanceof RegExp) {
- return this.walk(function (child, i) {
- if (child.type === 'rule' && selector.test(child.selector)) {
- return callback(child, i);
- }
- });
- }
- return this.walk(function (child, i) {
- if (child.type === 'rule' && child.selector === selector) {
- return callback(child, i);
- }
- });
- }
-
- ;
- _proto.walkAtRules = function walkAtRules(name, callback) {
- if (!callback) {
- callback = name;
- return this.walk(function (child, i) {
- if (child.type === 'atrule') {
- return callback(child, i);
- }
- });
- }
- if (name instanceof RegExp) {
- return this.walk(function (child, i) {
- if (child.type === 'atrule' && name.test(child.name)) {
- return callback(child, i);
- }
- });
- }
- return this.walk(function (child, i) {
- if (child.type === 'atrule' && child.name === name) {
- return callback(child, i);
- }
- });
- }
-
- ;
- _proto.walkComments = function walkComments(callback) {
- return this.walk(function (child, i) {
- if (child.type === 'comment') {
- return callback(child, i);
- }
- });
- }
-
- ;
- _proto.append = function append() {
- for (var _len = arguments.length, children = new Array(_len), _key = 0; _key < _len; _key++) {
- children[_key] = arguments[_key];
- }
- for (var _i = 0, _children = children; _i < _children.length; _i++) {
- var child = _children[_i];
- var nodes = this.normalize(child, this.last);
- for (var _iterator = _createForOfIteratorHelperLoose(nodes), _step; !(_step = _iterator()).done;) {
- var node = _step.value;
- this.nodes.push(node);
- }
- }
- return this;
- }
-
- ;
- _proto.prepend = function prepend() {
- for (var _len2 = arguments.length, children = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- children[_key2] = arguments[_key2];
- }
- children = children.reverse();
- for (var _iterator2 = _createForOfIteratorHelperLoose(children), _step2; !(_step2 = _iterator2()).done;) {
- var child = _step2.value;
- var nodes = this.normalize(child, this.first, 'prepend').reverse();
- for (var _iterator3 = _createForOfIteratorHelperLoose(nodes), _step3; !(_step3 = _iterator3()).done;) {
- var node = _step3.value;
- this.nodes.unshift(node);
- }
- for (var id in this.indexes) {
- this.indexes[id] = this.indexes[id] + nodes.length;
- }
- }
- return this;
- };
- _proto.cleanRaws = function cleanRaws(keepBetween) {
- _Node.prototype.cleanRaws.call(this, keepBetween);
- if (this.nodes) {
- for (var _iterator4 = _createForOfIteratorHelperLoose(this.nodes), _step4; !(_step4 = _iterator4()).done;) {
- var node = _step4.value;
- node.cleanRaws(keepBetween);
- }
- }
- }
-
- ;
- _proto.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 _iterator5 = _createForOfIteratorHelperLoose(nodes), _step5; !(_step5 = _iterator5()).done;) {
- var node = _step5.value;
- this.nodes.splice(exist, 0, node);
- }
- var index;
- for (var id in this.indexes) {
- index = this.indexes[id];
- if (exist <= index) {
- this.indexes[id] = index + nodes.length;
- }
- }
- return this;
- }
-
- ;
- _proto.insertAfter = function insertAfter(exist, add) {
- exist = this.index(exist);
- var nodes = this.normalize(add, this.nodes[exist]).reverse();
- for (var _iterator6 = _createForOfIteratorHelperLoose(nodes), _step6; !(_step6 = _iterator6()).done;) {
- var node = _step6.value;
- this.nodes.splice(exist + 1, 0, node);
- }
- var index;
- for (var id in this.indexes) {
- index = this.indexes[id];
- if (exist < index) {
- this.indexes[id] = index + nodes.length;
- }
- }
- return this;
- }
-
- ;
- _proto.removeChild = function removeChild(child) {
- child = this.index(child);
- this.nodes[child].parent = undefined;
- this.nodes.splice(child, 1);
- var index;
- for (var id in this.indexes) {
- index = this.indexes[id];
- if (index >= child) {
- this.indexes[id] = index - 1;
- }
- }
- return this;
- }
-
- ;
- _proto.removeAll = function removeAll() {
- for (var _iterator7 = _createForOfIteratorHelperLoose(this.nodes), _step7; !(_step7 = _iterator7()).done;) {
- var node = _step7.value;
- node.parent = undefined;
- }
- this.nodes = [];
- return this;
- }
-
- ;
- _proto.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;
- }
-
- ;
- _proto.every = function every(condition) {
- return this.nodes.every(condition);
- }
-
- ;
- _proto.some = function some(condition) {
- return this.nodes.some(condition);
- }
-
- ;
- _proto.index = function index(child) {
- if (typeof child === 'number') {
- return child;
- }
- return this.nodes.indexOf(child);
- }
-
- ;
- _proto.normalize = function normalize(nodes, sample) {
- var _this = this;
- if (typeof nodes === 'string') {
- var parse = require('./parse');
- nodes = cleanSource(parse(nodes).nodes);
- } else if (Array.isArray(nodes)) {
- nodes = nodes.slice(0);
- for (var _iterator8 = _createForOfIteratorHelperLoose(nodes), _step8; !(_step8 = _iterator8()).done;) {
- var i = _step8.value;
- if (i.parent) i.parent.removeChild(i, 'ignore');
- }
- } else if (nodes.type === 'root') {
- nodes = nodes.nodes.slice(0);
- for (var _iterator9 = _createForOfIteratorHelperLoose(nodes), _step9; !(_step9 = _iterator9()).done;) {
- var _i2 = _step9.value;
- if (_i2.parent) _i2.parent.removeChild(_i2, 'ignore');
- }
- } 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 _declaration.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 _comment.default(nodes)];
- } else {
- throw new Error('Unknown node type in node creation');
- }
- var processed = nodes.map(function (i) {
- if (i.parent) i.parent.removeChild(i);
- if (typeof i.raws.before === 'undefined') {
- if (sample && typeof sample.raws.before !== 'undefined') {
- i.raws.before = sample.raws.before.replace(/[^\s]/g, '');
- }
- }
- i.parent = _this;
- return i;
- });
- return processed;
- }
-
- ;
- _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];
- }
- }]);
- return Container;
- }(_node.default);
- var _default = Container;
- exports.default = _default;
- module.exports = exports.default;
|