Skip to content

Commit

Permalink
Improve tokenization. Add escaping HTML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
unlok committed Oct 14, 2014
1 parent c857761 commit 7e87ace
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -11,7 +11,7 @@ var _ = require('lodash'),
HtmlDiff.prototype.tokenize = function (html) {
html = modifyHtmlAccordingToOptions(html, this.options);

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

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/utils/serialize.js
Expand Up @@ -37,7 +37,7 @@ module.exports = {
var res = '<' + tagName;

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

selfClosing && (res += '/');
Expand All @@ -63,3 +63,11 @@ module.exports = {
return '<!--' + text + '-->';
}
};

function escape(str) {
return String(str)
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}

0 comments on commit 7e87ace

Please sign in to comment.