From dcb9bac309c71a007063329744df93310d621b43 Mon Sep 17 00:00:00 2001 From: miripiruni Date: Mon, 28 Dec 2015 16:19:48 +0300 Subject: [PATCH] Reimplement elemMatch --- lib/bemxjst/tree.js | 2 +- test/bemhtml/tree-test.js | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/bemxjst/tree.js b/lib/bemxjst/tree.js index 1a3d56d1..e48d8870 100644 --- a/lib/bemxjst/tree.js +++ b/lib/bemxjst/tree.js @@ -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) { diff --git a/test/bemhtml/tree-test.js b/test/bemhtml/tree-test.js index 06433916..430fe088 100644 --- a/test/bemhtml/tree-test.js +++ b/test/bemhtml/tree-test.js @@ -175,6 +175,51 @@ describe('BEMHTML compiler/Tree', function() { }, '
ok
') }); + 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' } + ], '
') + }); + + 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');