Added error checking and tests for evaluated parameters

print-window-tiddler
Jeremy Ruston 2011-12-21 17:21:28 +00:00
rodzic 4c11503bae
commit 629a5b12da
8 zmienionych plików z 26 dodań i 15 usunięć

Wyświetl plik

@ -26,7 +26,7 @@ Options and their defaults are:
/*jslint node: true */
"use strict";
var Sandbox = require("./Sandbox.js").Sandbox;
var sandbox = require("./Sandbox.js").sandbox;
var ArgParser = function(argString,options) {
var parseToken = function(match,p) {
@ -38,7 +38,7 @@ var ArgParser = function(argString,options) {
} else if(match[p+2]) { // Double-square-bracket quoted
n = match[p+2];
} else if(match[p+3]) { // Double-brace quoted
n = options.allowEval === false ? match[p+3] : Sandbox(match[p+3],options.globals);
n = options.allowEval === false ? match[p+3] : sandbox(match[p+3],options.globals);
} else if(match[p+4]) { // Unquoted
n = match[p+4];
} else if(match[p+5]) { // empty quote

Wyświetl plik

@ -6,16 +6,12 @@ Execute a fragment of JavaScript in a sandbox
\*/
(function(){
/*jslint node: true */
/*jslint evil: true, node: true */
"use strict";
var uglify = require("uglify-js");
var safeEval = function(e) {
return eval(e);
};
var Sandbox = function(code,globals) {
var sandbox = function(code,globals) {
var globalNames = [],
globalValues = [],
collectGlobals = function(globals) {
@ -34,7 +30,7 @@ var Sandbox = function(code,globals) {
});
// Compose the code
var out = [];
out.push("(function(")
out.push("(function(");
out.push(globalNames.join(","));
out.push(") { return ");
out.push(code);
@ -45,9 +41,15 @@ var Sandbox = function(code,globals) {
// Recompile the code
var compiledCode = uglify.uglify.gen_code(tree);
// Execute it
return eval(compiledCode).apply(null,globalValues);
var result;
try {
result = eval(compiledCode).apply(null,globalValues);
} catch(err) {
result = "{{** Evaluation error: " + err + " **}}";
}
return result;
};
exports.Sandbox = Sandbox;
exports.sandbox = sandbox;
})();

Wyświetl plik

@ -0,0 +1 @@
4<br /><br />SeventhTiddler<br /><br />{{** Evaluation error: ReferenceError: window is not defined **}}<br />

Wyświetl plik

@ -0,0 +1,7 @@
title: SeventhTiddler
<<echo {{2+2}}>>
<<echo {{title}}>>
<<echo {{window}}>>

Wyświetl plik

@ -0,0 +1 @@
4SeventhTiddler{{** Evaluation error: ReferenceError: window is not defined **}}

Wyświetl plik

@ -1 +1 @@
SixthTiddler<br /><a href="SixthTiddler">SixthTiddler</a><br />11 February 2011<br /><a href="Jermolene">Jermolene</a><br /><ul><li><a href="Fifth Tiddler">Fifth Tiddler</a></li><li><a href="FirstTiddler">FirstTiddler</a></li><li><a href="Fourth Tiddler">Fourth Tiddler</a></li><li><a href="SecondTiddler">SecondTiddler</a></li><li><a href="SixthTiddler">SixthTiddler</a></li><li><a href="ThirdTiddler">ThirdTiddler</a></li></ul><br />
SixthTiddler<br /><a href="SixthTiddler">SixthTiddler</a><br />11 February 2011<br /><a href="Jermolene">Jermolene</a><br /><ul><li><a href="Fifth Tiddler">Fifth Tiddler</a></li><li><a href="FirstTiddler">FirstTiddler</a></li><li><a href="Fourth Tiddler">Fourth Tiddler</a></li><li><a href="SecondTiddler">SecondTiddler</a></li><li><a href="SeventhTiddler">SeventhTiddler</a></li><li><a href="SixthTiddler">SixthTiddler</a></li><li><a href="ThirdTiddler">ThirdTiddler</a></li></ul><br />

Wyświetl plik

@ -1 +1 @@
SixthTiddlerSixthTiddler11 February 2011JermoleneFifth TiddlerFirstTiddlerFourth TiddlerSecondTiddlerSixthTiddlerThirdTiddler
SixthTiddlerSixthTiddler11 February 2011JermoleneFifth TiddlerFirstTiddlerFourth TiddlerSecondTiddlerSeventhTiddlerSixthTiddlerThirdTiddler

Wyświetl plik

@ -59,10 +59,10 @@ for(t=0; t<titles.length; t++) {
htmlRender = store.renderTiddler("text/html",titles[t]),
plainRender = store.renderTiddler("text/plain",titles[t]);
if(htmlTarget !== htmlRender) {
console.error("Tiddler %s html error\nTarget: %s\nFound: %s\n",titles[t],htmlTarget,htmlRender);
console.error("Tiddler %s html error\nTarget: %s\nFound: %s\n",titles[t],htmlTarget,htmlRender);
}
if(plainTarget !== plainRender) {
console.error("Tiddler %s plain text error\nTarget: %s\nFound: %s\n",titles[t],plainTarget,plainRender);
console.error("Tiddler %s plain text error\nTarget: %s\nFound: %s\n",titles[t],plainTarget,plainRender);
}
}