From 10550d5fa9e1cf54edec5b86e30b261b533488c6 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Tue, 16 Jan 2018 20:25:37 +0300 Subject: [PATCH 1/3] bem-xjst: clean flags for wrap() and extend() (fix for #495) --- lib/bemxjst/index.js | 14 ++++++++++++++ lib/bemxjst/match.js | 14 ++++++++++++-- test/modes-extend-test.js | 20 ++++++++++++++++++++ test/modes-wrap-test.js | 24 ++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) diff --git a/lib/bemxjst/index.js b/lib/bemxjst/index.js index d54cb181..77890243 100644 --- a/lib/bemxjst/index.js +++ b/lib/bemxjst/index.js @@ -311,12 +311,26 @@ BEMXJST.prototype._run = function(context) { return this.runOne(context); }; +BEMXJST.prototype.cleanWrapFlags = function(field) { + var cached = this[field]; + + if (!Array.isArray(cached)) + return; + + for (var i = 0; i < cached.length; i++) + cached[i].wrap = null; +}; + BEMXJST.prototype.run = function(json) { var match = this.match; var context = this.context; var depth = this.depth; this.match = null; + + this.cleanWrapFlags('_extended'); + this.cleanWrapFlags('_wraped'); + this.context = new this.contextConstructor(this); this.canFlush = this.context._flush !== null; this.depth = 0; diff --git a/lib/bemxjst/match.js b/lib/bemxjst/match.js index 718e64b1..03abd8eb 100644 --- a/lib/bemxjst/match.js +++ b/lib/bemxjst/match.js @@ -45,6 +45,11 @@ function MatchWrap(template) { MatchWrap.prototype.exec = function(context) { var res = this.wrap !== context.ctx; this.wrap = context.ctx; + + if (!Array.isArray(context._bemxjst._wraped)) + context._bemxjst._wraped = []; + context._bemxjst._wraped.push(this); + return res; }; @@ -54,8 +59,13 @@ function MatchExtend(template) { } MatchExtend.prototype.exec = function(context) { - var res = this.ext !== context.ctx; - this.ext = context.ctx; + var res = this.wrap !== context.ctx; + this.wrap = context.ctx; + + if (!Array.isArray(context._bemxjst._extended)) + context._bemxjst._extended = []; + context._bemxjst._extended.push(this); + return res; }; diff --git a/test/modes-extend-test.js b/test/modes-extend-test.js index a291ad65..237532ba 100644 --- a/test/modes-extend-test.js +++ b/test/modes-extend-test.js @@ -81,4 +81,24 @@ describe('Modes extend', function() { }, { block: 'b', foo: 'This is' }, '
This is ContextChild
'); }); + + it('should work with several apply() calls', function() { + var bemjson = { block: 'b1' }; + var expected = '
42
'; + var tmpl = fixtures.compile(function() { + block('b1').extend()({ 'ctx.content': 42 }); + }); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'first apply() call returns not expected value' + ); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'second apply() call returns not expected value' + ); + }); }); diff --git a/test/modes-wrap-test.js b/test/modes-wrap-test.js index 834cf5d2..536103a2 100644 --- a/test/modes-wrap-test.js +++ b/test/modes-wrap-test.js @@ -58,6 +58,30 @@ describe('Modes wrap', function() { }, { block: 'b1' }, '
'); }); + it('should work with several apply() calls', function() { + var bemjson = { block: 'b1' }; + var expected = '
'; + var tmpl = fixtures.compile(function() { + block('b1').wrap()(function() { + return { + block: 'b2', + content: this.ctx + }; + }); + }); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'first apply() call returns not expected value' + ); + + assert.equal( + tmpl.apply(bemjson), + expected, + 'second apply() call returns not expected value' + ); + }); it('should use current context (with simple value)', function() { test(function() { From 285637ced9a5b6590805fb93340ea765e70320d0 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Mon, 9 Apr 2018 14:23:12 +0300 Subject: [PATCH 2/3] fixup: optimization --- lib/bemxjst/index.js | 5 ++++- lib/bemxjst/match.js | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/bemxjst/index.js b/lib/bemxjst/index.js index 77890243..71f2f808 100644 --- a/lib/bemxjst/index.js +++ b/lib/bemxjst/index.js @@ -19,6 +19,9 @@ function BEMXJST(options) { // Current match this.match = null; + this._wraped = []; + this._extended = []; + // Create new Context constructor for overriding prototype this.contextConstructor = function ContextChild(bemxjst) { Context.call(this, bemxjst); @@ -314,7 +317,7 @@ BEMXJST.prototype._run = function(context) { BEMXJST.prototype.cleanWrapFlags = function(field) { var cached = this[field]; - if (!Array.isArray(cached)) + if (cached.length === 0) return; for (var i = 0; i < cached.length; i++) diff --git a/lib/bemxjst/match.js b/lib/bemxjst/match.js index 03abd8eb..3b1683c5 100644 --- a/lib/bemxjst/match.js +++ b/lib/bemxjst/match.js @@ -46,8 +46,6 @@ MatchWrap.prototype.exec = function(context) { var res = this.wrap !== context.ctx; this.wrap = context.ctx; - if (!Array.isArray(context._bemxjst._wraped)) - context._bemxjst._wraped = []; context._bemxjst._wraped.push(this); return res; @@ -62,8 +60,6 @@ MatchExtend.prototype.exec = function(context) { var res = this.wrap !== context.ctx; this.wrap = context.ctx; - if (!Array.isArray(context._bemxjst._extended)) - context._bemxjst._extended = []; context._bemxjst._extended.push(this); return res; From 3b55c9ac7f363aaecd39bcc5010864bc167994d8 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Mon, 16 Apr 2018 16:09:20 +0300 Subject: [PATCH 3/3] v8.9.2-rc.1 --- CHANGELOG.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 620444e1..023bd844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # BEM-XJST Changelog +# 2018-04-16 [v8.9.2-rc.1](https://github.com/bem/bem-xjst/compare/v8.9.1...v8.9.2-rc.1), @miripiruni + +Release candidate from https://github.com/bem/bem-xjst/pull/496 to check benchmarks on real project. + + # 2018-03-05 [v8.9.1](https://github.com/bem/bem-xjst/compare/v8.9.0...v8.9.1), @miripiruni Bug fixed: dot-delimited dependencies from global scope in object notation. diff --git a/package.json b/package.json index 79da04ef..97e6bee3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bem-xjst", - "version": "8.9.1", + "version": "8.9.2-rc.1", "description": "Declarative Template Engine for the browser and server", "keywords": [ "template",