combinators.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. var _types = require("../selectors/types");
  3. var _helpers = require("./util/helpers");
  4. (0, _helpers.test)('multiple combinating spaces', 'h1 h2', function (t, tree) {
  5. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  6. t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
  7. t.deepEqual(tree.nodes[0].nodes[1].toString(), ' ');
  8. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  9. });
  10. (0, _helpers.test)('column combinator', '.selected||td', function (t, tree) {
  11. t.deepEqual(tree.nodes[0].nodes[0].value, 'selected');
  12. t.deepEqual(tree.nodes[0].nodes[1].value, '||');
  13. t.deepEqual(tree.nodes[0].nodes[2].value, 'td');
  14. });
  15. (0, _helpers.test)('column combinator (2)', '.selected || td', function (t, tree) {
  16. t.deepEqual(tree.nodes[0].nodes[0].value, 'selected');
  17. t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
  18. t.deepEqual(tree.nodes[0].nodes[1].value, '||');
  19. t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
  20. t.deepEqual(tree.nodes[0].nodes[2].value, 'td');
  21. });
  22. (0, _helpers.test)('descendant combinator', 'h1 h2', function (t, tree) {
  23. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  24. t.deepEqual(tree.nodes[0].nodes[1].value, ' ');
  25. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  26. });
  27. (0, _helpers.test)('multiple descendant combinators', 'h1 h2 h3 h4', function (t, tree) {
  28. t.deepEqual(tree.nodes[0].nodes[1].value, ' ', 'should have a combinator');
  29. t.deepEqual(tree.nodes[0].nodes[3].value, ' ', 'should have a combinator');
  30. t.deepEqual(tree.nodes[0].nodes[5].value, ' ', 'should have a combinator');
  31. });
  32. (0, _helpers.test)('adjacent sibling combinator', 'h1~h2', function (t, tree) {
  33. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  34. t.deepEqual(tree.nodes[0].nodes[1].value, '~');
  35. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  36. });
  37. (0, _helpers.test)('adjacent sibling combinator (2)', 'h1 ~h2', function (t, tree) {
  38. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  39. t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
  40. t.deepEqual(tree.nodes[0].nodes[1].value, '~');
  41. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  42. });
  43. (0, _helpers.test)('adjacent sibling combinator (3)', 'h1~ h2', function (t, tree) {
  44. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  45. t.deepEqual(tree.nodes[0].nodes[1].value, '~');
  46. t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
  47. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  48. });
  49. (0, _helpers.test)('adjacent sibling combinator (4)', 'h1 ~ h2', function (t, tree) {
  50. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  51. t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
  52. t.deepEqual(tree.nodes[0].nodes[1].value, '~');
  53. t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
  54. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  55. });
  56. (0, _helpers.test)('adjacent sibling combinator (5)', 'h1~h2~h3', function (t, tree) {
  57. t.deepEqual(tree.nodes[0].nodes[0].value, 'h1');
  58. t.deepEqual(tree.nodes[0].nodes[1].value, '~');
  59. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  60. t.deepEqual(tree.nodes[0].nodes[3].value, '~');
  61. t.deepEqual(tree.nodes[0].nodes[4].value, 'h3');
  62. });
  63. (0, _helpers.test)('piercing combinator', '.a >>> .b', function (t, tree) {
  64. t.deepEqual(tree.nodes[0].nodes[0].value, 'a');
  65. t.deepEqual(tree.nodes[0].nodes[1].spaces.before, ' ');
  66. t.deepEqual(tree.nodes[0].nodes[1].value, '>>>');
  67. t.deepEqual(tree.nodes[0].nodes[1].spaces.after, ' ');
  68. t.deepEqual(tree.nodes[0].nodes[2].value, 'b');
  69. });
  70. (0, _helpers.test)('named combinators', 'a /deep/ b', function (t, tree) {
  71. var nodes = tree.nodes[0].nodes;
  72. t.deepEqual(nodes[0].value, 'a');
  73. t.deepEqual(nodes[1].type, _types.COMBINATOR);
  74. t.deepEqual(nodes[1].toString(), ' /deep/ ');
  75. t.deepEqual(nodes[1].value, '/deep/');
  76. t.deepEqual(nodes[2].value, 'b');
  77. });
  78. (0, _helpers.test)('named combinators with escapes', 'a /dee\\p/ b', function (t, tree) {
  79. var nodes = tree.nodes[0].nodes;
  80. t.deepEqual(nodes[0].value, 'a');
  81. t.deepEqual(nodes[1].type, _types.COMBINATOR);
  82. t.deepEqual(nodes[1].toString(), ' /dee\\p/ ');
  83. t.deepEqual(nodes[1].value, '/deep/');
  84. t.deepEqual(nodes[2].value, 'b');
  85. });
  86. (0, _helpers.test)('named combinators with escapes and uppercase', 'a /DeE\\p/ b', function (t, tree) {
  87. var nodes = tree.nodes[0].nodes;
  88. t.deepEqual(nodes[0].value, 'a');
  89. t.deepEqual(nodes[1].type, _types.COMBINATOR);
  90. t.deepEqual(nodes[1].toString(), ' /DeE\\p/ ');
  91. t.deepEqual(nodes[1].value, '/deep/');
  92. t.deepEqual(nodes[2].value, 'b');
  93. });
  94. (0, _helpers.test)('multiple combinators', 'h1~h2>h3', function (t, tree) {
  95. t.deepEqual(tree.nodes[0].nodes[1].value, '~', 'should have a combinator');
  96. t.deepEqual(tree.nodes[0].nodes[3].value, '>', 'should have a combinator');
  97. });
  98. (0, _helpers.test)('multiple combinators with whitespaces', 'h1 + h2 > h3', function (t, tree) {
  99. t.deepEqual(tree.nodes[0].nodes[1].value, '+', 'should have a combinator');
  100. t.deepEqual(tree.nodes[0].nodes[3].value, '>', 'should have a combinator');
  101. });
  102. (0, _helpers.test)('multiple combinators with whitespaces (2)', 'h1+ h2 >h3', function (t, tree) {
  103. t.deepEqual(tree.nodes[0].nodes[1].value, '+', 'should have a combinator');
  104. t.deepEqual(tree.nodes[0].nodes[3].value, '>', 'should have a combinator');
  105. });
  106. (0, _helpers.test)('trailing combinator & spaces', 'p + ', function (t, tree) {
  107. t.deepEqual(tree.nodes[0].nodes[0].value, 'p', 'should be a paragraph');
  108. t.deepEqual(tree.nodes[0].nodes[1].value, '+', 'should have a combinator');
  109. });
  110. (0, _helpers.test)('trailing sibling combinator', 'p ~', function (t, tree) {
  111. t.deepEqual(tree.nodes[0].nodes[0].value, 'p', 'should be a paragraph');
  112. t.deepEqual(tree.nodes[0].nodes[1].value, '~', 'should have a combinator');
  113. });
  114. (0, _helpers.test)('ending in comment has no trailing combinator', ".bar /* comment 3 */", function (t, tree) {
  115. var nodeTypes = tree.nodes[0].map(function (n) {
  116. return n.type;
  117. });
  118. t.deepEqual(nodeTypes, ["class"]);
  119. });
  120. (0, _helpers.test)('The combinating space is not a space character', ".bar\n.baz", function (t, tree) {
  121. var nodeTypes = tree.nodes[0].map(function (n) {
  122. return n.type;
  123. });
  124. t.deepEqual(nodeTypes, ["class", "combinator", "class"]);
  125. t.deepEqual(tree.nodes[0].nodes[1].value, ' ', 'should have a combinator');
  126. t.deepEqual(tree.nodes[0].nodes[1].raws.value, '\n', 'should have a raw combinator value');
  127. });
  128. (0, _helpers.test)('with spaces and a comment has only one combinator', ".bar /* comment 3 */ > .foo", function (t, tree) {
  129. var nodeTypes = tree.nodes[0].map(function (n) {
  130. return n.type;
  131. });
  132. t.deepEqual(nodeTypes, ["class", "combinator", "class"]);
  133. });
  134. (0, _helpers.test)('with a meaningful comment in the middle of a compound selector', "div/* wtf */.foo", function (t, tree) {
  135. var nodeTypes = tree.nodes[0].map(function (n) {
  136. return n.type;
  137. });
  138. t.deepEqual(nodeTypes, ["tag", "comment", "class"]);
  139. });
  140. (0, _helpers.test)('with a comment in the middle of a descendant selector', "div/* wtf */ .foo", function (t, tree) {
  141. var nodeTypes = tree.nodes[0].map(function (n) {
  142. return n.type;
  143. });
  144. t.deepEqual(nodeTypes, ["tag", "comment", "combinator", "class"]);
  145. });