Skip to content
Permalink
Browse files

mark studentAnswer string as 'safe'

So variables aren't substituted into the string when evaluating it.

This adds a function `jme.makeSafe`, which marks a token and any strings
it contains as safe.
  • Loading branch information...
christianp committed Nov 15, 2018
1 parent e41acbb commit 8e5b8349e3def49837b027b98f9bf34758d91e14
Showing with 33 additions and 1 deletion.
  1. +32 −1 runtime/scripts/jme.js
  2. +1 −0 runtime/scripts/part.js
@@ -530,6 +530,35 @@ var jme = Numbas.jme = /** @lends Numbas.jme */ {
return v.value;
}
},

/** Mark a token as 'safe', so it doesn't have {@link Numbas.jme.subvars} applied to it, or any strings it contains, when it's evaluated
* @param {Numbas.jme.token} t
* @returns {Numbas.jme.token}
*/
makeSafe: function(t) {
switch(t.type) {
case 'string':
t.safe = true;
var t2 = new TString(t.value);
if(t.latex!==undefined) {
t2.latex = t.latex;
}
t2.safe = true;
return t2;
case 'list':
console.log(t);
return new TList(t.value.map(jme.makeSafe));
case 'dict':
var o = {};
for(var x in t.value) {
o[x] = jme.makeSafe(t.value[x]);
}
return new TDict(o);
default:
return t;
}
},

/** Wrap up a plain JavaScript value (number, string, bool or array) as a {@link Numbas.jme.token}.
* @param {Object} v
* @param {String} typeHint - name of the expected type (to differentiate between, for example, matrices, vectors and lists
@@ -1331,7 +1360,9 @@ Scope.prototype = /** @lends Numbas.jme.Scope.prototype */ {
if(!tok.safe && value.contains('{')) {
value = jme.contentsubvars(value,scope)
var t = new TString(value);
t.latex = tok.latex
if(tok.latex!==undefined) {
t.latex = tok.latex
}
return t;
} else {
return tok;
@@ -929,6 +929,7 @@ Part.prototype = /** @lends Numbas.parts.Part.prototype */ {
part.answered = valid;
},
marking_parameters: function(studentAnswer) {
studentAnswer = jme.makeSafe(studentAnswer);
return {
path: jme.wrapValue(this.path),
studentAnswer: studentAnswer,

0 comments on commit 8e5b834

Please sign in to comment.
You can’t perform that action at this time.