forked from whatnick/ATM90E26_SPI_DIN_Wemos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminifier.html
172 lines (160 loc) · 5.49 KB
/
minifier.html
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
<html>
<head>
<style>
html,
body {
height: 100%;
margin: 0;
}
body {
display: flex;
flex-direction: column;
}
.a,
.c {
flex: none;
margin: 10px;
}
.b {
overflow-y: hidden;
flex: auto;
margin: 0 10px;
border: 1px solid black;
}
label {
min-width: 120px;
display: inline-block;
}
#h {
height: 100%;
width: 100%;
border: none;
white-space: pre;
}
</style>
</head>
<body>
<div class="a">
<label for="varName">Variable Name:</label>
<input type="text" id="varName" value="webPage">
<br>
<label for="chunkSize">Chunk Size:</label>
<input type="text" id="chunkSize" value="75">
<br>
<label for="isCut">Cut?</label>
<input id="isCut" type="checkbox" checked></input>
<br>
<br> Copy and paste html .. or select html file:
<input type="file" id="inFile" onclick="reset(); this.value = null" ; onchange="processFile(this, event)"></input>
<br>
<br> Then,
<button id="btnMin" onclick="m()">Minifiy</button> ..then,
<button id="btnTest" onclick="test()" disabled>Test</button> (on IE, this opens a Save/Open dialog. On Chrome, this opens a new tab)
</div>
<div class="b">
<textarea contenteditable="true" id="h"></textarea>
</div>
<div class="c">
<button onclick="reset()">Reset</button>
<span id="savings"></span>
</div>
<script>
var uncut = "";
var orig = "";
function getValue(selector) {
return document.querySelector(selector).value;
}
function setInnerText(selector, val) {
document.querySelector(selector).innerText = val || "";
}
function setValue(selector, val) {
document.querySelector(selector).value = val;
}
function disableDom(selector) {
document.querySelector(selector) && (document.querySelector(selector).disabled = true);
}
function enableDom(selector) {
document.querySelector(selector) && (document.querySelector(selector).disabled = false);
}
function getSizeInKb(str) {
var m = encodeURIComponent(str).match(/%[89ABab]/g);
return (str.length + (m ? m.length : 0)) / 1000;
}
function reset() {
setValue("#h", null);
setValue("#inFile", null);
uncut = "";
orig = "";
disableDom("#btnTest");
enableDom("#btnMin");
setInnerText("#savings", "");
}
function round(num) {
return Math.round(num * 100) / 100;
}
function minify(s) {
orig = s;
var isCut = document.querySelector("#isCut").checked;
var varName = getValue("#varName") || "webPage";
var replaced = s
/* Remove comments (both html and js comments) */
.replace(/((<!--[\s\S]*?(?:-->)?<!---+>?|<!(?![dD][oO][cC][tT][yY][pP][eE]|\[CDATA\[)[^>]*>?|<[?][^>]*>?)|(\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$))/g, "")
.replace(/"/g, '\"') /* Escape quote */
.replace(/\s\s+/g, " ") /* Replace double spaces with a single space */
.replace("\r", "") /* Remove carriage returns */
.replace("\n", "") /* Remove new lines */
.replace(/\s*\(\s*/g, "(") /* Remove space before and after ( */
.replace(/\s*\)\s*/g, ")") /* Remove space before and after ) */
.replace(/\s*{\s*/g, "{") /* Remove space before and after { */
.replace(/\s*}\s*/g, "}") /* Remove space before and after { */
.replace(/\s*=\s*/g, "=") /* Remove space before and after = */
.replace(/\s*:\s*/g, ":") /* Remove space before and after : */
.replace(/\s*;\s*/g, ";") /* Remove space before and after ; */
.replace(/\s*,\s*/g, ",") /* Remove space before and after , */
.replace(/\s*>\s*/g, ">") /* Remove space before and after > */
.replace(/\s*<\s*/g, "<") /* Remove space before and after < */
;
uncut = replaced;
if (isCut) {
var chunkSize = getValue("#chunkSize") || 75;
var chunker = new RegExp(".{1," + chunkSize + "}", "g");
var chunks = replaced.match(chunker);
replaced = chunks.join("\";\r\n" + varName + " += \"");
}
enableDom("#btnTest");
disableDom("#btnMin");
var origSize = round(getSizeInKb(orig));
var replacedSize = round(getSizeInKb(replaced));
var diff = round(origSize - replacedSize);
var percentage = round(diff / origSize) * 100;
var savings = "Orig(" + origSize + "kb) - Minified(" + replacedSize + "kb) = " + diff + "kb (" + percentage + "% saved!)";
setInnerText("#savings", savings);
return varName + ' = "' + replaced + '";'
}
function m(s) {
setValue("#h", minify(getValue("#h")));
}
function processFile(source, event) {
var file = (event.target.files && event.target.files[0]) || null;
var fileReader = new FileReader();
fileReader.onload = function (e) {
var contents = e.target.result;
setValue("#h", contents);
}
fileReader.readAsText(file);
}
function test() {
/* create blob */
var b = new Blob(
[uncut],
{ type: 'text/html' }
);
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(b, "test.html");
} else {
window.open(URL.createObjectURL(b));
}
}
</script>
</body>
</html>