-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathattributes.js
More file actions
697 lines (620 loc) · 18.8 KB
/
attributes.js
File metadata and controls
697 lines (620 loc) · 18.8 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/**
* Methods for getting and modifying attributes.
*
* @module cheerio/attributes
*/
var text = require('../static').text;
var utils = require('../utils');
var isTag = utils.isTag;
var domEach = utils.domEach;
var hasOwn = Object.prototype.hasOwnProperty;
var camelCase = utils.camelCase;
var cssCase = utils.cssCase;
var rspace = /\s+/;
var dataAttrPrefix = 'data-';
// Lookup table for coercing string data-* attributes to their corresponding
// JavaScript primitives
var primitives = {
null: null,
true: true,
false: false,
};
// Attributes that are booleans
var rboolean = /^(?:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected)$/i;
// Matches strings that look like JSON objects or arrays
var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;
/**
* Gets a node's attribute. For boolean attributes, it will return the value's
* name should it be set.
*
* Also supports getting the `value` of several form elements.
*
* @private
* @param {Element} elem - Elenent to get the attribute of.
* @param {string} name - Name of the attribute.
* @returns {object | string | undefined} The attribute's value.
*/
function getAttr(elem, name) {
if (!elem || !isTag(elem)) return;
if (!elem.attribs) {
elem.attribs = {};
}
// Return the entire attribs object if no attribute specified
if (!name) {
return elem.attribs;
}
if (hasOwn.call(elem.attribs, name)) {
// Get the (decoded) attribute
return rboolean.test(name) ? name : elem.attribs[name];
}
// Mimic the DOM and return text content as value for `option's`
if (elem.name === 'option' && name === 'value') {
return text(elem.children);
}
// Mimic DOM with default value for radios/checkboxes
if (
elem.name === 'input' &&
(elem.attribs.type === 'radio' || elem.attribs.type === 'checkbox') &&
name === 'value'
) {
return 'on';
}
}
/**
* Sets the value of an attribute. The attribute will be deleted if the value is `null`.
*
* @private
* @param {Element} el - The element to set the attribute on.
* @param {string} name - The attribute's name.
* @param {string | null} value - The attribute's value.
*/
function setAttr(el, name, value) {
if (value === null) {
removeAttribute(el, name);
} else {
el.attribs[name] = value + '';
}
}
/**
* Method for getting and setting attributes. Gets the attribute value for only
* the first element in the matched set. If you set an attribute's value to
* `null`, you remove that attribute. You may also pass a `map` and `function`
* like jQuery.
*
* @example
* $('ul').attr('id');
* //=> fruits
*
* $('.apple').attr('id', 'favorite').html();
* //=> <li class="apple" id="favorite">Apple</li>
*
* @param {string} name - Name of the attribute.
* @param {string | Function} [value] - If specified sets the value of the attribute.
* @see {@link https://api.jquery.com/attr/}
*
* @returns {string | Cheerio} If `value` is specified the instance itself,
* otherwise the attribute's value.
*/
exports.attr = function (name, value) {
// Set the value (with attr map support)
if (typeof name === 'object' || arguments.length > 1) {
if (typeof value === 'function') {
return domEach(this, function (i, el) {
setAttr(el, name, value.call(el, i, el.attribs[name]));
});
}
return domEach(this, function (i, el) {
if (!isTag(el)) return;
if (typeof name === 'object') {
Object.keys(name).forEach(function (objName) {
var objValue = name[objName];
setAttr(el, objName, objValue);
});
} else {
setAttr(el, name, value);
}
});
}
return getAttr(this[0], name);
};
/**
* Gets a node's prop.
*
* @private
* @param {Node} el - Elenent to get the prop of.
* @param {string} name - Name of the prop.
* @returns {string | undefined} The prop's value.
*/
function getProp(el, name) {
if (!el || !isTag(el)) return;
return name in el
? el[name]
: rboolean.test(name)
? getAttr(el, name) !== undefined
: getAttr(el, name);
}
/**
* Sets the value of a prop.
*
* @private
* @param {Element} el - The element to set the prop on.
* @param {string} name - The prop's name.
* @param {string | null} value - The prop's value.
*/
function setProp(el, name, value) {
if (name in el) {
el[name] = value;
} else {
setAttr(el, name, rboolean.test(name) ? (value ? '' : null) : value);
}
}
/**
* Method for getting and setting properties. Gets the property value for only
* the first element in the matched set.
*
* @example
* $('input[type="checkbox"]').prop('checked');
* //=> false
*
* $('input[type="checkbox"]').prop('checked', true).val();
* //=> ok
*
* @param {string} name - Name of the property.
* @param {any} [value] - If specified set the property to this.
* @see {@link https://api.jquery.com/prop/}
*
* @returns {string | Cheerio} If `value` is specified the instance itself,
* otherwise the prop's value.
*/
exports.prop = function (name, value) {
if (typeof name === 'string' && value === undefined) {
switch (name) {
case 'style': {
var property = this.css();
var keys = Object.keys(property);
keys.forEach(function (p, i) {
property[i] = p;
});
property.length = keys.length;
return property;
}
case 'tagName':
case 'nodeName':
return this[0].name.toUpperCase();
case 'outerHTML':
return this.clone().wrap('<container />').parent().html();
case 'innerHTML':
return this.html();
default:
return getProp(this[0], name);
}
}
if (typeof name === 'object' || value !== undefined) {
if (typeof value === 'function') {
return domEach(this, function (j, el) {
setProp(el, name, value.call(el, j, getProp(el, name)));
});
}
return domEach(this, function (__, el) {
if (!isTag(el)) return;
if (typeof name === 'object') {
Object.keys(name).forEach(function (key) {
var val = name[key];
setProp(el, key, val);
});
} else {
setProp(el, name, value);
}
});
}
};
/**
* Sets the value of a data attribute.
*
* @private
* @param {Element} el - The element to set the data attribute on.
* @param {string | object} name - The data attribute's name.
* @param {string | null} value - The data attribute's value.
*/
function setData(el, name, value) {
if (!el.data) {
el.data = {};
}
if (typeof name === 'object') Object.assign(el.data, name);
else if (typeof name === 'string' && value !== undefined) {
el.data[name] = value;
}
}
/**
* Read the specified attribute from the equivalent HTML5 `data-*` attribute,
* and (if present) cache the value in the node's internal data store. If no
* attribute name is specified, read *all* HTML5 `data-*` attributes in this manner.
*
* @private
* @param {Element} el - Elenent to get the data attribute of.
* @param {string} [name] - Name of the data attribute.
* @returns {any} The data attribute's value, or a map with all of the data attribute.
*/
function readData(el, name) {
var readAll = arguments.length === 1;
var domNames;
var jsNames;
if (readAll) {
domNames = Object.keys(el.attribs).filter(function (attrName) {
return attrName.slice(0, dataAttrPrefix.length) === dataAttrPrefix;
});
jsNames = domNames.map(function (_domName) {
return camelCase(_domName.slice(dataAttrPrefix.length));
});
} else {
domNames = [dataAttrPrefix + cssCase(name)];
jsNames = [name];
}
for (var idx = 0; idx < domNames.length; ++idx) {
var domName = domNames[idx];
var jsName = jsNames[idx];
var value;
if (hasOwn.call(el.attribs, domName) && !hasOwn.call(el.data, jsName)) {
value = el.attribs[domName];
if (hasOwn.call(primitives, value)) {
value = primitives[value];
} else if (value === String(Number(value))) {
value = Number(value);
} else if (rbrace.test(value)) {
try {
value = JSON.parse(value);
} catch (e) {
/* ignore */
}
}
el.data[jsName] = value;
}
}
return readAll ? el.data : value;
}
/**
* Method for getting and setting data attributes. Gets or sets the data
* attribute value for only the first element in the matched set.
*
* @example
* $('<div data-apple-color="red"></div>').data();
* //=> { appleColor: 'red' }
*
* $('<div data-apple-color="red"></div>').data('apple-color');
* //=> 'red'
*
* const apple = $('.apple').data('kind', 'mac');
* apple.data('kind');
* //=> 'mac'
*
* @param {string} name - Name of the attribute.
* @param {any} [value] - If specified new value.
* @see {@link https://api.jquery.com/data/}
*
* @returns {string | Cheerio | undefined} If `value` is specified the instance itself,
* otherwise the data attribute's value.
*/
exports.data = function (name, value) {
var elem = this[0];
if (!elem || !isTag(elem)) return;
if (!elem.data) {
elem.data = {};
}
// Return the entire data object if no data specified
if (!name) {
return readData(elem);
}
// Set the value (with attr map support)
if (typeof name === 'object' || value !== undefined) {
domEach(this, function (i, el) {
setData(el, name, value);
});
return this;
}
if (hasOwn.call(elem.data, name)) {
return elem.data[name];
}
return readData(elem, name);
};
/**
* Method for getting and setting the value of input, select, and textarea.
* Note: Support for `map`, and `function` has not been added yet.
*
* @example
* $('input[type="text"]').val();
* //=> input_text
*
* $('input[type="text"]').val('test').html();
* //=> <input type="text" value="test"/>
*
* @param {string | string[]} [value] - If specified new value.
* @see {@link https://api.jquery.com/val/}
* @returns {string | Cheerio | undefined} If a new `value` is specified the instance
* itself, otherwise the value.
*/
exports.val = function (value) {
var querying = arguments.length === 0;
var element = this[0];
if (!element) return;
switch (element.name) {
case 'textarea':
return this.text(value);
case 'select': {
var option = this.find('option:selected');
if (!option) return;
if (!querying) {
if (this.attr('multiple') == null && typeof value == 'object') {
return this;
}
if (typeof value != 'object') {
value = [value];
}
this.find('option').removeAttr('selected');
for (var i = 0; i < value.length; i++) {
this.find('option[value="' + value[i] + '"]').attr('selected', '');
}
return this;
}
return this.attr('multiple')
? option.toArray().map(function (el) {
return getAttr(el, 'value');
})
: option.attr('value');
}
case 'input':
case 'option':
return querying ? this.attr('value') : this.attr('value', value);
}
};
/**
* Remove an attribute.
*
* @private
* @param {Element} elem - Node to remove attribute from.
* @param {string} name - Name of the attribute to remove.
*/
function removeAttribute(elem, name) {
if (!elem.attribs || !hasOwn.call(elem.attribs, name)) return;
delete elem.attribs[name];
}
/**
* Splits a space-separated list of names to individual names.
*
* @param {string} names - Names to split.
* @returns {string[]} - Split names.
*/
function splitNames(names) {
return names ? names.trim().split(rspace) : [];
}
/**
* Method for removing attributes by `name`.
*
* @example
* $('.pear').removeAttr('class').html();
* //=> <li>Pear</li>
*
* $('.apple').attr('id', 'favorite');
* $('.apple').removeAttr('id class').html();
* //=> <li>Apple</li>
*
* @param {string} name - Name of the attribute.
* @see {@link https://api.jquery.com/removeAttr/}
*
* @returns {Cheerio} The instance itself.
*/
exports.removeAttr = function (name) {
var attrNames = splitNames(name);
for (var i = 0; i < attrNames.length; i++) {
domEach(this, function (_, elem) {
removeAttribute(elem, attrNames[i]);
});
}
return this;
};
/**
* Check to see if *any* of the matched elements have the given `className`.
*
* @example
* $('.pear').hasClass('pear');
* //=> true
*
* $('apple').hasClass('fruit');
* //=> false
*
* $('li').hasClass('pear');
* //=> true
*
* @param {string} className - Name of the class.
* @see {@link https://api.jquery.com/hasClass/}
*
* @returns {boolean} Indicates if an element has the given `className`.
*/
exports.hasClass = function (className) {
return this.toArray().some(function (elem) {
var clazz = elem.attribs && elem.attribs['class'];
var idx = -1;
if (clazz && className.length) {
while ((idx = clazz.indexOf(className, idx + 1)) > -1) {
var end = idx + className.length;
if (
(idx === 0 || rspace.test(clazz[idx - 1])) &&
(end === clazz.length || rspace.test(clazz[end]))
) {
return true;
}
}
}
return false;
});
};
/**
* Adds class(es) to all of the matched elements. Also accepts a `function` like jQuery.
*
* @example
* $('.pear').addClass('fruit').html();
* //=> <li class="pear fruit">Pear</li>
*
* $('.apple').addClass('fruit red').html();
* //=> <li class="apple fruit red">Apple</li>
*
* @param {string | Function} value - Name of new class.
* @see {@link https://api.jquery.com/addClass/}
* @returns {Cheerio} The instance itself.
*/
exports.addClass = function (value) {
// Support functions
if (typeof value === 'function') {
return domEach(this, function (i, el) {
var className = el.attribs['class'] || '';
exports.addClass.call([el], value.call(el, i, className));
});
}
// Return if no value or not a string or function
if (!value || typeof value !== 'string') return this;
var classNames = value.split(rspace);
var numElements = this.length;
for (var i = 0; i < numElements; i++) {
// If selected element isn't a tag, move on
if (!isTag(this[i])) continue;
// If we don't already have classes
var className = getAttr(this[i], 'class');
if (!className) {
setAttr(this[i], 'class', classNames.join(' ').trim());
} else {
var setClass = ' ' + className + ' ';
// Check if class already exists
for (var j = 0; j < classNames.length; j++) {
var appendClass = classNames[j] + ' ';
if (setClass.indexOf(' ' + appendClass) < 0) setClass += appendClass;
}
setAttr(this[i], 'class', setClass.trim());
}
}
return this;
};
/**
* Removes one or more space-separated classes from the selected elements. If no
* `className` is defined, all classes will be removed. Also accepts a
* `function` like jQuery.
*
* @example
* $('.pear').removeClass('pear').html();
* //=> <li class="">Pear</li>
*
* $('.apple').addClass('red').removeClass().html();
* //=> <li class="">Apple</li>
*
* @param {string | Function} value - Name of the class.
* @see {@link https://api.jquery.com/removeClass/}
* @returns {Cheerio} The instance itself.
*/
exports.removeClass = function (value) {
// Handle if value is a function
if (typeof value === 'function') {
return domEach(this, function (i, el) {
exports.removeClass.call(
[el],
value.call(el, i, el.attribs['class'] || '')
);
});
}
var classes = splitNames(value);
var numClasses = classes.length;
var removeAll = arguments.length === 0;
return domEach(this, function (_, el) {
if (!isTag(el)) return;
if (removeAll) {
// Short circuit the remove all case as this is the nice one
el.attribs.class = '';
} else {
var elClasses = splitNames(el.attribs.class);
var changed = false;
for (var j = 0; j < numClasses; j++) {
var index = elClasses.indexOf(classes[j]);
if (index >= 0) {
elClasses.splice(index, 1);
changed = true;
// We have to do another pass to ensure that there are not duplicate
// classes listed
j--;
}
}
if (changed) {
el.attribs.class = elClasses.join(' ');
}
}
});
};
/**
* Add or remove class(es) from the matched elements, depending on either the
* class's presence or the value of the switch argument. Also accepts a
* `function` like jQuery.
*
* @example
* $('.apple.green').toggleClass('fruit green red').html();
* //=> <li class="apple fruit red">Apple</li>
*
* $('.apple.green').toggleClass('fruit green red', true).html();
* //=> <li class="apple green fruit red">Apple</li>
*
* @param {string | Function} value - Name of the class. Can also be a function.
* @param {boolean} [stateVal] - If specified the state of the class.
* @see {@link https://api.jquery.com/toggleClass/}
*
* @returns {Cheerio} The instance itself.
*/
exports.toggleClass = function (value, stateVal) {
// Support functions
if (typeof value === 'function') {
return domEach(this, function (i, el) {
exports.toggleClass.call(
[el],
value.call(el, i, el.attribs['class'] || '', stateVal),
stateVal
);
});
}
// Return if no value or not a string or function
if (!value || typeof value !== 'string') return this;
var classNames = value.split(rspace);
var numClasses = classNames.length;
var state = typeof stateVal === 'boolean' ? (stateVal ? 1 : -1) : 0;
var numElements = this.length;
for (var i = 0; i < numElements; i++) {
// If selected element isn't a tag, move on
if (!isTag(this[i])) continue;
var elementClasses = splitNames(this[i].attribs.class);
// Check if class already exists
for (var j = 0; j < numClasses; j++) {
// Check if the class name is currently defined
var index = elementClasses.indexOf(classNames[j]);
// Add if stateValue === true or we are toggling and there is no value
if (state >= 0 && index < 0) {
elementClasses.push(classNames[j]);
} else if (state <= 0 && index >= 0) {
// Otherwise remove but only if the item exists
elementClasses.splice(index, 1);
}
}
this[i].attribs.class = elementClasses.join(' ');
}
return this;
};
/**
* Checks the current list of elements and returns `true` if _any_ of the
* elements match the selector. If using an element or Cheerio selection,
* returns `true` if _any_ of the elements match. If using a predicate
* function, the function is executed in the context of the selected element,
* so `this` refers to the current element.
*
* @param {string | Function | Cheerio | Node} selector - Selector for the selection.
* @see {@link https://api.jquery.com/is/}
*
* @returns {boolean} Whether or not the selector matches an element of the instance.
*/
exports.is = function (selector) {
if (selector) {
return this.filter(selector).length > 0;
}
return false;
};