root.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. * Stylus - Root
  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 `Root` node.
  12. *
  13. * @api public
  14. */
  15. var Root = module.exports = function Root(){
  16. this.nodes = [];
  17. };
  18. /**
  19. * Inherit from `Node.prototype`.
  20. */
  21. Root.prototype.__proto__ = Node.prototype;
  22. /**
  23. * Push a `node` to this block.
  24. *
  25. * @param {Node} node
  26. * @api public
  27. */
  28. Root.prototype.push = function(node){
  29. this.nodes.push(node);
  30. };
  31. /**
  32. * Unshift a `node` to this block.
  33. *
  34. * @param {Node} node
  35. * @api public
  36. */
  37. Root.prototype.unshift = function(node){
  38. this.nodes.unshift(node);
  39. };
  40. /**
  41. * Return a clone of this node.
  42. *
  43. * @return {Node}
  44. * @api public
  45. */
  46. Root.prototype.clone = function(){
  47. var clone = new Root();
  48. clone.lineno = this.lineno;
  49. clone.column = this.column;
  50. clone.filename = this.filename;
  51. this.nodes.forEach(function(node){
  52. clone.push(node.clone(clone, clone));
  53. });
  54. return clone;
  55. };
  56. /**
  57. * Return "root".
  58. *
  59. * @return {String}
  60. * @api public
  61. */
  62. Root.prototype.toString = function(){
  63. return '[Root]';
  64. };
  65. /**
  66. * Return a JSON representation of this node.
  67. *
  68. * @return {Object}
  69. * @api public
  70. */
  71. Root.prototype.toJSON = function(){
  72. return {
  73. __type: 'Root',
  74. nodes: this.nodes,
  75. lineno: this.lineno,
  76. column: this.column,
  77. filename: this.filename
  78. };
  79. };