forked from kangax/html-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.js
112 lines (95 loc) · 3.89 KB
/
master.js
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
/* global HTMLLint, minify */
(function() {
'use strict';
function byId(id) {
return document.getElementById(id);
}
function escapeHTML(str) {
return (str + '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
function getOptions() {
return {
removeIgnored: byId('remove-ignored').checked,
removeComments: byId('remove-comments').checked,
removeCommentsFromCDATA: byId('remove-comments-from-cdata').checked,
removeCDATASectionsFromCDATA: byId('remove-cdata-sections-from-cdata').checked,
collapseWhitespace: byId('collapse-whitespace').checked,
collapseBooleanAttributes: byId('collapse-boolean-attributes').checked,
removeAttributeQuotes: byId('remove-attribute-quotes').checked,
removeRedundantAttributes: byId('remove-redundant-attributes').checked,
useShortDoctype: byId('use-short-doctype').checked,
removeEmptyAttributes: byId('remove-empty-attributes').checked,
removeEmptyElements: byId('remove-empty-elements').checked,
removeOptionalTags: byId('remove-optional-tags').checked,
removeScriptTypeAttributes: byId('remove-script-type-attributes').checked,
removeStyleLinkTypeAttributes: byId('remove-style-link-type-attributes').checked,
caseSensitive: byId('case-sensitive').checked,
minifyJS: byId('minify-js').checked,
minifyCSS: byId('minify-css').checked,
lint: byId('use-htmllint').checked ? new HTMLLint() : null
};
}
function commify(str) {
return String(str)
.split('').reverse().join('')
.replace(/(...)(?!$)/g, '$1,')
.split('').reverse().join('');
}
byId('minify-btn').onclick = function() {
try {
var options = getOptions(),
lint = options.lint,
originalValue = byId('input').value,
minifiedValue = minify(originalValue, options),
diff = originalValue.length - minifiedValue.length,
savings = originalValue.length ? ((100 * diff) / originalValue.length).toFixed(2) : 0;
byId('output').value = minifiedValue;
byId('stats').innerHTML =
'<span class="success">' +
'Original size: <strong>' + commify(originalValue.length) + '</strong>' +
'. Minified size: <strong>' + commify(minifiedValue.length) + '</strong>' +
'. Savings: <strong>' + commify(diff) + ' (' + savings + '%)</strong>.' +
'</span>';
if (lint) {
lint.populate(byId('report'));
}
}
catch (err) {
byId('output').value = '';
byId('stats').innerHTML = '<span class="failure">' + escapeHTML(err) + '</span>';
}
};
function setCheckedAttrOnCheckboxes(attrValue) {
var checkboxes = byId('options').getElementsByTagName('input');
for (var i = checkboxes.length; i--; ) {
checkboxes[i].checked = attrValue;
}
}
byId('select-all').onclick = function() {
setCheckedAttrOnCheckboxes(true);
return false;
};
byId('select-none').onclick = function() {
setCheckedAttrOnCheckboxes(false);
return false;
};
byId('select-safe').onclick = function() {
setCheckedAttrOnCheckboxes(true);
var inputEls = byId('options').getElementsByTagName('input');
inputEls[10].checked = false;
inputEls[11].checked = false;
return false;
};
})();
/* jshint ignore:start */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1128111-22']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
document.getElementsByTagName('head')[0].appendChild(ga);
})();
/* jshint ignore:end */