8000 Squashed commit of the following: · drzraf/twig.js@8604ecf · GitHub
[go: up one dir, main page]

Skip to content

Commit 8604ecf

Browse files
author
Raphaël Droz
committed
Squashed commit of the following:
commit ae6c641 Author: Raphaël Droz <raphael.droz+floss@gmail.com> Date: Sun Dec 22 00:35:23 2019 -0300 The raw filter returns a new Markup(content) which may itself returns the content if the content is an empty string or undefined. The resulting output is [Object] instead of the (expected) empty string. commit 7a4ae6e Author: Raphaël Droz <raphael.droz+floss@gmail.com> Date: Sun Dec 22 00:46:27 2019 -0300 support for "do" tag commit ec635df Author: Raphaël Droz <raphael.droz+floss@gmail.com> Date: Sat Dec 21 17:20:14 2019 -0300 Avoid attribute() to fail on undefined object commit 001f05c Author: Raphaël Droz <raphael.droz+floss@gmail.com> Date: Sat Dec 21 15:43:01 2019 -0300 Provides sourcemaps commit 8c4d9c2 Author: Raphaël Droz <raphael.droz+floss@gmail.com> Date: Tue Dec 17 21:56:29 2019 -0300 search for block within all ascendants instead of parent only. fix twigjs#694 reroll twigjs#695
1 parent 39680b8 commit 8604ecf

File tree

9 files changed

+96
-5
lines changed

9 files changed

+96
-5
lines changed

src/twig.core.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,17 +1284,26 @@ module.exports = function (Twig) {
12841284
*
12851285
* @return {Twig.Block|undefined}
12861286
*/
1287-
Twig.Template.prototype.getBlock = function (name, checkOnlyInheritedBlocks) {
1287+
Twig.Template.prototype.getBlock = function (name, checkOnlyInheritedBlocks, checkImports = true) {
12881288
let block;
12891289

12901290
if (checkOnlyInheritedBlocks !== true) {
12911291
block = this.blocks.defined[name];
12921292
}
12931293

1294-
if (block === undefined) {
1294+
if (checkImports && block === undefined) {
12951295
block = this.blocks.imported[name];
12961296
}
12971297

1298+
if (block === undefined && this.parentTemplate !== null) {
1299+
/**
1300+
* Block defined in the parent template when extending.
1301+
* This recursion is useful to inherit from ascendants.
1302+
* But take care of not considering ascendants' {% use %}
1303+
*/
1304+
block = this.parentTemplate.getBlock(name, checkOnlyInheritedBlocks, checkImports = false);
1305+
}
1306+
12981307
return block;
12991308
};
13001309

@@ -1443,7 +1452,7 @@ module.exports = function (Twig) {
14431452
*/
14441453

14451454
Twig.Markup = function (content, strategy) {
1446-
if (typeof content !== 'string' || content.length === 0) {
1455+
if (typeof content !== 'string') {
14471456
return content;
14481457
}
14491458

src/twig.filters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ module.exports = function (Twig) {
745745
return value[value.length - 1];
746746
},
747747
raw(value) {
748-
return new Twig.Markup(value);
748+
return new Twig.Markup(value || '');
749749
},
750750
batch(items, params) {
751751
let size = params.shift();

src/twig.functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module.exports = function (Twig) {
188188
}
189189

190190
// Array will return element 0-index
191-
return object[method] || undefined;
191+
return object ? (object[method] || undefined) : undefined;
192192
},
193193
max(values, ...args) {
194194
if (Twig.lib.is('Object', values)) {

src/twig.logic.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module.exports = function (Twig) {
2626
endfilter: 'Twig.logic.type.endfilter',
2727
apply: 'Twig.logic.type.apply',
2828
endapply: 'Twig.logic.type.endapply',
29+
do: 'Twig.logic.type.do',
2930
shortblock: 'Twig.logic.type.shortblock',
3031
block: 'Twig.logic.type.block',
3132
endblock: 'Twig.logic.type.endblock',
@@ -570,6 +571,41 @@ module.exports = function (Twig) {
570571
next: [],
571572
open: false
572573
},
574+
{
575+
/**
576+
* Set type logic tokens.
577+
*
578+
* Format: {% do expression %}
579+
*/
580+
type: Twig.logic.type.do,
581+
regex: /^do\s+(.+)$/,
582+
next: [],
583+
open: true,
584+
compile(token) { //
585+
const expression = token.match[1];
586+
// Compile the expression.
587+
const expressionStack = Twig.expression.compile.call(this, {
588+
type: Twig.expression.type.expression,
589+
value: expression
590+
}).stack;
591+
592+
token.expression = expressionStack;
593+
594+
delete token.match;
595+
return token;
596+
},
597+
parse(token, context, continueChain) {
598+
const state = this;
599+
600+
return Twig.expression.parseAsync.call(state, token.expression, context)
601+
.then(() => {
602+
return {
603+
chain: continueChain,
604+
context
605+
};
606+
});
607+
}
608+
},
573609
{
574610
/**
575611
* Block logic tokens.

test/test.filters.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,13 @@ describe('Twig.js Filters ->', function () {
778778
value: '<test>&</test>'
779779
}).should.equal('<test>&</test>');
780780
});
781+
782+
it('should output an empty string', function () {
783+
const template = twig({data: '{{ value|raw }}'});
784+
template.render({value: ''}).should.equal('');
785+
template.render({}).should.equal('');
786+
});
787+
781788
});
782789

783790
describe('round ->', function () {

test/test.functions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ describe('Twig.js Functions ->', function () {
274274
})
275275
.should.equal('');
276276
});
277+
278+
it('should return undef for undefined object', function () {
279+
twig({data: '{{ attribute(arr, "bar") }}'}).render({})
280+
.should.equal('');
281+
});
277282
});
278283
describe('template_from_string ->', function () {
279284
it('should load a template from a string', function () {

test/test.inheritance.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const {factory} = require('../twig');
2+
3+
let twig;
4+
5+
describe('Twig.js Blocks ->', function () {
6+
beforeEach(function () {
7+
twig = factory().twig;
8+
});
9+
10+
describe('"extends" tag and inheritance', function () {
11+
it('"extends" applies recursively to grand-parents', function () {
12+
twig({
13+
id: 'grand-parent.twig',
14+
data: '{% block content %}grand-parent.twig{% endblock%}'
15+
});
16+
twig({
17+
id: 'parent.twig',
18+
data: '{% extends "grand-parent.twig" %}'
19+
});
20+
21+
twig({
22+
allowInlineIncludes: true,
23+
data: '{% extends "parent.twig" %}{% block content %}main.twig > {{ parent() }}{% endblock %}'
24+
}).render().should.equal('main.twig > grand-parent.twig');
25+
});
26+
});
27+
});

test/test.tags.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ describe('Twig.js Tags ->', function () {
6464

6565
consoleSpy.should.be.calledWith('Deprecation notice: \'`foo` is deprecated use `bar`\'');
6666
});
67+
68+
it('should support do', function () {
69+
twig({data: '{% do 1 + 2 %}'}).render().should.equal('');
70+
twig({data: '{% do arr %}'}).render({arr:[1]}).should.equal('');
71+
});
6772
});

webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const clientBuild = {
2020
mode: 'production',
2121
entry: './src/twig.js',
2222
target: 'web',
23+
devtool: '#source-map',
2324
node: {
2425
__dirname: false,
2526
__filename: false
@@ -50,6 +51,7 @@ const clientBuild = {
5051
optimization: {
5152
minimize: true,
5253
minimizer: [new TerserJsPlugin({
54+
sourceMap: true,
5355
include: /\.min\.js$/
5456
})]
5557
}

0 commit comments

Comments
 (0)
0