870 lines
65 KiB
JavaScript
870 lines
65 KiB
JavaScript
|
"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 _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;
|
|||
|
});
|
|||
|
}
|
|||
|
/**
|
|||
|
* The {@link Root}, {@link AtRule}, and {@link Rule} container nodes
|
|||
|
* inherit some common methods to help work with their children.
|
|||
|
*
|
|||
|
* Note that all containers can store any content. If you write a rule inside
|
|||
|
* a rule, PostCSS will parse it.
|
|||
|
*
|
|||
|
* @extends Node
|
|||
|
* @abstract
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
var Container =
|
|||
|
/*#__PURE__*/
|
|||
|
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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Iterates through the container’s immediate children,
|
|||
|
* calling `callback` for each child.
|
|||
|
*
|
|||
|
* Returning `false` in the callback will break iteration.
|
|||
|
*
|
|||
|
* This method only iterates through the container’s immediate children.
|
|||
|
* If you need to recursively iterate through all the container’s descendant
|
|||
|
* nodes, use {@link Container#walk}.
|
|||
|
*
|
|||
|
* Unlike the for `{}`-cycle or `Array#forEach` this iterator is safe
|
|||
|
* if you are mutating the array of child nodes during iteration.
|
|||
|
* PostCSS will adjust the current index to match the mutations.
|
|||
|
*
|
|||
|
* @param {childIterator} callback Iterator receives each node and index.
|
|||
|
*
|
|||
|
* @return {false|undefined} Returns `false` if iteration was broke.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const root = postcss.parse('a { color: black; z-index: 1 }')
|
|||
|
* const rule = root.first
|
|||
|
*
|
|||
|
* for (const decl of rule.nodes) {
|
|||
|
* decl.cloneBefore({ prop: '-webkit-' + decl.prop })
|
|||
|
* // Cycle will be infinite, because cloneBefore moves the current node
|
|||
|
* // to the next index
|
|||
|
* }
|
|||
|
*
|
|||
|
* rule.each(decl => {
|
|||
|
* decl.cloneBefore({ prop: '-webkit-' + decl.prop })
|
|||
|
* // Will be executed only for color and z-index
|
|||
|
* })
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Traverses the container’s descendant nodes, calling callback
|
|||
|
* for each node.
|
|||
|
*
|
|||
|
* Like container.each(), this method is safe to use
|
|||
|
* if you are mutating arrays during iteration.
|
|||
|
*
|
|||
|
* If you only need to iterate through the container’s immediate children,
|
|||
|
* use {@link Container#each}.
|
|||
|
*
|
|||
|
* @param {childIterator} callback Iterator receives each node and index.
|
|||
|
*
|
|||
|
* @return {false|undefined} Returns `false` if iteration was broke.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* root.walk(node => {
|
|||
|
* // Traverses all descendant nodes.
|
|||
|
* })
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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;
|
|||
|
});
|
|||
|
}
|
|||
|
/**
|
|||
|
* Traverses the container’s descendant nodes, calling callback
|
|||
|
* for each declaration node.
|
|||
|
*
|
|||
|
* If you pass a filter, iteration will only happen over declarations
|
|||
|
* with matching properties.
|
|||
|
*
|
|||
|
* Like {@link Container#each}, this method is safe
|
|||
|
* to use if you are mutating arrays during iteration.
|
|||
|
*
|
|||
|
* @param {string|RegExp} [prop] String or regular expression
|
|||
|
* to filter declarations by property name.
|
|||
|
* @param {childIterator} callback Iterator receives each node and index.
|
|||
|
*
|
|||
|
* @return {false|undefined} Returns `false` if iteration was broke.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* root.walkDecls(decl => {
|
|||
|
* checkPropertySupport(decl.prop)
|
|||
|
* })
|
|||
|
*
|
|||
|
* root.walkDecls('border-radius', decl => {
|
|||
|
* decl.remove()
|
|||
|
* })
|
|||
|
*
|
|||
|
* root.walkDecls(/^background/, decl => {
|
|||
|
* decl.value = takeFirstColorFromGradient(decl.value)
|
|||
|
* })
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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);
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
/**
|
|||
|
* Traverses the container’s descendant nodes, calling callback
|
|||
|
* for each rule node.
|
|||
|
*
|
|||
|
* If you pass a filter, iteration will only happen over rules
|
|||
|
* with matching selectors.
|
|||
|
*
|
|||
|
* Like {@link Container#each}, this method is safe
|
|||
|
* to use if you are mutating arrays during iteration.
|
|||
|
*
|
|||
|
* @param {string|RegExp} [selector] String or regular expression
|
|||
|
* to filter rules by selector.
|
|||
|
* @param {childIterator} callback Iterator receives each node and index.
|
|||
|
*
|
|||
|
* @return {false|undefined} returns `false` if iteration was broke.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const selectors = []
|
|||
|
* root.walkRules(rule => {
|
|||
|
* selectors.push(rule.selector)
|
|||
|
* })
|
|||
|
* console.log(`Your CSS uses ${ selectors.length } selectors`)
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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);
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
/**
|
|||
|
* Traverses the container’s descendant nodes, calling callback
|
|||
|
* for each at-rule node.
|
|||
|
*
|
|||
|
* If you pass a filter, iteration will only happen over at-rules
|
|||
|
* that have matching names.
|
|||
|
*
|
|||
|
* Like {@link Container#each}, this method is safe
|
|||
|
* to use if you are mutating arrays during iteration.
|
|||
|
*
|
|||
|
* @param {string|RegExp} [name] String or regular expression
|
|||
|
* to filter at-rules by name.
|
|||
|
* @param {childIterator} callback Iterator receives each node and index.
|
|||
|
*
|
|||
|
* @return {false|undefined} Returns `false` if iteration was broke.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* root.walkAtRules(rule => {
|
|||
|
* if (isOld(rule.name)) rule.remove()
|
|||
|
* })
|
|||
|
*
|
|||
|
* let first = false
|
|||
|
* root.walkAtRules('charset', rule => {
|
|||
|
* if (!first) {
|
|||
|
* first = true
|
|||
|
* } else {
|
|||
|
* rule.remove()
|
|||
|
* }
|
|||
|
* })
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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);
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
/**
|
|||
|
* Traverses the container’s descendant nodes, calling callback
|
|||
|
* for each comment node.
|
|||
|
*
|
|||
|
* Like {@link Container#each}, this method is safe
|
|||
|
* to use if you are mutating arrays during iteration.
|
|||
|
*
|
|||
|
* @param {childIterator} callback Iterator receives each node and index.
|
|||
|
*
|
|||
|
* @return {false|undefined} Returns `false` if iteration was broke.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* root.walkComments(comment => {
|
|||
|
* comment.remove()
|
|||
|
* })
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.walkComments = function walkComments(callback) {
|
|||
|
return this.walk(function (child, i) {
|
|||
|
if (child.type === 'comment') {
|
|||
|
return callback(child, i);
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
/**
|
|||
|
* Inserts new nodes to the end of the container.
|
|||
|
*
|
|||
|
* @param {...(Node|object|string|Node[])} children New nodes.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const decl1 = postcss.decl({ prop: 'color', value: 'black' })
|
|||
|
* const decl2 = postcss.decl({ prop: 'background-color', value: 'white' })
|
|||
|
* rule.append(decl1, decl2)
|
|||
|
*
|
|||
|
* root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
|
|||
|
* root.append({ selector: 'a' }) // rule
|
|||
|
* rule.append({ prop: 'color', value: 'black' }) // declaration
|
|||
|
* rule.append({ text: 'Comment' }) // comment
|
|||
|
*
|
|||
|
* root.append('a {}')
|
|||
|
* root.first.append('color: black; z-index: 1')
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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 = nodes, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
|||
|
var _ref;
|
|||
|
|
|||
|
if (_isArray) {
|
|||
|
if (_i2 >= _iterator.length) break;
|
|||
|
_ref = _iterator[_i2++];
|
|||
|
} else {
|
|||
|
_i2 = _iterator.next();
|
|||
|
if (_i2.done) break;
|
|||
|
_ref = _i2.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref;
|
|||
|
this.nodes.push(node);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return this;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Inserts new nodes to the start of the container.
|
|||
|
*
|
|||
|
* @param {...(Node|object|string|Node[])} children New nodes.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const decl1 = postcss.decl({ prop: 'color', value: 'black' })
|
|||
|
* const decl2 = postcss.decl({ prop: 'background-color', value: 'white' })
|
|||
|
* rule.prepend(decl1, decl2)
|
|||
|
*
|
|||
|
* root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
|
|||
|
* root.append({ selector: 'a' }) // rule
|
|||
|
* rule.append({ prop: 'color', value: 'black' }) // declaration
|
|||
|
* rule.append({ text: 'Comment' }) // comment
|
|||
|
*
|
|||
|
* root.append('a {}')
|
|||
|
* root.first.append('color: black; z-index: 1')
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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 = children, _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
|||
|
var _ref2;
|
|||
|
|
|||
|
if (_isArray2) {
|
|||
|
if (_i3 >= _iterator2.length) break;
|
|||
|
_ref2 = _iterator2[_i3++];
|
|||
|
} else {
|
|||
|
_i3 = _iterator2.next();
|
|||
|
if (_i3.done) break;
|
|||
|
_ref2 = _i3.value;
|
|||
|
}
|
|||
|
|
|||
|
var child = _ref2;
|
|||
|
var nodes = this.normalize(child, this.first, 'prepend').reverse();
|
|||
|
|
|||
|
for (var _iterator3 = nodes, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
|
|||
|
var _ref3;
|
|||
|
|
|||
|
if (_isArray3) {
|
|||
|
if (_i4 >= _iterator3.length) break;
|
|||
|
_ref3 = _iterator3[_i4++];
|
|||
|
} else {
|
|||
|
_i4 = _iterator3.next();
|
|||
|
if (_i4.done) break;
|
|||
|
_ref3 = _i4.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref3;
|
|||
|
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 = this.nodes, _isArray4 = Array.isArray(_iterator4), _i5 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
|
|||
|
var _ref4;
|
|||
|
|
|||
|
if (_isArray4) {
|
|||
|
if (_i5 >= _iterator4.length) break;
|
|||
|
_ref4 = _iterator4[_i5++];
|
|||
|
} else {
|
|||
|
_i5 = _iterator4.next();
|
|||
|
if (_i5.done) break;
|
|||
|
_ref4 = _i5.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref4;
|
|||
|
node.cleanRaws(keepBetween);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
/**
|
|||
|
* Insert new node before old node within the container.
|
|||
|
*
|
|||
|
* @param {Node|number} exist Child or child’s index.
|
|||
|
* @param {Node|object|string|Node[]} add New node.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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 = nodes, _isArray5 = Array.isArray(_iterator5), _i6 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
|
|||
|
var _ref5;
|
|||
|
|
|||
|
if (_isArray5) {
|
|||
|
if (_i6 >= _iterator5.length) break;
|
|||
|
_ref5 = _iterator5[_i6++];
|
|||
|
} else {
|
|||
|
_i6 = _iterator5.next();
|
|||
|
if (_i6.done) break;
|
|||
|
_ref5 = _i6.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref5;
|
|||
|
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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Insert new node after old node within the container.
|
|||
|
*
|
|||
|
* @param {Node|number} exist Child or child’s index.
|
|||
|
* @param {Node|object|string|Node[]} add New node.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain.
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.insertAfter = function insertAfter(exist, add) {
|
|||
|
exist = this.index(exist);
|
|||
|
var nodes = this.normalize(add, this.nodes[exist]).reverse();
|
|||
|
|
|||
|
for (var _iterator6 = nodes, _isArray6 = Array.isArray(_iterator6), _i7 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
|
|||
|
var _ref6;
|
|||
|
|
|||
|
if (_isArray6) {
|
|||
|
if (_i7 >= _iterator6.length) break;
|
|||
|
_ref6 = _iterator6[_i7++];
|
|||
|
} else {
|
|||
|
_i7 = _iterator6.next();
|
|||
|
if (_i7.done) break;
|
|||
|
_ref6 = _i7.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref6;
|
|||
|
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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Removes node from the container and cleans the parent properties
|
|||
|
* from the node and its children.
|
|||
|
*
|
|||
|
* @param {Node|number} child Child or child’s index.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain
|
|||
|
*
|
|||
|
* @example
|
|||
|
* rule.nodes.length //=> 5
|
|||
|
* rule.removeChild(decl)
|
|||
|
* rule.nodes.length //=> 4
|
|||
|
* decl.parent //=> undefined
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Removes all children from the container
|
|||
|
* and cleans their parent properties.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* rule.removeAll()
|
|||
|
* rule.nodes.length //=> 0
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.removeAll = function removeAll() {
|
|||
|
for (var _iterator7 = this.nodes, _isArray7 = Array.isArray(_iterator7), _i8 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
|
|||
|
var _ref7;
|
|||
|
|
|||
|
if (_isArray7) {
|
|||
|
if (_i8 >= _iterator7.length) break;
|
|||
|
_ref7 = _iterator7[_i8++];
|
|||
|
} else {
|
|||
|
_i8 = _iterator7.next();
|
|||
|
if (_i8.done) break;
|
|||
|
_ref7 = _i8.value;
|
|||
|
}
|
|||
|
|
|||
|
var node = _ref7;
|
|||
|
node.parent = undefined;
|
|||
|
}
|
|||
|
|
|||
|
this.nodes = [];
|
|||
|
return this;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Passes all declaration values within the container that match pattern
|
|||
|
* through callback, replacing those values with the returned result
|
|||
|
* of callback.
|
|||
|
*
|
|||
|
* This method is useful if you are using a custom unit or function
|
|||
|
* and need to iterate through all values.
|
|||
|
*
|
|||
|
* @param {string|RegExp} pattern Replace pattern.
|
|||
|
* @param {object} opts Options to speed up the search.
|
|||
|
* @param {string|string[]} opts.props An array of property names.
|
|||
|
* @param {string} opts.fast String that’s used to narrow down
|
|||
|
* values and speed up the regexp search.
|
|||
|
* @param {function|string} callback String to replace pattern or callback
|
|||
|
* that returns a new value. The callback
|
|||
|
* will receive the same arguments
|
|||
|
* as those passed to a function parameter
|
|||
|
* of `String#replace`.
|
|||
|
*
|
|||
|
* @return {Node} This node for methods chain.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* root.replaceValues(/\d+rem/, { fast: 'rem' }, string => {
|
|||
|
* return 15 * parseInt(string) + 'px'
|
|||
|
* })
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* Returns `true` if callback returns `true`
|
|||
|
* for all of the container’s children.
|
|||
|
*
|
|||
|
* @param {childCondition} condition Iterator returns true or false.
|
|||
|
*
|
|||
|
* @return {boolean} Is every child pass condition.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const noPrefixes = rule.every(i => i.prop[0] !== '-')
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.every = function every(condition) {
|
|||
|
return this.nodes.every(condition);
|
|||
|
}
|
|||
|
/**
|
|||
|
* Returns `true` if callback returns `true` for (at least) one
|
|||
|
* of the container’s children.
|
|||
|
*
|
|||
|
* @param {childCondition} condition Iterator returns true or false.
|
|||
|
*
|
|||
|
* @return {boolean} Is some child pass condition.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const hasPrefix = rule.some(i => i.prop[0] === '-')
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.some = function some(condition) {
|
|||
|
return this.nodes.some(condition);
|
|||
|
}
|
|||
|
/**
|
|||
|
* Returns a `child`’s index within the {@link Container#nodes} array.
|
|||
|
*
|
|||
|
* @param {Node} child Child of the current container.
|
|||
|
*
|
|||
|
* @return {number} Child index.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* rule.index( rule.nodes[2] ) //=> 2
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_proto.index = function index(child) {
|
|||
|
if (typeof child === 'number') {
|
|||
|
return child;
|
|||
|
}
|
|||
|
|
|||
|
return this.nodes.indexOf(child);
|
|||
|
}
|
|||
|
/**
|
|||
|
* The container’s first child.
|
|||
|
*
|
|||
|
* @type {Node}
|
|||
|
*
|
|||
|
* @example
|
|||
|
* rule.first === rules.nodes[0]
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_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 = nodes, _isArray8 = Array.isArray(_iterator8), _i9 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
|
|||
|
var _ref8;
|
|||
|
|
|||
|
if (_isArray8) {
|
|||
|
if (_i9 >= _iterator8.length) break;
|
|||
|
_ref8 = _iterator8[_i9++];
|
|||
|
} else {
|
|||
|
_i9 = _iterator8.next();
|
|||
|
if (_i9.done) break;
|
|||
|
_ref8 = _i9.value;
|
|||
|
}
|
|||
|
|
|||
|
var i = _ref8;
|
|||
|
if (i.parent) i.parent.removeChild(i, 'ignore');
|
|||
|
}
|
|||
|
} else if (nodes.type === 'root') {
|
|||
|
nodes = nodes.nodes.slice(0);
|
|||
|
|
|||
|
for (var _iterator9 = nodes, _isArray9 = Array.isArray(_iterator9), _i10 = 0, _iterator9 = _isArray9 ? _iterator9 : _iterator9[Symbol.iterator]();;) {
|
|||
|
var _ref9;
|
|||
|
|
|||
|
if (_isArray9) {
|
|||
|
if (_i10 >= _iterator9.length) break;
|
|||
|
_ref9 = _iterator9[_i10++];
|
|||
|
} else {
|
|||
|
_i10 = _iterator9.next();
|
|||
|
if (_i10.done) break;
|
|||
|
_ref9 = _i10.value;
|
|||
|
}
|
|||
|
|
|||
|
var _i11 = _ref9;
|
|||
|
if (_i11.parent) _i11.parent.removeChild(_i11, '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;
|
|||
|
}
|
|||
|
/**
|
|||
|
* @memberof Container#
|
|||
|
* @member {Node[]} nodes An array containing the container’s children.
|
|||
|
*
|
|||
|
* @example
|
|||
|
* const root = postcss.parse('a { color: black }')
|
|||
|
* root.nodes.length //=> 1
|
|||
|
* root.nodes[0].selector //=> 'a'
|
|||
|
* root.nodes[0].nodes[0].prop //=> 'color'
|
|||
|
*/
|
|||
|
;
|
|||
|
|
|||
|
_createClass(Container, [{
|
|||
|
key: "first",
|
|||
|
get: function get() {
|
|||
|
if (!this.nodes) return undefined;
|
|||
|
return this.nodes[0];
|
|||
|
}
|
|||
|
/**
|
|||
|
* The container’s last child.
|
|||
|
*
|
|||
|
* @type {Node}
|
|||
|
*
|
|||
|
* @example
|
|||
|
* rule.last === rule.nodes[rule.nodes.length - 1]
|
|||
|
*/
|
|||
|
|
|||
|
}, {
|
|||
|
key: "last",
|
|||
|
get: function get() {
|
|||
|
if (!this.nodes) return undefined;
|
|||
|
return this.nodes[this.nodes.length - 1];
|
|||
|
}
|
|||
|
}]);
|
|||
|
|
|||
|
return Container;
|
|||
|
}(_node.default);
|
|||
|
|
|||
|
var _default = Container;
|
|||
|
/**
|
|||
|
* @callback childCondition
|
|||
|
* @param {Node} node Container child.
|
|||
|
* @param {number} index Child index.
|
|||
|
* @param {Node[]} nodes All container children.
|
|||
|
* @return {boolean}
|
|||
|
*/
|
|||
|
|
|||
|
/**
|
|||
|
* @callback childIterator
|
|||
|
* @param {Node} node Container child.
|
|||
|
* @param {number} index Child index.
|
|||
|
* @return {false|undefined} Returning `false` will break iteration.
|
|||
|
*/
|
|||
|
|
|||
|
exports.default = _default;
|
|||
|
module.exports = exports.default;
|
|||
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNvbnRhaW5lci5lczYiXSwibmFtZXMiOlsiY2xlYW5Tb3VyY2UiLCJub2RlcyIsIm1hcCIsImkiLCJzb3VyY2UiLCJDb250YWluZXIiLCJwdXNoIiwiY2hpbGQiLCJwYXJlbnQiLCJlYWNoIiwiY2FsbG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