selector-parser.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*!
  2. * Stylus - Selector Parser
  3. * Copyright (c) Automattic <developer.wordpress.com>
  4. * MIT Licensed
  5. */
  6. var COMBINATORS = ['>', '+', '~'];
  7. /**
  8. * Initialize a new `SelectorParser`
  9. * with the given `str` and selectors `stack`.
  10. *
  11. * @param {String} str
  12. * @param {Array} stack
  13. * @param {Array} parts
  14. * @api private
  15. */
  16. var SelectorParser = module.exports = function SelectorParser(str, stack, parts) {
  17. this.str = str;
  18. this.stack = stack || [];
  19. this.parts = parts || [];
  20. this.pos = 0;
  21. this.level = 2;
  22. this.nested = true;
  23. this.ignore = false;
  24. };
  25. /**
  26. * Consume the given `len` and move current position.
  27. *
  28. * @param {Number} len
  29. * @api private
  30. */
  31. SelectorParser.prototype.skip = function(len) {
  32. this.str = this.str.substr(len);
  33. this.pos += len;
  34. };
  35. /**
  36. * Consume spaces.
  37. */
  38. SelectorParser.prototype.skipSpaces = function() {
  39. while (' ' == this.str[0]) this.skip(1);
  40. };
  41. /**
  42. * Fetch next token.
  43. *
  44. * @return {String}
  45. * @api private
  46. */
  47. SelectorParser.prototype.advance = function() {
  48. return this.root()
  49. || this.relative()
  50. || this.initial()
  51. || this.escaped()
  52. || this.parent()
  53. || this.partial()
  54. || this.char();
  55. };
  56. /**
  57. * '/'
  58. */
  59. SelectorParser.prototype.root = function() {
  60. if (!this.pos && '/' == this.str[0]
  61. && 'deep' != this.str.slice(1, 5)) {
  62. this.nested = false;
  63. this.skip(1);
  64. }
  65. };
  66. /**
  67. * '../'
  68. */
  69. SelectorParser.prototype.relative = function(multi) {
  70. if ((!this.pos || multi) && '../' == this.str.slice(0, 3)) {
  71. this.nested = false;
  72. this.skip(3);
  73. while (this.relative(true)) this.level++;
  74. if (!this.raw) {
  75. var ret = this.stack[this.stack.length - this.level];
  76. if (ret) {
  77. return ret;
  78. } else {
  79. this.ignore = true;
  80. }
  81. }
  82. }
  83. };
  84. /**
  85. * '~/'
  86. */
  87. SelectorParser.prototype.initial = function() {
  88. if (!this.pos && '~' == this.str[0] && '/' == this.str[1]) {
  89. this.nested = false;
  90. this.skip(2);
  91. return this.stack[0];
  92. }
  93. };
  94. /**
  95. * '\' ('&' | '^')
  96. */
  97. SelectorParser.prototype.escaped = function() {
  98. if ('\\' == this.str[0]) {
  99. var char = this.str[1];
  100. if ('&' == char || '^' == char) {
  101. this.skip(2);
  102. return char;
  103. }
  104. }
  105. };
  106. /**
  107. * '&'
  108. */
  109. SelectorParser.prototype.parent = function() {
  110. if ('&' == this.str[0]) {
  111. this.nested = false;
  112. if (!this.pos && (!this.stack.length || this.raw)) {
  113. var i = 0;
  114. while (' ' == this.str[++i]) ;
  115. if (~COMBINATORS.indexOf(this.str[i])) {
  116. this.skip(i + 1);
  117. return;
  118. }
  119. }
  120. this.skip(1);
  121. if (!this.raw)
  122. return this.stack[this.stack.length - 1];
  123. }
  124. };
  125. /**
  126. * '^[' range ']'
  127. */
  128. SelectorParser.prototype.partial = function() {
  129. if ('^' == this.str[0] && '[' == this.str[1]) {
  130. this.skip(2);
  131. this.skipSpaces();
  132. var ret = this.range();
  133. this.skipSpaces();
  134. if (']' != this.str[0]) return '^[';
  135. this.nested = false;
  136. this.skip(1);
  137. if (ret) {
  138. return ret;
  139. } else {
  140. this.ignore = true;
  141. }
  142. }
  143. };
  144. /**
  145. * '-'? 0-9+
  146. */
  147. SelectorParser.prototype.number = function() {
  148. var i = 0, ret = '';
  149. if ('-' == this.str[i])
  150. ret += this.str[i++];
  151. while (this.str.charCodeAt(i) >= 48
  152. && this.str.charCodeAt(i) <= 57)
  153. ret += this.str[i++];
  154. if (ret) {
  155. this.skip(i);
  156. return Number(ret);
  157. }
  158. };
  159. /**
  160. * number ('..' number)?
  161. */
  162. SelectorParser.prototype.range = function() {
  163. var start = this.number()
  164. , ret;
  165. if ('..' == this.str.slice(0, 2)) {
  166. this.skip(2);
  167. var end = this.number()
  168. , len = this.parts.length;
  169. if (start < 0) start = len + start - 1;
  170. if (end < 0) end = len + end - 1;
  171. if (start > end) {
  172. var tmp = start;
  173. start = end;
  174. end = tmp;
  175. }
  176. if (end < len - 1) {
  177. ret = this.parts.slice(start, end + 1).map(function(part) {
  178. var selector = new SelectorParser(part, this.stack, this.parts);
  179. selector.raw = true;
  180. return selector.parse();
  181. }, this).map(function(selector) {
  182. return (selector.nested ? ' ' : '') + selector.val;
  183. }).join('').trim();
  184. }
  185. } else {
  186. ret = this.stack[
  187. start < 0 ? this.stack.length + start - 1 : start
  188. ];
  189. }
  190. if (ret) {
  191. return ret;
  192. } else {
  193. this.ignore = true;
  194. }
  195. };
  196. /**
  197. * .+
  198. */
  199. SelectorParser.prototype.char = function() {
  200. var char = this.str[0];
  201. this.skip(1);
  202. return char;
  203. };
  204. /**
  205. * Parses the selector.
  206. *
  207. * @return {Object}
  208. * @api private
  209. */
  210. SelectorParser.prototype.parse = function() {
  211. var val = '';
  212. while (this.str.length) {
  213. val += this.advance() || '';
  214. if (this.ignore) {
  215. val = '';
  216. break;
  217. }
  218. }
  219. return { val: val.trimRight(), nested: this.nested };
  220. };