Fixes #260 - Circular custom tags causes infinite recursion when writeToDisk is set to false

This commit is contained in:
Patrick Steele-Idem 2016-03-31 14:59:12 -06:00
parent 1ea1808c71
commit 1fe897c386
8 changed files with 67 additions and 20 deletions

View File

@ -91,7 +91,7 @@ exports.install = function(options) {
module.exports = loaded.exports;
return;
}
// Resolve the appropriate compiler relative to the location of the
// marko template file on disk using the "resolve-from" module.
var dirname = path.dirname(filename);

View File

@ -54,10 +54,18 @@ fixFlush();
function loadSource(templatePath, compiledSrc) {
var templateModulePath = templatePath + '.js';
// Short-circuit loading if the template has already been cached in the Node.js require cache
var cached = require.cache[templateModulePath];
if (cached) {
return cached.exports;
}
var templateModule = new Module(templateModulePath, module);
templateModule.paths = Module._nodeModulePaths(nodePath.dirname(templateModulePath));
templateModule.filename = templateModulePath;
Module._cache[templateModulePath] = templateModule;
templateModule._compile(
compiledSrc,
templateModulePath);

View File

@ -0,0 +1 @@
<ul><li><b>a</b></li><li><b>b</b><ul><li><b>b1</b></li><li><b>b2</b></li></ul></li></ul>

View File

@ -0,0 +1 @@
{ "tags-dir": "./tags/" }

View File

@ -0,0 +1,6 @@
<ul>
<li for(item in data.items)>
<b>${item.label}</b>
<navigation-item items=item.children if(item.children)/>
</li>
</ul>

View File

@ -0,0 +1 @@
<navigation-item items=data.items/>

View File

@ -0,0 +1,20 @@
exports.templateData = {
items: [
{
label: 'a'
},
{
label: 'b',
children: [
{
label: 'b1'
},
{
label: 'b2'
}
]
}
]
};
exports.writeToDisk = false;

View File

@ -20,28 +20,38 @@ describe('render', function() {
var main = fs.existsSync(mainPath) ? require(mainPath) : {};
var loadOptions = main && main.loadOptions;
if (main.checkError) {
var e;
if (main.writeToDisk === false) {
require('marko/compiler').defaultOptions.writeToDisk = false;
}
try {
marko.load(templatePath, loadOptions);
} catch(_e) {
e = _e;
var errorFile = path.join(dir, 'error.txt');
fs.writeFileSync(errorFile, e.stack.toString(), { encoding: 'utf8' });
try {
if (main.checkError) {
var e;
try {
marko.load(templatePath, loadOptions);
} catch(_e) {
e = _e;
var errorFile = path.join(dir, 'error.txt');
fs.writeFileSync(errorFile, e.stack.toString(), { encoding: 'utf8' });
}
if (!e) {
throw new Error('Error expected');
}
main.checkError(e);
return '$PASS$';
} else {
var template = marko.load(templatePath, loadOptions);
var templateData = main.templateData || {};
var html = template.renderSync(templateData);
return html;
}
if (!e) {
throw new Error('Error expected');
} finally {
if (main.writeToDisk === false) {
require('marko/compiler').defaultOptions.writeToDisk = false;
}
main.checkError(e);
return '$PASS$';
} else {
var template = marko.load(templatePath, loadOptions);
var templateData = main.templateData || {};
var html = template.renderSync(templateData);
return html;
}
},
{