Skip to content

Commit

Permalink
store as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Jun 18, 2024
1 parent 26974dc commit d5609fb
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ const symbolAsyncIterator = Symbol.asyncIterator;

const escapeRegExp = /["&'<>`]/;

const escapeRegExpTest = escapeRegExp.test.bind(escapeRegExp);

const escapeFunction = (string) => {
const stringCharCodeAt = string.charCodeAt.bind(string);
const stringSlice = string.slice.bind(string);

const stringLength = string.length;
let start = 0;
let end = 0;
Expand All @@ -16,33 +20,33 @@ const escapeFunction = (string) => {
do {
switch (stringCharCodeAt(end++)) {
case 34: // "
escaped += string.slice(start, end - 1) + "&#34;";
escaped += stringSlice(start, end - 1) + "&#34;";
start = end;
continue;
case 38: // &
escaped += string.slice(start, end - 1) + "&#38;";
escaped += stringSlice(start, end - 1) + "&#38;";
start = end;
continue;
case 39: // '
escaped += string.slice(start, end - 1) + "&#39;";
escaped += stringSlice(start, end - 1) + "&#39;";
start = end;
continue;
case 60: // <
escaped += string.slice(start, end - 1) + "&#60;";
escaped += stringSlice(start, end - 1) + "&#60;";
start = end;
continue;
case 62: // >
escaped += string.slice(start, end - 1) + "&#62;";
escaped += stringSlice(start, end - 1) + "&#62;";
start = end;
continue;
case 96: // `
escaped += string.slice(start, end - 1) + "&#96;";
escaped += stringSlice(start, end - 1) + "&#96;";
start = end;
continue;
}
} while (end !== stringLength);

escaped += string.slice(start, end);
escaped += stringSlice(start, end);

return escaped;
};
Expand Down Expand Up @@ -71,7 +75,7 @@ const html = ({ raw: literals }, ...expressions) => {

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else if (string && escapeRegExp.test(string)) {
} else if (string && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand Down Expand Up @@ -135,7 +139,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
}

if (string) {
if (!isRaw && escapeRegExp.test(string)) {
if (!isRaw && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand All @@ -150,7 +154,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
}

if (string) {
if (!isRaw && escapeRegExp.test(string)) {
if (!isRaw && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand All @@ -166,7 +170,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else if (string && escapeRegExp.test(string)) {
} else if (string && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand Down Expand Up @@ -232,7 +236,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
}

if (string) {
if (!isRaw && escapeRegExp.test(string)) {
if (!isRaw && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand All @@ -247,7 +251,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
}

if (string) {
if (!isRaw && escapeRegExp.test(string)) {
if (!isRaw && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand All @@ -263,7 +267,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else if (string && escapeRegExp.test(string)) {
} else if (string && escapeRegExpTest(string)) {
string = escapeFunction(string);
}

Expand Down

0 comments on commit d5609fb

Please sign in to comment.