-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cleaup stream and handle errors (#1769)
- Loading branch information
1 parent
22ec9ad
commit 1258fdd
Showing
8 changed files
with
401 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const matchHtmlRegExp = /["'&<>]/; | ||
|
||
/** | ||
* @param {string} string raw HTML | ||
* @returns {string} escaped HTML | ||
*/ | ||
function escapeHtml(string) { | ||
const str = `${string}`; | ||
const match = matchHtmlRegExp.exec(str); | ||
|
||
if (!match) { | ||
return str; | ||
} | ||
|
||
let escape; | ||
let html = ""; | ||
let index = 0; | ||
let lastIndex = 0; | ||
|
||
for ({ index } = match; index < str.length; index++) { | ||
switch (str.charCodeAt(index)) { | ||
// " | ||
case 34: | ||
escape = """; | ||
break; | ||
// & | ||
case 38: | ||
escape = "&"; | ||
break; | ||
// ' | ||
case 39: | ||
escape = "'"; | ||
break; | ||
// < | ||
case 60: | ||
escape = "<"; | ||
break; | ||
// > | ||
case 62: | ||
escape = ">"; | ||
break; | ||
default: | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
|
||
if (lastIndex !== index) { | ||
html += str.substring(lastIndex, index); | ||
} | ||
|
||
lastIndex = index + 1; | ||
html += escape; | ||
} | ||
|
||
return lastIndex !== index ? html + str.substring(lastIndex, index) : html; | ||
} | ||
|
||
module.exports = escapeHtml; |
Oops, something went wrong.