Skip to content

Commit

Permalink
runtime: escaping optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mishanga authored and indutny committed Sep 22, 2015
1 parent c433b9f commit 8d41f98
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
8 changes: 2 additions & 6 deletions lib/bemhtml/runtime/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ exports.Context = Context;
Context.prototype._flush = null;
Context.prototype.isArray = utils.isArray;

Context.prototype.isSimple = function isSimple(obj) {
if (!obj || obj === true)
return true;
var t = typeof obj;
return t === 'string' || t === 'number';
};
Context.prototype.isSimple = utils.isSimple;

Context.prototype.isShortTag = utils.isShortTag;
Context.prototype.extend = utils.extend;
Context.prototype.identify = utils.identify;

Context.prototype.xmlEscape = utils.xmlEscape;
Context.prototype.attrEscape = utils.attrEscape;
Context.prototype.jsAttrEscape = utils.jsAttrEscape;

Context.prototype.BEM = {};

Expand Down
2 changes: 1 addition & 1 deletion lib/bemhtml/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ BEMHTML.prototype.render = function render(context,
out += '"';

if (isBEM && jsParams)
out += ' data-bem="' + utils.attrEscape(JSON.stringify(jsParams)) + '"';
out += ' data-bem=\'' + utils.jsAttrEscape(JSON.stringify(jsParams)) + '\'';

return this.renderClose(out, context, tag, attrs, isBEM, ctx, content);
};
Expand Down
27 changes: 16 additions & 11 deletions lib/bemhtml/runtime/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ if (!exports.isArray) {
};
}

var buildEscape = (function() {
var ts = { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' };
var f = function(t) { return ts[t] || t; };
return function(r) {
r = new RegExp(r, 'g');
return function(s) { return ('' + s).replace(r, f); };
};
})();

exports.xmlEscape = buildEscape('[&<>]');
exports.attrEscape = buildEscape('["&<>]');
exports.xmlEscape = function(str) {
return (str + '')
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
};
exports.attrEscape = function(str) {
return (str + '')
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;');
};
exports.jsAttrEscape = function(str) {
return (str + '')
.replace(/&/g, '&amp;')
.replace(/'/g, '&#39;');
};

exports.extend = function extend(o1, o2) {
if (!o1 || !o2)
Expand Down
4 changes: 2 additions & 2 deletions test/runtime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('BEMHTML compiler/Runtime', function() {
);
}, {
block: 'b1'
}, '<div class="b1 b2 i-bem" data-bem="{&quot;b1&quot;:{}}"></div>');
}, '<div class="b1 b2 i-bem" data-bem=\'{"b1":{}}\'></div>');
});

it('should support singular mix', function() {
Expand Down Expand Up @@ -457,7 +457,7 @@ describe('BEMHTML compiler/Runtime', function() {
block: 'b1',
elem: 'e1',
mix: { block: 'b2', js: true }
}, '<div class="b1__e1 b2 i-bem" data-bem="{&quot;b2&quot;:{}}"></div>');
}, '<div class="b1__e1 b2 i-bem" data-bem=\'{"b2":{}}\'></div>');
});
});

Expand Down

0 comments on commit 8d41f98

Please sign in to comment.