underscore-template-loader
Advanced tools
Comparing version 0.8.0 to 1.0.0
172
index.js
@@ -6,6 +6,8 @@ var path = require('path'); | ||
// Try getting underscore first, then lodash | ||
var _; | ||
try { | ||
var _ = require('underscore'); | ||
_ = require('underscore'); | ||
} catch (e) { | ||
var _ = require('lodash'); | ||
_ = require('lodash'); | ||
} | ||
@@ -17,108 +19,108 @@ | ||
module.exports = function(content) { | ||
this.cacheable && this.cacheable(); | ||
var callback = this.async(); | ||
this.cacheable && this.cacheable(); | ||
var callback = this.async(); | ||
// Default arguments | ||
var root, | ||
parseMacros = true, | ||
engine = false, | ||
withImports = false, | ||
attributes = ['img:src'], | ||
parseDynamicRoutes = false; | ||
// Default arguments | ||
var root, | ||
parseMacros = true, | ||
engine = false, | ||
withImports = false, | ||
attributes = ['img:src'], | ||
parseDynamicRoutes = false; | ||
// Parse arguments | ||
var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query); | ||
// Parse arguments | ||
var query = this.query instanceof Object ? this.query : loaderUtils.parseQuery(this.query); | ||
if (_.isObject(query)) { | ||
root = query.root; | ||
if (_.isObject(query)) { | ||
root = query.root; | ||
// Apply template settings | ||
_.each(_.pick(query, 'interpolate', 'escape', 'evaluate'), function(value, key) { | ||
_.templateSettings[key] = new RegExp(value, 'g'); | ||
}); | ||
// Apply template settings | ||
_.each(_.pick(query, 'interpolate', 'escape', 'evaluate'), function(value, key) { | ||
_.templateSettings[key] = new RegExp(value, 'g'); | ||
}); | ||
// Apply template variable | ||
if (query.variable !== undefined) { | ||
_.templateSettings.variable = query.variable; | ||
} | ||
// Apply template variable | ||
if (query.variable !== undefined) { | ||
_.templateSettings.variable = query.variable; | ||
} | ||
// Set tag+attribute to parse for external resources | ||
if (query.attributes !== undefined) { | ||
attributes = _.isArray(query.attributes) ? query.attributes : []; | ||
} | ||
// Set tag+attribute to parse for external resources | ||
if (query.attributes !== undefined) { | ||
attributes = _.isArray(query.attributes) ? query.attributes : []; | ||
} | ||
// Parse / ignore macros | ||
if (query.parseMacros !== undefined) { | ||
parseMacros = !!query.parseMacros; | ||
} | ||
// Parse / ignore macros | ||
if (query.parseMacros !== undefined) { | ||
parseMacros = !!query.parseMacros; | ||
} | ||
// Template engine | ||
if (query.engine !== undefined) { | ||
engine = query.engine; | ||
} | ||
// Template engine | ||
if (query.engine !== undefined) { | ||
engine = query.engine; | ||
} | ||
// Template settings imports (on by default for lodash) | ||
if (query.withImports !== undefined) { | ||
withImports = query.withImports; | ||
} else if (engine === 'lodash') { | ||
withImports = true; | ||
} | ||
// Prepend a html comment with the filename in it | ||
if (query.prependFilenameComment) { | ||
var filenameRelative = path.relative(query.prependFilenameComment, this.resource); | ||
content = "\n<!-- " + filenameRelative + " -->\n" + content; | ||
} | ||
// Check if dynamic routes must be parsed | ||
if (query.parseDynamicRoutes !== undefined) { | ||
parseDynamicRoutes = !!query.parseDynamicRoutes; | ||
} | ||
// Template settings imports (on by default for lodash) | ||
if (query.withImports !== undefined) { | ||
withImports = query.withImports; | ||
} else if (engine === 'lodash') { | ||
withImports = true; | ||
} | ||
// Include additional macros | ||
if (_.isObject(this.options.macros)) { | ||
_.extend(macros, this.options.macros); | ||
// Prepend a html comment with the filename in it | ||
if (query.prependFilenameComment) { | ||
var filenameRelative = path.relative(query.prependFilenameComment, this.resource); | ||
content = "\n<!-- " + filenameRelative + " -->\n" + content; | ||
} | ||
// Parse macros | ||
if (parseMacros) { | ||
var macrosContext = macroParser(content, function (macro) { | ||
return _.isFunction(macros[macro]); | ||
}, 'MACRO'); | ||
content = macrosContext.replaceMatches(content); | ||
// Check if dynamic routes must be parsed | ||
if (query.parseDynamicRoutes !== undefined) { | ||
parseDynamicRoutes = !!query.parseDynamicRoutes; | ||
} | ||
} | ||
// Parse attributes | ||
var attributesContext = attributeParser(content, function (tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) != -1; | ||
}, 'ATTRIBUTE', root, parseDynamicRoutes); | ||
content = attributesContext.replaceMatches(content); | ||
// Include additional macros | ||
if (this.options && _.isObject(this.options.macros)) { | ||
_.extend(macros, this.options.macros); | ||
} | ||
// Compile template | ||
var source = _.template(content).source; | ||
// Parse macros | ||
if (parseMacros) { | ||
var macrosContext = macroParser(content, function (macro) { | ||
return _.isFunction(macros[macro]); | ||
}, 'MACRO'); | ||
content = macrosContext.replaceMatches(content); | ||
} | ||
// Resolve macros | ||
if (parseMacros) { | ||
source = macrosContext.resolveMacros(source, macros); | ||
} | ||
// Parse attributes | ||
var attributesContext = attributeParser(content, function (tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) != -1; | ||
}, 'ATTRIBUTE', root, parseDynamicRoutes); | ||
content = attributesContext.replaceMatches(content); | ||
// Resolve attributes | ||
source = attributesContext.resolveAttributes(source); | ||
// Compile template | ||
var source = _.template(content).source; | ||
// Build the module export, optionally with template imports | ||
if (withImports) { | ||
source = 'module.exports = Function(_.keys(_.templateSettings.imports), \'return \' + ' + source + '.toString()).apply(undefined, _.values(_.templateSettings.imports));\n'; | ||
} else { | ||
source = 'module.exports = ' + source + ';\n'; | ||
} | ||
// Resolve macros | ||
if (parseMacros) { | ||
source = macrosContext.resolveMacros(source, macros); | ||
} | ||
// Explicitly require the engine, otherwise it will rely on the global _ | ||
if (engine) { | ||
source = 'var _ = require(\'' + engine + '\');\n' + source; | ||
} | ||
// Resolve attributes | ||
source = attributesContext.resolveAttributes(source); | ||
callback(null, source); | ||
// Build the module export, optionally with template imports | ||
if (withImports) { | ||
source = 'module.exports = Function(_.keys(_.templateSettings.imports), \'return \' + ' + source + '.toString()).apply(undefined, _.values(_.templateSettings.imports));\n'; | ||
} else { | ||
source = 'module.exports = ' + source + ';\n'; | ||
} | ||
// Explicitly require the engine, otherwise it will rely on the global _ | ||
if (engine) { | ||
source = 'var _ = require(\'' + engine + '\');\n' + source; | ||
} | ||
callback(null, source); | ||
}; | ||
module.exports._ = _; |
{ | ||
"name": "underscore-template-loader", | ||
"version": "0.8.0", | ||
"version": "1.0.0", | ||
"description": "An Underscore and Lodash template loader for Webpack", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ underscore-template-loader | ||
<br> | ||
* 0.8: Macros now support object literals as arguments | ||
* 1.0: Loader now works with Webpack 4. Still a beta release. | ||
@@ -12,0 +12,0 @@ ### Installation |
@@ -11,13 +11,13 @@ var path = require('path'); | ||
function testMatch(name, html, result) { | ||
it('should parse ' + name, function() { | ||
// The loader has an absolute path so we have to use a placeholder: | ||
it('should parse ' + name, function() { | ||
// The loader has an absolute path so we have to use a placeholder: | ||
var parsed = attributeParser(html, function(tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) != -1; | ||
}, 'ATTRIBUTE', '/asdf/'); | ||
var parsed = attributeParser(html, function(tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) != -1; | ||
}, 'ATTRIBUTE', '/asdf/'); | ||
// We are only interested in the `value` property of `matches`. | ||
var values = parsed.matches.map(function(match) { return match.value; }); | ||
assert.deepEqual(values, result); | ||
}); | ||
// We are only interested in the `value` property of `matches`. | ||
var values = parsed.matches.map(function(match) { return match.value; }); | ||
assert.deepEqual(values, result); | ||
}); | ||
} | ||
@@ -27,5 +27,5 @@ | ||
function replaceMatch(html) { | ||
return attributeParser(html, function(tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) != -1; | ||
}, 'ATTRIBUTE').replaceMatches(html); | ||
return attributeParser(html, function(tag, attr) { | ||
return attributes.indexOf(tag + ':' + attr) != -1; | ||
}, 'ATTRIBUTE').replaceMatches(html); | ||
} | ||
@@ -35,35 +35,35 @@ | ||
describe('attribute parser', function() { | ||
testMatch('normal', 'Text <img src="image.png"><img src="image2.png">', ['image.png', 'image2.png']); | ||
testMatch('single-quotes', "Text <img src='image.png'><img src='image2.png'>", ['image.png', 'image2.png']); | ||
testMatch('whitespace', 'T ex t <img \t src = "image.png" > <img\t\nsrc\n=\n"image2.png"\n>', ['image.png', 'image2.png']); | ||
testMatch('whitespace2', 'Text < img src="image.png" >', []); | ||
testMatch('wrong <', 'Text <<img src="image.png">', ['image.png']); | ||
testMatch("wrong >", 'Text ><img src="image.png">', ['image.png']); | ||
testMatch('no quot', '<img src=image.png>', ['image.png']); | ||
testMatch('first tag', '<img src="image.png">', ['image.png']); | ||
testMatch('comment', '<!--<img src="image.png">-->', []); | ||
testMatch('comment2', '<!--<!--<img src="image.png">-->', []); | ||
testMatch('comment3', '<!--><img src="image.png">-->', []); | ||
testMatch('comment4', '<!----><img src="image.png">-->', ['image.png']); | ||
testMatch('tags', '<img src="image.png"><script src="script.js"></script><link type="stylesheet" href="style.css">', ['image.png', 'style.css']); | ||
testMatch('cdata', '<![CDATA[<img src="image.png">]]><img src="image2.png">', ['image2.png']); | ||
testMatch('doctype', '<!doctype html><img src="image.png">', ['image.png']); | ||
testMatch('normal', 'Text <img src="image.png"><img src="image2.png">', ['image.png', 'image2.png']); | ||
testMatch('single-quotes', "Text <img src='image.png'><img src='image2.png'>", ['image.png', 'image2.png']); | ||
testMatch('whitespace', 'T ex t <img \t src = "image.png" > <img\t\nsrc\n=\n"image2.png"\n>', ['image.png', 'image2.png']); | ||
testMatch('whitespace2', 'Text < img src="image.png" >', []); | ||
testMatch('wrong <', 'Text <<img src="image.png">', ['image.png']); | ||
testMatch("wrong >", 'Text ><img src="image.png">', ['image.png']); | ||
testMatch('no quot', '<img src=image.png>', ['image.png']); | ||
testMatch('first tag', '<img src="image.png">', ['image.png']); | ||
testMatch('comment', '<!--<img src="image.png">-->', []); | ||
testMatch('comment2', '<!--<!--<img src="image.png">-->', []); | ||
testMatch('comment3', '<!--><img src="image.png">-->', []); | ||
testMatch('comment4', '<!----><img src="image.png">-->', ['image.png']); | ||
testMatch('tags', '<img src="image.png"><script src="script.js"></script><link type="stylesheet" href="style.css">', ['image.png', 'style.css']); | ||
testMatch('cdata', '<![CDATA[<img src="image.png">]]><img src="image2.png">', ['image2.png']); | ||
testMatch('doctype', '<!doctype html><img src="image.png">', ['image.png']); | ||
it('should replace image paths', function() { | ||
var html = '<img src="image.png">'; | ||
var result = replaceMatch(html); | ||
assert.startsWith(result, '<img src="____ATTRIBUTE'); | ||
}); | ||
it('should replace image paths', function() { | ||
var html = '<img src="image.png">'; | ||
var result = replaceMatch(html); | ||
assert.startsWith(result, '<img src="____ATTRIBUTE'); | ||
}); | ||
it('should append url hash', function() { | ||
var html = '<img src="image.png#asdf">'; | ||
var result = replaceMatch(html); | ||
assert.endsWith(result, '____#asdf">'); | ||
}); | ||
it('should append url hash', function() { | ||
var html = '<img src="image.png#asdf">'; | ||
var result = replaceMatch(html); | ||
assert.endsWith(result, '____#asdf">'); | ||
}); | ||
it('should not replace urls', function() { | ||
var html = '<img src="https://example.com/test.png">'; | ||
var result = replaceMatch(html); | ||
assert.equal(result, html); | ||
}); | ||
it('should not replace urls', function() { | ||
var html = '<img src="https://example.com/test.png">'; | ||
var result = replaceMatch(html); | ||
assert.equal(result, html); | ||
}); | ||
}); |
@@ -5,7 +5,7 @@ var fs = require('fs'); | ||
function loadOutput(outputPath) { | ||
return fs.readFileSync(path.join(path.dirname(__dirname), 'templates/output', outputPath)) | ||
.toString() | ||
.replace(/%%LOADER%%/g, require.resolve('../../file-loader.js')); | ||
return fs.readFileSync(path.join(path.dirname(__dirname), 'templates/output', outputPath)) | ||
.toString() | ||
.replace(/%%LOADER%%/g, require.resolve('../../file-loader.js')); | ||
} | ||
module.exports = loadOutput; | ||
module.exports = loadOutput; |
@@ -5,5 +5,5 @@ var fs = require('fs'); | ||
function loadTemplate(templatePath) { | ||
return fs.readFileSync(path.join(path.dirname(__dirname), 'templates', templatePath)).toString(); | ||
return fs.readFileSync(path.join(path.dirname(__dirname), 'templates', templatePath)).toString(); | ||
} | ||
module.exports = loadTemplate; | ||
module.exports = loadTemplate; |
@@ -5,7 +5,7 @@ var fs = require('fs'); | ||
function WebpackLoaderMock (options) { | ||
this.context = options.context || ''; | ||
this.query = options.query; | ||
this.options = options.options || {}; | ||
this.resource = options.resource; | ||
this._asyncCallback = options.async; | ||
this.context = options.context || ''; | ||
this.query = options.query; | ||
this.options = options.options || {}; | ||
this.resource = options.resource; | ||
this._asyncCallback = options.async; | ||
this._resolveStubs = options.resolveStubs || {}; | ||
@@ -15,5 +15,5 @@ } | ||
WebpackLoaderMock.prototype.async = function () { | ||
return this._asyncCallback; | ||
return this._asyncCallback; | ||
}; | ||
module.exports = WebpackLoaderMock; |
@@ -13,83 +13,83 @@ var fs = require('fs'); | ||
function testTemplate(loader, template, options, testFn) { | ||
loader.call(new WebpackLoaderMock({ | ||
query: options.query, | ||
resource: path.join(__dirname, 'templates', template), | ||
options: options.options, | ||
async: function (err, source) { | ||
testFn(source); | ||
} | ||
}), loadTemplate(template)); | ||
loader.call(new WebpackLoaderMock({ | ||
query: options.query, | ||
resource: path.join(__dirname, 'templates', template), | ||
options: options.options, | ||
async: function (err, source) { | ||
testFn(source); | ||
} | ||
}), loadTemplate(template)); | ||
} | ||
describe('loader', function () { | ||
it('should load simple underscore template', function (done) { | ||
testTemplate(loader, 'simple.html', {}, function (output) { | ||
// Copy and paste the result of `console.log(output)` to templates/output/simple.txt | ||
assert.equal(output, loadOutput('simple.txt')); | ||
done(); | ||
}); | ||
it('should load simple underscore template', function (done) { | ||
testTemplate(loader, 'simple.html', {}, function (output) { | ||
// Copy and paste the result of `console.log(output)` to templates/output/simple.txt | ||
assert.equal(output, loadOutput('simple.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should prepend html comment', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
prependFilenameComment: __dirname | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-with-comment.txt')); | ||
done(); | ||
}); | ||
it('should prepend html comment', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
prependFilenameComment: __dirname | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-with-comment.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should use underscore as the engine when specified', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
engine: 'underscore' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-underscore.txt')); | ||
done(); | ||
}); | ||
it('should use underscore as the engine when specified', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
engine: 'underscore' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-underscore.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should use lodash as the engine when specified and include imports automatically', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
engine: 'lodash' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-lodash.txt')); | ||
done(); | ||
}); | ||
it('should use lodash as the engine when specified and include imports automatically', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
engine: 'lodash' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-lodash.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should include the template imports when withImports is true', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
withImports: true | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-with-imports.txt')); | ||
done(); | ||
}); | ||
it('should include the template imports when withImports is true', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
withImports: true | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-with-imports.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should NOT include template imports when withImports is false', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
engine: 'lodash', | ||
withImports: false | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-lodash-no-imports.txt')); | ||
done(); | ||
}); | ||
it('should NOT include template imports when withImports is false', function (done) { | ||
testTemplate(loader, 'simple.html', { | ||
query: { | ||
engine: 'lodash', | ||
withImports: false | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('simple-lodash-no-imports.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should be possible to require a template', function (done) { | ||
testTemplate(loader, 'require.html', {}, function (output) { | ||
assert.equal(output, loadOutput('require.txt')); | ||
done(); | ||
}); | ||
it('should be possible to require a template', function (done) { | ||
testTemplate(loader, 'require.html', {}, function (output) { | ||
assert.equal(output, loadOutput('require.txt')); | ||
done(); | ||
}); | ||
}); | ||
@@ -103,91 +103,91 @@ it('should be possible to require a template with custom args', function (done) { | ||
it('should be possible to include a template', function (done) { | ||
testTemplate(loader, 'include.html', {}, function (output) { | ||
assert.equal(output, loadOutput('include.txt')); | ||
done(); | ||
}); | ||
it('should be possible to include a template', function (done) { | ||
testTemplate(loader, 'include.html', {}, function (output) { | ||
assert.equal(output, loadOutput('include.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should require an image', function (done) { | ||
testTemplate(loader, 'image.html', {}, function (output) { | ||
assert.equal(output, loadOutput('image.txt')); | ||
done(); | ||
}); | ||
it('should require an image', function (done) { | ||
testTemplate(loader, 'image.html', {}, function (output) { | ||
assert.equal(output, loadOutput('image.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should require given custom attributes', function (done) { | ||
testTemplate(loader, 'custom-attributes.html', { | ||
query: { | ||
attributes: ['img:src', 'link:href'] | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('custom-attributes.txt')); | ||
done(); | ||
}); | ||
it('should require given custom attributes', function (done) { | ||
testTemplate(loader, 'custom-attributes.html', { | ||
query: { | ||
attributes: ['img:src', 'link:href'] | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('custom-attributes.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should not parse an absolute image without root option given', function (done) { | ||
testTemplate(loader, 'absolute-image.html', {}, function (output) { | ||
assert.equal(output, loadOutput('absolute-image.txt')); | ||
done(); | ||
}); | ||
it('should not parse an absolute image without root option given', function (done) { | ||
testTemplate(loader, 'absolute-image.html', {}, function (output) { | ||
assert.equal(output, loadOutput('absolute-image.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should parse an absolute image if root option is given', function (done) { | ||
testTemplate(loader, 'absolute-image.html', { | ||
query: { | ||
root: '/bar' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('absolute-image-with-root.txt')); | ||
done(); | ||
}); | ||
it('should parse an absolute image if root option is given', function (done) { | ||
testTemplate(loader, 'absolute-image.html', { | ||
query: { | ||
root: '/bar' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('absolute-image-with-root.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should leave dynamic attributes unaltered', function (done) { | ||
testTemplate(loader, 'dynamic-attribute.html', { | ||
query: { | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('dynamic-attribute.txt')); | ||
done(); | ||
}); | ||
it('should leave dynamic attributes unaltered', function (done) { | ||
testTemplate(loader, 'dynamic-attribute.html', { | ||
query: { | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('dynamic-attribute.txt')); | ||
done(); | ||
}); | ||
it('should leave dynamic attributes unaltered with root', function (done) { | ||
testTemplate(loader, 'dynamic-attribute-with-root.html', { | ||
query: { | ||
root: '/bar' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('dynamic-attribute-with-root.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should leave dynamic attributes unaltered with root', function (done) { | ||
testTemplate(loader, 'dynamic-attribute-with-root.html', { | ||
query: { | ||
root: '/bar' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('dynamic-attribute-with-root.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should parse dynamic attributes with parseDynamicRoutes', function (done) { | ||
testTemplate(loader, 'dynamic-attribute-with-parseDynamicRoutes.html', { | ||
query: { | ||
root: 'myapp', | ||
parseDynamicRoutes: true | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('dynamic-attribute-with-parseDynamicRoutes.txt')); | ||
done(); | ||
}); | ||
it('should parse dynamic attributes with parseDynamicRoutes', function (done) { | ||
testTemplate(loader, 'dynamic-attribute-with-parseDynamicRoutes.html', { | ||
query: { | ||
root: 'myapp', | ||
parseDynamicRoutes: true | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('dynamic-attribute-with-parseDynamicRoutes.txt')); | ||
done(); | ||
}); | ||
// FIXME: Changing the underscore tags changes it globally | ||
it('should allow custom underscore tags', function (done) { | ||
testTemplate(loader, 'custom-tags.html', { | ||
query: { | ||
interpolate: '\\{\\[(.+?)\\]\\}', | ||
evaluate: '\\{%([\\s\\S]+?)%\\}', | ||
escape: '\\{\\{(.+?)\\}\\}' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('custom-tags.txt')); | ||
done(); | ||
}); | ||
}); | ||
// FIXME: Changing the underscore tags changes it globally | ||
it('should allow custom underscore tags', function (done) { | ||
testTemplate(loader, 'custom-tags.html', { | ||
query: { | ||
interpolate: '\\{\\[(.+?)\\]\\}', | ||
evaluate: '\\{%([\\s\\S]+?)%\\}', | ||
escape: '\\{\\{(.+?)\\}\\}' | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('custom-tags.txt')); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -14,75 +14,75 @@ var fs = require('fs'); | ||
function testTemplate(loader, template, options, testFn) { | ||
loader.call(new WebpackLoaderMock({ | ||
query: options.query, | ||
resource: path.join(__dirname, 'templates', template), | ||
options: options.options, | ||
async: function (err, source) { | ||
testFn(source); | ||
} | ||
}), loadTemplate(template)); | ||
loader.call(new WebpackLoaderMock({ | ||
query: options.query, | ||
resource: path.join(__dirname, 'templates', template), | ||
options: options.options, | ||
async: function (err, source) { | ||
testFn(source); | ||
} | ||
}), loadTemplate(template)); | ||
} | ||
describe('macro', function () { | ||
it('should be parsed', function (done) { | ||
testTemplate(loader, 'custom-macro.html', { | ||
options: { | ||
macros: { | ||
foo: function () { | ||
return '"<p>bar</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('custom-macro.txt')); | ||
done(); | ||
}); | ||
it('should be parsed', function (done) { | ||
testTemplate(loader, 'custom-macro.html', { | ||
options: { | ||
macros: { | ||
foo: function () { | ||
return '"<p>bar</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('custom-macro.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should receive boolean arguments', function (done) { | ||
testTemplate(loader, 'macro_boolean_args.html', { | ||
options: { | ||
macros: { | ||
bool_test: function (arg) { | ||
assert.typeOf(arg, 'boolean'); | ||
return arg ? '"<p>TRUE</p>"' : '"<p>FALSE</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_boolean_args.txt')); | ||
done(); | ||
}); | ||
it('should receive boolean arguments', function (done) { | ||
testTemplate(loader, 'macro_boolean_args.html', { | ||
options: { | ||
macros: { | ||
bool_test: function (arg) { | ||
assert.typeOf(arg, 'boolean'); | ||
return arg ? '"<p>TRUE</p>"' : '"<p>FALSE</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_boolean_args.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should receive numeric arguments', function (done) { | ||
testTemplate(loader, 'macro_numeric_args.html', { | ||
options: { | ||
macros: { | ||
num_test: function (arg) { | ||
assert.typeOf(arg, 'number'); | ||
return '"<p>' + arg + '</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_numeric_args.txt')); | ||
done(); | ||
}); | ||
it('should receive numeric arguments', function (done) { | ||
testTemplate(loader, 'macro_numeric_args.html', { | ||
options: { | ||
macros: { | ||
num_test: function (arg) { | ||
assert.typeOf(arg, 'number'); | ||
return '"<p>' + arg + '</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_numeric_args.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should receive string arguments', function (done) { | ||
testTemplate(loader, 'macro_string_args.html', { | ||
options: { | ||
macros: { | ||
str_test: function (arg) { | ||
assert.typeOf(arg, 'string'); | ||
return '"<p>' + arg.toUpperCase() + '</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_string_args.txt')); | ||
done(); | ||
}); | ||
it('should receive string arguments', function (done) { | ||
testTemplate(loader, 'macro_string_args.html', { | ||
options: { | ||
macros: { | ||
str_test: function (arg) { | ||
assert.typeOf(arg, 'string'); | ||
return '"<p>' + arg.toUpperCase() + '</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_string_args.txt')); | ||
done(); | ||
}); | ||
}); | ||
@@ -105,101 +105,101 @@ it('should receive object arguments', function(done) { | ||
it('should receive argument list', function (done) { | ||
testTemplate(loader, 'macro_argument_list.html', { | ||
options: { | ||
macros: { | ||
numbers: function (first, second, third) { | ||
assert.typeOf(first, 'number'); | ||
assert.typeOf(second, 'number'); | ||
assert.typeOf(third, 'number'); | ||
it('should receive argument list', function (done) { | ||
testTemplate(loader, 'macro_argument_list.html', { | ||
options: { | ||
macros: { | ||
numbers: function (first, second, third) { | ||
assert.typeOf(first, 'number'); | ||
assert.typeOf(second, 'number'); | ||
assert.typeOf(third, 'number'); | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
output += '<p>' + arguments[i] + '</p>'; | ||
} | ||
return '"' + output + '"'; | ||
}, | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
output += '<p>' + arguments[i] + '</p>'; | ||
} | ||
return '"' + output + '"'; | ||
}, | ||
booleans: function (first, second, third) { | ||
assert.typeOf(first, 'boolean'); | ||
assert.typeOf(second, 'boolean'); | ||
assert.typeOf(third, 'boolean'); | ||
booleans: function (first, second, third) { | ||
assert.typeOf(first, 'boolean'); | ||
assert.typeOf(second, 'boolean'); | ||
assert.typeOf(third, 'boolean'); | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
output += '<p>' + (arguments[i] ? 'TRUE' : 'FALSE') + '</p>'; | ||
} | ||
return '"' + output + '"'; | ||
}, | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
output += '<p>' + (arguments[i] ? 'TRUE' : 'FALSE') + '</p>'; | ||
} | ||
return '"' + output + '"'; | ||
}, | ||
strings: function (first, second, third) { | ||
assert.typeOf(first, 'string'); | ||
assert.typeOf(second, 'string'); | ||
assert.typeOf(third, 'string'); | ||
strings: function (first, second, third) { | ||
assert.typeOf(first, 'string'); | ||
assert.typeOf(second, 'string'); | ||
assert.typeOf(third, 'string'); | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
output += '<p>' + arguments[i].toLowerCase().replace(/"/g, "\\\"") + '</p>'; | ||
} | ||
return '"' + output + '"'; | ||
}, | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
output += '<p>' + arguments[i].toLowerCase().replace(/"/g, "\\\"") + '</p>'; | ||
} | ||
return '"' + output + '"'; | ||
}, | ||
mixed: function () { | ||
assert.equal(arguments.length, 6); | ||
assert.typeOf(arguments[0], 'boolean'); | ||
assert.typeOf(arguments[1], 'number'); | ||
assert.typeOf(arguments[2], 'string'); | ||
assert.typeOf(arguments[3], 'boolean'); | ||
assert.typeOf(arguments[4], 'string'); | ||
assert.typeOf(arguments[5], 'number'); | ||
mixed: function () { | ||
assert.equal(arguments.length, 6); | ||
assert.typeOf(arguments[0], 'boolean'); | ||
assert.typeOf(arguments[1], 'number'); | ||
assert.typeOf(arguments[2], 'string'); | ||
assert.typeOf(arguments[3], 'boolean'); | ||
assert.typeOf(arguments[4], 'string'); | ||
assert.typeOf(arguments[5], 'number'); | ||
var output = ''; | ||
var output = ''; | ||
for (var i = 0; i < arguments.length; i++) { | ||
var type = typeof(arguments[i]); | ||
for (var i = 0; i < arguments.length; i++) { | ||
var type = typeof(arguments[i]); | ||
if (type == 'string') { | ||
output += '<p>' + arguments[i].toLowerCase().replace(/"/g, "\\\"") + '</p>'; | ||
} else if (type == 'number') { | ||
output += '<p>' + arguments[i] + '</p>'; | ||
} else if (type == 'boolean') { | ||
output += '<p>' + (arguments[i] ? 'TRUE' : 'FALSE') + '</p>'; | ||
} | ||
} | ||
if (type == 'string') { | ||
output += '<p>' + arguments[i].toLowerCase().replace(/"/g, "\\\"") + '</p>'; | ||
} else if (type == 'number') { | ||
output += '<p>' + arguments[i] + '</p>'; | ||
} else if (type == 'boolean') { | ||
output += '<p>' + (arguments[i] ? 'TRUE' : 'FALSE') + '</p>'; | ||
} | ||
} | ||
return '"' + output + '"'; | ||
return '"' + output + '"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_argument_list.txt')); | ||
done(); | ||
}); | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_argument_list.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should not be evaluated', function (done) { | ||
testTemplate(loader, 'macro.html', { | ||
query: { | ||
parseMacros: false | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('disabled-macro.txt')); | ||
done(); | ||
}); | ||
it('should not be evaluated', function (done) { | ||
testTemplate(loader, 'macro.html', { | ||
query: { | ||
parseMacros: false | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('disabled-macro.txt')); | ||
done(); | ||
}); | ||
}); | ||
it('should be replaced when escaped', function (done) { | ||
testTemplate(loader, 'macro_escaped.html', { | ||
options: { | ||
macros: { | ||
unescaped: function () { | ||
return '"<p>Ok</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_escaped.txt')); | ||
done(); | ||
}); | ||
it('should be replaced when escaped', function (done) { | ||
testTemplate(loader, 'macro_escaped.html', { | ||
options: { | ||
macros: { | ||
unescaped: function () { | ||
return '"<p>Ok</p>"'; | ||
} | ||
} | ||
} | ||
}, function (output) { | ||
assert.equal(output, loadOutput('macro_escaped.txt')); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
878
0
51887
60