UNPKG

4.4 kBJavaScriptView Raw
1var options = {
2 'attr-to-remove': [
3 'align',
4 'valign',
5 'bgcolor',
6 'color',
7 'width',
8 'height',
9 'border',
10 'cellpadding',
11 'cellspacing'
12 ],
13 'block-tags': [
14 'div',
15 'p',
16 'table',
17 'tr',
18 'td',
19 'blockquote',
20 'hr'
21 ],
22 'empty-tags': [
23 'br',
24 'hr',
25 'img'
26 ],
27 'pretty': true,
28 'remove-comments': false,
29 'tags-to-remove': [
30 'font'
31 ]
32 };
33
34function setup(opt) {
35 if (!opt) {
36 return;
37 }
38
39 options['attr-to-remove'] = opt['attr-to-remove'] || options['attr-to-remove'];
40 options['block-tags'] = opt['block-tags'] || options['block-tags'];
41 options['empty-tags'] = opt['empty-tags'] || options['empty-tags'];
42 options['pretty'] = opt['pretty'] || options['pretty'];
43 options['remove-comments'] = opt['remove-comments'] || options['remove-comments'];
44 options['tags-to-remove'] = opt['tags-to-remove'] || options['tags-to-remove'];
45
46 if (opt['add-attr-to-remove']) {
47 options['attr-to-remove'] = options['attr-to-remove'].concat(opt['add-attr-to-remove']);
48 }
49
50 if (opt['add-block-tags']) {
51 options['block-tags'] = options['block-tags'].concat(opt['add-block-tags']);
52 }
53
54 if (opt['add-empty-tags']) {
55 options['empty-tags'] = options['empty-tags'].concat(opt['add-empty-tags']);
56 }
57
58 if (opt['add-tags-to-remove']) {
59 options['tags-to-remove'] = options['tags-to-remove'].concat(opt['add-tags-to-remove']);
60 }
61}
62
63function replaceWhiteSpace(html) {
64 return html.replace(/\s/g, ' ');
65}
66
67function removeExtraSpaces(html) {
68 return html.replace(/ {2,}/g, ' ');
69}
70
71function removeTrailingSlash(tag) {
72 return tag.replace(/ ?\/>/, '>');
73}
74
75function cleanAttributes(tag) {
76 return tag.replace(/ (\w+)=['"].+?['"]/g, function (attribute, attributeName) {
77 if (options['attr-to-remove'].indexOf(attributeName) > -1) {
78 return '';
79 }
80
81 return attribute;
82 });
83}
84
85function cleanTags(html) {
86 return html.replace(/<\/?(\w+).*?>/g, function (tag, tagName) {
87 tag = tag.toLowerCase();
88 tagName = tagName.toLowerCase();
89
90 if (options['tags-to-remove'].indexOf(tagName) > -1) {
91 return '';
92 }
93
94 if (options['empty-tags'].indexOf(tagName) > -1) {
95 tag = removeTrailingSlash(tag);
96 }
97
98 tag = cleanAttributes(tag);
99
100 return tag;
101 });
102}
103
104function removeComments(html) {
105 return html.replace(/<!--.*?-->/g, '');
106}
107
108function addLineBreaks(html) {
109 return html.replace(/<\/?(\w+).*?>/g, function (tag, tagName) {
110 if (options['block-tags'].indexOf(tagName) > -1) {
111 return '\n' + tag + '\n';
112 }
113
114 if (tagName == 'br') {
115 return tag + '\n';
116 }
117
118 return tag;
119 });
120}
121
122function removeBlankLines(html) {
123 return html.replace(/\s{2,}/g, '\n');
124}
125
126function indentLine(line, indentLevel) {
127 var indent = '';
128
129 for (var i = 0; i < indentLevel; i++) {
130 indent += ' ';
131 }
132
133 return indent + line;
134}
135
136function indent(html) {
137 var indentLevel = 0;
138
139 return html.replace(/.*\n/g, function (line) {
140 var match = line.match(/<\/?(\w+).*?>/);
141
142 if (!match) {
143 return indentLine(line, indentLevel);
144 }
145
146 var tag = match[0],
147 tagName = match[1];
148
149 if (options['block-tags'].indexOf(tagName) > -1) {
150 if (tag.indexOf('</') === 0) {
151 indentLevel -= 2;
152 line = indentLine(line, indentLevel);
153 } else {
154 line = indentLine(line, indentLevel);
155 indentLevel += 2;
156 }
157
158 return line;
159 }
160
161 return indentLine(line, indentLevel);
162 });
163}
164
165function clean(html, opt) {
166 setup(opt);
167
168 html = replaceWhiteSpace(html);
169 html = removeExtraSpaces(html);
170 html = cleanTags(html);
171
172 if (options['remove-comments']) {
173 html = removeComments(html);
174 }
175
176 if (options['pretty']) {
177 html = addLineBreaks(html);
178 html = removeBlankLines(html);
179 html = indent(html);
180 }
181
182 return html.trim();
183}
184
185module.exports = {
186 clean: clean
187};