Code cleanup

This commit is contained in:
Phil Gates-Idem 2014-07-17 00:22:28 -04:00
parent 9ceded3078
commit f45d89b18c
4 changed files with 95 additions and 97 deletions

View File

@ -36,6 +36,5 @@
"unused": "vars", "unused": "vars",
"strict": false, "strict": false,
/* Relaxing options: */
"eqnull": true "eqnull": true
} }

View File

@ -1,29 +1,35 @@
'use strict'; 'use strict';
var promiseUtil = require('raptor-promises/util');
var FRAGMENT_CACHE_CONFIG = {
store: 'memory'
};
var raptorCache;
module.exports = { module.exports = {
render: function (input, context) { render: function (input, context) {
var attributes = context.attributes; if (raptorCache === undefined) {
var cacheProvider = attributes.cacheProvider; try {
var cache; raptorCache = require('raptor-cache');
}
catch(e) {
throw new Error('The "raptor-cache" module should be installed as an application-level dependency when using caching tags');
}
}
var cacheKey = input.cacheKey; var cacheKey = input.cacheKey;
if (!cacheKey) { if (!cacheKey) {
throw new Error('cache-key is required for <cached-fragment>'); throw new Error('cache-key is required for <cached-fragment>');
} }
if (!cacheProvider) {
var raptorCacheModulePath; // use the default cache manager
try { var cacheManager = raptorCache.getDefaultCacheManager(context);
raptorCacheModulePath = require.resolve('raptor-cache');
} var cache = cacheManager.getCache(input.cacheName, FRAGMENT_CACHE_CONFIG);
catch(e) {
throw new Error('The "raptor-cache" module should be installed as an application-level dependency when using caching tags'); var asyncContext = context.beginAsync();
}
cacheProvider = require(raptorCacheModulePath).getDefaultProvider(); cache.get(cacheKey,
}
cache = cacheProvider.getCache(input.cacheName);
var cachePromise = cache.get(
cacheKey,
{ {
builder: function() { builder: function() {
var result = context.captureString(function () { var result = context.captureString(function () {
@ -33,17 +39,12 @@ module.exports = {
}); });
return result; return result;
} }
}); }, function(err, result) {
if (err) {
var asyncContext = context.beginAsync(); return asyncContext.error(err);
}
promiseUtil.immediateThen(
cachePromise,
function (result) {
asyncContext.end(result); asyncContext.end(result);
},
function (e) {
asyncContext.error(e);
}); });
} }
}; };

View File

@ -15,8 +15,8 @@
"runs" "runs"
], ],
"globals": { "globals": {
"define": true, "define": true,
"require": true "require": true
}, },
@ -47,6 +47,5 @@
"latedef": true, "latedef": true,
"unused": "vars", "unused": "vars",
/* Relaxing options: */
"eqnull": true "eqnull": true
} }

View File

@ -17,14 +17,14 @@ function testRender(path, data, done, options) {
// var compiler = require('../compiler').createCompiler(inputPath); // var compiler = require('../compiler').createCompiler(inputPath);
// var src = fs.readFileSync(inputPath, {encoding: 'utf8'}); // var src = fs.readFileSync(inputPath, {encoding: 'utf8'});
// var compiledSrc = compiler.compile(src); // var compiledSrc = compiler.compile(src);
// fs.writeFileSync(compiledPath, compiledSrc, {encoding: 'utf8'}); // fs.writeFileSync(compiledPath, compiledSrc, {encoding: 'utf8'});
// console.log('\nCompiled (' + inputPath + '):\n---------\n' + compiledSrc); // console.log('\nCompiled (' + inputPath + '):\n---------\n' + compiledSrc);
var raptorTemplates = require('../'); var raptorTemplates = require('../');
@ -57,7 +57,7 @@ function testRender(path, data, done, options) {
}) })
.on('error', done) .on('error', done)
.end(); .end();
} }
describe('raptor-templates/rhtml' , function() { describe('raptor-templates/rhtml' , function() {
@ -105,11 +105,11 @@ describe('raptor-templates/rhtml' , function() {
colors: ["red", "green", "blue"] colors: ["red", "green", "blue"]
}, done); }, done);
}); });
it("should allow for simple template handlers", function(done) { it("should allow for simple template handlers", function(done) {
testRender("test-project/rhtml-templates/simple-handlers.rhtml", {dynamic: "universe"}, done); testRender("test-project/rhtml-templates/simple-handlers.rhtml", {dynamic: "universe"}, done);
}); });
it("should allow for template handlers with nested body content", function(done) { it("should allow for template handlers with nested body content", function(done) {
testRender("test-project/rhtml-templates/nested-handlers.rhtml", {showConditionalTab: false}, done); testRender("test-project/rhtml-templates/nested-handlers.rhtml", {showConditionalTab: false}, done);
}); });
@ -117,39 +117,39 @@ describe('raptor-templates/rhtml' , function() {
it("should allow entity expressions", function(done) { it("should allow entity expressions", function(done) {
testRender("test-project/rhtml-templates/entities.rhtml", {}, done); testRender("test-project/rhtml-templates/entities.rhtml", {}, done);
}); });
it("should allow escaped expressions", function(done) { it("should allow escaped expressions", function(done) {
testRender("test-project/rhtml-templates/escaped.rhtml", {}, done); testRender("test-project/rhtml-templates/escaped.rhtml", {}, done);
}); });
it("should allow complex expressions", function(done) { it("should allow complex expressions", function(done) {
testRender("test-project/rhtml-templates/expressions.rhtml", {}, done); testRender("test-project/rhtml-templates/expressions.rhtml", {}, done);
}); });
it("should allow whitespace to be removed", function(done) { it("should allow whitespace to be removed", function(done) {
testRender("test-project/rhtml-templates/whitespace.rhtml", {}, done); testRender("test-project/rhtml-templates/whitespace.rhtml", {}, done);
}); });
it("should handle whitespace when using expressions", function(done) { it("should handle whitespace when using expressions", function(done) {
testRender("test-project/rhtml-templates/whitespace2.rhtml", {}, done); testRender("test-project/rhtml-templates/whitespace2.rhtml", {}, done);
}); });
it("should handle whitespace when using expressions", function(done) { it("should handle whitespace when using expressions", function(done) {
testRender("test-project/rhtml-templates/whitespace2.rhtml", {}, done); testRender("test-project/rhtml-templates/whitespace2.rhtml", {}, done);
}); });
it("should normalize whitespace", function(done) { it("should normalize whitespace", function(done) {
testRender("test-project/rhtml-templates/whitespace3.rhtml", {}, done); testRender("test-project/rhtml-templates/whitespace3.rhtml", {}, done);
}); });
it("should handle whitespace correctly for mixed text and element children", function(done) { it("should handle whitespace correctly for mixed text and element children", function(done) {
testRender("test-project/rhtml-templates/whitespace-inline-elements.rhtml", {}, done); testRender("test-project/rhtml-templates/whitespace-inline-elements.rhtml", {}, done);
}); });
it("should allow HTML output that is not well-formed XML", function(done) { it("should allow HTML output that is not well-formed XML", function(done) {
testRender("test-project/rhtml-templates/html.rhtml", {}, done); testRender("test-project/rhtml-templates/html.rhtml", {}, done);
}); });
it("should allow for looping", function(done) { it("should allow for looping", function(done) {
testRender("test-project/rhtml-templates/looping.rhtml", {}, done); testRender("test-project/rhtml-templates/looping.rhtml", {}, done);
}); });
@ -158,20 +158,20 @@ describe('raptor-templates/rhtml' , function() {
testRender("test-project/rhtml-templates/looping-props.rhtml", {}, done); testRender("test-project/rhtml-templates/looping-props.rhtml", {}, done);
}); });
it.only("should allow for looping over ranges", function(done) { it("should allow for looping over ranges", function(done) {
testRender("test-project/rhtml-templates/looping-range.rhtml", {}, done); testRender("test-project/rhtml-templates/looping-range.rhtml", {}, done);
}); });
it("should allow for dynamic attributes", function(done) { it("should allow for dynamic attributes", function(done) {
testRender("test-project/rhtml-templates/attrs.rhtml", {"myAttrs": {style: "background-color: #FF0000; <test>", "class": "my-div"}}, done); testRender("test-project/rhtml-templates/attrs.rhtml", {"myAttrs": {style: "background-color: #FF0000; <test>", "class": "my-div"}}, done);
}); });
it("should allow for choose...when statements", function(done) { it("should allow for choose...when statements", function(done) {
testRender("test-project/rhtml-templates/choose-when.rhtml", {}, done); testRender("test-project/rhtml-templates/choose-when.rhtml", {}, done);
}); });
it("should not allow <c-otherwise> to be before a <c-when> tag", function(done) { it("should not allow <c-otherwise> to be before a <c-when> tag", function(done) {
var e; var e;
function fakeDone() { function fakeDone() {
@ -184,56 +184,56 @@ describe('raptor-templates/rhtml' , function() {
catch(_e) { catch(_e) {
e = _e; e = _e;
} }
expect(e != null).to.equal(true); expect(e != null).to.equal(true);
done(); done();
}); });
it("should allow for <c-def> functions", function(done) { it("should allow for <c-def> functions", function(done) {
testRender("test-project/rhtml-templates/def.rhtml", {}, done); testRender("test-project/rhtml-templates/def.rhtml", {}, done);
}); });
it("should allow for <c-with> functions", function(done) { it("should allow for <c-with> functions", function(done) {
testRender("test-project/rhtml-templates/with.rhtml", {}, done); testRender("test-project/rhtml-templates/with.rhtml", {}, done);
}); });
it("should allow for scriptlets", function(done) { it("should allow for scriptlets", function(done) {
testRender("test-project/rhtml-templates/scriptlet.rhtml", {}, done); testRender("test-project/rhtml-templates/scriptlet.rhtml", {}, done);
}); });
it("should allow for when and otherwise as attributes", function(done) { it("should allow for when and otherwise as attributes", function(done) {
testRender("test-project/rhtml-templates/choose-when-attributes.rhtml", {}, done); testRender("test-project/rhtml-templates/choose-when-attributes.rhtml", {}, done);
}); });
it("should allow for elements to be stripped out at compile time", function(done) { it("should allow for elements to be stripped out at compile time", function(done) {
testRender("test-project/rhtml-templates/strip.rhtml", {}, done); testRender("test-project/rhtml-templates/strip.rhtml", {}, done);
}); });
it("should allow for body content to be replaced with the result of an expression", function(done) { it("should allow for body content to be replaced with the result of an expression", function(done) {
testRender("test-project/rhtml-templates/content.rhtml", {}, done); testRender("test-project/rhtml-templates/content.rhtml", {}, done);
}); });
it("should allow for an element to be replaced with the result of an expression", function(done) { it("should allow for an element to be replaced with the result of an expression", function(done) {
testRender("test-project/rhtml-templates/replace.rhtml", {message: "Hello World!"}, done); testRender("test-project/rhtml-templates/replace.rhtml", {message: "Hello World!"}, done);
}); });
it("should allow for includes", function(done) { it("should allow for includes", function(done) {
testRender("test-project/rhtml-templates/include.rhtml", {}, done); testRender("test-project/rhtml-templates/include.rhtml", {}, done);
}); });
it("should allow for <c-invoke function... />", function(done) { it("should allow for <c-invoke function... />", function(done) {
testRender("test-project/rhtml-templates/invoke.rhtml", {}, done); testRender("test-project/rhtml-templates/invoke.rhtml", {}, done);
}); });
it("should allow for require", function(done) { it("should allow for require", function(done) {
testRender("test-project/rhtml-templates/require.rhtml", {}, done); testRender("test-project/rhtml-templates/require.rhtml", {}, done);
}); });
// it("should handle errors correctly", function(done) { // it("should handle errors correctly", function(done) {
// var tryTemplate = function(path, callback) { // var tryTemplate = function(path, callback) {
// try // try
// { // {
@ -241,7 +241,7 @@ describe('raptor-templates/rhtml' , function() {
// callback("", []); // callback("", []);
// } // }
// catch(e) { // catch(e) {
// if (!e.errors) { // if (!e.errors) {
// logger.error('Error message for template at path "' + path + '": ' + e, e); // logger.error('Error message for template at path "' + path + '": ' + e, e);
// } // }
@ -251,50 +251,50 @@ describe('raptor-templates/rhtml' , function() {
// callback(e.toString(), e.errors); // callback(e.toString(), e.errors);
// } // }
// }; // };
// tryTemplate("test-project/rhtml-templates/errors.rhtml", function(message, errors) { // tryTemplate("test-project/rhtml-templates/errors.rhtml", function(message, errors) {
// var len = errors ? errors.length : -1; // var len = errors ? errors.length : -1;
// expect(len).toEqual(25); // expect(len).toEqual(25);
// }); // });
// }); // });
it("should allow static file includes", function(done) { it("should allow static file includes", function(done) {
testRender("test-project/rhtml-templates/include-resource-static.rhtml", {}, done); testRender("test-project/rhtml-templates/include-resource-static.rhtml", {}, done);
}); });
it("should allow HTML pages with inline script", function(done) { it("should allow HTML pages with inline script", function(done) {
testRender("test-project/rhtml-templates/inline-script.rhtml", {name: "World"}, done); testRender("test-project/rhtml-templates/inline-script.rhtml", {name: "World"}, done);
}); });
it("should allow CDATA inside templates", function(done) { it("should allow CDATA inside templates", function(done) {
testRender("test-project/rhtml-templates/cdata.rhtml", {name: "World"}, done); testRender("test-project/rhtml-templates/cdata.rhtml", {name: "World"}, done);
}); });
// it("should allow type conversion", function(done) { // it("should allow type conversion", function(done) {
// var TypeConverter = require('raptor/templating/compiler/TypeConverter'); // var TypeConverter = require('raptor/templating/compiler/TypeConverter');
// expect(TypeConverter.convert('${entity:special}', "string", true).toString()).toEqual('"&special;"'); // expect(TypeConverter.convert('${entity:special}', "string", true).toString()).toEqual('"&special;"');
// }); // });
it("should allow for if...else", function(done) { it("should allow for if...else", function(done) {
testRender("test-project/rhtml-templates/if-else.rhtml", {}, done); testRender("test-project/rhtml-templates/if-else.rhtml", {}, done);
}); });
it("should allow for expressions and variables inside JavaScript strings", function(done) { it("should allow for expressions and variables inside JavaScript strings", function(done) {
testRender("test-project/rhtml-templates/string-expressions.rhtml", {name: "John", count: 10}, done); testRender("test-project/rhtml-templates/string-expressions.rhtml", {name: "John", count: 10}, done);
}); });
it("should allow for simple conditionals", function(done) { it("should allow for simple conditionals", function(done) {
testRender("test-project/rhtml-templates/simple-conditionals.rhtml", {name: "John", count: 51}, done); testRender("test-project/rhtml-templates/simple-conditionals.rhtml", {name: "John", count: 51}, done);
}); });
it("should allow for conditional attributes", function(done) { it("should allow for conditional attributes", function(done) {
testRender("test-project/rhtml-templates/conditional-attributes.rhtml", {}, done); testRender("test-project/rhtml-templates/conditional-attributes.rhtml", {}, done);
}); });
it("should allow for dynamic attributes to be passed to tag renderer using a custom property name", function(done) { it("should allow for dynamic attributes to be passed to tag renderer using a custom property name", function(done) {
testRender("test-project/rhtml-templates/dynamic-attributes.rhtml", {}, done); testRender("test-project/rhtml-templates/dynamic-attributes.rhtml", {}, done);
}); });
@ -306,7 +306,7 @@ describe('raptor-templates/rhtml' , function() {
it("should allow for dynamic attributes to be passed to tag renderer as part of input object", function(done) { it("should allow for dynamic attributes to be passed to tag renderer as part of input object", function(done) {
testRender("test-project/rhtml-templates/dynamic-attributes3.rhtml", {}, done); testRender("test-project/rhtml-templates/dynamic-attributes3.rhtml", {}, done);
}); });
// it("should allow for nodes to be converted to expressions", function(done) { // it("should allow for nodes to be converted to expressions", function(done) {
// var ElementNode = require('raptor/templating/compiler/ElementNode'); // var ElementNode = require('raptor/templating/compiler/ElementNode');
// var TextNode = require('raptor/templating/compiler/TextNode'); // var TextNode = require('raptor/templating/compiler/TextNode');
@ -314,37 +314,37 @@ describe('raptor-templates/rhtml' , function() {
// var compiler = require('raptor/templating/compiler').createCompiler(); // var compiler = require('raptor/templating/compiler').createCompiler();
// var template = new TemplateBuilder(compiler); // var template = new TemplateBuilder(compiler);
// var div = new ElementNode("div"); // var div = new ElementNode("div");
// var text = new TextNode("Hello World!"); // var text = new TextNode("Hello World!");
// div.appendChild(text); // div.appendChild(text);
// var expression = div.getExpression(template).toString(); // var expression = div.getExpression(template).toString();
// var bodyContentExpression = div.getBodyContentExpression(template).toString(); // var bodyContentExpression = div.getBodyContentExpression(template).toString();
// var sb = require('raptor/strings').createStringBuilder(); // var sb = require('raptor/strings').createStringBuilder();
// var context = require('raptor/templating').createContext(sb); // var context = require('raptor/templating').createContext(sb);
// var output = eval(expression); // var output = eval(expression);
// expect(output.toString()).toEqual('<div>Hello World!</div>'); // expect(output.toString()).toEqual('<div>Hello World!</div>');
// output = eval(bodyContentExpression); // output = eval(bodyContentExpression);
// expect(output.toString()).toEqual('Hello World!'); // expect(output.toString()).toEqual('Hello World!');
// }); // });
it("should allow for nested attributes", function(done) { it("should allow for nested attributes", function(done) {
testRender("test-project/rhtml-templates/nested-attrs.rhtml", {active: true}, done); testRender("test-project/rhtml-templates/nested-attrs.rhtml", {active: true}, done);
}); });
it("should allow for new variables to be created and assigned values", function(done) { it("should allow for new variables to be created and assigned values", function(done) {
testRender("test-project/rhtml-templates/var.rhtml", {active: true}, done); testRender("test-project/rhtml-templates/var.rhtml", {active: true}, done);
}); });
it("should handle XML escaping correctly", function(done) { it("should handle XML escaping correctly", function(done) {
testRender("test-project/rhtml-templates/xml-escaping.rhtml", {name: "<Patrick>", welcome: '<span>Welcome</span>'}, done); testRender("test-project/rhtml-templates/xml-escaping.rhtml", {name: "<Patrick>", welcome: '<span>Welcome</span>'}, done);
}); });
it("should allow for a doctype tag and a doctype attribute", function(done) { it("should allow for a doctype tag and a doctype attribute", function(done) {
testRender("test-project/rhtml-templates/doctype.rhtml", {}, done); testRender("test-project/rhtml-templates/doctype.rhtml", {}, done);
}); });
@ -356,7 +356,7 @@ describe('raptor-templates/rhtml' , function() {
it("should allow for caching HTML fragments", function(done) { it("should allow for caching HTML fragments", function(done) {
testRender("test-project/rhtml-templates/caching.rhtml", {}, done); testRender("test-project/rhtml-templates/caching.rhtml", {}, done);
}); });
it("should escape XML in text node when enabled", function(done) { it("should escape XML in text node when enabled", function(done) {
testRender("test-project/rhtml-templates/escape-xml-enabled.rhtml", {}, done); testRender("test-project/rhtml-templates/escape-xml-enabled.rhtml", {}, done);
}); });
@ -394,4 +394,3 @@ describe('raptor-templates/rhtml' , function() {
}); });
}); });