sourceIndex.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. var _helpers = require("./util/helpers");
  3. (0, _helpers.test)('universal selector', '*', function (t, tree) {
  4. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  5. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 1);
  6. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  7. });
  8. (0, _helpers.test)('lobotomized owl selector', ' * + * ', function (t, tree) {
  9. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 2);
  10. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 2);
  11. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 1);
  12. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 4);
  13. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 4);
  14. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 3);
  15. t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 6);
  16. t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 6);
  17. t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 5);
  18. });
  19. (0, _helpers.test)('comment', '/**\n * Hello!\n */', function (t, tree) {
  20. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  21. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 3);
  22. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  23. });
  24. (0, _helpers.test)('comment & universal selectors', '*/*test*/*', function (t, tree) {
  25. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  26. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 1);
  27. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  28. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 2);
  29. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 9);
  30. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 1);
  31. t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 10);
  32. t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 10);
  33. t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 9);
  34. });
  35. (0, _helpers.test)('tag selector', 'h1', function (t, tree) {
  36. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  37. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 2);
  38. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  39. });
  40. (0, _helpers.test)('id selector', '#id', function (t, tree) {
  41. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  42. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 3);
  43. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  44. });
  45. (0, _helpers.test)('tag selector followed by id selector', 'h1, #id', function (t, tree) {
  46. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  47. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 2);
  48. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  49. t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 5);
  50. t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 7);
  51. t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 4);
  52. });
  53. (0, _helpers.test)('multiple id selectors', '#one#two', function (t, tree) {
  54. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  55. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 4);
  56. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  57. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5);
  58. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 8);
  59. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4);
  60. });
  61. (0, _helpers.test)('multiple id selectors (2)', '#one#two#three#four', function (t, tree) {
  62. t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 9);
  63. t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 14);
  64. t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 8);
  65. t.deepEqual(tree.nodes[0].nodes[3].source.start.column, 15);
  66. t.deepEqual(tree.nodes[0].nodes[3].source.end.column, 19);
  67. t.deepEqual(tree.nodes[0].nodes[3].sourceIndex, 14);
  68. });
  69. (0, _helpers.test)('multiple id selectors (3)', '#one#two,#three#four', function (t, tree) {
  70. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5);
  71. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 8);
  72. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4);
  73. t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 16);
  74. t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 20);
  75. t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 15);
  76. });
  77. (0, _helpers.test)('multiple class selectors', '.one.two,.three.four', function (t, tree) {
  78. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5);
  79. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 8);
  80. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4);
  81. t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 16);
  82. t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 20);
  83. t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 15);
  84. });
  85. (0, _helpers.test)('attribute selector', '[name="james"]', function (t, tree) {
  86. t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1);
  87. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  88. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 14);
  89. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  90. });
  91. (0, _helpers.test)('multiple attribute selectors', '[name="james"][name="ed"],[name="snakeman"][name="a"]', function (t, tree) {
  92. t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1);
  93. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  94. t.deepEqual(tree.nodes[0].nodes[0].source.end.line, 1);
  95. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 14);
  96. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  97. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
  98. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 15);
  99. t.deepEqual(tree.nodes[0].nodes[1].source.end.line, 1);
  100. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 25);
  101. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 14);
  102. t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 1);
  103. t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 27);
  104. t.deepEqual(tree.nodes[1].nodes[0].source.end.line, 1);
  105. t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 43);
  106. t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 26);
  107. t.deepEqual(tree.nodes[1].nodes[1].source.start.line, 1);
  108. t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 44);
  109. t.deepEqual(tree.nodes[1].nodes[1].source.end.line, 1);
  110. t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 53);
  111. t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 43);
  112. });
  113. (0, _helpers.test)('pseudo-class', 'h1:first-child', function (t, tree) {
  114. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
  115. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
  116. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 14);
  117. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
  118. });
  119. (0, _helpers.test)('pseudo-class with argument', 'h1:not(.strudel, .food)', function (t, tree) {
  120. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
  121. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
  122. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 23);
  123. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
  124. });
  125. (0, _helpers.test)('pseudo-element', 'h1::before', function (t, tree) {
  126. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
  127. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
  128. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 10);
  129. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
  130. });
  131. (0, _helpers.test)('multiple pseudos', 'h1:not(.food)::before, a:first-child', function (t, tree) {
  132. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1);
  133. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3);
  134. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 13);
  135. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2);
  136. t.deepEqual(tree.nodes[0].nodes[2].source.start.line, 1);
  137. t.deepEqual(tree.nodes[0].nodes[2].source.start.column, 14);
  138. t.deepEqual(tree.nodes[0].nodes[2].source.end.column, 21);
  139. t.deepEqual(tree.nodes[0].nodes[2].sourceIndex, 13);
  140. t.deepEqual(tree.nodes[1].nodes[1].source.start.line, 1);
  141. t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 25);
  142. t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 36);
  143. t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 24);
  144. });
  145. (0, _helpers.test)('combinators', 'div > h1 span', function (t, tree) {
  146. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1, "> start line");
  147. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 5, "> start column");
  148. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 5, "> end column");
  149. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 4, "> sourceIndex");
  150. t.deepEqual(tree.nodes[0].nodes[3].source.start.line, 1, "' ' start line");
  151. t.deepEqual(tree.nodes[0].nodes[3].source.start.column, 9, "' ' start column");
  152. t.deepEqual(tree.nodes[0].nodes[3].source.end.column, 9, "' ' end column");
  153. t.deepEqual(tree.nodes[0].nodes[3].sourceIndex, 8, "' ' sourceIndex");
  154. });
  155. (0, _helpers.test)('combinators surrounded by superfluous spaces', 'div > h1 ~ span a', function (t, tree) {
  156. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1, "> start line");
  157. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 7, "> start column");
  158. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 7, "> end column");
  159. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 6, "> sourceIndex");
  160. t.deepEqual(tree.nodes[0].nodes[3].source.start.line, 1, "~ start line");
  161. t.deepEqual(tree.nodes[0].nodes[3].source.start.column, 13, "~ start column");
  162. t.deepEqual(tree.nodes[0].nodes[3].source.end.column, 13, "~ end column");
  163. t.deepEqual(tree.nodes[0].nodes[3].sourceIndex, 12, "~ sourceIndex");
  164. t.deepEqual(tree.nodes[0].nodes[5].source.start.line, 1, "' ' start line");
  165. t.deepEqual(tree.nodes[0].nodes[5].source.start.column, 21, "' ' start column");
  166. t.deepEqual(tree.nodes[0].nodes[5].source.end.column, 23, "' ' end column");
  167. t.deepEqual(tree.nodes[0].nodes[5].sourceIndex, 20, "' ' sourceIndex");
  168. });
  169. (0, _helpers.test)('multiple id selectors on different lines', '#one,\n#two', function (t, tree) {
  170. t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1);
  171. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1);
  172. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 4);
  173. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0);
  174. t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 2);
  175. t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 1);
  176. t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 4);
  177. t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 6);
  178. });
  179. (0, _helpers.test)('multiple id selectors on different CRLF lines', '#one,\r\n#two,\r\n#three', function (t, tree) {
  180. t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1, '#one start line');
  181. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 1, '#one start column');
  182. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 4, '#one end column');
  183. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 0, '#one sourceIndex');
  184. t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 2, '#two start line');
  185. t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 1, '#two start column');
  186. t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 4, '#two end column');
  187. t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 7, '#two sourceIndex');
  188. t.deepEqual(tree.nodes[2].nodes[0].source.start.line, 3, '#three start line');
  189. t.deepEqual(tree.nodes[2].nodes[0].source.start.column, 1, '#three start column');
  190. t.deepEqual(tree.nodes[2].nodes[0].source.end.column, 6, '#three end column');
  191. t.deepEqual(tree.nodes[2].nodes[0].sourceIndex, 14, '#three sourceIndex');
  192. });
  193. (0, _helpers.test)('id, tag, pseudo, and class selectors on different lines with indentation', '\t#one,\n\th1:after,\n\t\t.two', function (t, tree) {
  194. t.deepEqual(tree.nodes[0].nodes[0].source.start.line, 1, '#one start line');
  195. t.deepEqual(tree.nodes[0].nodes[0].source.start.column, 2, '#one start column');
  196. t.deepEqual(tree.nodes[0].nodes[0].source.end.column, 5, '#one end column');
  197. t.deepEqual(tree.nodes[0].nodes[0].sourceIndex, 1, '#one sourceIndex');
  198. t.deepEqual(tree.nodes[1].nodes[0].source.start.line, 2, 'h1 start line');
  199. t.deepEqual(tree.nodes[1].nodes[0].source.start.column, 2, 'h1 start column');
  200. t.deepEqual(tree.nodes[1].nodes[0].source.end.column, 3, 'h1 end column');
  201. t.deepEqual(tree.nodes[1].nodes[0].sourceIndex, 8, 'h1 sourceIndex');
  202. t.deepEqual(tree.nodes[1].nodes[1].source.start.line, 2, ':after start line');
  203. t.deepEqual(tree.nodes[1].nodes[1].source.start.column, 4, ':after start column');
  204. t.deepEqual(tree.nodes[1].nodes[1].source.end.column, 9, ':after end column');
  205. t.deepEqual(tree.nodes[1].nodes[1].sourceIndex, 10, ':after sourceIndex');
  206. t.deepEqual(tree.nodes[2].nodes[0].source.start.line, 3, '.two start line');
  207. t.deepEqual(tree.nodes[2].nodes[0].source.start.column, 3, '.two start column');
  208. t.deepEqual(tree.nodes[2].nodes[0].source.end.column, 6, '.two end column');
  209. t.deepEqual(tree.nodes[2].nodes[0].sourceIndex, 20, '.two sourceIndex');
  210. });
  211. (0, _helpers.test)('pseudo with arguments spanning multiple lines', 'h1:not(\n\t.one,\n\t.two\n)', function (t, tree) {
  212. t.deepEqual(tree.nodes[0].nodes[1].source.start.line, 1, ':not start line');
  213. t.deepEqual(tree.nodes[0].nodes[1].source.start.column, 3, ':not start column');
  214. t.deepEqual(tree.nodes[0].nodes[1].source.end.line, 4, ':not end line');
  215. t.deepEqual(tree.nodes[0].nodes[1].source.end.column, 1, ':not end column');
  216. t.deepEqual(tree.nodes[0].nodes[1].sourceIndex, 2, ':not sourceIndex');
  217. t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.start.line, 2, '.one start line');
  218. t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.start.column, 2, '.one start column');
  219. t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.end.line, 2, '.one end line');
  220. t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].source.end.column, 5, '.one end column');
  221. t.deepEqual(tree.nodes[0].nodes[1].nodes[0].nodes[0].sourceIndex, 9, '.one sourceIndex');
  222. t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.start.line, 3, '.two start line');
  223. t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.start.column, 2, '.two start column');
  224. t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.end.line, 3, '.two end line');
  225. t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].source.end.column, 5, '.two end column');
  226. t.deepEqual(tree.nodes[0].nodes[1].nodes[1].nodes[0].sourceIndex, 16, '.two sourceIndex');
  227. });