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