Skip to content

Commit

Permalink
Merge pull request #132 from bem/fix/escaping
Browse files Browse the repository at this point in the history
Add escaping of characters to their corresponding HTML entities
  • Loading branch information
eGavr committed Oct 1, 2015
2 parents 88d3454 + d889ba4 commit 3c5b6f0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
19 changes: 4 additions & 15 deletions lib/utils/serialize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var _ = require('lodash');

/**
* Serializes HTML elements back to raw HTML
*/

module.exports = {
/**
* @param {String} name
Expand Down Expand Up @@ -37,7 +38,7 @@ module.exports = {
var res = '<' + tagName;

attrs.forEach(function (attr) {
res += ' ' + attr.name + '="' + escape(attr.value) + '"';
res += ' ' + attr.name + '="' + _.escape(attr.value) + '"';
});

selfClosing && (res += '/');
Expand All @@ -56,7 +57,7 @@ module.exports = {
* @returns {String}
*/
text: function (text) {
return text;
return _.escape(text);
},
/**
* @param {String} text
Expand All @@ -66,15 +67,3 @@ module.exports = {
return '<!--' + text + '-->';
}
};

/**
* @param {String} str
* @returns {String}
*/
function escape(str) {
return String(str)
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
2 changes: 1 addition & 1 deletion test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('\'serialize\'', function () {
it('must serialize text', function () {
var output = '\nblah\t&quot;';

serialize.text('\nblah\t&quot;').must.be.equal(output);
serialize.text('\nblah\t"').must.be.equal(output);
});

it('must serialize comments', function () {
Expand Down

0 comments on commit 3c5b6f0

Please sign in to comment.