queryPseudoChild.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. define([
  2. "esquery",
  3. "jstestr/assert",
  4. "jstestr/test",
  5. "./fixtures/conditional",
  6. "./fixtures/forLoop",
  7. "./fixtures/simpleFunction",
  8. "./fixtures/simpleProgram"
  9. ], function (esquery, assert, test, conditional, forLoop, simpleFunction, simpleProgram) {
  10. test.defineSuite("Pseudo *-child query", {
  11. "conditional first child": function () {
  12. var matches = esquery(conditional, ":first-child");
  13. assert.contains([
  14. conditional.body[0],
  15. conditional.body[0].consequent.body[0],
  16. conditional.body[0].alternate.body[0],
  17. conditional.body[1].consequent.body[0],
  18. conditional.body[1].alternate.consequent.body[0]
  19. ], matches);
  20. },
  21. "conditional last child": function () {
  22. var matches = esquery(conditional, ":last-child");
  23. assert.contains([
  24. conditional.body[1],
  25. conditional.body[0].consequent.body[0],
  26. conditional.body[0].alternate.body[0],
  27. conditional.body[1].consequent.body[0],
  28. conditional.body[1].alternate.consequent.body[0]
  29. ], matches);
  30. },
  31. "conditional nth child": function () {
  32. var matches = esquery(conditional, ":nth-child(2)");
  33. assert.contains([
  34. conditional.body[1]
  35. ], matches);
  36. matches = esquery(conditional, ":nth-last-child(2)");
  37. assert.contains([
  38. conditional.body[0]
  39. ], matches);
  40. },
  41. "for loop first child": function () {
  42. var matches = esquery(forLoop, ":first-child");
  43. assert.contains([
  44. forLoop.body[0],
  45. forLoop.body[0].body.body[0]
  46. ], matches);
  47. },
  48. "for loop last child": function () {
  49. var matches = esquery(forLoop, ":last-child");
  50. assert.contains([
  51. forLoop.body[0],
  52. forLoop.body[0].body.body[0]
  53. ], matches);
  54. },
  55. "for loop nth child": function () {
  56. var matches = esquery(forLoop, ":nth-last-child(1)");
  57. assert.contains([
  58. forLoop.body[0],
  59. forLoop.body[0].body.body[0]
  60. ], matches);
  61. },
  62. "simple function first child": function () {
  63. var matches = esquery(simpleFunction, ":first-child");
  64. assert.contains([
  65. simpleFunction.body[0],
  66. simpleFunction.body[0].params[0],
  67. simpleFunction.body[0].body.body[0],
  68. simpleFunction.body[0].body.body[0].declarations[0]
  69. ], matches);
  70. },
  71. "simple function last child": function () {
  72. var matches = esquery(simpleFunction, ":last-child");
  73. assert.contains([
  74. simpleFunction.body[0],
  75. simpleFunction.body[0].params[1],
  76. simpleFunction.body[0].body.body[2],
  77. simpleFunction.body[0].body.body[0].declarations[0]
  78. ], matches);
  79. },
  80. "simple function nth child": function () {
  81. var matches = esquery(simpleFunction, ":nth-child(2)");
  82. assert.contains([
  83. simpleFunction.body[0].params[1],
  84. simpleFunction.body[0].body.body[1]
  85. ], matches);
  86. matches = esquery(simpleFunction, ":nth-child(3)");
  87. assert.contains([
  88. simpleFunction.body[0].body.body[2]
  89. ], matches);
  90. matches = esquery(simpleFunction, ":nth-last-child(2)");
  91. assert.contains([
  92. simpleFunction.body[0].params[0],
  93. simpleFunction.body[0].body.body[1]
  94. ], matches);
  95. },
  96. "simple program first child": function () {
  97. var matches = esquery(simpleProgram, ":first-child");
  98. assert.contains([
  99. simpleProgram.body[0],
  100. simpleProgram.body[0].declarations[0],
  101. simpleProgram.body[1].declarations[0],
  102. simpleProgram.body[3].consequent.body[0]
  103. ], matches);
  104. },
  105. "simple program last child": function () {
  106. var matches = esquery(simpleProgram, ":last-child");
  107. assert.contains([
  108. simpleProgram.body[3],
  109. simpleProgram.body[0].declarations[0],
  110. simpleProgram.body[1].declarations[0],
  111. simpleProgram.body[3].consequent.body[0]
  112. ], matches);
  113. },
  114. "simple program nth child": function () {
  115. var matches = esquery(simpleProgram, ":nth-child(2)");
  116. assert.contains([
  117. simpleProgram.body[1]
  118. ], matches);
  119. matches = esquery(simpleProgram, ":nth-child(3)");
  120. assert.contains([
  121. simpleProgram.body[2]
  122. ], matches);
  123. matches = esquery(simpleProgram, ":nth-last-child(2)");
  124. assert.contains([
  125. simpleProgram.body[2]
  126. ], matches);
  127. }
  128. });
  129. });