From cf0396218bdc8e05a235917ca59b6ed1e9558d80 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Mon, 20 Apr 2015 17:27:03 +0200 Subject: [PATCH] lib: render attrs for non-bem entities --- lib/bemhtml/runtime/index.js | 21 +++++++++++---------- test/api-test.js | 12 ++++++++++++ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/bemhtml/runtime/index.js b/lib/bemhtml/runtime/index.js index 70a1f958..02028f69 100644 --- a/lib/bemhtml/runtime/index.js +++ b/lib/bemhtml/runtime/index.js @@ -235,7 +235,7 @@ BEMHTML.prototype.render = function render(context, var addJSInitClass = entity.block && jsParams && !entity.elem; if (!isBEM && !cls) { - out += this.renderClose(tag, content); + out += this.renderClose(tag, attrs, ctx, content); return out; } @@ -262,6 +262,14 @@ BEMHTML.prototype.render = function render(context, if (isBEM && jsParams) out += ' data-bem="' + utils.attrEscape(JSON.stringify(jsParams)) + '"'; + out += this.renderClose(tag, attrs, ctx, content); + + return out; +}; + +BEMHTML.prototype.renderClose = function renderClose(tag, attrs, ctx, content) { + var out = ''; + // NOTE: maybe we need to make an array for quicker serialization attrs = utils.extend(attrs, ctx.attrs); if (attrs) { @@ -281,17 +289,10 @@ BEMHTML.prototype.render = function render(context, } } - out += this.renderClose(tag, content); - - return out; -}; - -BEMHTML.prototype.renderClose = function renderClose(tag, content) { - var out; if (utils.isShortTag(tag)) { - out = '/>'; + out += '/>'; } else { - out = '>'; + out += '>'; // TODO(indutny): skip apply next flags if (content || content === 0) diff --git a/test/api-test.js b/test/api-test.js index 844efac0..d080eb4e 100644 --- a/test/api-test.js +++ b/test/api-test.js @@ -199,6 +199,18 @@ describe('BEMHTML compiler', function() { ], '
'); }); + describe('attrs in BEMJSON', function() { + it('should render with block', function () { + test(function() { + }, { block: 'b1', attrs: { a: 'b' } }, '
'); + }); + + it('should render with just tag', function () { + test(function() { + }, { tag: 'span', attrs: { a: 'b' } }, ''); + }); + }); + describe('preserve/reset block/elem/mods', function() { it('should not preserve block on tag', function () { test(function() {