query-list.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*!
  2. * Stylus - QueryList
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Node = require('./node');
  10. /**
  11. * Initialize a new `QueryList`.
  12. *
  13. * @api public
  14. */
  15. var QueryList = module.exports = function QueryList(){
  16. Node.call(this);
  17. this.nodes = [];
  18. };
  19. /**
  20. * Inherit from `Node.prototype`.
  21. */
  22. QueryList.prototype.__proto__ = Node.prototype;
  23. /**
  24. * Return a clone of this node.
  25. *
  26. * @return {Node}
  27. * @api public
  28. */
  29. QueryList.prototype.clone = function(parent){
  30. var clone = new QueryList;
  31. clone.lineno = this.lineno;
  32. clone.column = this.column;
  33. clone.filename = this.filename;
  34. for (var i = 0; i < this.nodes.length; ++i) {
  35. clone.push(this.nodes[i].clone(parent, clone));
  36. }
  37. return clone;
  38. };
  39. /**
  40. * Push the given `node`.
  41. *
  42. * @param {Node} node
  43. * @api public
  44. */
  45. QueryList.prototype.push = function(node){
  46. this.nodes.push(node);
  47. };
  48. /**
  49. * Merges this query list with the `other`.
  50. *
  51. * @param {QueryList} other
  52. * @return {QueryList}
  53. * @api private
  54. */
  55. QueryList.prototype.merge = function(other){
  56. var list = new QueryList
  57. , merged;
  58. this.nodes.forEach(function(query){
  59. for (var i = 0, len = other.nodes.length; i < len; ++i){
  60. merged = query.merge(other.nodes[i]);
  61. if (merged) list.push(merged);
  62. }
  63. });
  64. return list;
  65. };
  66. /**
  67. * Return "<a>, <b>, <c>"
  68. *
  69. * @return {String}
  70. * @api public
  71. */
  72. QueryList.prototype.toString = function(){
  73. return '(' + this.nodes.map(function(node){
  74. return node.toString();
  75. }).join(', ') + ')';
  76. };
  77. /**
  78. * Return a JSON representation of this node.
  79. *
  80. * @return {Object}
  81. * @api public
  82. */
  83. QueryList.prototype.toJSON = function(){
  84. return {
  85. __type: 'QueryList',
  86. nodes: this.nodes,
  87. lineno: this.lineno,
  88. column: this.column,
  89. filename: this.filename
  90. };
  91. };