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