parser.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. "use strict";
  2. var _ava = _interopRequireDefault(require("ava"));
  3. var _index = _interopRequireDefault(require("../index"));
  4. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  5. // Node creation
  6. var nodeTypes = [['attribute', '[href]', {
  7. attribute: 'href'
  8. }], ['className', '.classy', {
  9. value: 'classy'
  10. }], ['combinator', ' >> ', {
  11. value: '>>',
  12. spaces: {
  13. before: ' ',
  14. after: ' '
  15. }
  16. }], ['comment', '/* comment */', {
  17. value: '/* comment */'
  18. }], ['id', '#test', {
  19. value: 'test'
  20. }], ['nesting', '&'], ['pseudo', '::before', {
  21. value: '::before'
  22. }], ['string', '"wow"', {
  23. value: '"wow"'
  24. }], ['tag', 'button', {
  25. value: 'button'
  26. }], ['universal', '*']];
  27. nodeTypes.forEach(function (type) {
  28. (0, _ava["default"])("parser#" + type[0], function (t) {
  29. var node = _index["default"][type[0]](type[2] || {});
  30. t.deepEqual(String(node), type[1]);
  31. });
  32. });
  33. (0, _ava["default"])('string constants', function (t) {
  34. t.truthy(_index["default"].TAG);
  35. t.truthy(_index["default"].STRING);
  36. t.truthy(_index["default"].SELECTOR);
  37. t.truthy(_index["default"].ROOT);
  38. t.truthy(_index["default"].PSEUDO);
  39. t.truthy(_index["default"].NESTING);
  40. t.truthy(_index["default"].ID);
  41. t.truthy(_index["default"].COMMENT);
  42. t.truthy(_index["default"].COMBINATOR);
  43. t.truthy(_index["default"].CLASS);
  44. t.truthy(_index["default"].ATTRIBUTE);
  45. t.truthy(_index["default"].UNIVERSAL);
  46. });
  47. (0, _ava["default"])('construct a whole tree', function (t) {
  48. var root = _index["default"].root();
  49. var selector = _index["default"].selector();
  50. selector.append(_index["default"].id({
  51. value: 'tree'
  52. }));
  53. root.append(selector);
  54. t.deepEqual(String(root), '#tree');
  55. });
  56. (0, _ava["default"])('no operation', function (t) {
  57. t.notThrows(function () {
  58. return (0, _index["default"])().processSync('h1 h2 h3');
  59. });
  60. });
  61. (0, _ava["default"])('empty selector string', function (t) {
  62. t.notThrows(function () {
  63. return (0, _index["default"])(function (selectors) {
  64. selectors.walk(function (selector) {
  65. selector.type = 'tag';
  66. });
  67. }).processSync('');
  68. });
  69. });
  70. (0, _ava["default"])('async parser', function (t) {
  71. return (0, _index["default"])(function (selectors) {
  72. return new Promise(function (res) {
  73. setTimeout(function () {
  74. selectors.first.nodes[0].value = 'bar';
  75. res();
  76. }, 1);
  77. });
  78. }).process('foo').then(function (result) {
  79. t.deepEqual(result, 'bar');
  80. });
  81. });
  82. (0, _ava["default"])('parse errors with the async parser', function (t) {
  83. return (0, _index["default"])(function (selectors) {
  84. return new Promise(function (res) {
  85. setTimeout(function () {
  86. selectors.first.nodes[0].value = 'bar';
  87. res();
  88. }, 1);
  89. });
  90. }).process('a b: c')["catch"](function (err) {
  91. return t.truthy(err);
  92. });
  93. });
  94. (0, _ava["default"])('parse errors within the async processor', function (t) {
  95. return (0, _index["default"])(function (selectors) {
  96. return new Promise(function (res, rej) {
  97. setTimeout(function () {
  98. rej(selectors.error("async error"));
  99. }, 1);
  100. });
  101. }).process('.foo')["catch"](function (err) {
  102. return t.truthy(err);
  103. });
  104. });
  105. (0, _ava["default"])('parse errors within the async processor before the promise returns', function (t) {
  106. return (0, _index["default"])(function (selectors) {
  107. throw selectors.error("async error");
  108. }).process('.foo')["catch"](function (err) {
  109. return t.truthy(err);
  110. });
  111. });
  112. (0, _ava["default"])('returning a promise to the sync processor fails', function (t) {
  113. t["throws"](function () {
  114. return (0, _index["default"])(function () {
  115. return new Promise(function (res) {
  116. setTimeout(function () {
  117. res();
  118. }, 1);
  119. });
  120. }).processSync('.foo');
  121. });
  122. });
  123. (0, _ava["default"])('Passing a rule works async', function (t) {
  124. var rule = {
  125. selector: '.foo'
  126. };
  127. return (0, _index["default"])(function (root) {
  128. return new Promise(function (res) {
  129. setTimeout(function () {
  130. root.walkClasses(function (node) {
  131. node.value = "bar";
  132. });
  133. res();
  134. }, 1);
  135. });
  136. }).process(rule).then(function (newSel) {
  137. t.deepEqual(newSel, ".bar");
  138. t.deepEqual(rule.selector, ".bar");
  139. });
  140. });
  141. (0, _ava["default"])('Passing a rule with mutation disabled works async', function (t) {
  142. var rule = {
  143. selector: '.foo'
  144. };
  145. return (0, _index["default"])(function (root) {
  146. return new Promise(function (res) {
  147. setTimeout(function () {
  148. root.walkClasses(function (node) {
  149. node.value = "bar";
  150. });
  151. res();
  152. }, 1);
  153. });
  154. }).process(rule, {
  155. updateSelector: false
  156. }).then(function (newSel) {
  157. t.deepEqual(newSel, ".bar");
  158. t.deepEqual(rule.selector, ".foo");
  159. });
  160. });
  161. (0, _ava["default"])('Passing a rule with mutation works sync', function (t) {
  162. var rule = {
  163. selector: '.foo'
  164. };
  165. var newSel = (0, _index["default"])(function (root) {
  166. root.walkClasses(function (node) {
  167. node.value = "bar";
  168. });
  169. }).processSync(rule, {
  170. updateSelector: true
  171. });
  172. t.deepEqual(newSel, ".bar");
  173. t.deepEqual(rule.selector, ".bar");
  174. });
  175. (0, _ava["default"])('Transform a selector synchronously', function (t) {
  176. var rule = {
  177. selector: '.foo'
  178. };
  179. var count = (0, _index["default"])(function (root) {
  180. var classCount = 0;
  181. root.walkClasses(function (node) {
  182. classCount++;
  183. node.value = "bar";
  184. });
  185. return classCount;
  186. }).transformSync(rule, {
  187. updateSelector: true
  188. });
  189. t.deepEqual(count, 1);
  190. t.deepEqual(rule.selector, ".bar");
  191. });
  192. (0, _ava["default"])('Transform a selector asynchronously', function (t) {
  193. var rule = {
  194. selector: '.foo'
  195. };
  196. return (0, _index["default"])(function (root) {
  197. return new Promise(function (res) {
  198. setTimeout(function () {
  199. var classCount = 0;
  200. root.walkClasses(function (node) {
  201. classCount++;
  202. node.value = "bar";
  203. });
  204. res(classCount);
  205. }, 1);
  206. });
  207. }).transform(rule, {
  208. updateSelector: true
  209. }).then(function (count) {
  210. t.deepEqual(count, 1);
  211. t.deepEqual(rule.selector, ".bar");
  212. });
  213. });
  214. (0, _ava["default"])('get AST of a selector synchronously', function (t) {
  215. var rule = {
  216. selector: '.foo'
  217. };
  218. var ast = (0, _index["default"])(function (root) {
  219. var classCount = 0;
  220. root.walkClasses(function (node) {
  221. classCount++;
  222. node.value = "bar";
  223. });
  224. return classCount;
  225. }).astSync(rule, {
  226. updateSelector: true
  227. });
  228. t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
  229. t.deepEqual(rule.selector, ".bar");
  230. });
  231. (0, _ava["default"])('get AST a selector asynchronously', function (t) {
  232. var rule = {
  233. selector: '.foo'
  234. };
  235. return (0, _index["default"])(function (root) {
  236. return new Promise(function (res) {
  237. setTimeout(function () {
  238. var classCount = 0;
  239. root.walkClasses(function (node) {
  240. classCount++;
  241. node.value = "bar";
  242. });
  243. res(classCount);
  244. }, 1);
  245. });
  246. }).ast(rule, {
  247. updateSelector: true
  248. }).then(function (ast) {
  249. t.deepEqual(ast.nodes[0].nodes[0].value, "bar");
  250. t.deepEqual(rule.selector, ".bar");
  251. });
  252. });