Skip to content

Commit 8d41f98

Browse files
mishangaindutny
authored andcommitted
runtime: escaping optimization
1 parent c433b9f commit 8d41f98

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

lib/bemhtml/runtime/context.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ exports.Context = Context;
2727
Context.prototype._flush = null;
2828
Context.prototype.isArray = utils.isArray;
2929

30-
Context.prototype.isSimple = function isSimple(obj) {
31-
if (!obj || obj === true)
32-
return true;
33-
var t = typeof obj;
34-
return t === 'string' || t === 'number';
35-
};
30+
Context.prototype.isSimple = utils.isSimple;
3631

3732
Context.prototype.isShortTag = utils.isShortTag;
3833
Context.prototype.extend = utils.extend;
3934
Context.prototype.identify = utils.identify;
4035

4136
Context.prototype.xmlEscape = utils.xmlEscape;
4237
Context.prototype.attrEscape = utils.attrEscape;
38+
Context.prototype.jsAttrEscape = utils.jsAttrEscape;
4339

4440
Context.prototype.BEM = {};
4541

lib/bemhtml/runtime/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ BEMHTML.prototype.render = function render(context,
486486
out += '"';
487487

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

491491
return this.renderClose(out, context, tag, attrs, isBEM, ctx, content);
492492
};

lib/bemhtml/runtime/utils.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@ if (!exports.isArray) {
1515
};
1616
}
1717

18-
var buildEscape = (function() {
19-
var ts = { '"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;' };
20-
var f = function(t) { return ts[t] || t; };
21-
return function(r) {
22-
r = new RegExp(r, 'g');
23-
return function(s) { return ('' + s).replace(r, f); };
24-
};
25-
})();
26-
27-
exports.xmlEscape = buildEscape('[&<>]');
28-
exports.attrEscape = buildEscape('["&<>]');
18+
exports.xmlEscape = function(str) {
19+
return (str + '')
20+
.replace(/&/g, '&amp;')
21+
.replace(/</g, '&lt;')
22+
.replace(/>/g, '&gt;');
23+
};
24+
exports.attrEscape = function(str) {
25+
return (str + '')
26+
.replace(/&/g, '&amp;')
27+
.replace(/"/g, '&quot;');
28+
};
29+
exports.jsAttrEscape = function(str) {
30+
return (str + '')
31+
.replace(/&/g, '&amp;')
32+
.replace(/'/g, '&#39;');
33+
};
2934

3035
exports.extend = function extend(o1, o2) {
3136
if (!o1 || !o2)

test/runtime-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ describe('BEMHTML compiler/Runtime', function() {
387387
);
388388
}, {
389389
block: 'b1'
390-
}, '<div class="b1 b2 i-bem" data-bem="{&quot;b1&quot;:{}}"></div>');
390+
}, '<div class="b1 b2 i-bem" data-bem=\'{"b1":{}}\'></div>');
391391
});
392392

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

0 commit comments

Comments
 (0)