Container.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _Node = require('./Node');
  6. var _Node2 = _interopRequireDefault(_Node);
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function Container(opts) {
  9. var _this = this;
  10. this.constructor(opts);
  11. this.nodes = opts.nodes;
  12. if (this.after === undefined) {
  13. this.after = this.nodes.length > 0 ? this.nodes[this.nodes.length - 1].after : '';
  14. }
  15. if (this.before === undefined) {
  16. this.before = this.nodes.length > 0 ? this.nodes[0].before : '';
  17. }
  18. if (this.sourceIndex === undefined) {
  19. this.sourceIndex = this.before.length;
  20. }
  21. this.nodes.forEach(function (node) {
  22. node.parent = _this; // eslint-disable-line no-param-reassign
  23. });
  24. } /**
  25. * A node that contains other nodes and support traversing over them
  26. */
  27. Container.prototype = Object.create(_Node2.default.prototype);
  28. Container.constructor = _Node2.default;
  29. /**
  30. * Iterate over descendant nodes of the node
  31. *
  32. * @param {RegExp|string} filter - Optional. Only nodes with node.type that
  33. * satisfies the filter will be traversed over
  34. * @param {function} cb - callback to call on each node. Takes theese params:
  35. * node - the node being processed, i - it's index, nodes - the array
  36. * of all nodes
  37. * If false is returned, the iteration breaks
  38. *
  39. * @return (boolean) false, if the iteration was broken
  40. */
  41. Container.prototype.walk = function walk(filter, cb) {
  42. var hasFilter = typeof filter === 'string' || filter instanceof RegExp;
  43. var callback = hasFilter ? cb : filter;
  44. var filterReg = typeof filter === 'string' ? new RegExp(filter) : filter;
  45. for (var i = 0; i < this.nodes.length; i++) {
  46. var node = this.nodes[i];
  47. var filtered = hasFilter ? filterReg.test(node.type) : true;
  48. if (filtered && callback && callback(node, i, this.nodes) === false) {
  49. return false;
  50. }
  51. if (node.nodes && node.walk(filter, cb) === false) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. };
  57. /**
  58. * Iterate over immediate children of the node
  59. *
  60. * @param {function} cb - callback to call on each node. Takes theese params:
  61. * node - the node being processed, i - it's index, nodes - the array
  62. * of all nodes
  63. * If false is returned, the iteration breaks
  64. *
  65. * @return (boolean) false, if the iteration was broken
  66. */
  67. Container.prototype.each = function each() {
  68. var cb = arguments.length <= 0 || arguments[0] === undefined ? function () {} : arguments[0];
  69. for (var i = 0; i < this.nodes.length; i++) {
  70. var node = this.nodes[i];
  71. if (cb(node, i, this.nodes) === false) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. };
  77. exports.default = Container;