Skip to content

Commit

Permalink
Merge pull request mde#91 from alubbe/fasterescape
Browse files Browse the repository at this point in the history
cache character encoding function
  • Loading branch information
TimothyGu committed Jun 9, 2015
2 parents 5abe361 + 5e97201 commit 33f0d7a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var _ENCODE_HTML_RULES = {
}
, _MATCH_HTML = /[&<>\'"]/g;

function encode_char(c) {
return _ENCODE_HTML_RULES[c] || c;
};

/**
* Stringified version of constants used by {@link module:utils.escapeXML}.
*
Expand All @@ -70,7 +74,10 @@ var escapeFuncStr =
+ ' , \'"\': "&#34;"\n'
+ ' , "\'": "&#39;"\n'
+ ' }\n'
+ ' , _MATCH_HTML = /[&<>\'"]/g;\n';
+ ' , _MATCH_HTML = /[&<>\'"]/g;\n'
+ 'function encode_char(c) {\n'
+ ' return _ENCODE_HTML_RULES[c] || c;\n'
+ '};\n';

/**
* Escape characters reserved in XML.
Expand All @@ -83,13 +90,12 @@ var escapeFuncStr =
* @static
* @private
*/

exports.escapeXML = function (markup) {
return markup == undefined
? ''
: String(markup)
.replace(_MATCH_HTML, function(m) {
return _ENCODE_HTML_RULES[m] || m;
});
.replace(_MATCH_HTML, encode_char);
};
exports.escapeXML.toString = function () {
return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr
Expand Down

0 comments on commit 33f0d7a

Please sign in to comment.