-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
53 lines (44 loc) · 1.67 KB
/
index.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
if (!document.querySelector('[data-inliner]')) return;
var inline = require('./lib/inline');
var copyHtml = require('./lib/copyHtml');
// DOM elements
var htmlInput = document.querySelector('[data-input-html]');
var cssInput = document.querySelector('[data-input-css]');
var output = document.querySelector('[data-output]');
var compileButton = document.querySelector('[data-compile]');
var compressFlag = document.querySelector('[data-compress]');
var copyButton = document.querySelector('[data-output-copy]');
var errorMessage = document.querySelector('[data-error]');
// Event listeners
compileButton.addEventListener('click', function() {
// Don't do anything if the HTML textarea is empty
if (!htmlInput.value) return;
// Reset the error state of the UI
errorMessage.classList.remove('is-visible');
// Inline CSS
inline(htmlInput.value, cssInput.value, {
compress: compressFlag.checked,
onSuccess: onSuccess,
onError: onError
});
});
copyButton.addEventListener('click', function() {
copyHtml(output, copyButton);
});
/**
* Updates the state of the UI to display inlined HTML. This function is called when the inlining process completes without errors.
* @param {string} html - Unescpaed HTML output.
*/
function onSuccess(html) {
html = html.replace(/</g, '<');
html = html.replace(/>/g, '>');
output.innerHTML = html;
}
/**
* Updates the state of the UI to display an error message. This function is called if the inlining process encounters an error.
*/
function onError() {
errorMessage.classList.add('is-visible');
}
// Remove the class that displays a loading indicator
document.querySelector('[data-inliner]').classList.remove('is-not-loaded');