Skip to content

Commit 3c5b6f0

Browse files
committed
Merge pull request #132 from bem/fix/escaping
Add escaping of characters to their corresponding HTML entities
2 parents 88d3454 + d889ba4 commit 3c5b6f0

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

lib/utils/serialize.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
var _ = require('lodash');
2+
13
/**
24
* Serializes HTML elements back to raw HTML
35
*/
4-
56
module.exports = {
67
/**
78
* @param {String} name
@@ -37,7 +38,7 @@ module.exports = {
3738
var res = '<' + tagName;
3839

3940
attrs.forEach(function (attr) {
40-
res += ' ' + attr.name + '="' + escape(attr.value) + '"';
41+
res += ' ' + attr.name + '="' + _.escape(attr.value) + '"';
4142
});
4243

4344
selfClosing && (res += '/');
@@ -56,7 +57,7 @@ module.exports = {
5657
* @returns {String}
5758
*/
5859
text: function (text) {
59-
return text;
60+
return _.escape(text);
6061
},
6162
/**
6263
* @param {String} text
@@ -66,15 +67,3 @@ module.exports = {
6667
return '<!--' + text + '-->';
6768
}
6869
};
69-
70-
/**
71-
* @param {String} str
72-
* @returns {String}
73-
*/
74-
function escape(str) {
75-
return String(str)
76-
.replace(/&/g, '&amp;')
77-
.replace(/"/g, '&quot;')
78-
.replace(/</g, '&lt;')
79-
.replace(/>/g, '&gt;');
80-
}

test/unit/serialize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('\'serialize\'', function () {
6363
it('must serialize text', function () {
6464
var output = '\nblah\t&quot;';
6565

66-
serialize.text('\nblah\t&quot;').must.be.equal(output);
66+
serialize.text('\nblah\t"').must.be.equal(output);
6767
});
6868

6969
it('must serialize comments', function () {

0 commit comments

Comments
 (0)