Skip to content

Commit

Permalink
Do not quote simple attr value with singleQuotesForDataAttrs option
Browse files Browse the repository at this point in the history
  • Loading branch information
vithar committed Jul 9, 2019
1 parent fc4b4ed commit b887017
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/bemhtml/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,19 @@ BEMHTML.prototype.renderAttrs = function(attrs) {
var attrVal = utils.isSimple(attr) ? attr : this.run(attr);
out += ' ' + name + '=';
out += (this._singleQuotesForDataAttrs && name.indexOf('data-') === 0) ?
'\'' + utils.jsAttrEscape(attrVal) + '\'' :
this.getAttrValue(attrVal);
this.getAttrValue(attrVal, utils.jsAttrEscape(attrVal), '\'') :
this.getAttrValue(attrVal, utils.attrEscape(attrVal), '"');
}
}
}

return out;
};

BEMHTML.prototype.getAttrValue = function(attrVal) {
BEMHTML.prototype.getAttrValue = function(attrVal, escapedAttrVal, quote) {
return this._unquotedAttrs && utils.isUnquotedAttr(attrVal) ?
attrVal :
('"' + utils.attrEscape(attrVal) + '"');
(quote + escapedAttrVal + quote);
};

BEMHTML.prototype.renderMix = function(entity, mix, jsParams, addJSInitClass) {
Expand Down
20 changes: 18 additions & 2 deletions test/bemhtml-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,27 @@ describe('BEMHTML engine tests', function() {
test(function() {},
{ block: 'b', attrs: {
id: 'without-changes',
'data-test': '{"reqid":"42"}'
'data-test': '{"reqid":"42"}',
'data-simple': '1234'
} },
'<div class="b" id="without-changes" ' +
'data-test=\'{"reqid":"42"}\'></div>',
'data-test=\'{"reqid":"42"}\' ' +
'data-simple=\'1234\'></div>',
{ singleQuotesForDataAttrs: true });
});

it('should render simple data-attr value without quotes if option true',
function() {
test(function() {},
{ block: 'b', attrs: {
id: 'without-changes',
'data-test': '{"reqid":"42"}',
'data-simple': '1234'
} },
'<div class=b id=without-changes ' +
'data-test=\'{"reqid":"42"}\' ' +
'data-simple=1234></div>',
{ singleQuotesForDataAttrs: true, unquotedAttrs: true });
});
});
});

0 comments on commit b887017

Please sign in to comment.