-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
397 lines (331 loc) · 11.4 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Template Diff Checker</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
background: #EEE;
font: 12px sans-serif;
}
body {
padding-top: 1.8em;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html,
body,
table,
tbody,
tr,
td {
height: 100%
}
html {
min-height: 100%;
}
body {
min-height: 100%;
font-family: sans-serif;
display: flex;
flex-direction: column;
}
#controls {
margin-bottom: 10px;
display: flex
}
.field {
display: flex;
align-items: center;
}
.field label {
font-weight: bold;
display: block;
}
#environmentSelector label {
margin-right: 10px
}
#diffContainer {
flex: 1;
width: 100%;
margin-top: 10px;
white-space: pre-wrap;
outline: 1px dashed red;
table-layout: fixed;
}
td {
width: 33%;
padding: 3px 4px;
border: 1px solid transparent;
vertical-align: top;
font: 1em monospace;
text-align: left;
white-space: pre-wrap;
}
h1 {
display: inline;
font-size: 100%;
}
del {
text-decoration: none;
color: #b30000;
background: #fadad7;
}
ins {
background: #eaf2c2;
color: #406619;
text-decoration: none;
}
#result {
white-space: pre-wrap;
}
#settings {
position: absolute;
top: 0;
left: 5px;
right: 5px;
height: 2em;
line-height: 2em;
}
#settings label {
margin-left: 1em;
}
.source {
position: absolute;
right: 1%;
top: .2em;
}
[contentEditable] {
background: #F9F9F9;
border-color: #BBB #D9D9D9 #DDD;
border-radius: 4px;
-webkit-user-modify: read-write-plaintext-only;
outline: none;
}
[contentEditable]:focus {
background: #FFF;
border-color: #6699cc;
box-shadow: 0 0 4px #2175c9;
}
.compare-button {
position: relative;
}
.compare-button__label {}
.compare-button__spinner {
opacity: 0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
.compare-button.is-loading .compare-button__spinner {
opacity: 1;
}
.compare-button.is-loading .compare-button__label {
opacity: 0;
}
.spinner {
user-select: none;
display: block;
height: 10px;
width: 10px;
border-width: 2px;
border-style: solid;
border-color: rgba(0, 0, 0, 0.2) rgba(0, 0, 0, 0.2) rgba(0, 0, 0, 0.2) transparent;
border-radius: 50%;
animation: spinner 1.2s linear infinite;
}
@keyframes spinner {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<nav id="controls">
<div id="templateSelector">
<div class="field">
<label for="templates">Select Template: </label>
<select id="templates"></select>
</div>
</div>
<div id="environmentSelector">
<div class="field">
<label for="env1">Environment 1: </label>
<select id="env1">
<option value="TEMPLATES">TEMPLATES</option>
<option value="DEV">DEV</option>
<option value="TESTING">TESTING</option>
<option value="LIVE">LIVE</option>
</select>
</div>
<div class="field">
<label for="env2">Environment 2: </label>
<select id="env2">
<option value="TEMPLATES">TEMPLATES</option>
<option value="DEV">DEV</option>
<option value="TESTING">TESTING</option>
<option value="LIVE" selected>LIVE</option>
</select>
</div>
</div>
<button class="compare-button" id="compareButton">
<span class="compare-button__label">Compare</span>
<span class="compare-button__spinner spinner"></span>
</button>
<!-- <progress id="progress" value="0" max="100"></progress> -->
</nav>
<table id="diffContainer">
<tr>
<td contenteditable="true" id="a"></td>
<td contenteditable="true" id="b"></td>
<td>
<pre id="result"></pre>
</td>
</tr>
</table>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jsdiff/5.1.0/diff.js"></script> -->
<script src="http://incaseofstairs.com/jsdiff/diff.js"></script>
<script>
const compareButton = document.getElementById('compareButton')
async function fetchHTML(url) {
try {
const response = await fetch(url, {
credentials: 'include'
})
console.log(`Fetching ${url} resulted in ${response.status}`)
// Log headers for debugging
for (var pair of response.headers.entries()) {
console.log(`${pair[0]}: ${pair[1]}`)
}
if (response.status >= 300 && response.status <= 399) {
const redirectURL = response.headers.get('Location')
console.log(`Redirecting to ${redirectURL}`)
return fetchHTML(redirectURL) // Recursive call to resolve redirect
}
return response.text()
} catch (error) {
console.log(`Failed to fetch ${url} with error ${error}`)
}
}
function generateURL(templateName, environment) {
const templateDetails = templateURLs[templateName]
if (!templateDetails) return null
let url
switch (environment) {
case 'TEMPLATES':
url = `templates/${templateDetails.TEMPLATES}`
break
case 'DEV':
url = `dev/${templateDetails.SERVER}`
break
case 'TESTING':
url = `testing/${templateDetails.SERVER}`
break
case 'LIVE':
url = `live/${templateDetails.SERVER}`
break
default:
return null
}
// Normalize URL to remove or fix double slashes
return url.replace(/([^:]\/)\/+/g, "$1")
}
async function performDiff(selectedTemplate, selectedEnv1, selectedEnv2) {
const url1 = generateURL(selectedTemplate, selectedEnv1)
const url2 = generateURL(selectedTemplate, selectedEnv2)
const html1 = formatHTML(await fetchHTML(url1))
const html2 = formatHTML(await fetchHTML(url2))
const filteredHTML1 = filterDOM(html1)
const filteredHTML2 = filterDOM(html2)
const diff = JsDiff.diffChars(filteredHTML1, filteredHTML2)
const a = document.getElementById('a')
const b = document.getElementById('b')
a.textContent = filteredHTML1
b.textContent = filteredHTML2
renderDiff(diff)
compareButton.classList.remove('is-loading')
}
function renderDiff(diff) {
const result = document.getElementById('result')
result.innerHTML = ''
diff.forEach(part => {
const color = part.added ? 'green' : part.removed ? 'red' : 'grey'
const span = document.createElement('span')
span.style.color = color
span.appendChild(document.createTextNode(part.value))
result.appendChild(span)
})
}
function filterDOM(html) {
// Filter out parts of the DOM that you want to ignore during the diff.
// Replace with actual logic for filtering the HTML
return html.replace(/<!--[\s\S]*?-->/g, '') // Removing all HTML comments as an example
}
function formatHTML(html) {
let formatted = html;
// Replace self-closing tags with open-close pair
formatted = formatted.replace(/<(\w+)\s*\/>/g, '<$1></$1>');
// Remove unnecessary white spaces between tags
formatted = formatted.replace(/\>\s+</g, '><');
// Add new lines after closing tags
formatted = formatted.replace(/<\/(\w+)>/g, '</$1>\n');
// Add new lines after self-closing tags
formatted = formatted.replace(/<(\w+)\s*><\/\1>/g, '<$1></$1>\n');
// Prevent double line breaks
formatted = formatted.replace(/\n\s*\n/g, '\n');
// Put closing tag of empty script elements on the same line
formatted = formatted.replace(/<script[^>]*>\s*<\/script>/g, match => match.replace(/\s+/g, ' '));
return formatted;
}
const templateURLs = {
'Users > Sign-in': {
'TEMPLATES': 'users/sign-in.html',
'SERVER': 'tcl/user/sign-in'
},
'Users > Register': {
'TEMPLATES': 'users/register.html',
'SERVER': 'tcl/user/register'
},
'Orders > Basket': {
'TEMPLATES': 'orders/basket.html',
'SERVER': 'tcl/my-order'
},
}
function getTemplates() {
return Object.keys(templateURLs)
}
document.addEventListener('DOMContentLoaded', () => {
const templateSelector = document.getElementById('templates')
const env1Selector = document.getElementById('env1')
const env2Selector = document.getElementById('env2')
getTemplates().forEach(template => {
const option = document.createElement('option')
option.value = template
option.textContent = template
templateSelector.appendChild(option)
})
compareButton.addEventListener('click', () => {
const selectedTemplate = templateSelector.value
const selectedEnv1 = env1Selector.value
const selectedEnv2 = env2Selector.value
compareButton.classList.add('is-loading')
performDiff(selectedTemplate, selectedEnv1, selectedEnv2)
})
})
</script>
</body>
</html>