Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement elemMatch #165

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bemxjst/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ Tree.prototype.block = function block(name) {
};

Tree.prototype.elemMatch = function elemMatch() {
return this.match.apply(this, arguments);
return this.elem('*').match.apply(this, arguments);
};

Tree.prototype.elem = function elem(name) {
Expand Down
45 changes: 45 additions & 0 deletions test/bemhtml/tree-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,51 @@ describe('BEMHTML compiler/Tree', function() {
}, '<div class="b1__e1 b1__e1_key_val">ok</div>')
});

it('should support elemMatch() match', function() {
test(function() {
block('b1').elemMatch(function() {
return this.elem === 'e1' || this.elem === 'e2';
}).tag()('b');
}, [
{ block: 'b1', elem: 'e1' },
{ block: 'b1', elem: 'e2' },
{ block: 'b1', elem: 'e3' }
], '<b class="b1__e1"></b><b class="b1__e2"></b><div class="b1__e3"></div>')
});

it('priority of elemMatch and elem is determined by templates order',
function() {
test(function() {
block('b1').def()(
elem('e')('elem'),
elemMatch(function() { return this.elem === 'e'; })('elemMatch')
);
block('b2').def()(
elemMatch(function() { return this.elem === 'e'; })('elemMatch'),
elem('e')('elem')
);
}, [ { block: 'b1', elem: 'e' }, ' ', { block: 'b2', elem: 'e' } ],
'elem elemMatch')
});

it('priority of elem(\'*\') and elem(\'e\') is determined by ' +
'templates order', function() {
test(function() {
block('b1').content()(
elem('*').match(function() { return this.elem === 'e'; })('wildcard'),
elem('e')('elem')
);
block('b2').content()(
elem('e')('elem'),
elem('*').match(function() { return this.elem === 'e'; })('wildcard')
);
}, [
{ block: 'b1', elem: 'e', tag: false },
' ',
{ block: 'b2', elem: 'e', tag: false }
], 'elem wildcard');
});

it('should group properly after elem', function() {
test(function() {
block('b1').content()('ok');
Expand Down