3 Object.defineProperty(exports, "__esModule", {
7 var _Node = require('./Node');
9 var _Node2 = _interopRequireDefault(_Node);
11 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13 function Container(opts) {
16 this.constructor(opts);
18 this.nodes = opts.nodes;
20 if (this.after === undefined) {
21 this.after = this.nodes.length > 0 ? this.nodes[this.nodes.length - 1].after : '';
24 if (this.before === undefined) {
25 this.before = this.nodes.length > 0 ? this.nodes[0].before : '';
28 if (this.sourceIndex === undefined) {
29 this.sourceIndex = this.before.length;
32 this.nodes.forEach(function (node) {
33 node.parent = _this; // eslint-disable-line no-param-reassign
36 * A node that contains other nodes and support traversing over them
\r
39 Container.prototype = Object.create(_Node2.default.prototype);
40 Container.constructor = _Node2.default;
43 * Iterate over descendant nodes of the node
\r
45 * @param {RegExp|string} filter - Optional. Only nodes with node.type that
\r
46 * satisfies the filter will be traversed over
\r
47 * @param {function} cb - callback to call on each node. Takes theese params:
\r
48 * node - the node being processed, i - it's index, nodes - the array
\r
50 * If false is returned, the iteration breaks
\r
52 * @return (boolean) false, if the iteration was broken
\r
54 Container.prototype.walk = function walk(filter, cb) {
55 var hasFilter = typeof filter === 'string' || filter instanceof RegExp;
56 var callback = hasFilter ? cb : filter;
57 var filterReg = typeof filter === 'string' ? new RegExp(filter) : filter;
59 for (var i = 0; i < this.nodes.length; i++) {
60 var node = this.nodes[i];
61 var filtered = hasFilter ? filterReg.test(node.type) : true;
62 if (filtered && callback && callback(node, i, this.nodes) === false) {
65 if (node.nodes && node.walk(filter, cb) === false) {
73 * Iterate over immediate children of the node
\r
75 * @param {function} cb - callback to call on each node. Takes theese params:
\r
76 * node - the node being processed, i - it's index, nodes - the array
\r
78 * If false is returned, the iteration breaks
\r
80 * @return (boolean) false, if the iteration was broken
\r
82 Container.prototype.each = function each() {
83 var cb = arguments.length <= 0 || arguments[0] === undefined ? function () {} : arguments[0];
85 for (var i = 0; i < this.nodes.length; i++) {
86 var node = this.nodes[i];
87 if (cb(node, i, this.nodes) === false) {
94 exports.default = Container;