Skip to content

Commit 5704bff

Browse files
committed
added frontend support for special HTML/CSS/JS outputs
1 parent d9219e7 commit 5704bff

3 files changed

Lines changed: 51 additions & 13 deletions

File tree

‎v3/css/pytutor.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ div.ExecutionVisualizer {
4343
div.ExecutionVisualizer table.visualizer {
4444
font-family: verdana, arial, helvetica, sans-serif;
4545
font-size: 10pt;
46+
margin-bottom: 10px;
4647
}
4748

4849
div.ExecutionVisualizer table.visualizer td.vizLayoutTd {
@@ -127,6 +128,10 @@ div.ExecutionVisualizer table#pyCodeOutput .cod {
127128
text-align: left; /* necessary or else doesn't work properly in IE */
128129
}
129130

131+
div.ExecutionVisualizer div#progOutputs {
132+
margin-top: 8px;
133+
}
134+
130135
div.ExecutionVisualizer div#legendDiv {
131136
margin-top: 10px;
132137
padding: 0px;

‎v3/js/opt-frontend.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var appMode = 'edit'; // 'edit', 'display', or 'display_no_frills'
4343
var preseededCode = null; // if you passed in a 'code=<code string>' in the URL, then set this var
4444
var preseededCurInstr = null; // if you passed in a 'curInstr=<number>' in the URL, then set this var
4545

46-
var rawInputLst = []; // a list of strings inputted by the user in calls to prompt()
46+
var rawInputLst = []; // a list of strings inputted by the user in response to raw_input or mouse_input events
4747

4848
var myVisualizer = null; // singleton ExecutionVisualizer instance
4949

‎v3/js/pytutor.js‎

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,19 @@ function ExecutionVisualizer(domRootID, dat, params) {
110110
// visualization to not show as much redundancy.
111111
this.curTrace = this.curTrace.filter(function(e) {return e.event != 'call';});
112112

113-
// if the final entry is raw_input, then trim it from the trace and
113+
// if the final entry is raw_input or mouse_input, then trim it from the trace and
114114
// set a flag to prompt for user input when execution advances to the
115115
// end of the trace
116116
if (this.curTrace.length > 0) {
117117
var lastEntry = this.curTrace[this.curTrace.length - 1];
118118
if (lastEntry.event == 'raw_input') {
119119
this.promptForUserInput = true;
120120
this.userInputPromptStr = lastEntry.prompt;
121-
121+
this.curTrace.pop() // kill last entry so that it doesn't get displayed
122+
}
123+
else if (lastEntry.event == 'mouse_input') {
124+
this.promptForMouseInput = true;
125+
this.userInputPromptStr = lastEntry.prompt;
122126
this.curTrace.pop() // kill last entry so that it doesn't get displayed
123127
}
124128
}
@@ -236,6 +240,7 @@ ExecutionVisualizer.prototype.render = function() {
236240
<div class="annotationText" id="stepAnnotationViewer"></div>\
237241
</div>\
238242
<div id="annotateLinkDiv"><button id="annotateBtn" type="button">Annotate this step</button></div>\
243+
<div id="htmlOutputDiv"></div>\
239244
<div id="progOutputs">\
240245
Program output:<br/>\
241246
<textarea id="pyStdout" cols="50" rows="10" wrap="off" readonly></textarea>\
@@ -1163,8 +1168,8 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
11631168
var vcrControls = myViz.domRoot.find("#vcrControls");
11641169

11651170
if (isLastInstr) {
1166-
if (this.promptForUserInput) {
1167-
vcrControls.find("#curInstr").html('<b><font color="' + brightRed + '">Processing user input ...</font></b>');
1171+
if (this.promptForUserInput || this.promptForMouseInput) {
1172+
vcrControls.find("#curInstr").html('<b><font color="' + brightRed + '">' + this.userInputPromptStr + '</font></b>');
11681173

11691174
// don't do smooth transition since prompt() is modal so it doesn't
11701175
// give the animation background thread time to run
@@ -1374,13 +1379,41 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
13741379

13751380
// render stdout:
13761381

1377-
// keep original horizontal scroll level:
1378-
var oldLeft = myViz.domRoot.find("#pyStdout").scrollLeft();
1379-
myViz.domRoot.find("#pyStdout").val(curEntry.stdout);
1382+
// if there isn't anything in curEntry.stdout, don't even bother
1383+
// displaying the pane
1384+
if (curEntry.stdout) {
1385+
this.domRoot.find('#progOutputs').show();
13801386

1381-
myViz.domRoot.find("#pyStdout").scrollLeft(oldLeft);
1382-
// scroll to bottom, though:
1383-
myViz.domRoot.find("#pyStdout").scrollTop(myViz.domRoot.find("#pyStdout")[0].scrollHeight);
1387+
// keep original horizontal scroll level:
1388+
var oldLeft = myViz.domRoot.find("#pyStdout").scrollLeft();
1389+
myViz.domRoot.find("#pyStdout").val(curEntry.stdout);
1390+
1391+
myViz.domRoot.find("#pyStdout").scrollLeft(oldLeft);
1392+
// scroll to bottom, though:
1393+
myViz.domRoot.find("#pyStdout").scrollTop(myViz.domRoot.find("#pyStdout")[0].scrollHeight);
1394+
}
1395+
else {
1396+
this.domRoot.find('#progOutputs').hide();
1397+
}
1398+
1399+
1400+
// inject user-specified HTML/CSS/JS output:
1401+
// YIKES -- HUGE CODE INJECTION VULNERABILITIES :O
1402+
myViz.domRoot.find("#htmlOutputDiv").empty();
1403+
if (curEntry.html_output) {
1404+
if (curEntry.css_output) {
1405+
myViz.domRoot.find("#htmlOutputDiv").append('<style type="text/css">' + curEntry.css_output + '</style>');
1406+
}
1407+
myViz.domRoot.find("#htmlOutputDiv").append(curEntry.html_output);
1408+
1409+
// inject and run JS *after* injecting HTML and CSS
1410+
if (curEntry.js_output) {
1411+
// NB: when jQuery injects JS, it executes the code immediately
1412+
// and then removes the entire <script> block from the DOM
1413+
// http://stackoverflow.com/questions/610995/jquery-cant-append-script-element
1414+
myViz.domRoot.find("#htmlOutputDiv").append('<script type="text/javascript">' + curEntry.js_output + '</script>');
1415+
}
1416+
}
13841417

13851418

13861419
// finally, render all of the data structures
@@ -1397,8 +1430,8 @@ ExecutionVisualizer.prototype.updateOutput = function(smoothTransition) {
13971430
}
13981431

13991432

1400-
if (isLastInstr && this.promptForUserInput) {
1401-
if (this.executeCodeWithRawInputFunc) {
1433+
if (isLastInstr && this.executeCodeWithRawInputFunc) {
1434+
if (this.promptForUserInput) {
14021435
// blocking prompt dialog!
14031436
// put a default string of '' or else it looks ugly in IE
14041437
var userInput = prompt(this.userInputPromptStr, '');

0 commit comments

Comments
 (0)