selector.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*!
  2. * Stylus - Selector
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var Block = require('./block')
  10. , Node = require('./node');
  11. /**
  12. * Initialize a new `Selector` with the given `segs`.
  13. *
  14. * @param {Array} segs
  15. * @api public
  16. */
  17. var Selector = module.exports = function Selector(segs){
  18. Node.call(this);
  19. this.inherits = true;
  20. this.segments = segs;
  21. this.optional = false;
  22. };
  23. /**
  24. * Inherit from `Node.prototype`.
  25. */
  26. Selector.prototype.__proto__ = Node.prototype;
  27. /**
  28. * Return the selector string.
  29. *
  30. * @return {String}
  31. * @api public
  32. */
  33. Selector.prototype.toString = function(){
  34. return this.segments.join('') + (this.optional ? ' !optional' : '');
  35. };
  36. /**
  37. * Check if this is placeholder selector.
  38. *
  39. * @return {Boolean}
  40. * @api public
  41. */
  42. Selector.prototype.__defineGetter__('isPlaceholder', function(){
  43. return this.val && ~this.val.substr(0, 2).indexOf('$');
  44. });
  45. /**
  46. * Return a clone of this node.
  47. *
  48. * @return {Node}
  49. * @api public
  50. */
  51. Selector.prototype.clone = function(parent){
  52. var clone = new Selector;
  53. clone.lineno = this.lineno;
  54. clone.column = this.column;
  55. clone.filename = this.filename;
  56. clone.inherits = this.inherits;
  57. clone.val = this.val;
  58. clone.segments = this.segments.map(function(node){ return node.clone(parent, clone); });
  59. clone.optional = this.optional;
  60. return clone;
  61. };
  62. /**
  63. * Return a JSON representation of this node.
  64. *
  65. * @return {Object}
  66. * @api public
  67. */
  68. Selector.prototype.toJSON = function(){
  69. return {
  70. __type: 'Selector',
  71. inherits: this.inherits,
  72. segments: this.segments,
  73. optional: this.optional,
  74. val: this.val,
  75. lineno: this.lineno,
  76. column: this.column,
  77. filename: this.filename
  78. };
  79. };