UNPKG

1.31 kBJavaScriptView Raw
1'use strict';
2
3exports.type = 'perItem';
4
5exports.active = true;
6
7exports.description = 'cleanups attributes from newlines, trailing and repeating spaces';
8
9exports.params = {
10 newlines: true,
11 trim: true,
12 spaces: true
13};
14
15var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g,
16 regNewlines = /\r?\n/g,
17 regSpaces = /\s{2,}/g;
18
19/**
20 * Cleanup attributes values from newlines, trailing and repeating spaces.
21 *
22 * @param {Object} item current iteration item
23 * @param {Object} params plugin params
24 * @return {Boolean} if false, item will be filtered out
25 *
26 * @author Kir Belevich
27 */
28exports.fn = function(item, params) {
29
30 if (item.isElem()) {
31
32 item.eachAttr(function(attr) {
33
34 if (params.newlines) {
35 // new line which requires a space instead of themselve
36 attr.value = attr.value.replace(regNewlinesNeedSpace, function(match, p1, p2) {
37 return p1 + ' ' + p2;
38 });
39
40 // simple new line
41 attr.value = attr.value.replace(regNewlines, '');
42 }
43
44 if (params.trim) {
45 attr.value = attr.value.trim();
46 }
47
48 if (params.spaces) {
49 attr.value = attr.value.replace(regSpaces, ' ');
50 }
51
52 });
53
54 }
55
56};