-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.html
317 lines (297 loc) · 9.66 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
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<style>
body {
font-family: sans-serif;
}
textarea {
width: 100%;
font-family: monospace;
}
#output, #error {
background-color: #f8f8f8;
border: 1px solid #ddd;
padding: 0.5em;
font-family: monospace;
white-space: pre-wrap;
}
#result {
font-family: monospace;
white-space: pre-wrap;
}
table {
border-collapse: collapse;
}
td {
border: 1px solid lightgray;
padding: 0.25em;
}
.default {
opacity: 0.5;
}
#error {
color: #c00;
}
#outer-output, #url {
display: none;
}
</style>
</head>
<body>
<h1>noulith</h1>
<label for="code">Code:</label>
<textarea id="code"></textarea>
<label for="input">Input:</label> <select id="input-select"><option value="">user input</option><option value="file">file</option></select> <input type="file" id="input-file" />
<textarea id="input"></textarea>
<button id="run" type="button" role="button" disabled>Loading...</button>
<button id="stop" type="button" role="button" disabled>Loading...</button>
<label><input type="checkbox" id="fancy" checked /> Fancy output</label>
<label><input type="checkbox" id="auto" /> Auto-rerun</label>
<label style="display: none"><input type="checkbox" id="sanitize" checked /> Sanitize HTML</label>
<div id="outer-output">
<div id="mid-output">Output: <div id="output"></div></div>
<div id="mid-result">Result: <div id="result"></div></div>
<div id="mid-error">Error: <div id="error"></div></div>
</div>
<button id="gen-url" type="button" role="button">Generate URL</button>
<textarea id="url"></textarea>
<script src='pkg/paradoc.js'></script>
<script type="module">
// https://rustwasm.github.io/docs/wasm-bindgen/examples/without-a-bundler.html (???)
// wasm-pack build --target web
// import init, { encapsulated_eval } from './pkg/paradoc.js';
// wasm-pack build --target no-modules
// const { encapsulated_eval } = wasm_bindgen;
let myWorker;
async function prepare() {
// await wasm_bindgen('./pkg/paradoc_bg.wasm');
const code = document.getElementById('code');
const input = document.getElementById('input');
const fileInput = document.getElementById('input-file');
const runner = document.getElementById('run');
const stopper = document.getElementById('stop');
const fancy = document.getElementById('fancy');
const auto = document.getElementById('auto');
function run() {
runner.disabled = true;
stopper.disabled = false;
let sel = document.getElementById('input-select').value;
if (sel === 'file') {
let file = fileInput.files[0];
console.log(file);
file.arrayBuffer().then(buf => {
myWorker.postMessage([code.value, new Uint8Array(buf), fancy.checked]);
});
} else if (sel) {
fetch(sel).then(x => x.arrayBuffer().then(buf => {
console.log(buf);
myWorker.postMessage([code.value, new Uint8Array(buf), fancy.checked]);
}));
} else {
myWorker.postMessage([code.value, new TextEncoder().encode(input.value), fancy.checked]);
}
}
function stop() {
myWorker.terminate();
runner.disabled = true;
stopper.disabled = true;
init();
}
runner.addEventListener('click', run);
stopper.addEventListener('click', stop);
[code, input].forEach(elem => elem.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key == "Enter") {
run();
}
}));
[code, input].forEach(elem => elem.addEventListener('input', function(event) {
if (auto.checked) {
run();
}
}));
runner.disabled = false;
runner.textContent = "Run!";
stopper.textContent = "Stop";
}
function b64encode(s) {
const carr = [];
new TextEncoder().encode(s).forEach(function(u8) {
carr.push(String.fromCharCode(u8));
});
return btoa(carr.join(""));
}
function b64decode(s) {
const bs = atob(s);
const uarr = new Uint8Array(bs.length);
for (let i = 0; i < bs.length; i++) {
uarr[i] = bs.charCodeAt(i);
}
return new TextDecoder().decode(uarr);
}
const htmlAllowList = "a abbr acronym address article aside b big blockquote br caption cite code col colgroup dd del details dfn div dl dt em figcaption figure footer h1 h2 h3 h4 h5 h6 header hr i img ins kbd li mark ol p pre q rp rt ruby s samp small span sub sup strike strong sub summary sup table tbody td th thead tfoot tr time u ul var wbr svg svg:svg svg:circle svg:ellipse svg:g svg:line svg:path svg:polygon svg:polyline svg:rect svg:text svg:tspan".split(" ");
const svgAttrs = "color cx cy dx dy fill font-size font-style font-weight height r rx ry stroke-dasharray stroke-dashoffset stroke-linecap stroke-linejoin stroke-miterlimit stroke-opacity stroke stroke-width width viewbox x x1 x2 y y1 y2".split(" ");
function isAttrSafe(tag, attr, value) {
if (typeof tag !== "string" || typeof attr !== "string" || typeof value !== "string") return false;
tag = tag.toLowerCase();
if (attr === "style:color") return true;
if (attr === "style:backgroundColor") return true;
if (attr === "style:fontWeight") return true;
if (attr === "style:fontStyle") return true;
attr = attr.toLowerCase();
if (attr === "alt") return true;
if (attr === "title") return true;
if (tag === "a") {
return attr === "href" && (value.startsWith("http://") || value.startsWith("https://"));
}
if (tag.startsWith("svg")) {
return svgAttrs.includes(attr);
}
}
function renderInto(obj, node) {
console.log(obj);
console.log(node.tagName);
const sanitize = document.getElementById('sanitize');
if (Array.isArray(obj) && obj[0] === true && obj[1] === 'HtmlTag') {
let tagName = obj[2];
let children = obj[3];
let attributes = obj[4];
if (typeof tagName === "string" && (!sanitize.checked || htmlAllowList.includes(tagName.toLowerCase()))) {
let elem =
tagName.startsWith("svg:") ?
document.createElementNS("http://www.w3.org/2000/svg", tagName.slice(4)) :
document.createElement(tagName);
if (!Array.isArray(children) || children[0] === true) children = [children];
children.forEach(child => renderInto(child, elem));
if (Array.isArray(attributes)) {
for (let attr of attributes) {
if (Array.isArray(attr)) {
let attrKey = attr[0];
let attrValue = attr[1];
if ((!sanitize.checked || isAttrSafe(tagName, attrKey, attrValue))) {
if (typeof attrKey === "string" && attrKey.startsWith("style:")) {
elem.style[attrKey.slice(6)] = attrValue;
} else {
elem.setAttributeNS(null, attrKey, attrValue);
}
}
}
}
}
node.appendChild(elem);
return;
}
}
if (node.tagName === 'TR') {
if (Array.isArray(obj)) {
if (obj[0] === true) {
let th = document.createElement("th");
renderInto(obj[1], th);
node.appendChild(th);
obj = obj.slice(2);
}
for (let elt of obj) {
let td = document.createElement("td");
renderInto(elt, td);
node.appendChild(td);
}
} else {
let td = document.createElement("td");
if (obj === false) {
td.textContent = "default";
td.classList.add("default");
} else {
renderInto(obj, td);
}
node.appendChild(td);
}
} else {
if (Array.isArray(obj)) {
let table = document.createElement("table");
if (obj[0] === true) {
let tr = document.createElement("tr");
let th = document.createElement("th");
renderInto(obj[1], th);
tr.appendChild(th);
node.appendChild(tr);
obj = obj.slice(2);
}
for (let elt of obj) {
let tr = document.createElement("tr");
renderInto(elt, tr);
table.appendChild(tr);
}
node.appendChild(table);
} else {
if (obj === false) {
node.textContent = "default";
node.classList.add("default");
} else {
node.appendChild(document.createTextNode(obj));
}
}
}
}
function init() {
const outerOutput = document.getElementById('outer-output');
const output = document.getElementById('output');
const result = document.getElementById('result');
const error = document.getElementById('error');
const midOutput = document.getElementById('mid-output');
const midResult = document.getElementById('mid-result');
const midError = document.getElementById('mid-error');
const runner = document.getElementById('run');
const stopper = document.getElementById('stop');
myWorker = new Worker('worker.js');
myWorker.onmessage = function(e) {
if (e.data === "ready") {
prepare();
} else {
runner.disabled = false;
stopper.disabled = true;
outerOutput.style.display = 'block';
output.textContent = e.data[0];
midOutput.style.display = e.data[0] ? 'block' : 'none';
error.textContent = e.data[1];
midError.style.display = e.data[1] ? 'block' : 'none';
result.textContent = '';
renderInto(e.data[2], result);
midResult.style.display = (e.data[2] === null || e.data[2] === undefined) ? 'none' : 'block';
}
};
}
document.addEventListener('DOMContentLoaded', function() {
var base64 = location.hash.substr(1);
const code = document.getElementById('code');
const urlElem = document.getElementById('url');
const inputSelect = document.getElementById('input-select');
const sanitize = document.getElementById('sanitize');
sanitize.checked = true;
let h = base64.indexOf("#");
if (h >= 0) {
inputSelect.value = base64.slice(h + 1);
base64 = base64.slice(0, h);
}
if (base64.length) {
code.value = b64decode(base64);
}
document.getElementById('gen-url').addEventListener('click', function() {
urlElem.value = location.protocol + '//' + location.host + location.pathname + '#' + b64encode(code.value);
if (inputSelect.value) {
urlElem.value += "#" + inputSelect.value;
}
urlElem.style.display = 'block';
});
function changeSelect() {
const input = document.getElementById('input');
const fileInput = document.getElementById('input-file');
input.disabled = Boolean(inputSelect.value);
fileInput.disabled = inputSelect.value !== 'file';
}
changeSelect();
inputSelect.addEventListener('change', changeSelect);
init();
});
</script>
</body>
</html>