1 | var 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 | 'break-after-br': true,
|
23 | 'close-empty-tags': false,
|
24 | 'empty-tags': [
|
25 | 'br',
|
26 | 'hr',
|
27 | 'img'
|
28 | ],
|
29 | 'indent': ' ',
|
30 | 'pretty': true,
|
31 | 'remove-comments': false,
|
32 | 'tags-to-remove': [
|
33 | 'font'
|
34 | ]
|
35 | };
|
36 |
|
37 | function setup(opt) {
|
38 | if (!opt) {
|
39 | return;
|
40 | }
|
41 |
|
42 | options['attr-to-remove'] = opt['attr-to-remove'] || options['attr-to-remove'];
|
43 | options['block-tags'] = opt['block-tags'] || options['block-tags'];
|
44 | options['break-after-br'] = opt['break-after-br'] === false ? false : options['break-after-br'];
|
45 | options['close-empty-tags'] = opt['close-empty-tags'] || options['close-empty-tags'];
|
46 | options['empty-tags'] = opt['empty-tags'] || options['empty-tags'];
|
47 | options['indent'] = opt['indent'] || options['indent'];
|
48 | options['pretty'] = opt['pretty'] === false ? false : options['pretty'];
|
49 | options['remove-comments'] = opt['remove-comments'] || options['remove-comments'];
|
50 | options['tags-to-remove'] = opt['tags-to-remove'] || options['tags-to-remove'];
|
51 |
|
52 | if (opt['add-attr-to-remove']) {
|
53 | options['attr-to-remove'] = options['attr-to-remove'].concat(opt['add-attr-to-remove']);
|
54 | }
|
55 |
|
56 | if (opt['add-block-tags']) {
|
57 | options['block-tags'] = options['block-tags'].concat(opt['add-block-tags']);
|
58 | }
|
59 |
|
60 | if (opt['add-empty-tags']) {
|
61 | options['empty-tags'] = options['empty-tags'].concat(opt['add-empty-tags']);
|
62 | }
|
63 |
|
64 | if (opt['add-tags-to-remove']) {
|
65 | options['tags-to-remove'] = options['tags-to-remove'].concat(opt['add-tags-to-remove']);
|
66 | }
|
67 | }
|
68 |
|
69 | function replaceWhiteSpace(html) {
|
70 | return html.replace(/\s/g, ' ');
|
71 | }
|
72 |
|
73 | function removeExtraSpaces(html) {
|
74 | return html.replace(/ {2,}/g, ' ');
|
75 | }
|
76 |
|
77 | function closeEmptyTag(tag) {
|
78 | return tag.replace(/ ?\/?>/, '/>');
|
79 | }
|
80 |
|
81 | function removeTrailingSlash(tag) {
|
82 | return tag.replace(/ ?\/>/, '>');
|
83 | }
|
84 |
|
85 | function cleanAttributes(tag) {
|
86 | return tag.replace(/ (\w+)=['"].+?['"]/g, function (attribute, attributeName) {
|
87 | if (options['attr-to-remove'].indexOf(attributeName) > -1) {
|
88 | return '';
|
89 | }
|
90 |
|
91 | return attribute;
|
92 | });
|
93 | }
|
94 |
|
95 | function cleanTags(html) {
|
96 | return html.replace(/<\/?(\w+).*?>/g, function (tag, tagName) {
|
97 | tag = tag.toLowerCase();
|
98 | tagName = tagName.toLowerCase();
|
99 |
|
100 | if (options['tags-to-remove'].indexOf(tagName) > -1) {
|
101 | return '';
|
102 | }
|
103 |
|
104 | if (options['empty-tags'].indexOf(tagName) > -1) {
|
105 | if (options['close-empty-tags']) {
|
106 | tag = closeEmptyTag(tag);
|
107 | } else {
|
108 | tag = removeTrailingSlash(tag);
|
109 | }
|
110 | }
|
111 |
|
112 | tag = cleanAttributes(tag);
|
113 |
|
114 | return tag;
|
115 | });
|
116 | }
|
117 |
|
118 | function removeComments(html) {
|
119 | return html.replace(/<!--.*?-->/g, '');
|
120 | }
|
121 |
|
122 | function addLineBreaks(html) {
|
123 | return html.replace(/<\/?(\w+).*?>/g, function (tag, tagName) {
|
124 | if (options['block-tags'].indexOf(tagName) > -1) {
|
125 | return '\n' + tag + '\n';
|
126 | }
|
127 |
|
128 | if (tagName == 'br' && options['break-after-br']) {
|
129 | return tag + '\n';
|
130 | }
|
131 |
|
132 | return tag;
|
133 | });
|
134 | }
|
135 |
|
136 | function removeBlankLines(html) {
|
137 | return html.replace(/\s{2,}/g, '\n');
|
138 | }
|
139 |
|
140 | function indentLine(line, indentLevel) {
|
141 | var indent = '';
|
142 |
|
143 | for (var i = 0; i < indentLevel; i++) {
|
144 | indent += options['indent'];
|
145 | }
|
146 |
|
147 | return indent + line;
|
148 | }
|
149 |
|
150 | function indent(html) {
|
151 | if (!options['indent']) {
|
152 | return;
|
153 | }
|
154 |
|
155 | var indentLevel = 0;
|
156 |
|
157 | return html.replace(/.*\n/g, function (line) {
|
158 | var match = line.match(/<\/?(\w+).*?>/);
|
159 |
|
160 | if (!match) {
|
161 | return indentLine(line, indentLevel);
|
162 | }
|
163 |
|
164 | var tag = match[0],
|
165 | tagName = match[1];
|
166 |
|
167 | if (options['block-tags'].indexOf(tagName) > -1) {
|
168 | if (tag.indexOf('</') === 0) {
|
169 | indentLevel--;
|
170 | line = indentLine(line, indentLevel);
|
171 | } else {
|
172 | line = indentLine(line, indentLevel);
|
173 | indentLevel++;
|
174 | }
|
175 |
|
176 | return line;
|
177 | }
|
178 |
|
179 | return indentLine(line, indentLevel);
|
180 | });
|
181 | }
|
182 |
|
183 | function clean(html, opt) {
|
184 | setup(opt);
|
185 |
|
186 | html = replaceWhiteSpace(html);
|
187 | html = removeExtraSpaces(html);
|
188 | html = cleanTags(html);
|
189 |
|
190 | if (options['remove-comments']) {
|
191 | html = removeComments(html);
|
192 | }
|
193 |
|
194 | if (options['pretty']) {
|
195 | html = addLineBreaks(html);
|
196 | html = removeBlankLines(html);
|
197 | html = indent(html);
|
198 | }
|
199 |
|
200 | return html.trim();
|
201 | }
|
202 |
|
203 | module.exports = {
|
204 | clean: clean
|
205 | };
|