-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding in a way to not explicitly set context #303
Open
Garethp
wants to merge
2
commits into
olado:master
Choose a base branch
from
Garethp:noContext
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
'use strict'; | ||
|
||
var test = require('./util').testWithoutContext; | ||
var assert = require("assert") | ||
var doT = require(".."); | ||
|
||
var settings = { | ||
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g, | ||
interpolate: /\{\{=([\s\S]+?)\}\}/g, | ||
encode: /\{\{!([\s\S]+?)\}\}/g, | ||
use: /\{\{#([\s\S]+?)\}\}/g, | ||
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g, | ||
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g, | ||
defineParams:/^\s*([\w$]+):([\s\S]+)/, | ||
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g, | ||
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g, | ||
varname: "", | ||
strip: true, | ||
append: true, | ||
selfcontained: false, | ||
doNotSkipEncoded: false | ||
}; | ||
|
||
describe('noContext', function(){ | ||
describe('evaluate JavaScript', function() { | ||
it('should print numbers next to each other', function() { | ||
test([ | ||
'{{ one = 1; two = 2; }}{{= one }}{{= two }}', | ||
], {}, '12'); | ||
}); | ||
}); | ||
|
||
describe('interpolate 2 numbers', function() { | ||
it('should print numbers next to each other', function() { | ||
test([ | ||
'{{=one}}{{=two}}', | ||
'{{= one}}{{= two}}', | ||
'{{= one }}{{= two }}' | ||
], {one:1, two: 2}, '12'); | ||
}); | ||
}); | ||
|
||
describe('encoding with doNotSkipEncoded=false', function() { | ||
it('should not replace &', function() { | ||
global._encodeHTML = undefined; | ||
doT.templateSettings.doNotSkipEncoded = false; | ||
var fn = doT.template('<div>{{!foo}}</div>', settings); | ||
assert.equal(fn({foo:"&"}), "<div>&</div>"); | ||
}); | ||
}); | ||
|
||
describe('encoding with doNotSkipEncoded=true', function() { | ||
it('should replace &', function() { | ||
global._encodeHTML = undefined; | ||
settings.doNotSkipEncoded = true; | ||
assert.equal(doT.template('<div>{{!foo}}</div>', settings)({foo:"&"}), "<div>&amp;</div>"); | ||
assert.equal(doT.template('{{!a}}', settings)({a:"& < > / ' \""}), "& < > / ' ""); | ||
assert.equal(doT.template('{{!"& < > / \' \\""}}')(), "& < > / ' ""); | ||
}); | ||
}); | ||
|
||
describe('invalid JS in templates', function() { | ||
it('should throw exception', function() { | ||
assert.throws(function() { | ||
var fn = doT.template('<div>{{= foo + }}</div>', settings)(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('conditionals', function() { | ||
describe('without else', function() { | ||
var templates = [ | ||
'{{?one < 2}}{{=one}}{{?}}{{=two}}', | ||
'{{? one < 2 }}{{= one }}{{?}}{{= two }}' | ||
]; | ||
|
||
it('should evaluate condition and include template if valid', function() { | ||
test(templates, {one: 1, two: 2}, '12') | ||
}); | ||
|
||
it('should evaluate condition and do NOT include template if invalid', function() { | ||
test(templates, {one: 3, two: 2}, '2') | ||
}); | ||
}); | ||
|
||
|
||
describe('with else', function() { | ||
var templates = [ | ||
'{{?one < 2}}{{=one}}{{??}}{{=two}}{{?}}', | ||
'{{? one < 2 }}{{= one }}{{??}}{{= two }}{{?}}' | ||
]; | ||
|
||
it('should evaluate condition and include "if" template if valid', function() { | ||
test(templates, {one: 1, two: 2}, '1') | ||
}); | ||
|
||
it('should evaluate condition and include "else" template if invalid', function() { | ||
test(templates, {one: 3, two: 2}, '2') | ||
}); | ||
}); | ||
|
||
describe('with else if', function() { | ||
var templates = [ | ||
'{{?one < 2}}{{=one}}{{??two < 3}}{{=two}}{{??}}{{=three}}{{?}}', | ||
'{{? one < 2 }}{{= one }}{{?? two < 3 }}{{= two }}{{??}}{{= three }}{{?}}' | ||
]; | ||
|
||
it('should evaluate condition and include "if" template if valid', function() { | ||
test(templates, {one: 1, two: 2, three: 3}, '1') | ||
}); | ||
|
||
it('should evaluate condition and include "else if" template if second condition valid', function() { | ||
test(templates, {one: 10, two: 2, three: 3}, '2') | ||
}); | ||
|
||
it('should evaluate condition and include "else" template if invalid', function() { | ||
test(templates, {one: 10, two: 20, three: 3}, '3') | ||
}); | ||
}); | ||
}); | ||
|
||
describe('iteration', function() { | ||
describe('without index', function() { | ||
it('should repeat string N times', function() { | ||
test([ | ||
'{{~arr:x}}*{{~}}', | ||
'{{~ arr:x }}*{{~}}', | ||
'{{~ arr: x }}*{{~}}', | ||
'{{~ arr :x }}*{{~}}' | ||
], {arr: Array(3)}, '***'); | ||
}); | ||
|
||
it('should concatenate items', function() { | ||
test(['{{~arr:x}}{{=x}}{{~}}'], {arr: [1,2,3]}, '123'); | ||
}); | ||
}); | ||
|
||
describe('with index', function() { | ||
it('should repeat string N times', function() { | ||
test([ | ||
'{{~arr:x:i}}*{{~}}', | ||
'{{~ arr : x : i }}*{{~}}' | ||
], {arr: Array(3)}, '***'); | ||
}); | ||
|
||
it('should concatenate indices', function() { | ||
test(['{{~arr:x:i}}{{=i}}{{~}}'], {arr: Array(3)}, '012'); | ||
}); | ||
|
||
it('should concatenate indices and items', function() { | ||
test([ | ||
'{{~arr:x:i}}{{?i}}, {{?}}{{=i}}:{{=x}}{{~}}' | ||
], {arr: [10,20,30]}, '0:10, 1:20, 2:30'); | ||
}); | ||
}); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we're getting all the keys in the object and exploding them so that we end up with
new Function({foo, bar, baz}, "escapedFunctionBody")
, which is what allows us to use the template in a non-context way.Object.keys
is called when the outer function is called, as part of the process of building the inner function because it's not technically wrapped in"
's.str
is called when the inner function is executed because it is wrapped in"
'sThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we want to perform the explosion at runtime, our outside function returns a self-calling function so that the user doesn't have to do something like
doT.template(template)(data)()
; This way when they dodoT.template(template)(data)
the inner function created and called at the same timeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Garethp, having this implementation would cause doT v1 to fail on older browsers which are not es5 compliant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elcharitas True, but I think that it should still be backwards compatible with older v1 versions. All of the ES6 syntax exists inside the function that gets built at run-time only if you use the no-context option. Anyone should be able to upgrade to this change without affecting older browsers, it just means that the new feature wouldn't work on older browsers.
Is having a feature that's only available to browsers in the last 5-ish years a problem if it doesn't stop the package from working on older browsers the way it did previously?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish i had a say on this as you've fully convinced me.
I was of the opinion that new features should just be implemented in v2 but taking a look at v2 again, i see a lot of breakage :(
Also, i think it's noteworthy that v1 hasn't been updated in a while. Hopefully this merge would be accepted as I'd love to see that happen