Skip to content

Commit

Permalink
lib: render attrs for non-bem entities
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Apr 20, 2015
1 parent e21f60b commit cf03962
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/bemhtml/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ describe('BEMHTML compiler', function() {
], '<div class="b1"></div>');
});

describe('attrs in BEMJSON', function() {
it('should render with block', function () {
test(function() {
}, { block: 'b1', attrs: { a: 'b' } }, '<div class="b1" a="b"></div>');
});

it('should render with just tag', function () {
test(function() {
}, { tag: 'span', attrs: { a: 'b' } }, '<span a="b"></span>');
});
});

describe('preserve/reset block/elem/mods', function() {
it('should not preserve block on tag', function () {
test(function() {
Expand Down

0 comments on commit cf03962

Please sign in to comment.