Skip to content

Commit

Permalink
Merge pull request #181 from preactjs/escape-perf
Browse files Browse the repository at this point in the history
Perf: Improve HTML entity escaping
  • Loading branch information
marvinhagemeister authored Mar 7, 2021
2 parents 132a794 + aae5ef3 commit ffda361
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions server/src/util.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
// DOM properties that should NOT have "px" added when numeric
export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/i;

const HTML_ENTITY_REG = /[&<>"]/g;
const tagsToReplace = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;'
};
const replaceTag = (tag) => tagsToReplace[tag] || tag;
export function encodeEntities(s) {
if (typeof s !== 'string') s = String(s);
let out = '';
for (let i = 0; i < s.length; i++) {
let ch = s[i];
// prettier-ignore
switch (ch) {
case '<': out += '&lt;'; break;
case '>': out += '&gt;'; break;
case '"': out += '&quot;'; break;
case '&': out += '&amp;'; break;
default: out += ch;
}
}
return out;
return s.replace(HTML_ENTITY_REG, replaceTag);
}

export let indent = (s, char) =>
Expand Down

0 comments on commit ffda361

Please sign in to comment.