Skip to content

Commit

Permalink
bem-xjst: clean flags for wrap() and extend() (fix for #495)
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Apr 9, 2018
1 parent 4ee9bbd commit 5b5aa73
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/bemxjst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions lib/bemxjst/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -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;
};

Expand Down
20 changes: 20 additions & 0 deletions test/modes-extend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,24 @@ describe('Modes extend', function() {
}, { block: 'b', foo: 'This is' },
'<div class="b">This is ContextChild</div>');
});

it('should work with several apply() calls', function() {
var bemjson = { block: 'b1' };
var expected = '<div class="b1">42</div>';
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,
'first apply() call returns not expected value'
);
});
});
24 changes: 24 additions & 0 deletions test/modes-wrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ describe('Modes wrap', function() {
}, { block: 'b1' }, '<div class="b1"></div>');
});

it('should work with several apply() calls', function() {
var bemjson = { block: 'b1' };
var expected = '<div class="b2"><div class="b1"></div></div>';
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,
'first apply() call returns not expected value'
);
});

it('should use current context (with simple value)', function() {
test(function() {
Expand Down

0 comments on commit 5b5aa73

Please sign in to comment.