@@ -5,6 +5,9 @@ if (typeof String.prototype.startsWith != 'function') {
5
5
} ;
6
6
}
7
7
8
+ // Regex for finding new lines
9
+ var newLineRegex = / (?: \r \n | \r | \n ) / g;
10
+
8
11
// Fetching DOM items
9
12
var activeCode = document . getElementById ( "active-code" ) ;
10
13
var editorDiv = document . getElementById ( "editor" ) ;
@@ -56,6 +59,16 @@ function updateEditorHeight() {
56
59
// Set initial size to match initial content
57
60
updateEditorHeight ( ) ;
58
61
62
+ function escapeHTML ( unsafe ) {
63
+ return unsafe
64
+ . replace ( / & / g, "&" )
65
+ . replace ( / < / g, "<" )
66
+ . replace ( / > / g, ">" )
67
+ . replace ( / " / g, """ )
68
+ . replace ( / ' / g, "'" )
69
+ . replace ( newLineRegex , '<br />' ) ;
70
+ }
71
+
59
72
// Dispatches a XMLHttpRequest to the Rust playpen, running the program, and
60
73
// issues a callback to `callback` with the result (or null on error)
61
74
function runProgram ( program , callback ) {
@@ -96,8 +109,6 @@ function runProgram(program, callback) {
96
109
97
110
// The callback to runProgram
98
111
function handleResult ( statusCode , message ) {
99
- var message = message . replace ( / (?: \r \n | \r | \n ) / g, '<br />' ) ;
100
-
101
112
// Dispatch depending on result type
102
113
if ( result == null ) {
103
114
resultDiv . style . backgroundColor = errorColor ;
@@ -114,7 +125,7 @@ function handleResult(statusCode, message) {
114
125
// Called on successful program run
115
126
function handleSuccess ( message ) {
116
127
resultDiv . style . backgroundColor = successColor ;
117
- resultDiv . innerHTML = message ;
128
+ resultDiv . innerHTML = escapeHTML ( message ) ;
118
129
}
119
130
120
131
// Called when program run results in warning(s)
@@ -134,7 +145,7 @@ function handleError(message) {
134
145
// in the code.
135
146
function handleProblem ( message , problem ) {
136
147
// Getting list of ranges with problems
137
- var lines = message . split ( "<br />" ) ;
148
+ var lines = message . split ( newLineRegex ) ;
138
149
139
150
// Cleaning up the message: keeps only relevant problem output
140
151
var cleanMessage = lines . map ( function ( line ) {
@@ -149,6 +160,8 @@ function handleProblem(message, problem) {
149
160
return line ;
150
161
} ) . filter ( function ( line ) {
151
162
return line !== "" ;
163
+ } ) . map ( function ( line ) {
164
+ return escapeHTML ( line ) ;
152
165
} ) . join ( "<br />" ) ;
153
166
154
167
// Setting message
0 commit comments