node.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. var _ava = _interopRequireDefault(require("ava"));
  3. var _ = _interopRequireDefault(require(".."));
  4. var _helpers = require("./util/helpers");
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  6. (0, _ava["default"])('node#clone', function (t) {
  7. (0, _helpers.parse)('[href="test"]', function (selectors) {
  8. var selector = selectors.first.first;
  9. var clone = selector.clone();
  10. delete selector.parent;
  11. t.deepEqual(clone, selectors.first.first);
  12. });
  13. });
  14. (0, _ava["default"])('node#clone of attribute', function (t) {
  15. (0, _helpers.parse)('[href=test]', function (selectors) {
  16. var selector = selectors.first.first;
  17. var clone = selector.clone();
  18. delete selector.parent;
  19. t.deepEqual(clone, selectors.first.first);
  20. });
  21. });
  22. (0, _ava["default"])('node#replaceWith', function (t) {
  23. var out = (0, _helpers.parse)('[href="test"]', function (selectors) {
  24. var attr = selectors.first.first;
  25. var id = _["default"].id({
  26. value: 'test'
  27. });
  28. var className = _["default"].className({
  29. value: 'test'
  30. });
  31. attr.replaceWith(id, className);
  32. });
  33. t.deepEqual(out, '#test.test');
  34. });
  35. (0, _ava["default"])('Node#appendToPropertyAndEscape', function (t) {
  36. var out = (0, _helpers.parse)('.fo\\o', function (selectors) {
  37. var className = selectors.first.first;
  38. t.deepEqual(className.raws, {
  39. value: "fo\\o"
  40. });
  41. className.appendToPropertyAndEscape("value", "bar", "ba\\r");
  42. t.deepEqual(className.raws, {
  43. value: "fo\\oba\\r"
  44. });
  45. });
  46. t.deepEqual(out, '.fo\\oba\\r');
  47. });
  48. (0, _ava["default"])('Node#setPropertyAndEscape with existing raws', function (t) {
  49. var out = (0, _helpers.parse)('.fo\\o', function (selectors) {
  50. var className = selectors.first.first;
  51. t.deepEqual(className.raws, {
  52. value: "fo\\o"
  53. });
  54. className.setPropertyAndEscape("value", "bar", "ba\\r");
  55. t.deepEqual(className.raws, {
  56. value: "ba\\r"
  57. });
  58. });
  59. t.deepEqual(out, '.ba\\r');
  60. });
  61. (0, _ava["default"])('Node#setPropertyAndEscape without existing raws', function (t) {
  62. var out = (0, _helpers.parse)('.foo', function (selectors) {
  63. var className = selectors.first.first;
  64. t.deepEqual(className.raws, undefined);
  65. className.setPropertyAndEscape("value", "bar", "ba\\r");
  66. t.deepEqual(className.raws, {
  67. value: "ba\\r"
  68. });
  69. });
  70. t.deepEqual(out, '.ba\\r');
  71. });
  72. (0, _ava["default"])('Node#setPropertyWithoutEscape with existing raws', function (t) {
  73. var out = (0, _helpers.parse)('.fo\\o', function (selectors) {
  74. var className = selectors.first.first;
  75. t.deepEqual(className.raws, {
  76. value: "fo\\o"
  77. });
  78. className.setPropertyWithoutEscape("value", "w+t+f");
  79. t.deepEqual(className.raws, {});
  80. });
  81. t.deepEqual(out, '.w+t+f');
  82. });
  83. (0, _ava["default"])('Node#setPropertyWithoutEscape without existing raws', function (t) {
  84. var out = (0, _helpers.parse)('.foo', function (selectors) {
  85. var className = selectors.first.first;
  86. t.deepEqual(className.raws, undefined);
  87. className.setPropertyWithoutEscape("value", "w+t+f");
  88. t.deepEqual(className.raws, {});
  89. t.deepEqual(className.value, "w+t+f");
  90. });
  91. t.deepEqual(out, '.w+t+f');
  92. });
  93. (0, _ava["default"])('Node#isAtPosition', function (t) {
  94. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  95. t.deepEqual(root.isAtPosition(1, 1), true);
  96. t.deepEqual(root.isAtPosition(1, 10), true);
  97. t.deepEqual(root.isAtPosition(2, 23), true);
  98. t.deepEqual(root.isAtPosition(2, 24), false);
  99. var selector = root.first;
  100. t.deepEqual(selector.isAtPosition(1, 1), true);
  101. t.deepEqual(selector.isAtPosition(1, 10), true);
  102. t.deepEqual(selector.isAtPosition(1, 11), false);
  103. var pseudoNot = selector.first;
  104. t.deepEqual(pseudoNot.isAtPosition(1, 1), true);
  105. t.deepEqual(pseudoNot.isAtPosition(1, 7), true);
  106. t.deepEqual(pseudoNot.isAtPosition(1, 10), true);
  107. t.deepEqual(pseudoNot.isAtPosition(1, 11), false);
  108. var notSelector = pseudoNot.first;
  109. t.deepEqual(notSelector.isAtPosition(1, 1), false);
  110. t.deepEqual(notSelector.isAtPosition(1, 4), false);
  111. t.deepEqual(notSelector.isAtPosition(1, 5), true);
  112. t.deepEqual(notSelector.isAtPosition(1, 6), true);
  113. t.deepEqual(notSelector.isAtPosition(1, 9), true);
  114. t.deepEqual(notSelector.isAtPosition(1, 10), true);
  115. t.deepEqual(notSelector.isAtPosition(1, 11), false);
  116. var notClass = notSelector.first;
  117. t.deepEqual(notClass.isAtPosition(1, 5), false);
  118. t.deepEqual(notClass.isAtPosition(1, 6), true);
  119. t.deepEqual(notClass.isAtPosition(1, 9), true);
  120. t.deepEqual(notClass.isAtPosition(1, 10), false);
  121. var secondSel = root.at(1);
  122. t.deepEqual(secondSel.isAtPosition(1, 11), false);
  123. t.deepEqual(secondSel.isAtPosition(2, 1), true);
  124. t.deepEqual(secondSel.isAtPosition(2, 23), true);
  125. t.deepEqual(secondSel.isAtPosition(2, 24), false);
  126. var combinator = secondSel.at(1);
  127. t.deepEqual(combinator.isAtPosition(2, 5), false);
  128. t.deepEqual(combinator.isAtPosition(2, 6), true);
  129. t.deepEqual(combinator.isAtPosition(2, 7), false);
  130. });
  131. });