-
Notifications
You must be signed in to change notification settings - Fork 275
/
test.extends.js
156 lines (131 loc) · 4.62 KB
/
test.extends.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const Twig = require('..').factory();
const {twig} = Twig;
describe('Twig.js Extensions ->', function () {
it('should be able to extend a meta-type tag', function () {
const flags = {};
Twig.extend(Twig => {
Twig.exports.extendTag({
type: 'flag',
regex: /^flag\s+(.+)$/,
next: [],
open: true,
compile(token) {
const expression = token.match[1];
// Compile the expression.
token.stack = Reflect.apply(Twig.expression.compile, this, [{
type: Twig.expression.type.expression,
value: expression
}]).stack;
delete token.match;
return token;
},
parse(token, context, _) {
const name = Reflect.apply(Twig.expression.parse, this, [token.stack, context]);
const output = '';
flags[name] = true;
return {
chain: false,
output
};
}
});
});
twig({data: '{% flag \'enabled\' %}'}).render();
flags.enabled.should.equal(true);
});
it('should be able to extend paired tags', function () {
// Demo data
const App = {
user: 'john',
users: {
john: {level: 'admin'},
tom: {level: 'user'}
}
};
Twig.extend(Twig => {
// Example of extending a tag type that would
// restrict content to the specified "level"
Twig.exports.extendTag({
type: 'auth',
regex: /^auth\s+(.+)$/,
next: ['endauth'], // Match the type of the end tag
open: true,
compile(token) {
const expression = token.match[1];
// Turn the string expression into tokens.
token.stack = Reflect.apply(Twig.expression.compile, this, [{
type: Twig.expression.type.expression,
value: expression
}]).stack;
delete token.match;
return token;
},
parse(token, context, chain) {
const level = Reflect.apply(Twig.expression.parse, this, [token.stack, context]);
let output = '';
if (App.users[App.currentUser].level === level) {
output = this.parse(token.output, context);
}
return {
chain,
output
};
}
});
Twig.exports.extendTag({
type: 'endauth',
regex: /^endauth$/,
next: [],
open: false
});
});
const template = twig({data: 'Welcome{% auth \'admin\' %} ADMIN{% endauth %}!'});
App.currentUser = 'john';
template.render().should.equal('Welcome ADMIN!');
App.currentUser = 'tom';
template.render().should.equal('Welcome!');
});
it('should be able to extend the same tag twice, replacing it', function () {
let result;
Twig.extend(Twig => {
Twig.exports.extendTag({
type: 'noop',
regex: /^noop$/,
next: [],
open: true,
parse(_) {
return {
chain: false,
output: 'noop1'
};
}
});
});
result = twig({data: '{% noop %}'}).render();
result.should.equal('noop1');
Twig.extend(Twig => {
Twig.exports.extendTag({
type: 'noop',
regex: /^noop$/,
next: [],
open: true,
parse(_) {
return {
chain: false,
output: 'noop2'
};
}
});
});
result = twig({data: '{% noop %}'}).render();
result.should.equal('noop2');
});
it('should extend the parent context when extending', function () {
const template = twig({
path: 'test/templates/extender.twig',
async: false
});
const output = template.render();
output.trim().should.equal('ok!');
});
});