forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeBox.js
More file actions
executable file
·374 lines (288 loc) · 9.92 KB
/
Copy pathcodeBox.js
File metadata and controls
executable file
·374 lines (288 loc) · 9.92 KB
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
var resizeOnload = require('client/head/resizeOnload');
var isScrolledIntoView = require('client/isScrolledIntoView');
var addLineNumbers = require('./addLineNumbers');
function CodeBox(elem) {
var preElem = elem.querySelector('pre');
var codeElem = preElem.querySelector('code');
var code = codeElem.textContent;
Prism.highlightElement(codeElem);
addLineNumbers(preElem);
addBlockHighlight(preElem, elem.getAttribute('data-highlight-block'));
addInlineHighlight(preElem, elem.getAttribute('data-highlight-inline'));
var isJS = preElem.classList.contains('language-javascript');
var isHTML = preElem.classList.contains('language-markup');
var isTrusted = +elem.getAttribute('data-trusted');
var isNoStrict = +elem.getAttribute('data-no-strict');
if (!isNoStrict && isJS) code="'use strict';\n" + code;
var jsFrame;
var globalFrame;
var htmlResult;
var isFirstRun = true;
if (!isJS && !isHTML) return;
var runElem = elem.querySelector('[data-action="run"]');
if (runElem) {
runElem.onclick = function() {
this.blur();
run();
return false;
};
}
var editElem = elem.querySelector('[data-action="edit"]');
if (editElem) {
editElem.onclick = function() {
this.blur();
edit();
return false;
};
}
// some code can't be shown by epub engine
if (elem.hasAttribute('data-autorun')) {
if(window.ebookType == 'epub' && elem.getAttribute('data-autorun') == 'no-epub') {
elem.querySelector('iframe').remove();
} else {
// timeout should be small, around 10ms, or remove it to make crawler process the autorun
setTimeout(run, 100);
}
}
function postJSFrame() {
var win = jsFrame.contentWindow;
if (typeof win.postMessage != 'function') {
alert("Извините, запуск кода требует более современный браузер");
return;
}
win.postMessage(code, 'https://ru.lookatcode.com/showjs');
}
function runHTML() {
var frame;
if (htmlResult && elem.hasAttribute('data-refresh')) {
htmlResult.remove();
htmlResult = null;
}
if (!htmlResult) {
// take from HTML if exists there (in markup when autorun is specified)
htmlResult = elem.querySelector('.code-result');
}
if (!htmlResult) {
// otherwise create (or recreate if refresh)
htmlResult = document.createElement('div');
htmlResult.className = "code-result code-example__result";
frame = document.createElement('iframe');
frame.name = 'frame-' + Math.random();
frame.className = 'code-result__iframe';
if (elem.getAttribute('data-demo-height') === "0") {
// this html has nothing to show
frame.style.display = 'none';
} else if (elem.hasAttribute('data-demo-height')) {
var height = +elem.getAttribute('data-demo-height');
frame.style.height = height + 'px';
}
htmlResult.appendChild(frame);
elem.appendChild(htmlResult);
} else {
frame = htmlResult.querySelector('iframe');
}
if (isTrusted) {
var doc = frame.contentDocument || frame.contentWindow.document;
doc.open();
doc.write(normalizeHtml(code));
doc.close();
if(window.ebookType == 'epub') {
setTimeout(function() {
// remove script from iframes
// firefox saves the file with full iframe content (including script-generated) and the scripts
// scripts must not execute and autogenerate content again
[].forEach.call(doc.querySelectorAll('script'), function(script) {
script.remove();
});
// do it after timeout to allow external scripts (if any) to execute
}, 2000);
}
if (!elem.hasAttribute('data-demo-height')) {
resizeOnload.iframe(frame);
}
if (!(isFirstRun && elem.hasAttribute('data-autorun'))) {
if (!isScrolledIntoView(htmlResult)) {
htmlResult.scrollIntoView(false);
}
}
} else {
var form = document.createElement('form');
form.style.display = 'none';
form.method = 'POST';
form.enctype = "multipart/form-data";
form.action = "https://ru.lookatcode.com/showhtml";
form.target = frame.name;
var textarea = document.createElement('textarea');
textarea.name = 'code';
textarea.value = normalizeHtml(code);
form.appendChild(textarea);
frame.parentNode.insertBefore(form, frame.nextSibling);
form.submit();
form.remove();
if (!(isFirstRun && elem.hasAttribute('data-autorun'))) {
frame.onload = function() {
if (!elem.hasAttribute('data-demo-height')) {
resizeOnload.iframe(frame);
}
if (!isScrolledIntoView(htmlResult)) {
htmlResult.scrollIntoView(false);
}
};
}
}
}
// Evaluates a script in a global context
function globalEval( code ) {
var script = document.createElement( "script" );
script.text = code;
document.head.appendChild( script ).parentNode.removeChild( script );
}
function runJS() {
if (elem.hasAttribute('data-global')) {
if (!globalFrame) {
globalFrame = document.createElement('iframe');
globalFrame.className = 'js-frame';
globalFrame.style.width = 0;
globalFrame.style.height = 0;
globalFrame.style.border = 'none';
globalFrame.name = 'js-global-frame';
document.body.appendChild(globalFrame);
}
let form = document.createElement('form');
form.style.display = 'none';
form.method = 'POST';
form.enctype = "multipart/form-data";
form.action = "https://ru.lookatcode.com/showhtml";
form.target = 'js-global-frame';
var textarea = document.createElement('textarea');
textarea.name = 'code';
textarea.value = normalizeHtml('<script>\n' + code + '\n</script>');
form.appendChild(textarea);
globalFrame.parentNode.insertBefore(form, globalFrame.nextSibling);
form.submit();
form.remove();
} else if (isTrusted) {
if (elem.hasAttribute('data-autorun')) {
// make sure functions from "autorun" go to global scope
globalEval(code);
return;
}
try {
/* jshint -W061 */
window["eval"].call(window, code);
} catch (e) {
alert("Error: " + e.message);
}
} else {
if (elem.hasAttribute('data-refresh') && jsFrame) {
jsFrame.remove();
jsFrame = null;
}
if (!jsFrame) {
// create iframe for js
jsFrame = document.createElement('iframe');
jsFrame.className = 'js-frame';
jsFrame.src = 'https://ru.lookatcode.com/showjs';
jsFrame.style.width = 0;
jsFrame.style.height = 0;
jsFrame.style.border = 'none';
jsFrame.onload = function() {
postJSFrame();
};
document.body.appendChild(jsFrame);
} else {
postJSFrame();
}
}
}
function edit() {
var html;
if (isHTML) {
html = normalizeHtml(code);
} else {
var codeIndented = code.replace(/^/gim, ' ');
html = '<!DOCTYPE html>\n<html>\n\n<body>\n <script>\n' + codeIndented + '\n </script>\n</body>\n\n</html>';
}
var form = document.createElement('form');
form.action = "http://plnkr.co/edit/?p=preview";
form.method = "POST";
form.target = "_blank";
document.body.appendChild(form);
var textarea = document.createElement('textarea');
textarea.name = "files[index.html]";
textarea.value = html;
form.appendChild(textarea);
var input = document.createElement('input');
input.name = "description";
input.value = "Fork from " + window.location;
form.appendChild(input);
form.submit();
form.remove();
}
function normalizeHtml(code) {
var codeLc = code.toLowerCase();
var hasBodyStart = codeLc.match('<body>');
var hasBodyEnd = codeLc.match('</body>');
var hasHtmlStart = codeLc.match('<html>');
var hasHtmlEnd = codeLc.match('</html>');
var hasDocType = codeLc.match(/^\s*<!doctype/);
if (hasDocType) {
return code;
}
var result = code;
if (!hasHtmlStart) {
result = '<html>\n' + result;
}
if (!hasHtmlEnd) {
result = result + '\n</html>';
}
if (!hasBodyStart) {
result = result.replace('<html>', '<html>\n<head>\n <meta charset="utf-8">\n</head><body>\n');
}
if (!hasBodyEnd) {
result = result.replace('</html>', '\n</body>\n</html>');
}
result = '<!DOCTYPE HTML>\n' + result;
return result;
}
function run() {
if (isJS) {
runJS();
} else {
runHTML();
}
isFirstRun = false;
}
}
function addBlockHighlight(pre, lines) {
if (!lines) {
return;
}
var ranges = lines.replace(/\s+/g, '').split(',');
/*jshint -W084 */
for (var i = 0, range; range = ranges[i++];) {
range = range.split('-');
var start = +range[0],
end = +range[1] || start;
var mask = '<code class="block-highlight" data-start="' + start + '" data-end="' + end + '">' +
new Array(start + 1).join('\n') +
'<code class="mask">' + new Array(end - start + 2).join('\n') + '</code></code>';
pre.insertAdjacentHTML("afterBegin", mask);
}
}
function addInlineHighlight(pre, ranges) {
// select code with the language text, not block-highlighter
var codeElem = pre.querySelector('code[class*="language-"]');
ranges = ranges ? ranges.split(",") : [];
for (var i = 0; i < ranges.length; i++) {
var piece = ranges[i].split(':');
var lineNum = +piece[0], strRange = piece[1].split('-');
var start = +strRange[0], end = +strRange[1];
var mask = '<code class="inline-highlight">' +
new Array(lineNum + 1).join('\n') +
new Array(start + 1).join(' ') +
'<code class="mask">' + new Array(end - start + 1).join(' ') + '</code></code>';
codeElem.insertAdjacentHTML("afterBegin", mask);
}
}
module.exports = CodeBox;