2015-08-27 12:37:41 -07:00
|
|
|
/* globals jailed, CodeMirror, challenge_Id, challenge_Name, challengeType */
|
2015-08-27 00:02:07 -07:00
|
|
|
// codeStorage
|
|
|
|
var codeStorageFactory = (function($, localStorage) {
|
|
|
|
|
|
|
|
var CodeStorageProps = {
|
|
|
|
version: 0.01,
|
|
|
|
keyVersion: 'saveVersion',
|
|
|
|
keyValue: null,
|
|
|
|
updateWait: 2000,
|
|
|
|
updateTimeoutId: null
|
|
|
|
};
|
|
|
|
|
|
|
|
var CodeStorage = {
|
|
|
|
hasSaved: function() {
|
|
|
|
return this.updateTimeoutId === null;
|
|
|
|
},
|
|
|
|
|
|
|
|
onSave: function(func) {
|
|
|
|
this.eventArray.push(func);
|
|
|
|
},
|
|
|
|
|
|
|
|
setSaveKey: function(key) {
|
|
|
|
this.keyValue = key + 'Val';
|
|
|
|
},
|
|
|
|
|
|
|
|
getStoredValue: function() {
|
|
|
|
return '' + localStorage.getItem(this.keyValue);
|
|
|
|
},
|
|
|
|
|
|
|
|
setEditor: function(editor) {
|
|
|
|
this.editor = editor;
|
|
|
|
},
|
|
|
|
|
|
|
|
isAlive: function() {
|
|
|
|
var val = this.getStoredValue();
|
|
|
|
return val !== 'null' &&
|
|
|
|
val !== 'undefined' &&
|
|
|
|
(val && val.length > 0);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateStorage: function() {
|
|
|
|
if (typeof localStorage !== 'undefined') {
|
|
|
|
var value = this.editor.getValue();
|
|
|
|
localStorage.setItem(this.keyValue, value);
|
|
|
|
} else {
|
|
|
|
console.log('no web storage');
|
|
|
|
}
|
|
|
|
this.updateTimeoutId = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function codeStorageFactory(editor, challengeName) {
|
|
|
|
var codeStorage = Object.create(CodeStorage);
|
|
|
|
$.extend(codeStorage, CodeStorageProps);
|
|
|
|
codeStorage.setEditor(editor);
|
|
|
|
codeStorage.setSaveKey(challengeName);
|
|
|
|
return codeStorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
var savedVersion = localStorage.getItem(CodeStorageProps.keyVersion);
|
|
|
|
if (savedVersion === null) {
|
|
|
|
localStorage.setItem(
|
|
|
|
CodeStorageProps.keyVersion,
|
|
|
|
CodeStorageProps.version
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return codeStorageFactory;
|
|
|
|
}($, localStorage));
|
|
|
|
|
2015-08-27 12:37:41 -07:00
|
|
|
var sandBox = (function() {
|
|
|
|
|
|
|
|
var plugin = null;
|
|
|
|
|
|
|
|
var sandBox = {
|
|
|
|
};
|
|
|
|
|
|
|
|
var printCallback;
|
|
|
|
|
|
|
|
// sends the input to the plugin for evaluation
|
|
|
|
function submit(code, callback) {
|
|
|
|
printCallback = callback;
|
|
|
|
|
|
|
|
// postpone the evaluation until the plugin is initialized
|
|
|
|
plugin.whenConnected(function() {
|
|
|
|
if (requests === 0) {
|
|
|
|
startLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
requests++;
|
|
|
|
plugin.remote.run(code);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// puts the message on the terminal
|
|
|
|
var print = function(cls, msg) {
|
|
|
|
printCallback(cls, msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// will restart the plugin if it does not respond
|
|
|
|
var disconnectTimeout = null;
|
|
|
|
var startLoading = function() {
|
|
|
|
disconnectTimeout = setTimeout(disconnect, 3000);
|
|
|
|
};
|
|
|
|
|
|
|
|
var endLoading = function() {
|
|
|
|
clearTimeout(disconnectTimeout);
|
|
|
|
};
|
|
|
|
|
|
|
|
var disconnect = function() {
|
|
|
|
plugin.disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// interface provided to the plugin
|
|
|
|
var api = {
|
|
|
|
output: function(data) {
|
|
|
|
endLoading();
|
|
|
|
// print('input', data.input);
|
|
|
|
|
|
|
|
if (data.error) {
|
|
|
|
print('Error', data);
|
|
|
|
reset();
|
|
|
|
} else {
|
|
|
|
print(null, data);
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// obtaining absolute path of this script
|
|
|
|
var scripts = document.getElementsByTagName('script');
|
|
|
|
var path = scripts[scripts.length - 1].src
|
|
|
|
.split('?')[0]
|
|
|
|
.split('/')
|
|
|
|
.slice(0, -1)
|
|
|
|
.join('/') + '/';
|
|
|
|
|
|
|
|
var requests;
|
|
|
|
|
|
|
|
// (re)initializes the plugin
|
|
|
|
var reset = function() {
|
|
|
|
requests = 0;
|
|
|
|
plugin = new jailed.Plugin(path + 'plugin.js', api);
|
|
|
|
plugin.whenDisconnected( function() {
|
|
|
|
// give some time to handle the last responce
|
|
|
|
setTimeout( function() {
|
|
|
|
endLoading();
|
|
|
|
console.log('resetting on fatal plugin error');
|
|
|
|
|
|
|
|
if (challengeType === 0) {
|
|
|
|
codeOutput.setValue(
|
|
|
|
'Sorry, your code is either too slow, has a fatal error, ' +
|
|
|
|
'or contains an infinite loop.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
reset();
|
|
|
|
}, 10);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
reset();
|
|
|
|
sandBox.submit = submit;
|
|
|
|
return sandBox;
|
|
|
|
}());
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
function replaceSafeTags(value) {
|
|
|
|
return value
|
|
|
|
.replace(/fccss/gi, '<script>')
|
|
|
|
.replace(/fcces/gi, '</script>');
|
|
|
|
}
|
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
var BDDregex = new RegExp(
|
2015-08-27 12:37:41 -07:00
|
|
|
'(expect(\\s+)?\\(.*\\;)|' +
|
|
|
|
'(assert(\\s+)?\\(.*\\;)|' +
|
|
|
|
'(assert\\.\\w.*\\;)|' +
|
|
|
|
'(.*\\.should\\..*\\;)/'
|
2015-08-27 11:23:17 -07:00
|
|
|
);
|
|
|
|
|
2015-08-25 19:01:48 -07:00
|
|
|
var isInitRun = false;
|
2015-08-26 13:47:38 -07:00
|
|
|
var initPreview = true;
|
2015-08-23 21:59:29 +01:00
|
|
|
var editor;
|
2015-08-27 00:02:07 -07:00
|
|
|
|
|
|
|
editor = CodeMirror.fromTextArea(document.getElementById('codeEditor'), {
|
|
|
|
lineNumbers: true,
|
|
|
|
mode: 'text',
|
|
|
|
theme: 'monokai',
|
|
|
|
runnable: true,
|
|
|
|
matchBrackets: true,
|
|
|
|
autoCloseBrackets: true,
|
|
|
|
scrollbarStyle: 'null',
|
|
|
|
lineWrapping: true,
|
|
|
|
gutters: ['CodeMirror-lint-markers']
|
2015-08-23 21:59:29 +01:00
|
|
|
});
|
|
|
|
|
2015-08-27 00:02:07 -07:00
|
|
|
var codeStorage = codeStorageFactory(editor, challenge_Name);
|
2015-08-23 21:59:29 +01:00
|
|
|
var myCodeMirror = editor;
|
|
|
|
|
2015-08-27 00:02:07 -07:00
|
|
|
editor.on('keyup', function() {
|
|
|
|
clearTimeout(codeStorage.updateTimeoutId);
|
|
|
|
codeStorage.updateTimeoutId = setTimeout(
|
2015-08-31 22:48:20 -07:00
|
|
|
codeStorage.updateStorage.bind(codeStorage),
|
2015-08-27 00:02:07 -07:00
|
|
|
codeStorage.updateWait
|
|
|
|
);
|
2015-08-23 21:59:29 +01:00
|
|
|
});
|
2015-08-27 00:02:07 -07:00
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
var editorValue;
|
|
|
|
var challengeSeed = challengeSeed || null;
|
|
|
|
var tests = tests || [];
|
|
|
|
var allSeeds = '';
|
2015-08-27 00:02:07 -07:00
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
(function() {
|
|
|
|
challengeSeed.forEach(function(elem) {
|
|
|
|
allSeeds += elem + '\n';
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
2015-09-09 14:39:01 -07:00
|
|
|
if (typeof emmetCodeMirror !== 'undefined') {
|
|
|
|
var defaultKeymap = {
|
|
|
|
'Cmd-E': 'emmet.expand_abbreviation',
|
|
|
|
'Tab': 'emmet.expand_abbreviation_with_tab',
|
|
|
|
'Enter': 'emmet.insert_formatted_line_break_only'
|
|
|
|
};
|
2015-09-06 21:34:33 +05:30
|
|
|
|
2015-09-09 14:39:01 -07:00
|
|
|
emmetCodeMirror(editor, defaultKeymap);
|
|
|
|
}
|
2015-09-06 21:34:33 +05:30
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
editor.setOption('extraKeys', {
|
|
|
|
Tab: function(cm) {
|
|
|
|
if (cm.somethingSelected()) {
|
|
|
|
cm.indentSelection('add');
|
|
|
|
} else {
|
|
|
|
var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
|
|
|
|
cm.replaceSelection(spaces);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'Shift-Tab': function(cm) {
|
|
|
|
if (cm.somethingSelected()) {
|
|
|
|
cm.indentSelection('subtract');
|
|
|
|
} else {
|
|
|
|
var spaces = Array(cm.getOption('indentUnit') + 1).join(' ');
|
|
|
|
cm.replaceSelection(spaces);
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 00:15:13 -07:00
|
|
|
},
|
|
|
|
'Ctrl-Enter': function() {
|
2015-09-09 20:13:35 -07:00
|
|
|
isInitRun = false;
|
2015-08-27 00:15:13 -07:00
|
|
|
bonfireExecute(true);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
});
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
editor.setSize('100%', 'auto');
|
2015-08-23 21:59:29 +01:00
|
|
|
|
|
|
|
var libraryIncludes = "<script src='//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>" +
|
|
|
|
"<script src='/js/lib/chai/chai.js'></script>" +
|
|
|
|
"<script src='/js/lib/chai/chai-jquery.js'></script>" +
|
|
|
|
"<script src='//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js'></script>" +
|
|
|
|
"<link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.min.css'/>" +
|
|
|
|
"<link rel='stylesheet' href='//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css'/>" +
|
|
|
|
"<link rel='stylesheet' href='//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css'/>" +
|
2015-08-27 11:23:17 -07:00
|
|
|
'<style>body { padding: 0px 3px 0px 3px; }</style>' +
|
|
|
|
'<script>var expect = chai.expect; var should = chai.should(); var assert = chai.assert;</script>';
|
2015-08-23 21:59:29 +01:00
|
|
|
|
|
|
|
var editorValueForIFrame;
|
2015-08-26 23:20:56 -07:00
|
|
|
var iFrameScript = "<script src='/js/iFrameScripts.js'></script>";
|
2015-08-23 21:59:29 +01:00
|
|
|
var delay;
|
2015-08-27 00:15:13 -07:00
|
|
|
|
|
|
|
function workerError(error) {
|
|
|
|
var display = $('.runTimeError');
|
|
|
|
var housing = $('#testSuite');
|
|
|
|
if (display.html() !== error) {
|
|
|
|
display.remove();
|
|
|
|
housing.prepend(
|
|
|
|
'<div class="runTimeError" style="font-size: 18px;"><code>' +
|
|
|
|
error.replace(/j\$/gi, '$').replace(/jdocument/gi, 'document').replace(/jjQuery/gi, 'jQuery') +
|
|
|
|
'</code></div>'
|
|
|
|
);
|
|
|
|
display.hide().fadeIn(function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
display.fadeOut(function() {
|
|
|
|
display.remove();
|
2015-08-25 19:28:13 +01:00
|
|
|
});
|
2015-08-27 00:15:13 -07:00
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
2015-08-25 19:28:13 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
function scopejQuery(str) {
|
|
|
|
return str
|
|
|
|
.replace(/\$/gi, 'j$')
|
|
|
|
.replace(/document/gi, 'jdocument')
|
|
|
|
.replace(/jQuery/gi, 'jjQuery');
|
2015-08-25 19:28:13 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
function safeHTMLRun(test) {
|
2015-08-27 11:23:17 -07:00
|
|
|
if (challengeType === '0') {
|
|
|
|
var previewFrame = document.getElementById('preview');
|
|
|
|
var preview = previewFrame.contentDocument || previewFrame.contentWindow.document;
|
|
|
|
if (editor.getValue().match(/\<script\>/gi) !== null) {
|
|
|
|
var s = editor
|
|
|
|
.getValue()
|
|
|
|
.split(/\<\s?script\s?\>/gi)[1]
|
|
|
|
.split(/\<\s?\/\s?script\s?\>/gi)[0];
|
|
|
|
|
|
|
|
// add feuxQuery
|
|
|
|
s = "var document = \"\"; var $ = function() {return(new function() {this.add=function() {return(this);};this.addBack=function() {return(this);};this.addClass=function() {return(this);};this.after=function() {return(this);};this.ajaxComplete=function() {return(this);};this.ajaxError=function() {return(this);};this.ajaxSend=function() {return(this);};this.ajaxStart=function() {return(this);};this.ajaxStop=function() {return(this);};this.ajaxSuccess=function() {return(this);};this.andSelf=function() {return(this);};this.animate=function() {return(this);};this.append=function() {return(this);};this.appendTo=function() {return(this);};this.attr=function() {return(this);};this.before=function() {return(this);};this.bind=function() {return(this);};this.blur=function() {return(this);};this.callbacksadd=function() {return(this);};this.callbacksdisable=function() {return(this);};this.callbacksdisabled=function() {return(this);};this.callbacksempty=function() {return(this);};this.callbacksfire=function() {return(this);};this.callbacksfired=function() {return(this);};this.callbacksfireWith=function() {return(this);};this.callbackshas=function() {return(this);};this.callbackslock=function() {return(this);};this.callbackslocked=function() {return(this);};this.callbacksremove=function() {return(this);};this.change=function() {return(this);};this.children=function() {return(this);};this.clearQueue=function() {return(this);};this.click=function() {return(this);};this.clone=function() {return(this);};this.closest=function() {return(this);};this.contents=function() {return(this);};this.context=function() {return(this);};this.css=function() {return(this);};this.data=function() {return(this);};this.dblclick=function() {return(this);};this.delay=function() {return(this);};this.delegate=function() {return(this);};this.dequeue=function() {return(this);};this.detach=function() {return(this);};this.die=function() {return(this);};this.each=function() {return(this);};this.empty=function() {return(this);};this.end=function() {return(this);};this.eq=function() {return(this);};this.error=function() {return(this);};this.fadeIn=function() {return(this);};this.fadeOut=function() {return(this);};this.fadeTo=function() {return(this);};this.fadeToggle=function() {return(this);};this.filter=function() {return(this);};this.find=function() {return(this);};this.finish=function() {return(this);};this.first=function() {return(this);};this.focus=function() {return(this);};this.focusin=function() {return(this);};this.focusout=function() {return(this);};this.get=function() {return(this);};this.has=function() {return(this);};this.hasClass=function() {return(this);};this.height=function() {return(this);};this.hide=function() {return(this);};this.hover=function() {return(this);};this.html=function() {return(this);};this.index=function() {return(this);};this.innerHeight=function() {return(this);};this.innerWidth=function() {return(this);};this.insertAfter=function() {return(this);};this.insertBefore=function() {return(this);};this.is=function() {return(this);};this.jQuery=function() {return(this);};this.jquery=function() {return(this);};this.keydown=function() {return(this);};this.keypress=function() {return(this);};this.keyup=function() {return(this);};this.last=function() {return(this);};this.length=function() {return(this);};this.live=function() {return(this);};this.load=function() {return(this);};this.load=function() {return(this);};this.map=function() {return(this);};this.mousedown=function() {return(this);};this.mouseenter=function() {return(this);};this.mouseleave=function() {return(this);};this.mousemove=function() {return(this);};this.mouseout=function() {return(this);};this.mouseover=function() {return(this);};this.mouseup=function() {return(this);};this.next=function() {return(this);};this.nextAll=function() {return(this);};this.nextUntil=function() {return(this);};this.not=function() {return(this);};this.off=function() {return(this);};this.offset=function() {return(this);};this.offsetParent=function() {return(this);};this.on=function() {return(this);
|
|
|
|
|
2015-08-27 12:37:41 -07:00
|
|
|
sandBox.submit(scopejQuery(s), function(cls, message) {
|
2015-08-27 11:23:17 -07:00
|
|
|
if (cls) {
|
|
|
|
console.log(message.error);
|
|
|
|
workerError(message.error);
|
|
|
|
} else if (test) {
|
|
|
|
preview.open();
|
|
|
|
preview.write(libraryIncludes + editor.getValue() + iFrameScript);
|
|
|
|
codeStorage.updateStorage();
|
|
|
|
preview.close();
|
|
|
|
} else {
|
|
|
|
preview.open();
|
|
|
|
preview.write(libraryIncludes + editor.getValue());
|
|
|
|
codeStorage.updateStorage();
|
|
|
|
preview.close();
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
});
|
|
|
|
} else if (test) {
|
|
|
|
preview.open();
|
|
|
|
preview.write(libraryIncludes + editor.getValue() + iFrameScript);
|
|
|
|
codeStorage.updateStorage();
|
|
|
|
preview.close();
|
|
|
|
} else {
|
|
|
|
preview.open();
|
|
|
|
preview.write(libraryIncludes + editor.getValue());
|
|
|
|
codeStorage.updateStorage();
|
|
|
|
preview.close();
|
2015-08-24 01:16:15 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
2015-08-24 01:16:15 +01:00
|
|
|
}
|
2015-08-23 22:45:28 +01:00
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
function updatePreview() {
|
|
|
|
editorValueForIFrame = editor.getValue();
|
|
|
|
var failedCommentTest = false;
|
|
|
|
if (
|
|
|
|
editorValueForIFrame.match(/\<\!\-\-/gi) &&
|
|
|
|
editorValueForIFrame.match(/\-\-\>/gi) == null
|
|
|
|
) {
|
|
|
|
failedCommentTest = true;
|
|
|
|
} else if (
|
|
|
|
editorValueForIFrame.match(/\<\!\-\-/gi) &&
|
|
|
|
editorValueForIFrame.match(/\<\!\-\-/gi).length > editorValueForIFrame.match(/\-\-\>/gi).length
|
|
|
|
) {
|
|
|
|
failedCommentTest = true;
|
|
|
|
}
|
2015-08-23 22:45:28 +01:00
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
if (failedCommentTest) {
|
|
|
|
editor.setValue(editor.getValue() + '-->');
|
|
|
|
editorValueForIFrame = editorValueForIFrame + '-->';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!editor.getValue().match(/\$\s*?\(\s*?\$\s*?\)/gi)) {
|
|
|
|
safeHTMLRun(false);
|
|
|
|
} else {
|
|
|
|
workerError('Unsafe $($)');
|
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
|
|
|
|
if (typeof prodOrDev !== 'undefined') {
|
|
|
|
|
|
|
|
var nodeEnv = prodOrDev === 'production' ?
|
|
|
|
'http://www.freecodecamp.com' :
|
|
|
|
'http://localhost:3001';
|
|
|
|
|
|
|
|
if (challengeType === '0') {
|
|
|
|
setTimeout(updatePreview, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize CodeMirror editor with a nice html5 canvas demo.
|
|
|
|
editor.on('keyup', function() {
|
|
|
|
clearTimeout(delay);
|
|
|
|
delay = setTimeout(updatePreview, 300);
|
|
|
|
});
|
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
/**
|
|
|
|
* "post" methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
var testResults = [];
|
|
|
|
var postSuccess = function(data) {
|
2015-08-27 11:23:17 -07:00
|
|
|
var testDoc = document.createElement('div');
|
|
|
|
$(testDoc).html(
|
|
|
|
"<div class='row'><div class='col-xs-2 text-center'><i class='ion-checkmark-circled big-success-icon'></i></div><div class='col-xs-10 test-output test-vertical-center wrappable'>" +
|
|
|
|
JSON.parse(data) +
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
|
|
|
|
$('#testSuite').append(testDoc);
|
|
|
|
testSuccess();
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var postError = function(data) {
|
2015-08-27 11:23:17 -07:00
|
|
|
var testDoc = document.createElement('div');
|
|
|
|
|
|
|
|
$(testDoc).html(
|
|
|
|
"<div class='row'><div class='col-xs-2 text-center'><i class='ion-close-circled big-error-icon'></i></div><div class='col-xs-10 test-vertical-center test-output wrappable'>" +
|
|
|
|
JSON.parse(data) +
|
|
|
|
'</div>'
|
|
|
|
);
|
|
|
|
|
|
|
|
$('#testSuite').append(testDoc);
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
2015-08-27 11:23:17 -07:00
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
var goodTests = 0;
|
|
|
|
var testSuccess = function() {
|
2015-08-27 11:23:17 -07:00
|
|
|
goodTests++;
|
2015-09-09 12:07:00 -07:00
|
|
|
// test successful run show completion
|
2015-08-27 11:23:17 -07:00
|
|
|
if (goodTests === tests.length) {
|
2015-09-09 12:07:00 -07:00
|
|
|
return showCompletion();
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
2015-09-09 18:59:56 -07:00
|
|
|
function ctrlEnterClickHandler(e) {
|
|
|
|
// ctrl + enter
|
|
|
|
if (e.ctrlKey && e.keyCode === 13) {
|
2015-09-09 21:33:45 -07:00
|
|
|
$('#complete-courseware-dialog').off('keydown', ctrlEnterClickHandler);
|
2015-09-09 18:59:56 -07:00
|
|
|
$('#submit-challenge').click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
function showCompletion() {
|
2015-08-27 11:23:17 -07:00
|
|
|
if (isInitRun) {
|
|
|
|
isInitRun = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var time = Math.floor(Date.now()) - started;
|
|
|
|
ga(
|
|
|
|
'send',
|
|
|
|
'event',
|
|
|
|
'Challenge',
|
|
|
|
'solved',
|
|
|
|
challenge_Name + ', Time: ' + time + ', Attempts: ' + attempts
|
|
|
|
);
|
|
|
|
var bonfireSolution = myCodeMirror.getValue();
|
|
|
|
var didCompleteWith = $('#completed-with').val() || null;
|
2015-09-09 18:59:56 -07:00
|
|
|
|
2015-09-04 21:51:34 +01:00
|
|
|
$('#complete-courseware-dialog').modal('show');
|
|
|
|
$('#complete-courseware-dialog .modal-header').click();
|
2015-09-09 12:52:31 -07:00
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
$('#submit-challenge').click(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
$('#submit-challenge')
|
|
|
|
.attr('disabled', 'true')
|
|
|
|
.removeClass('btn-primary')
|
|
|
|
.addClass('btn-warning disabled');
|
|
|
|
|
|
|
|
var $checkmarkContainer = $('#checkmark-container');
|
|
|
|
$checkmarkContainer.css({ height: $checkmarkContainer.innerHeight() });
|
|
|
|
|
|
|
|
$('#challenge-checkmark')
|
|
|
|
.addClass('zoomOutUp')
|
|
|
|
// .removeClass('zoomInDown')
|
|
|
|
.delay(1000)
|
|
|
|
.queue(function(next) {
|
|
|
|
$(this).replaceWith(
|
|
|
|
'<div id="challenge-spinner" class="animated zoomInUp inner-circles-loader">submitting...</div>'
|
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
$.post(
|
|
|
|
'/completed-bonfire/', {
|
|
|
|
challengeInfo: {
|
|
|
|
challengeId: challenge_Id,
|
|
|
|
challengeName: challenge_Name,
|
|
|
|
completedWith: didCompleteWith,
|
|
|
|
challengeType: challengeType,
|
|
|
|
solution: bonfireSolution
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
},
|
|
|
|
function(res) {
|
|
|
|
if (res) {
|
2015-09-08 21:45:53 -07:00
|
|
|
window.location = '/challenges/next-challenge?id=' + challenge_Id;
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var resetEditor = function resetEditor() {
|
2015-08-27 00:15:13 -07:00
|
|
|
editor.setValue(replaceSafeTags(allSeeds));
|
|
|
|
$('#testSuite').empty();
|
|
|
|
bonfireExecute(true);
|
|
|
|
codeStorage.updateStorage();
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var attempts = 0;
|
|
|
|
if (attempts) {
|
2015-08-27 00:15:13 -07:00
|
|
|
attempts = 0;
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
if (challengeType !== '0') {
|
|
|
|
var codeOutput = CodeMirror.fromTextArea(
|
|
|
|
document.getElementById('codeOutput'),
|
|
|
|
{
|
|
|
|
lineNumbers: false,
|
|
|
|
mode: 'text',
|
|
|
|
theme: 'monokai',
|
|
|
|
readOnly: 'nocursor',
|
|
|
|
lineWrapping: true
|
|
|
|
}
|
|
|
|
);
|
2015-08-23 21:59:29 +01:00
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
codeOutput.setValue('/**\n' +
|
|
|
|
' * Your output will go here.\n' + ' * Console.log() -type statements\n' +
|
|
|
|
' * will appear in your browser\'s\n' + ' * DevTools JavaScript console.\n' +
|
|
|
|
' */');
|
2015-08-27 11:23:17 -07:00
|
|
|
codeOutput.setSize('100%', '100%');
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 00:15:13 -07:00
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
var info = editor.getScrollInfo();
|
2015-08-27 11:23:17 -07:00
|
|
|
|
2015-08-23 21:59:29 +01:00
|
|
|
var after = editor.charCoords({
|
2015-08-27 11:23:17 -07:00
|
|
|
line: editor.getCursor().line + 1,
|
|
|
|
ch: 0
|
|
|
|
}, 'local').top;
|
|
|
|
|
|
|
|
if (info.top + info.clientHeight < after) {
|
|
|
|
editor.scrollTo(null, after - info.clientHeight + 3);
|
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
|
|
|
|
var userTests;
|
|
|
|
var testSalt = Math.random();
|
|
|
|
|
|
|
|
|
|
|
|
var scrapeTests = function(userJavaScript) {
|
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
// insert tests from mongo
|
|
|
|
for (var i = 0; i < tests.length; i++) {
|
|
|
|
userJavaScript += '\n' + tests[i];
|
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
var counter = 0;
|
|
|
|
var match = BDDregex.exec(userJavaScript);
|
|
|
|
|
|
|
|
while (match) {
|
|
|
|
var replacement = '//' + counter + testSalt;
|
|
|
|
userJavaScript = userJavaScript.substring(0, match.index) +
|
|
|
|
replacement +
|
|
|
|
userJavaScript.substring(match.index + match[0].length);
|
|
|
|
|
|
|
|
if (!userTests) {
|
|
|
|
userTests = [];
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
userTests.push({
|
|
|
|
'text': match[0],
|
|
|
|
'line': counter,
|
|
|
|
'err': null
|
|
|
|
});
|
|
|
|
|
|
|
|
counter++;
|
|
|
|
match = BDDregex.exec(userJavaScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
return userJavaScript;
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
function removeComments(userJavaScript) {
|
|
|
|
var regex = new RegExp(/(\/\*[^(\*\/)]*\*\/)|\/\/[^\n]*/g);
|
|
|
|
return userJavaScript.replace(regex, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeLogs(userJavaScript) {
|
|
|
|
return userJavaScript.replace(/(console\.[\w]+\s*\(.*\;)/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
var pushed = false;
|
|
|
|
var createTestDisplay = function() {
|
2015-08-27 11:23:17 -07:00
|
|
|
if (pushed) {
|
|
|
|
userTests.pop();
|
|
|
|
}
|
|
|
|
for (var i = 0; i < userTests.length; i++) {
|
|
|
|
var test = userTests[i];
|
|
|
|
var testDoc = document.createElement('div');
|
|
|
|
|
|
|
|
if (test.err) {
|
|
|
|
console.log('Should be displaying bad tests');
|
|
|
|
|
|
|
|
$(testDoc).html(
|
|
|
|
"<div class='row'><div class='col-xs-2 text-center'><i class='ion-close-circled big-error-icon'></i></div><div class='col-xs-10 test-output wrappable test-vertical-center grayed-out-test-output'>" +
|
|
|
|
test.text + "</div><div class='col-xs-10 test-output wrappable'>" +
|
|
|
|
test.err + "</div></div><div class='ten-pixel-break'/>"
|
|
|
|
)
|
|
|
|
.appendTo($('#testSuite'));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$(testDoc).html(
|
|
|
|
"<div class='row'><div class='col-xs-2 text-center'><i class='ion-checkmark-circled big-success-icon'></i></div><div class='col-xs-10 test-output test-vertical-center wrappable grayed-out-test-output'>" +
|
|
|
|
test.text +
|
|
|
|
"</div></div><div class='ten-pixel-break'/>"
|
|
|
|
)
|
|
|
|
.appendTo($('#testSuite'));
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var expect = chai.expect;
|
|
|
|
var assert = chai.assert;
|
|
|
|
var should = chai.should();
|
|
|
|
|
|
|
|
|
|
|
|
var reassembleTest = function(test, data) {
|
2015-08-27 11:23:17 -07:00
|
|
|
var lineNum = test.line;
|
|
|
|
var regexp = new RegExp('\/\/' + lineNum + testSalt);
|
|
|
|
return data.input.replace(regexp, test.text);
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
var runTests = function(err, data) {
|
2015-08-27 11:23:17 -07:00
|
|
|
// userTests = userTests ? null : [];
|
|
|
|
var allTestsPassed = true;
|
|
|
|
pushed = false;
|
|
|
|
$('#testSuite').children().remove();
|
|
|
|
if (err && userTests.length > 0) {
|
|
|
|
userTests = [{
|
|
|
|
text: 'Program Execution Failure',
|
|
|
|
err: 'No user tests were run.'
|
|
|
|
}];
|
|
|
|
createTestDisplay();
|
|
|
|
|
|
|
|
// Add blocks to test exploits here!
|
|
|
|
} else if (editorValue.match(/if\s\(null\)\sconsole\.log\(1\);/gi)) {
|
|
|
|
allTestsPassed = false;
|
|
|
|
userTests = [{
|
|
|
|
text: 'Program Execution Failure',
|
|
|
|
err: 'Invalid if (null) console.log(1); detected'
|
|
|
|
}];
|
|
|
|
createTestDisplay();
|
|
|
|
} else if (userTests) {
|
|
|
|
userTests.push(false);
|
|
|
|
pushed = true;
|
|
|
|
userTests.forEach(function(
|
|
|
|
chaiTestFromJSON,
|
|
|
|
indexOfTestArray,
|
|
|
|
__testArray
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
if (chaiTestFromJSON) {
|
2015-09-09 12:07:00 -07:00
|
|
|
/* eslint-disable no-eval */
|
2015-08-27 11:23:17 -07:00
|
|
|
var output = eval(reassembleTest(chaiTestFromJSON, data));
|
2015-09-09 12:07:00 -07:00
|
|
|
/* eslint-enable no-eval */
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2015-08-23 21:59:29 +01:00
|
|
|
allTestsPassed = false;
|
2015-08-27 11:23:17 -07:00
|
|
|
__testArray[indexOfTestArray].err = error.message;
|
|
|
|
} finally {
|
|
|
|
if (!chaiTestFromJSON) {
|
|
|
|
createTestDisplay();
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (allTestsPassed) {
|
|
|
|
allTestsPassed = false;
|
|
|
|
showCompletion();
|
2015-09-09 12:07:00 -07:00
|
|
|
} else {
|
2015-09-04 21:34:07 +01:00
|
|
|
isInitRun = false;
|
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
};
|
|
|
|
|
2015-08-27 11:23:17 -07:00
|
|
|
function bonfireExecute(shouldTest) {
|
|
|
|
initPreview = false;
|
|
|
|
goodTests = 0;
|
|
|
|
attempts++;
|
|
|
|
ga('send', 'event', 'Challenge', 'ran-code', challenge_Name);
|
|
|
|
userTests = null;
|
|
|
|
$('#testSuite').empty();
|
|
|
|
|
|
|
|
if (
|
|
|
|
challengeType !== '0' &&
|
|
|
|
!editor.getValue().match(/\$\s*?\(\s*?\$\s*?\)/gi)
|
|
|
|
) {
|
|
|
|
var userJavaScript = myCodeMirror.getValue();
|
|
|
|
var failedCommentTest = false;
|
|
|
|
|
|
|
|
// checks if the number of opening comments(/*) matches the number of
|
|
|
|
// closing comments(*/)
|
|
|
|
if (
|
|
|
|
userJavaScript.match(/\/\*/gi) &&
|
|
|
|
userJavaScript.match(/\/\*/gi).length > userJavaScript.match(/\*\//gi).length
|
|
|
|
) {
|
|
|
|
failedCommentTest = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
userJavaScript = removeComments(userJavaScript);
|
|
|
|
userJavaScript = scrapeTests(userJavaScript);
|
|
|
|
// simple fix in case the user forgets to invoke their function
|
|
|
|
|
|
|
|
if (userJavaScript.match(/function/gi)) {
|
|
|
|
if (userJavaScript.match(/function\s*?\(|function\s+\w+\s*?\(/gi)) {
|
2015-08-27 12:37:41 -07:00
|
|
|
sandBox.submit(userJavaScript, function(cls, message) {
|
2015-08-27 11:23:17 -07:00
|
|
|
if (failedCommentTest) {
|
|
|
|
myCodeMirror.setValue(myCodeMirror.getValue() + '*/');
|
|
|
|
console.log('Caught Unfinished Comment');
|
|
|
|
codeOutput.setValue('Unfinished multi-line comment');
|
|
|
|
failedCommentTest = false;
|
|
|
|
} else if (cls) {
|
|
|
|
codeOutput.setValue(message.error);
|
2015-08-27 12:37:41 -07:00
|
|
|
if (shouldTest) {
|
2015-08-27 11:23:17 -07:00
|
|
|
runTests('Error', null);
|
2015-08-24 01:16:15 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
} else {
|
|
|
|
codeOutput.setValue(message.output);
|
|
|
|
codeOutput.setValue(codeOutput.getValue().replace(/\\\"/gi, ''));
|
|
|
|
message.input = removeLogs(message.input);
|
|
|
|
if (shouldTest) {
|
|
|
|
runTests(null, message);
|
2015-08-24 01:16:15 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
codeOutput.setValue('Unsafe or unfinished function declaration');
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-27 12:37:41 -07:00
|
|
|
sandBox.submit(userJavaScript, function(cls, message) {
|
2015-08-27 11:23:17 -07:00
|
|
|
|
2015-08-24 01:16:15 +01:00
|
|
|
if (failedCommentTest) {
|
2015-08-27 11:23:17 -07:00
|
|
|
myCodeMirror.setValue(myCodeMirror.getValue() + '*/');
|
|
|
|
console.log('Caught Unfinished Comment');
|
|
|
|
codeOutput.setValue('Unfinished mulit-line comment');
|
|
|
|
failedCommentTest = false;
|
|
|
|
} else if (cls) {
|
|
|
|
codeOutput.setValue(message.error);
|
|
|
|
if (shouldTest) {
|
|
|
|
runTests('Error', null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
codeOutput.setValue(message.output);
|
|
|
|
codeOutput.setValue(codeOutput.getValue().replace(/\\\"/gi, ''));
|
|
|
|
message.input = removeLogs(message.input);
|
|
|
|
|
|
|
|
if (shouldTest) {
|
|
|
|
runTests(null, message);
|
|
|
|
}
|
2015-08-25 21:07:36 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
});
|
2015-08-24 01:16:15 +01:00
|
|
|
}
|
2015-08-27 11:23:17 -07:00
|
|
|
} else {
|
|
|
|
|
|
|
|
editorValueForIFrame = editor.getValue();
|
|
|
|
|
|
|
|
if (failedCommentTest) {
|
|
|
|
editor.setValue(editor.getValue() + '-->');
|
|
|
|
editorValueForIFrame = editorValueForIFrame + '-->';
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!editor.getValue().match(/\$\s*?\(\s*?\$\s*?\)/gi) &&
|
|
|
|
challengeType === '0'
|
|
|
|
) {
|
2015-08-27 12:37:41 -07:00
|
|
|
safeHTMLRun(shouldTest);
|
2015-08-27 11:23:17 -07:00
|
|
|
} else {
|
|
|
|
workerError('Unsafe $($)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(function() {
|
|
|
|
var $marginFix = $('.innerMarginFix');
|
|
|
|
$marginFix.css('min-height', $marginFix.height());
|
|
|
|
}, 1000);
|
2015-08-23 21:59:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$('#submitButton').on('click', function() {
|
2015-09-09 20:13:35 -07:00
|
|
|
isInitRun = false;
|
2015-08-27 11:23:17 -07:00
|
|
|
bonfireExecute(true);
|
2015-08-23 21:59:29 +01:00
|
|
|
});
|
|
|
|
|
2015-08-27 00:02:07 -07:00
|
|
|
$(document).ready(function() {
|
2015-09-09 18:59:56 -07:00
|
|
|
|
|
|
|
// init modal keybindings on open
|
|
|
|
$('#complete-courseware-dialog').on('shown.bs.modal', function() {
|
2015-09-09 21:33:45 -07:00
|
|
|
$('#complete-courseware-dialog').keydown(ctrlEnterClickHandler);
|
2015-09-09 18:59:56 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
// remove modal keybinds on close
|
|
|
|
$('#complete-courseware-dialog').on('hidden.bs.modal', function() {
|
2015-09-09 21:33:45 -07:00
|
|
|
$('#complete-courseware-dialog').off('keydown', ctrlEnterClickHandler);
|
2015-09-09 18:59:56 -07:00
|
|
|
});
|
|
|
|
|
2015-08-27 00:15:13 -07:00
|
|
|
var $preview = $('#preview');
|
|
|
|
isInitRun = true;
|
|
|
|
|
|
|
|
editorValue = codeStorage.isAlive() ?
|
|
|
|
codeStorage.getStoredValue() :
|
|
|
|
allSeeds;
|
|
|
|
|
|
|
|
myCodeMirror.setValue(replaceSafeTags(editorValue));
|
|
|
|
if (typeof $preview.html() !== 'undefined') {
|
|
|
|
$preview.load(function() {
|
|
|
|
if (initPreview) {
|
2015-08-26 20:31:31 +01:00
|
|
|
bonfireExecute(true);
|
2015-08-27 00:15:13 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
bonfireExecute(true);
|
|
|
|
}
|
2015-08-23 21:59:29 +01:00
|
|
|
});
|