Skip to content

Commit da82fdd

Browse files
committed
Merge pull request #110 from bem/feature/tokenize-and-escaping
Improve tokenization and add escaping of HTML attributes values
2 parents c857761 + 7e87ace commit da82fdd

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var _ = require('lodash'),
1111
HtmlDiff.prototype.tokenize = function (html) {
1212
html = modifyHtmlAccordingToOptions(html, this.options);
1313

14-
return _.filter(html.split(/(\s+|\b)/));
14+
return _.filter(html.split(/([{}:;,<>"'\[\]]|\s+)/));
1515
};
1616

1717
/**

lib/utils/serialize.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = {
3737
var res = '<' + tagName;
3838

3939
attrs.forEach(function (attr) {
40-
res += ' ' + attr.name + '="' + attr.value + '"';
40+
res += ' ' + attr.name + '="' + escape(attr.value) + '"';
4141
});
4242

4343
selfClosing && (res += '/');
@@ -63,3 +63,11 @@ module.exports = {
6363
return '<!--' + text + '-->';
6464
}
6565
};
66+
67+
function escape(str) {
68+
return String(str)
69+
.replace(/&/g, '&amp;')
70+
.replace(/"/g, '&quot;')
71+
.replace(/</g, '&lt;')
72+
.replace(/>/g, '&gt;');
73+
}

0 commit comments

Comments
 (0)