freeCodeCamp/client/commonFramework/execute-challenge-stream.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-11-18 05:25:16 +00:00
window.common = (function(global) {
const {
ga,
common = { init: [] }
} = global;
2015-11-30 22:27:39 +00:00
const {
addLoopProtect,
2015-12-02 01:24:50 +00:00
getJsFromHtml,
2015-11-30 22:27:39 +00:00
detectUnsafeCode$,
updatePreview$,
challengeType,
challengeTypes
} = common;
2015-11-18 05:25:16 +00:00
common.executeChallenge$ = function executeChallenge$() {
const code = common.editor.getValue();
const originalCode = code;
2015-11-18 05:25:16 +00:00
const head = common.arrayToNewLineString(common.head);
const tail = common.arrayToNewLineString(common.tail);
2015-11-30 22:27:39 +00:00
const combinedCode = head + code + tail;
2015-11-18 05:25:16 +00:00
ga('send', 'event', 'Challenge', 'ran-code', common.challengeName);
2015-11-24 21:48:14 +00:00
// run checks for unsafe code
2015-11-30 22:27:39 +00:00
return detectUnsafeCode$(code)
2015-11-24 21:48:14 +00:00
// add head and tail and detect loops
2015-11-30 22:27:39 +00:00
.map(() => {
if (challengeType !== challengeTypes.HTML) {
return `<script>;${addLoopProtect(combinedCode)}/**/</script>`;
}
2015-11-18 05:25:16 +00:00
2015-11-30 22:27:39 +00:00
return addLoopProtect(combinedCode);
})
.flatMap(code => updatePreview$(code))
.flatMap(code => {
let output;
if (
challengeType === challengeTypes.HTML &&
common.hasJs(code)
) {
2015-12-02 01:24:50 +00:00
output = common.getJsOutput(getJsFromHtml(code));
2015-11-30 22:27:39 +00:00
} else if (challengeType !== challengeTypes.HTML) {
2015-12-02 01:24:50 +00:00
output = common.getJsOutput(addLoopProtect(combinedCode));
2015-11-30 22:27:39 +00:00
}
2015-11-18 05:25:16 +00:00
2015-11-30 22:27:39 +00:00
return common.runPreviewTests$({
tests: common.tests.slice(),
originalCode,
output
2015-11-23 03:42:53 +00:00
});
2015-11-18 05:25:16 +00:00
});
};
return common;
}(window));