Skip to content

Commit 4dab9e3

Browse files
committed
Include necessary script tags in html snippets in code sandbox
To make the result less confusing
1 parent 75af596 commit 4dab9e3

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

html/js/code.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ addEventListener("load", function() {
2222
}, 500);
2323
});
2424

25+
function addIncludes(code, include) {
26+
if (!include) return code;
27+
return include.map(function(s) {
28+
return "<script src=\"" + s + "\"></script>\n";
29+
}).join("") + code;
30+
}
31+
function hasIncludes(code, include) {
32+
if (!include) return code;
33+
34+
var re = /(?:\s|<!--.*?-->)*<script src="(.*?)"><\/script>/g, m;
35+
var found = [];
36+
while (m = re.exec(code)) found.push(m[1]);
37+
return include.every(function(s) { return found.indexOf(s) > -1; });
38+
}
39+
2540
function setEditorCode(code, type) {
2641
editor.setValue(code);
2742
editor.setOption("mode", (type || guessType(code)) == "html" ? "text/html" : "javascript");
@@ -110,18 +125,22 @@ addEventListener("load", function() {
110125
code = "// " + code;
111126
else
112127
code = "<!-- " + code + "-->";
113-
if (chapter.start_code) code += "\n\n" + chapter.start_code;
128+
if (chapter.start_code) code += "\n\n" + addIncludes(chapter.start_code, chapter.include);
114129
context = {include: chapter.include};
115130
setEditorCode(code, guessed);
116131
visible = "box";
117132
} else {
118-
var exercise = findExercise(value, chapter);
133+
var exercise = findExercise(value, chapter), code = exercise.code, sol = exercise.solution;
134+
if (exercise.type == "html") {
135+
code = addIncludes(code, chapter.include);
136+
sol = addIncludes(sol, chapter.include);
137+
}
119138
context = {include: chapter.include,
120-
solution: exercise.solution,
139+
solution: sol,
121140
type: exercise.type};
122-
setEditorCode(exercise.code, exercise.type);
141+
setEditorCode(code, exercise.type);
123142
visible = "exercise";
124-
document.querySelector("#download").href = exercise.file;
143+
document.querySelector("#download").href = "data:text/plain;base64," + btoa(sol);
125144
}
126145
["box", "exercise"].forEach(function(id) {
127146
document.querySelector("#" + id + "_info").style.display =
@@ -144,17 +163,17 @@ addEventListener("load", function() {
144163

145164
function runCode() {
146165
clearSandbox();
147-
var type = context.type || guessType(editor.getValue());
166+
var val = editor.getValue(), type = context.type || guessType(val);
148167
sandbox = new SandBox({
149-
loadFiles: context.include,
168+
loadFiles: hasIncludes(val, context.include) ? [] : context.include,
150169
place: type == "html" &&
151170
function(node) { outnode.parentNode.insertBefore(node, outnode); }
152171
}, function(box) {
153172
output.clear();
154173
if (type == "html")
155-
box.setHTML(editor.getValue(), output);
174+
box.setHTML(val, output);
156175
else
157-
box.run(editor.getValue(), output);
176+
box.run(val, output);
158177
});
159178
}
160179

0 commit comments

Comments
 (0)