container.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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"])('container#append', function (t) {
  7. var out = (0, _helpers.parse)('h1', function (selectors) {
  8. var selector = selectors.first;
  9. var clone = selector.first.clone({
  10. value: 'h2'
  11. });
  12. selectors.append(clone);
  13. });
  14. t.deepEqual(out, 'h1,h2');
  15. });
  16. (0, _ava["default"])('container#prepend', function (t) {
  17. var out = (0, _helpers.parse)('h2', function (selectors) {
  18. var selector = selectors.first;
  19. var clone = selector.first.clone({
  20. value: 'h1'
  21. });
  22. selectors.prepend(clone);
  23. });
  24. t.deepEqual(out, 'h1,h2');
  25. });
  26. (0, _ava["default"])('container#each', function (t) {
  27. var str = '';
  28. (0, _helpers.parse)('h1, h2:not(h3, h4)', function (selectors) {
  29. selectors.each(function (selector) {
  30. if (selector.first.type === 'tag') {
  31. str += selector.first.value;
  32. }
  33. });
  34. });
  35. t.deepEqual(str, 'h1h2');
  36. });
  37. (0, _ava["default"])('container#each (safe iteration)', function (t) {
  38. var out = (0, _helpers.parse)('.x, .y', function (selectors) {
  39. selectors.each(function (selector) {
  40. selector.parent.insertBefore(selector, _["default"].className({
  41. value: 'b'
  42. }));
  43. selector.parent.insertAfter(selector, _["default"].className({
  44. value: 'a'
  45. }));
  46. });
  47. });
  48. t.deepEqual(out, '.b,.x,.a,.b, .y,.a');
  49. });
  50. (0, _ava["default"])('container#each (early exit)', function (t) {
  51. var str = '';
  52. (0, _helpers.parse)('h1, h2, h3, h4', function (selectors) {
  53. var eachReturn = selectors.each(function (selector) {
  54. var tag = selector.first.value;
  55. str += tag;
  56. return tag !== 'h2';
  57. });
  58. t["false"](eachReturn);
  59. });
  60. t.deepEqual(str, 'h1h2');
  61. });
  62. (0, _ava["default"])('container#walk', function (t) {
  63. var str = '';
  64. (0, _helpers.parse)('h1, h2:not(h3, h4)', function (selectors) {
  65. selectors.walk(function (selector) {
  66. if (selector.type === 'tag') {
  67. str += selector.value;
  68. }
  69. });
  70. });
  71. t.deepEqual(str, 'h1h2h3h4');
  72. });
  73. (0, _ava["default"])('container#walk (safe iteration)', function (t) {
  74. var out = (0, _helpers.parse)('[class] + *[href] *:not(*.green)', function (selectors) {
  75. selectors.walkUniversals(function (selector) {
  76. var next = selector.next();
  77. if (next && next.type !== 'combinator') {
  78. selector.remove();
  79. }
  80. });
  81. });
  82. t.deepEqual(out, '[class] + [href] :not(.green)');
  83. });
  84. (0, _ava["default"])('container#walk (early exit)', function (t) {
  85. var str = '';
  86. (0, _helpers.parse)('h1, h2:not(h3, h4)', function (selectors) {
  87. var walkReturn = selectors.walk(function (selector) {
  88. if (selector.type === 'tag') {
  89. var tag = selector.value;
  90. str += tag;
  91. return tag !== 'h3';
  92. }
  93. });
  94. t["false"](walkReturn);
  95. });
  96. t.deepEqual(str, 'h1h2h3');
  97. });
  98. (0, _ava["default"])('container#walkAttribute', function (t) {
  99. var out = (0, _helpers.parse)('[href][class].class', function (selectors) {
  100. selectors.walkAttributes(function (attr) {
  101. if (attr.attribute === 'class') {
  102. attr.remove();
  103. }
  104. });
  105. });
  106. t.deepEqual(out, '[href].class');
  107. });
  108. (0, _ava["default"])('container#walkClass', function (t) {
  109. var out = (0, _helpers.parse)('.one, .two, .three:not(.four, .five)', function (selectors) {
  110. selectors.walkClasses(function (className) {
  111. className.value = className.value.slice(0, 1);
  112. });
  113. });
  114. t.deepEqual(out, '.o, .t, .t:not(.f, .f)');
  115. });
  116. (0, _ava["default"])('container#walkCombinator', function (t) {
  117. var out = (0, _helpers.parse)('h1 h2 h3 h4', function (selectors) {
  118. selectors.walkCombinators(function (comment) {
  119. comment.remove();
  120. });
  121. });
  122. t.deepEqual(out, 'h1h2h3h4');
  123. });
  124. (0, _ava["default"])('container#walkComment', function (t) {
  125. var out = (0, _helpers.parse)('.one/*test*/.two', function (selectors) {
  126. selectors.walkComments(function (comment) {
  127. comment.remove();
  128. });
  129. });
  130. t.deepEqual(out, '.one.two');
  131. });
  132. (0, _ava["default"])('container#walkId', function (t) {
  133. var out = (0, _helpers.parse)('h1#one, h2#two', function (selectors) {
  134. selectors.walkIds(function (id) {
  135. id.value = id.value.slice(0, 1);
  136. });
  137. });
  138. t.deepEqual(out, 'h1#o, h2#t');
  139. });
  140. (0, _ava["default"])('container#walkNesting', function (t) {
  141. var out = (0, _helpers.parse)('& h1', function (selectors) {
  142. selectors.walkNesting(function (node) {
  143. node.replaceWith(_["default"].tag({
  144. value: 'body'
  145. }));
  146. });
  147. });
  148. t.deepEqual(out, 'body h1');
  149. });
  150. (0, _ava["default"])('container#walkPseudo', function (t) {
  151. var out = (0, _helpers.parse)('a:before, a:after', function (selectors) {
  152. selectors.walkPseudos(function (pseudo) {
  153. pseudo.value = pseudo.value.slice(0, 2);
  154. });
  155. });
  156. t.deepEqual(out, 'a:b, a:a');
  157. });
  158. (0, _ava["default"])('container#walkTag', function (t) {
  159. var out = (0, _helpers.parse)('1 2 3', function (selectors) {
  160. selectors.walkTags(function (tag) {
  161. tag.value = 'h' + tag.value;
  162. });
  163. });
  164. t.deepEqual(out, 'h1 h2 h3');
  165. });
  166. (0, _ava["default"])('container#walkUniversal', function (t) {
  167. var out = (0, _helpers.parse)('*.class,*.class,*.class', function (selectors) {
  168. selectors.walkUniversals(function (universal) {
  169. universal.remove();
  170. });
  171. });
  172. t.deepEqual(out, '.class,.class,.class');
  173. });
  174. (0, _ava["default"])('container#map', function (t) {
  175. (0, _helpers.parse)('1 2 3', function (selectors) {
  176. var arr = selectors.first.map(function (selector) {
  177. if (/[0-9]/.test(selector.value)) {
  178. return 'h' + selector.value;
  179. }
  180. return selector.value;
  181. });
  182. t.deepEqual(arr, ['h1', ' ', 'h2', ' ', 'h3']);
  183. });
  184. });
  185. (0, _ava["default"])('container#every', function (t) {
  186. (0, _helpers.parse)('.one.two.three', function (selectors) {
  187. var allClasses = selectors.first.every(function (selector) {
  188. return selector.type === 'class';
  189. });
  190. t.truthy(allClasses);
  191. });
  192. });
  193. (0, _ava["default"])('container#some', function (t) {
  194. (0, _helpers.parse)('one#two.three', function (selectors) {
  195. var someClasses = selectors.first.some(function (selector) {
  196. return selector.type === 'class';
  197. });
  198. t.truthy(someClasses);
  199. });
  200. });
  201. (0, _ava["default"])('container#reduce', function (t) {
  202. (0, _helpers.parse)('h1, h2, h3, h4', function (selectors) {
  203. var str = selectors.reduce(function (memo, selector) {
  204. if (selector.first.type === 'tag') {
  205. memo += selector.first.value;
  206. }
  207. return memo;
  208. }, '');
  209. t.deepEqual(str, 'h1h2h3h4');
  210. });
  211. });
  212. (0, _ava["default"])('container#filter', function (t) {
  213. (0, _helpers.parse)('h1, h2, c1, c2', function (selectors) {
  214. var ast = selectors.filter(function (selector) {
  215. return ~selector.first.value.indexOf('h');
  216. });
  217. t.deepEqual(String(ast), 'h1, h2');
  218. });
  219. });
  220. (0, _ava["default"])('container#split', function (t) {
  221. (0, _helpers.parse)('h1 h2 >> h3', function (selectors) {
  222. var list = selectors.first.split(function (selector) {
  223. return selector.value === '>>';
  224. }).map(function (group) {
  225. return group.map(String);
  226. });
  227. t.deepEqual(list, [['h1', ' ', 'h2', ' >> '], ['h3']]);
  228. t.deepEqual(list.length, 2);
  229. });
  230. });
  231. (0, _ava["default"])('container#sort', function (t) {
  232. var out = (0, _helpers.parse)('h2,h3,h1,h4', function (selectors) {
  233. selectors.sort(function (a, b) {
  234. return a.first.value.slice(-1) - b.first.value.slice(-1);
  235. });
  236. });
  237. t.deepEqual(out, 'h1,h2,h3,h4');
  238. });
  239. (0, _ava["default"])('container#at', function (t) {
  240. (0, _helpers.parse)('h1, h2, h3', function (selectors) {
  241. t.deepEqual(selectors.at(1).first.value, 'h2');
  242. });
  243. });
  244. (0, _ava["default"])('container#first, container#last', function (t) {
  245. (0, _helpers.parse)('h1, h2, h3, h4', function (selectors) {
  246. t.deepEqual(selectors.first.first.value, 'h1');
  247. t.deepEqual(selectors.last.last.value, 'h4');
  248. });
  249. });
  250. (0, _ava["default"])('container#index', function (t) {
  251. (0, _helpers.parse)('h1 h2 h3', function (selectors) {
  252. var middle = selectors.first.at(1);
  253. t.deepEqual(selectors.first.index(middle), 1);
  254. });
  255. });
  256. (0, _ava["default"])('container#length', function (t) {
  257. (0, _helpers.parse)('h1, h2, h3', function (selectors) {
  258. t.deepEqual(selectors.length, 3);
  259. });
  260. });
  261. (0, _ava["default"])('container#removeChild', function (t) {
  262. var out = (0, _helpers.parse)('h1.class h2.class h3.class', function (selectors) {
  263. selectors.walk(function (selector) {
  264. if (selector.type === 'class') {
  265. selector.parent.removeChild(selector);
  266. }
  267. });
  268. });
  269. t.deepEqual(out, 'h1 h2 h3');
  270. });
  271. (0, _ava["default"])('container#removeAll, container#empty', function (t) {
  272. var wipe = function wipe(method) {
  273. return function (selectors) {
  274. return selectors[method]();
  275. };
  276. };
  277. var out1 = (0, _helpers.parse)('h1 h2, h2 h3, h3 h4', wipe('empty'));
  278. var out2 = (0, _helpers.parse)('h1 h2, h2 h3, h3 h4', wipe('removeAll'));
  279. t.deepEqual(out1, '');
  280. t.deepEqual(out2, '');
  281. });
  282. (0, _ava["default"])('container#insertBefore', function (t) {
  283. var out = (0, _helpers.parse)('h2', function (selectors) {
  284. var selector = selectors.first;
  285. var clone = selector.first.clone({
  286. value: 'h1'
  287. });
  288. selectors.insertBefore(selector, clone);
  289. });
  290. t.deepEqual(out, 'h1,h2');
  291. });
  292. (0, _ava["default"])('container#insertBefore and node#remove', function (t) {
  293. var out = (0, _helpers.parse)('h2', function (selectors) {
  294. var selector = selectors.first;
  295. var newSel = _["default"].tag({
  296. value: 'h1'
  297. });
  298. selectors.insertBefore(selector, newSel);
  299. newSel.remove();
  300. });
  301. t.deepEqual(out, 'h2');
  302. });
  303. (0, _ava["default"])('container#insertAfter', function (t) {
  304. var out = (0, _helpers.parse)('h1', function (selectors) {
  305. var selector = selectors.first;
  306. var clone = selector.first.clone({
  307. value: 'h2'
  308. });
  309. selectors.insertAfter(selector, clone);
  310. });
  311. t.deepEqual(out, 'h1,h2');
  312. });
  313. (0, _ava["default"])('container#insertAfter and node#remove', function (t) {
  314. var out = (0, _helpers.parse)('h2', function (selectors) {
  315. var selector = selectors.first;
  316. var newSel = _["default"].tag({
  317. value: 'h1'
  318. });
  319. selectors.insertAfter(selector, newSel);
  320. newSel.remove();
  321. });
  322. t.deepEqual(out, 'h2');
  323. });
  324. (0, _ava["default"])('container#insertAfter (during iteration)', function (t) {
  325. var out = (0, _helpers.parse)('h1, h2, h3', function (selectors) {
  326. selectors.walkTags(function (selector) {
  327. var attribute = _["default"].attribute({
  328. attribute: 'class'
  329. });
  330. selector.parent.insertAfter(selector, attribute);
  331. });
  332. });
  333. t.deepEqual(out, 'h1[class], h2[class], h3[class]');
  334. });
  335. (0, _ava["default"])('Container#atPosition first pseudo', function (t) {
  336. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  337. var node = root.atPosition(1, 1);
  338. t.deepEqual(node.type, "pseudo");
  339. t.deepEqual(node.toString(), ":not(.foo)");
  340. });
  341. });
  342. (0, _ava["default"])('Container#atPosition class in pseudo', function (t) {
  343. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  344. var node = root.atPosition(1, 6);
  345. t.deepEqual(node.type, "class");
  346. t.deepEqual(node.toString(), ".foo");
  347. });
  348. });
  349. (0, _ava["default"])('Container#atPosition id in second selector', function (t) {
  350. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  351. var node = root.atPosition(2, 1);
  352. t.deepEqual(node.type, "id");
  353. t.deepEqual(node.toString(), "\n#foo");
  354. });
  355. });
  356. (0, _ava["default"])('Container#atPosition combinator in second selector', function (t) {
  357. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  358. var node = root.atPosition(2, 6);
  359. t.deepEqual(node.type, "combinator");
  360. t.deepEqual(node.toString(), " > ");
  361. var nodeSpace = root.atPosition(2, 5);
  362. t.deepEqual(nodeSpace.type, "selector");
  363. t.deepEqual(nodeSpace.toString(), "\n#foo > :matches(ol, ul)");
  364. });
  365. });
  366. (0, _ava["default"])('Container#atPosition tag in second selector pseudo', function (t) {
  367. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  368. var node = root.atPosition(2, 17);
  369. t.deepEqual(node.type, "tag");
  370. t.deepEqual(node.toString(), "ol");
  371. });
  372. });
  373. (0, _ava["default"])('Container#atPosition comma in second selector pseudo', function (t) {
  374. (0, _helpers.parse)(':not(.foo),\n#foo > :matches(ol, ul)', function (root) {
  375. var node = root.atPosition(2, 19);
  376. t.deepEqual(node.type, "pseudo");
  377. t.deepEqual(node.toString(), ":matches(ol, ul)");
  378. });
  379. });