encodeHTML.js 420 B

123456789101112131415161718192021
  1. "use strict"
  2. /* istanbul ignore file */
  3. function getEncodeHtml() {
  4. const encodeHTMLRules = {
  5. "&": "&",
  6. "<": "&#60;",
  7. ">": "&#62;",
  8. '"': "&#34;",
  9. "'": "&#39;",
  10. "/": "&#47;",
  11. }
  12. const matchHTML = /&(?!#?\w+;)|<|>|"|'|\//g
  13. return function encodeHtml(s) {
  14. return typeof s === "string" ? s.replace(matchHTML, (m) => encodeHTMLRules[m] || m) : s
  15. }
  16. }
  17. module.exports = getEncodeHtml