From c66d9b19573bdcf0faea8f9f257fadc5fe3d1d88 Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Wed, 7 Jul 2021 11:22:23 +0100 Subject: [PATCH 1/3] Failing test for async nested extension --- tests/compiler.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/compiler.js b/tests/compiler.js index 8e133e7f7..cfc0dc3e7 100644 --- a/tests/compiler.js +++ b/tests/compiler.js @@ -1853,6 +1853,65 @@ finish(done); }); + it('should allow async custom tag within sync custom tag compilation', function(done) { + function TestSyncExtension() { + this.tags = ['testsync']; + + this.parse = function(parser, nodes) { + var content; + var tag; + parser.advanceAfterBlockEnd(); + + content = parser.parseUntilBlocks('endtestsync'); + tag = new nodes.CallExtension(this, 'run', null, [content]); + parser.advanceAfterBlockEnd(); + + return tag; + }; + + this.run = function(context, content) { + // Reverse the string + return content((err, body) => { + console.log(body.split('').reverse().join('')); + }).split('').reverse().join(''); + }; + } + + function TestAsyncExtension() { + this.tags = ['testasync']; + + this.parse = function(parser, nodes) { + var content; + var tag; + parser.advanceAfterBlockEnd(); + + content = parser.parseUntilBlocks('endtestasync'); + tag = new nodes.CallExtension(this, 'run', null, [content]); + parser.advanceAfterBlockEnd(); + + return tag; + }; + + this.run = function(context, content, callback) { + // Uppercase the string + setTimeout(() => { + callback(null, content().toUpperCase()); + }, 1); + }; + } + + equal('{% testsync %}123456789{% testasync %}abcdefghi{% endtestasync %}{% endtestsync %}', null, + { + extensions: { + TestSyncExtension: new TestSyncExtension(), + TestAsyncExtension: new TestAsyncExtension() + } + }, + 'IHGFEDCBA987654321'); + + finish(done); + }); + it('should autoescape by default', function(done) { equal('{{ foo }}', { foo: '"\'<>&' From 9948f3aaf2a9c1942171cc0dde125a14c8b3b31f Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Wed, 7 Jul 2021 11:41:46 +0100 Subject: [PATCH 2/3] Linting fixes --- tests/compiler.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/compiler.js b/tests/compiler.js index cfc0dc3e7..b6782bb52 100644 --- a/tests/compiler.js +++ b/tests/compiler.js @@ -1871,9 +1871,7 @@ this.run = function(context, content) { // Reverse the string - return content((err, body) => { - console.log(body.split('').reverse().join('')); - }).split('').reverse().join(''); + return content().split('').reverse().join(''); }; } @@ -1901,7 +1899,7 @@ } equal('{% testsync %}123456789{% testasync %}abcdefghi{% endtestasync %}{% endtestsync %}', null, - { + { extensions: { TestSyncExtension: new TestSyncExtension(), TestAsyncExtension: new TestAsyncExtension() From 635f3fe4b6bf648070aff2eb2cf8c4cf693c6ad2 Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Wed, 17 Nov 2021 16:23:31 +0000 Subject: [PATCH 3/3] Update compiler.js --- tests/compiler.js | 51 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/tests/compiler.js b/tests/compiler.js index b6782bb52..52a74a30c 100644 --- a/tests/compiler.js +++ b/tests/compiler.js @@ -1884,30 +1884,61 @@ parser.advanceAfterBlockEnd(); content = parser.parseUntilBlocks('endtestasync'); - tag = new nodes.CallExtension(this, 'run', null, [content]); + tag = new nodes.CallExtensionAsync(this, 'run', null, [content]); parser.advanceAfterBlockEnd(); return tag; }; - this.run = function(context, content, callback) { + this.run = function(context, body, callback) { // Uppercase the string setTimeout(() => { - callback(null, content().toUpperCase()); + callback(null, body().toUpperCase()); }, 1); }; } - equal('{% testsync %}123456789{% testasync %}abcdefghi{% endtestasync %}{% endtestsync %}', null, + // First prove it works normally + render( + '{% testasync %}abcdefghi{% endtestasync %}', + null, { - extensions: { - TestSyncExtension: new TestSyncExtension(), - TestAsyncExtension: new TestAsyncExtension() - } + extensions: { TestAsyncExtension: new TestAsyncExtension()}, + autoescape: true }, - 'IHGFEDCBA987654321'); + function(err1, res1) { + expect(res1).to.be('ABCDEFGHI'); - finish(done); + render( + '{% testsync %}abcdefghi{% endtestsync %}', + null, + { + extensions: { TestSyncExtension: new TestSyncExtension()}, + autoescape: true + }, + function(err2, res2) { + expect(res2).to.be('ihgfedcba'); + + // Then fails with custom tag + render( + '{% testsync %}{% testasync %}abcdefghi{% endtestasync %}{% endtestsync %}', + null, + { + extensions: { + TestExtension: new TestAsyncExtension(), + TestSyncExtension: new TestSyncExtension() + }, + autoescape: true + }, + function(err3, res3) { + expect(res3).to.be('IHGFEDCBA'); + finish(done); + } + ); + } + ); + } + ); }); it('should autoescape by default', function(done) {