attributes.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. "use strict";
  2. var _process = _interopRequireDefault(require("process"));
  3. var _attribute = _interopRequireDefault(require("../selectors/attribute"));
  4. var _helpers = require("./util/helpers");
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  6. _process["default"].throwDeprecation = true;
  7. (0, _helpers.test)('attribute selector', '[href]', function (t, tree) {
  8. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  9. t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
  10. t.falsy(tree.nodes[0].nodes[0].quoted);
  11. });
  12. (0, _helpers.test)('attribute selector spaces (before)', '[ href]', function (t, tree) {
  13. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  14. t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, ' ');
  15. t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
  16. t.falsy(tree.nodes[0].nodes[0].quoted);
  17. });
  18. (0, _helpers.test)('attribute selector spaces (after)', '[href ]', function (t, tree) {
  19. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  20. t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, ' ');
  21. t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
  22. t.falsy(tree.nodes[0].nodes[0].quoted);
  23. });
  24. (0, _helpers.test)('attribute selector spaces with namespace (both)', '[ foo|bar ]', function (t, tree) {
  25. t.deepEqual(tree.nodes[0].nodes[0].ns, 'foo');
  26. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'bar');
  27. t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, ' ');
  28. t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, ' ');
  29. t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
  30. t.falsy(tree.nodes[0].nodes[0].quoted);
  31. });
  32. (0, _helpers.test)('attribute selector spaces (both)', '[ href ]', function (t, tree) {
  33. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  34. t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.before, ' ');
  35. t.deepEqual(tree.nodes[0].nodes[0].spaces.attribute.after, ' ');
  36. t.deepEqual(tree.nodes[0].nodes[0].type, 'attribute');
  37. t.falsy(tree.nodes[0].nodes[0].quoted);
  38. });
  39. (0, _helpers.test)('multiple attribute selectors', '[href][class][name]', function (t, tree) {
  40. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  41. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'class');
  42. t.deepEqual(tree.nodes[0].nodes[2].attribute, 'name');
  43. });
  44. (0, _helpers.test)('select elements with or without a namespace', '[*|href]', function (t, tree) {
  45. t.deepEqual(tree.nodes[0].nodes[0].namespace, '*');
  46. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  47. });
  48. (0, _helpers.test)('namespace with escapes', '[\\31 \\#\\32 |href]', function (t, tree) {
  49. var attr = tree.nodes[0].nodes[0];
  50. t.deepEqual(attr.namespace, '1#2');
  51. t.deepEqual(attr.raws.namespace, '\\31 \\#\\32 ');
  52. attr.namespace = "foo";
  53. t.deepEqual(attr.namespace, 'foo');
  54. t.deepEqual(attr.raws.namespace, undefined);
  55. attr.namespace = "1";
  56. t.deepEqual(attr.namespace, '1');
  57. t.deepEqual(attr.raws.namespace, '\\31');
  58. });
  59. (0, _helpers.test)('attribute selector with a empty value', '[href=""]', function (t, tree) {
  60. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  61. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  62. t.deepEqual(tree.nodes[0].nodes[0].value, '');
  63. t["true"](tree.nodes[0].nodes[0].quoted);
  64. });
  65. (0, _helpers.test)('attribute selector with a value', '[name=james]', function (t, tree) {
  66. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'name');
  67. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  68. t.deepEqual(tree.nodes[0].nodes[0].value, 'james');
  69. t.falsy(tree.nodes[0].nodes[0].quoted);
  70. });
  71. (0, _helpers.test)('attribute selector with quoted value', '[name="james"]', function (t, tree) {
  72. var attr = tree.nodes[0].nodes[0];
  73. t.deepEqual(attr.attribute, 'name');
  74. t.deepEqual(attr.operator, '=');
  75. t.deepEqual(attr.value, 'james');
  76. t.deepEqual(attr.quoteMark, '"');
  77. t.truthy(attr.quoted);
  78. t.deepEqual(attr.getQuotedValue(), '"james"');
  79. });
  80. (0, _helpers.test)('attribute selector with escaped quote', '[title="Something \\"weird\\""]', function (t, tree) {
  81. var attr = tree.nodes[0].nodes[0];
  82. t.deepEqual(attr.value, 'Something "weird"');
  83. t.deepEqual(attr.getQuotedValue(), '\"Something \\"weird\\"\"');
  84. t.deepEqual(attr.getQuotedValue({
  85. smart: true
  86. }), '\'Something "weird"\'');
  87. t.deepEqual(attr.getQuotedValue({
  88. quoteMark: null
  89. }), 'Something\\ \\"weird\\"');
  90. t.deepEqual(attr.quoteMark, '"');
  91. t.truthy(attr.quoted);
  92. t.deepEqual(attr.raws.value, '"Something \\"weird\\""');
  93. t.deepEqual(tree.toString(), '[title="Something \\"weird\\""]');
  94. });
  95. (0, _helpers.test)('attribute selector with escaped colon', '[ng\\:cloak]', function (t, tree) {
  96. t.deepEqual(tree.toString(), '[ng\\:cloak]');
  97. var attr = tree.nodes[0].nodes[0];
  98. t.deepEqual(attr.raws.attribute, 'ng\\:cloak');
  99. t.deepEqual(attr.attribute, 'ng:cloak');
  100. });
  101. (0, _helpers.test)('attribute selector with short hex escape', '[ng\\3a cloak]', function (t, tree) {
  102. t.deepEqual(tree.toString(), '[ng\\3a cloak]');
  103. var attr = tree.nodes[0].nodes[0];
  104. t.deepEqual(attr.raws.attribute, 'ng\\3a cloak');
  105. t.deepEqual(attr.attribute, 'ng:cloak');
  106. });
  107. (0, _helpers.test)('attribute selector with hex escape', '[ng\\00003acloak]', function (t, tree) {
  108. t.deepEqual(tree.toString(), '[ng\\00003acloak]');
  109. var attr = tree.nodes[0].nodes[0];
  110. t.deepEqual(attr.raws.attribute, 'ng\\00003acloak');
  111. t.deepEqual(attr.attribute, 'ng:cloak');
  112. });
  113. (0, _helpers.test)('assign attribute name requiring escape', '[ng\\:cloak]', function (t, tree) {
  114. var attr = tree.nodes[0].nodes[0];
  115. attr.attribute = "ng:foo";
  116. t.deepEqual(attr.raws.attribute, 'ng\\:foo');
  117. t.deepEqual(attr.attribute, 'ng:foo');
  118. t.deepEqual(tree.toString(), '[ng\\:foo]');
  119. });
  120. (0, _helpers.test)('multiple attribute selectors + combinator', '[href][class][name] h1 > h2', function (t, tree) {
  121. t.deepEqual(tree.nodes[0].nodes[2].attribute, 'name');
  122. t.deepEqual(tree.nodes[0].nodes[3].value, ' ');
  123. t.deepEqual(tree.nodes[0].nodes[5].value, '>');
  124. t.deepEqual(tree.nodes[0].nodes[6].value, 'h2');
  125. });
  126. (0, _helpers.test)('attribute, class, combinator', '[href] > h2.test', function (t, tree) {
  127. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  128. t.deepEqual(tree.nodes[0].nodes[1].value, '>');
  129. t.deepEqual(tree.nodes[0].nodes[2].value, 'h2');
  130. t.deepEqual(tree.nodes[0].nodes[3].value, 'test');
  131. });
  132. (0, _helpers.test)('attribute selector with quoted value & combinator', '[name="james"] > h1', function (t, tree) {
  133. t.deepEqual(tree.nodes[0].nodes[0].value, 'james');
  134. t.deepEqual(tree.nodes[0].nodes[0].quoteMark, '"');
  135. t.deepEqual(tree.nodes[0].nodes[1].value, '>');
  136. t.deepEqual(tree.nodes[0].nodes[2].value, 'h1');
  137. });
  138. (0, _helpers.test)('multiple quoted attribute selectors', '[href*="test.com"][rel=\'external\'][id][class~="test"] > [name]', function (t, tree) {
  139. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  140. t.deepEqual(tree.nodes[0].nodes[0].value, 'test.com');
  141. t.is(tree.nodes[0].nodes[0].quoteMark, '"');
  142. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'rel');
  143. t.deepEqual(tree.nodes[0].nodes[1].value, 'external');
  144. t.is(tree.nodes[0].nodes[1].quoteMark, "'");
  145. t.deepEqual(tree.nodes[0].nodes[2].attribute, 'id');
  146. t.falsy(tree.nodes[0].nodes[2].value, 'should not have a value');
  147. t.is(tree.nodes[0].nodes[2].quoteMark, undefined, 'should not have a quoteMark set');
  148. t.deepEqual(tree.nodes[0].nodes[3].attribute, 'class');
  149. t.deepEqual(tree.nodes[0].nodes[3].value, 'test');
  150. t.deepEqual(tree.nodes[0].nodes[3].quoteMark, '"');
  151. t.deepEqual(tree.nodes[0].nodes[4].value, '>');
  152. t.deepEqual(tree.nodes[0].nodes[5].attribute, 'name');
  153. t.falsy(tree.nodes[0].nodes[5].value, 'should not have a value');
  154. t.is(tree.nodes[0].nodes[5].quoteMark, undefined, 'should not have a quoteMark set');
  155. });
  156. (0, _helpers.test)('more attribute operators', '[href*=test],[href^=test],[href$=test],[href|=test]', function (t, tree) {
  157. t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
  158. t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
  159. t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
  160. t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
  161. });
  162. (0, _helpers.test)('attribute selector with quoted value containing "="', '[data-weird-attr="Something=weird"]', function (t, tree) {
  163. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
  164. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  165. t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird');
  166. t.is(tree.nodes[0].nodes[0].quoteMark, '"');
  167. t.deepEqual(tree.nodes[0].nodes[0].getQuotedValue(), '"Something=weird"');
  168. });
  169. var selector = '[data-weird-attr*="Something=weird"],' + '[data-weird-attr^="Something=weird"],' + '[data-weird-attr$="Something=weird"],' + '[data-weird-attr|="Something=weird"]';
  170. (0, _helpers.test)('more attribute selector with quoted value containing "="', selector, function (t, tree) {
  171. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
  172. t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
  173. t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird');
  174. t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-attr');
  175. t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
  176. t.deepEqual(tree.nodes[1].nodes[0].value, 'Something=weird');
  177. t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-attr');
  178. t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
  179. t.deepEqual(tree.nodes[2].nodes[0].value, 'Something=weird');
  180. t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-attr');
  181. t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
  182. t.deepEqual(tree.nodes[3].nodes[0].value, 'Something=weird');
  183. });
  184. (0, _helpers.test)('attribute selector with quoted value containing multiple "="', '[data-weird-attr="Something=weird SomethingElse=weirder"]', function (t, tree) {
  185. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
  186. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  187. t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird SomethingElse=weirder');
  188. });
  189. selector = '[data-weird-attr*="Something=weird SomethingElse=weirder"],' + '[data-weird-attr^="Something=weird SomethingElse=weirder"],' + '[data-weird-attr$="Something=weird SomethingElse=weirder"],' + '[data-weird-attr|="Something=weird SomethingElse=weirder"]';
  190. (0, _helpers.test)('more attribute selector with quoted value containing multiple "="', selector, function (t, tree) {
  191. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-attr');
  192. t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
  193. t.deepEqual(tree.nodes[0].nodes[0].value, 'Something=weird SomethingElse=weirder');
  194. t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-attr');
  195. t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
  196. t.deepEqual(tree.nodes[1].nodes[0].value, 'Something=weird SomethingElse=weirder');
  197. t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-attr');
  198. t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
  199. t.deepEqual(tree.nodes[2].nodes[0].value, 'Something=weird SomethingElse=weirder');
  200. t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-attr');
  201. t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
  202. t.deepEqual(tree.nodes[3].nodes[0].value, 'Something=weird SomethingElse=weirder');
  203. });
  204. (0, _helpers.test)('multiple attribute selectors with quoted value containing "="', '[data-weird-foo="foo=weird"][data-weird-bar="bar=weird"]', function (t, tree) {
  205. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
  206. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  207. t.deepEqual(tree.nodes[0].nodes[0].value, 'foo=weird');
  208. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
  209. t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
  210. t.deepEqual(tree.nodes[0].nodes[1].value, 'bar=weird');
  211. });
  212. (0, _helpers.test)('multiple attribute selectors with value containing escaped "="', '[data-weird-foo=foo\\=weird][data-weird-bar=bar\\3d weird]', function (t, tree) {
  213. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
  214. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  215. t.deepEqual(tree.nodes[0].nodes[0].value, 'foo=weird');
  216. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
  217. t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
  218. t.deepEqual(tree.nodes[0].nodes[1].value, 'bar=weird');
  219. });
  220. selector = '[data-weird-foo*="foo2=weirder"][data-weird-bar*="bar2=weirder"],' + '[data-weird-foo^="foo2=weirder"][data-weird-bar^="bar2=weirder"],' + '[data-weird-foo$="foo2=weirder"][data-weird-bar$="bar2=weirder"],' + '[data-weird-foo|="foo2=weirder"][data-weird-bar|="bar2=weirder"]';
  221. (0, _helpers.test)('more multiple attribute selectors with quoted value containing "="', selector, function (t, tree) {
  222. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
  223. t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
  224. t.deepEqual(tree.nodes[0].nodes[0].value, 'foo2=weirder');
  225. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
  226. t.deepEqual(tree.nodes[0].nodes[1].operator, '*=');
  227. t.deepEqual(tree.nodes[0].nodes[1].value, 'bar2=weirder');
  228. t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-foo');
  229. t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
  230. t.deepEqual(tree.nodes[1].nodes[0].value, 'foo2=weirder');
  231. t.deepEqual(tree.nodes[1].nodes[1].attribute, 'data-weird-bar');
  232. t.deepEqual(tree.nodes[1].nodes[1].operator, '^=');
  233. t.deepEqual(tree.nodes[1].nodes[1].value, 'bar2=weirder');
  234. t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-foo');
  235. t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
  236. t.deepEqual(tree.nodes[2].nodes[0].value, 'foo2=weirder');
  237. t.deepEqual(tree.nodes[2].nodes[1].attribute, 'data-weird-bar');
  238. t.deepEqual(tree.nodes[2].nodes[1].operator, '$=');
  239. t.deepEqual(tree.nodes[2].nodes[1].value, 'bar2=weirder');
  240. t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-foo');
  241. t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
  242. t.deepEqual(tree.nodes[3].nodes[0].value, 'foo2=weirder');
  243. t.deepEqual(tree.nodes[3].nodes[1].attribute, 'data-weird-bar');
  244. t.deepEqual(tree.nodes[3].nodes[1].operator, '|=');
  245. t.deepEqual(tree.nodes[3].nodes[1].value, 'bar2=weirder');
  246. });
  247. (0, _helpers.test)('multiple attribute selectors with quoted value containing multiple "="', '[data-weird-foo="foo1=weirder foo2=weirder"][data-weird-bar="bar1=weirder bar2=weirder"]', function (t, tree) {
  248. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
  249. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  250. t.deepEqual(tree.nodes[0].nodes[0].value, 'foo1=weirder foo2=weirder');
  251. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
  252. t.deepEqual(tree.nodes[0].nodes[1].operator, '=');
  253. t.deepEqual(tree.nodes[0].nodes[1].value, 'bar1=weirder bar2=weirder');
  254. });
  255. selector = '[data-weird-foo*="foo1=weirder foo2=weirder"][data-weird-bar*="bar1=weirder bar2=weirder"],' + '[data-weird-foo^="foo1=weirder foo2=weirder"][data-weird-bar^="bar1=weirder bar2=weirder"],' + '[data-weird-foo$="foo1=weirder foo2=weirder"][data-weird-bar$="bar1=weirder bar2=weirder"],' + '[data-weird-foo|="foo1=weirder foo2=weirder"][data-weird-bar|="bar1=weirder bar2=weirder"]';
  256. (0, _helpers.test)('more multiple attribute selectors with quoted value containing multiple "="', selector, function (t, tree) {
  257. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'data-weird-foo');
  258. t.deepEqual(tree.nodes[0].nodes[0].operator, '*=');
  259. t.deepEqual(tree.nodes[0].nodes[0].value, 'foo1=weirder foo2=weirder');
  260. t.deepEqual(tree.nodes[0].nodes[1].attribute, 'data-weird-bar');
  261. t.deepEqual(tree.nodes[0].nodes[1].operator, '*=');
  262. t.deepEqual(tree.nodes[0].nodes[1].value, 'bar1=weirder bar2=weirder');
  263. t.deepEqual(tree.nodes[1].nodes[0].attribute, 'data-weird-foo');
  264. t.deepEqual(tree.nodes[1].nodes[0].operator, '^=');
  265. t.deepEqual(tree.nodes[1].nodes[0].value, 'foo1=weirder foo2=weirder');
  266. t.deepEqual(tree.nodes[1].nodes[1].attribute, 'data-weird-bar');
  267. t.deepEqual(tree.nodes[1].nodes[1].operator, '^=');
  268. t.deepEqual(tree.nodes[1].nodes[1].value, 'bar1=weirder bar2=weirder');
  269. t.deepEqual(tree.nodes[2].nodes[0].attribute, 'data-weird-foo');
  270. t.deepEqual(tree.nodes[2].nodes[0].operator, '$=');
  271. t.deepEqual(tree.nodes[2].nodes[0].value, 'foo1=weirder foo2=weirder');
  272. t.deepEqual(tree.nodes[2].nodes[1].attribute, 'data-weird-bar');
  273. t.deepEqual(tree.nodes[2].nodes[1].operator, '$=');
  274. t.deepEqual(tree.nodes[2].nodes[1].value, 'bar1=weirder bar2=weirder');
  275. t.deepEqual(tree.nodes[3].nodes[0].attribute, 'data-weird-foo');
  276. t.deepEqual(tree.nodes[3].nodes[0].operator, '|=');
  277. t.deepEqual(tree.nodes[3].nodes[0].value, 'foo1=weirder foo2=weirder');
  278. t.deepEqual(tree.nodes[3].nodes[1].attribute, 'data-weird-bar');
  279. t.deepEqual(tree.nodes[3].nodes[1].operator, '|=');
  280. t.deepEqual(tree.nodes[3].nodes[1].value, 'bar1=weirder bar2=weirder');
  281. });
  282. (0, _helpers.test)('spaces in attribute selectors', 'h1[ href *= "test" ]', function (t, tree) {
  283. var attr = tree.nodes[0].nodes[1];
  284. t.deepEqual(attr.attribute, 'href');
  285. t.deepEqual(attr.spaces.attribute.before, ' ');
  286. t.deepEqual(attr.spaces.attribute.after, ' ');
  287. t.deepEqual(attr.operator, '*=');
  288. t.deepEqual(attr.spaces.operator.after, ' ');
  289. t.deepEqual(attr.value, 'test');
  290. t.deepEqual(attr.spaces.value.after, ' ');
  291. t.truthy(tree.nodes[0].nodes[1].quoted);
  292. });
  293. (0, _helpers.test)('insensitive attribute selector 1', '[href="test" i]', function (t, tree) {
  294. t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
  295. t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
  296. t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
  297. });
  298. (0, _helpers.test)('insensitive attribute selector with a empty value', '[href="" i]', function (t, tree) {
  299. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  300. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  301. t.deepEqual(tree.nodes[0].nodes[0].value, '');
  302. t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
  303. t["true"](tree.nodes[0].nodes[0].quoted);
  304. });
  305. (0, _helpers.test)('insensitive attribute selector 2', '[href=TEsT i ]', function (t, tree) {
  306. t.deepEqual(tree.nodes[0].nodes[0].value, 'TEsT');
  307. t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
  308. t.deepEqual(tree.nodes[0].nodes[0].spaces.value.after, ' ');
  309. t.deepEqual(tree.nodes[0].nodes[0].spaces.insensitive.after, ' ');
  310. });
  311. (0, _helpers.test)('insensitive attribute selector 3', '[href=test i]', function (t, tree) {
  312. t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
  313. t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
  314. });
  315. (0, _helpers.test)('capitalized insensitive attribute selector 3', '[href=test I]', function (t, tree) {
  316. t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
  317. t.deepEqual(tree.nodes[0].nodes[0].insensitive, true);
  318. });
  319. (0, _helpers.test)('extraneous non-combinating whitespace', ' [href] , [class] ', function (t, tree) {
  320. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  321. t.deepEqual(tree.nodes[0].nodes[0].spaces.before, ' ');
  322. t.deepEqual(tree.nodes[0].nodes[0].spaces.after, ' ');
  323. t.deepEqual(tree.nodes[1].nodes[0].attribute, 'class');
  324. t.deepEqual(tree.nodes[1].nodes[0].spaces.before, ' ');
  325. t.deepEqual(tree.nodes[1].nodes[0].spaces.after, ' ');
  326. });
  327. (0, _helpers.test)('newline in attribute selector', '[class="woop \\\nwoop woop"]', function (t, tree) {
  328. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'class');
  329. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  330. t.deepEqual(tree.nodes[0].nodes[0].value, 'woop \nwoop woop');
  331. t["true"](tree.nodes[0].nodes[0].quoted);
  332. });
  333. (0, _helpers.test)('comments within attribute selectors', '[href/* wow */=/* wow */test]', function (t, tree) {
  334. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  335. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  336. t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
  337. t.deepEqual(tree.nodes[0].nodes[0].raws.attribute, 'href/* wow */');
  338. t.deepEqual(tree.nodes[0].nodes[0].raws.operator, '=/* wow */');
  339. t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
  340. });
  341. (0, _helpers.test)('comments within attribute selectors (2)', '[/* wow */href=test/* wow */]', function (t, tree) {
  342. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  343. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  344. t.deepEqual(tree.nodes[0].nodes[0].value, 'test');
  345. t.deepEqual(tree.nodes[0].nodes[0].raws.spaces.attribute.before, '/* wow */');
  346. t.deepEqual(tree.nodes[0].nodes[0].operator, '=');
  347. t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'test/* wow */');
  348. });
  349. (0, _helpers.test)('comments within attribute selectors (3)', '[href=test/* wow */i]', function (t, tree) {
  350. t.deepEqual(tree.nodes[0].nodes[0].attribute, 'href');
  351. t.deepEqual(tree.nodes[0].nodes[0].value, 'testi');
  352. t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'test/* wow */i');
  353. t.falsy(tree.nodes[0].nodes[0].insensitive);
  354. });
  355. (0, _helpers.test)('comments within attribute selectors (4)', '[ /*before*/ href /* after-attr */ = /* after-operator */ te/*inside-value*/st/* wow */ /*omg*/i/*bbq*/ /*whodoesthis*/]', function (t, tree) {
  356. var attr = tree.nodes[0].nodes[0];
  357. t.deepEqual(attr.attribute, 'href');
  358. t.deepEqual(attr.value, 'test');
  359. t.deepEqual(attr.getQuotedValue(), 'test');
  360. t.deepEqual(attr.raws.value, 'te/*inside-value*/st');
  361. t.deepEqual(attr.raws.spaces.value.after, '/* wow */ /*omg*/');
  362. t.truthy(attr.insensitive);
  363. t.deepEqual(attr.offsetOf("attribute"), 13);
  364. t.deepEqual(attr.offsetOf("operator"), 35);
  365. t.deepEqual(attr.offsetOf("insensitive"), 95);
  366. t.deepEqual(attr.raws.spaces.insensitive.after, '/*bbq*/ /*whodoesthis*/');
  367. attr.value = "foo";
  368. t.is(attr.raws.value, undefined);
  369. });
  370. (0, _helpers.test)('non standard modifiers', '[href="foo" y]', function (t, tree) {
  371. var attr = tree.atPosition(1, 13);
  372. t.deepEqual(attr.insensitive, false);
  373. t.deepEqual(attr.insensitiveFlag, '');
  374. t.deepEqual(attr.raws.insensitiveFlag, 'y');
  375. t.deepEqual(tree.toString(), '[href="foo" y]');
  376. });
  377. (0, _helpers.test)('comment after insensitive(non space)', '[href="foo" i/**/]', function (t, tree) {
  378. // https://github.com/postcss/postcss-selector-parser/issues/150
  379. var attr = tree.atPosition(1, 13);
  380. t.deepEqual(attr.insensitive, true);
  381. t.deepEqual(attr.insensitiveFlag, 'i');
  382. t.is(attr.raws.insensitiveFlag, undefined);
  383. t.deepEqual(attr.raws.spaces.insensitive.after, '/**/');
  384. t.deepEqual(tree.toString(), '[href="foo" i/**/]');
  385. });
  386. (0, _helpers.test)('comment after insensitive(space after)', '[href="foo" i/**/ ]', function (t, tree) {
  387. var attr = tree.atPosition(1, 13);
  388. t.deepEqual(attr.insensitive, true);
  389. t.deepEqual(attr.insensitiveFlag, 'i');
  390. t.deepEqual(attr.raws.spaces.insensitive.after, '/**/ ');
  391. t.deepEqual(tree.toString(), '[href="foo" i/**/ ]');
  392. });
  393. (0, _helpers.test)('comment after insensitive(space before)', '[href="foo" i /**/]', function (t, tree) {
  394. var attr = tree.atPosition(1, 13);
  395. t.deepEqual(attr.insensitive, true);
  396. t.deepEqual(attr.insensitiveFlag, 'i');
  397. t.deepEqual(attr.raws.spaces.insensitive.after, ' /**/');
  398. t.deepEqual(tree.toString(), '[href="foo" i /**/]');
  399. });
  400. var testDeprecation = (0, _helpers.nodeVersionAtLeast)('7.0.0') || (0, _helpers.nodeVersionBefore)('6.0.0') ? _helpers.test : _helpers.test.skip;
  401. testDeprecation('deprecated constructor', '', function (t) {
  402. t["throws"](function () {
  403. return new _attribute["default"]({
  404. value: '"foo"',
  405. attribute: "data-bar"
  406. });
  407. }, {
  408. message: "Constructing an Attribute selector with a value without specifying quoteMark is deprecated. Note: The value should be unescaped now."
  409. });
  410. });
  411. testDeprecation('deprecated get of raws.unquoted ', '', function (t) {
  412. t["throws"](function () {
  413. var attr = new _attribute["default"]({
  414. value: 'foo',
  415. quoteMark: '"',
  416. attribute: "data-bar"
  417. });
  418. return attr.raws.unquoted;
  419. }, {
  420. message: "attr.raws.unquoted is deprecated. Call attr.value instead."
  421. });
  422. });
  423. testDeprecation('deprecated set of raws.unquoted ', '', function (t) {
  424. t["throws"](function () {
  425. var attr = new _attribute["default"]({
  426. value: 'foo',
  427. quoteMark: '"',
  428. attribute: "data-bar"
  429. });
  430. attr.raws.unquoted = 'fooooo';
  431. }, {
  432. message: "Setting attr.raws.unquoted is deprecated and has no effect. attr.value is unescaped by default now."
  433. });
  434. });
  435. testDeprecation('smart quotes', '[data-foo=bar]', function (t, tree) {
  436. var attr = tree.nodes[0].nodes[0];
  437. attr.setValue('changed', {
  438. quoteMark: '"'
  439. });
  440. t.deepEqual(attr.toString(), '[data-foo="changed"]');
  441. attr.setValue('changed again', {
  442. quoteMark: "'",
  443. preferCurrentQuoteMark: true
  444. });
  445. t.deepEqual(attr.toString(), '[data-foo="changed again"]');
  446. attr.setValue('smart-ident', {
  447. smart: true
  448. });
  449. t.deepEqual(attr.toString(), '[data-foo=smart-ident]');
  450. attr.setValue('smart quoted', {
  451. smart: true
  452. });
  453. t.deepEqual(attr.toString(), '[data-foo=smart\\ quoted]');
  454. attr.setValue('smart quoted three spaces', {
  455. smart: true
  456. });
  457. t.deepEqual(attr.toString(), '[data-foo="smart quoted three spaces"]');
  458. attr.setValue('smart quoted three spaces', {
  459. smart: true,
  460. quoteMark: "'"
  461. });
  462. t.deepEqual(attr.toString(), "[data-foo='smart quoted three spaces']");
  463. attr.setValue("smart with 'single quotes'", {
  464. smart: true
  465. });
  466. t.deepEqual(attr.toString(), "[data-foo=\"smart with 'single quotes'\"]");
  467. attr.setValue('smart with "double quotes"', {
  468. smart: true
  469. });
  470. t.deepEqual(attr.toString(), "[data-foo='smart with \"double quotes\"']");
  471. });
  472. testDeprecation('set Attribute#quoteMark', '[data-foo=bar]', function (t, tree) {
  473. var attr = tree.nodes[0].nodes[0];
  474. attr.quoteMark = '"';
  475. t.deepEqual(attr.toString(), '[data-foo="bar"]');
  476. attr.quoteMark = "'";
  477. t.deepEqual(attr.toString(), "[data-foo='bar']");
  478. attr.quoteMark = null;
  479. t.deepEqual(attr.toString(), "[data-foo=bar]");
  480. attr.value = "has space";
  481. t.deepEqual(attr.toString(), "[data-foo=has\\ space]");
  482. attr.quoteMark = '"';
  483. t.deepEqual(attr.toString(), '[data-foo="has space"]');
  484. });