html字符解码

@param {string} str
@returns {string}
@example

htmlDecode('&lt;script&gt;');
// => <script>
558329
function htmlDecode(str) {
  if (typeof str === 'string' && str.length === 0) return;

  let s = str.replace(/&amp;/g, '&');

  return s.replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&#39;/g, '\'')
        .replace(/&nbsp;/g, ' ')
        .replace(/&quot;/g, '"')
        .replace(/<br>/g, '\\n');
}
66705
function(str) {
		var xmlEncodeChars = {'<' : 'lt', '>' : 'gt', '&' : 'amp', '"' : 'quot', '\'' : '#39'}, xmlEncodeRegExp = /[<>&\"\']/g;

		return str ? ('' + str).replace(xmlEncodeRegExp, function(chr) {
			return xmlEncodeChars[chr] ? '&' + xmlEncodeChars[chr] + ';' : chr;
		}) : str;
	}
650658
public static String decode(String str) {
    if (str!=null) {
      try {
        StringBuilder sb = new StringBuilder();
        for (int idx = str.toLowerCase().indexOf("%2f"); idx>=0; idx = str.toLowerCase().indexOf("%2f")) {
          sb.append(java.net.URLDecoder.decode(str.substring(0, idx),"UTF-8")).append(str.substring(idx, idx+3));
          str = str.substring(idx+3);
        }
        sb.append(java.net.URLDecoder.decode(str,"UTF-8"));
        str = sb.toString();
      } catch (Exception ex) {
      }
    }
    return str;
  }
931535
public static String encode(String str) {
    if (str == null) {
      return "";
    }

    return ESCAPER.escape(str)
      .replaceAll("\\!", "%21")
      .replaceAll("\\$", "%24")
      .replaceAll("\\&", "%26")
      .replaceAll("\\'", "%27")
      .replaceAll("\\(", "%28")
      .replaceAll("\\)", "%29")
      .replaceAll("\\*", "%2A")
      .replaceAll("\\+", "%2B")
      .replaceAll("\\,", "%2C")
      .replaceAll("\\/", "%2F")
      .replaceAll("\\:", "%3A")
      .replaceAll("\\;", "%3B")
      .replaceAll("\\=", "%3D")
      .replaceAll("\\@", "%40")
      .replaceAll("\\[", "%5B")
      .replaceAll("\\]", "%5D");
  }
503587
function decode(str) {
  let off = 0;
  let olen = 0;
  const slen = str.length;
  const stra = [];
  const len = str.length;

  while (off < slen - 1 && olen < len) {
    let code = str.charCodeAt(off++);
    const c1 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
    code = str.charCodeAt(off++);
    const c2 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
    if (c1 === -1 || c2 === -1) break;
    let o = (c1 << 2) >>> 0;
    o |= (c2 & 0x30) >> 4;
    stra.push(String.fromCharCode(o));
    if (++olen >= len || off >= slen) break;
    code = str.charCodeAt(off++);
    const c3 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
    if (c3 === -1) break;
    o = ((c2 & 0x0f) << 4) >>> 0;
    o |= (c3 & 0x3c) >> 2;
    stra.push(String.fromCharCode(o));
    if (++olen >= len || off >= slen) break;
    code = str.charCodeAt(off++);
    const c4 = code < BASE64_INDEX.length ? BASE64_INDEX[code] : -1;
    o = ((c3 & 0x03) << 6) >>> 0;
    o |= c4;
    stra.push(String.fromCharCode(o));
    ++olen;
  }
  const buffa = [];
  for (off = 0; off < olen; off++) buffa.push(stra[off].charCodeAt(0));
  return Buffer.from(buffa);
}
519977
function (str) {
    var lit = this.ast_stringify(t.stringLiteral(str));
    var ast = this.ast_buffer(lit);
    return ast;
  }
979477
function(str, pad) {
			var padding = '';
			var il = str.length;
				
			while (pad > il++) padding += '0';
			return padding + str; 
		}
824345
public static String decode(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			//should never happen
			throw new RuntimeException(e);
		}
	}
944685
function (str) {
      var strlen = str.length;
      if (this.html.substr(this.currentChar, strlen).toLowerCase() === str.toLowerCase()) {
        this.currentChar += strlen;
        return true;
      }
      return false;
    }
761590
function htmlParser(html, handler) {
  if (typeof html !== 'string') {
    if (html === null || typeof html === 'undefined') {
      html = '';
    } else {
      html = '' + html;
    }
  }
  var index, chars, match, stack = [],
    last = html,
    text;
  stack.last = function () {
    return stack[stack.length - 1];
  };

  while (html) {
    text = '';
    chars = true;

    // Make sure we're not in a script or style element
    if (!stack.last() || !specialElements[stack.last()]) {

      // Comment
      if (html.indexOf("<!--") === 0) {
        // comments containing -- are not allowed unless they terminate the comment
        index = html.indexOf("--", 4);

        if (index >= 0 && html.lastIndexOf("-->", index) === index) {
          if (handler.comment) handler.comment(html.substring(4, index));
          html = html.substring(index + 3);
          chars = false;
        }
        // DOCTYPE
      } else if (DOCTYPE_REGEXP.test(html)) {
        match = html.match(DOCTYPE_REGEXP);

        if (match) {
          html = html.replace(match[0], '');
          chars = false;
        }
        // end tag
      } else if (BEGING_END_TAGE_REGEXP.test(html)) {
        match = html.match(END_TAG_REGEXP);

        if (match) {
          html = html.substring(match[0].length);
          match[0].replace(END_TAG_REGEXP, parseEndTag);
          chars = false;
        }

        // start tag
      } else if (BEGIN_TAG_REGEXP.test(html)) {
        match = html.match(START_TAG_REGEXP);

        if (match) {
          // We only have a valid start-tag if there is a '>'.
          if (match[4]) {
            html = html.substring(match[0].length);
            match[0].replace(START_TAG_REGEXP, parseStartTag);
          }
          chars = false;
        } else {
          // no ending tag found --- this piece should be encoded as an entity.
          text += '<';
          html = html.substring(1);
        }
      }

      if (chars) {
        index = html.indexOf("<");

        text += index < 0 ? html : html.substring(0, index);
        html = index < 0 ? "" : html.substring(index);

        if (handler.chars) handler.chars(decodeEntities(text));
      }

    } else {
      // IE versions 9 and 10 do not understand the regex '[^]', so using a workaround with [\W\w].
      html = html.replace(new RegExp("([\\W\\w]*)<\\s*\\/\\s*" + stack.last() + "[^>]*>", 'i'),
        function (all, text) {
          text = text.replace(COMMENT_REGEXP, "$1").replace(CDATA_REGEXP, "$1");

          if (handler.chars) handler.chars(decodeEntities(text));

          return "";
        });

      parseEndTag("", stack.last());
    }

    if (html == last) {
      // benweet
      // throw $sanitizeMinErr('badparse', "The sanitizer was unable to parse the following block " +
      // 	"of html: {0}", html);
      stack.reverse();
      return stack.cl_each(function (tag) {
        buf.push('</');
        buf.push(tag);
        buf.push('>');
      });
    }
    last = html;
  }

  // Clean up any remaining tags
  parseEndTag();

  function parseStartTag(tag, tagName, rest, unary) {
    tagName = tagName && tagName.toLowerCase();
    if (blockElements[tagName]) {
      while (stack.last() && inlineElements[stack.last()]) {
        parseEndTag("", stack.last());
      }
    }

    if (optionalEndTagElements[tagName] && stack.last() == tagName) {
      parseEndTag("", tagName);
    }

    unary = voidElements[tagName] || !!unary;

    if (!unary) {
      stack.push(tagName);
    }

    var attrs = {};

    rest.replace(ATTR_REGEXP,
      function (match, name, doubleQuotedValue, singleQuotedValue, unquotedValue) {
        var value = doubleQuotedValue || singleQuotedValue || unquotedValue || '';

        attrs[name] = decodeEntities(value);
      });
    if (handler.start) handler.start(tagName, attrs, unary);
  }

  function parseEndTag(tag, tagName) {
    var pos = 0,
      i;
    tagName = tagName && tagName.toLowerCase();
    if (tagName) {
      // Find the closest opened tag of the same type
      for (pos = stack.length - 1; pos >= 0; pos--) {
        if (stack[pos] == tagName) break;
      }
    }

    if (pos >= 0) {
      // Close all the open elements, up the stack
      for (i = stack.length - 1; i >= pos; i--)
        if (handler.end) handler.end(stack[i]);

      // Remove the open elements from the stack
      stack.length = pos;
    }
  }
}
569815
function(str) {
    if(typeof(str)=='undefined') return undefined;
    if(typeof(str)=='undefined') return undefined;

    var surrogate_1st = 0;
    var unicode_codes = [];
    for (var i = 0; i < str.length; ++i) {
	var utf16_code = str.charCodeAt(i);
	if (surrogate_1st != 0) {
	    if (utf16_code >= 0xdc00 && utf16_code <= 0xdfff) {
		var surrogate_2nd = utf16_code;
		var unicode_code = (surrogate_1st - 0xd800) * (1 << 10) + (1 << 16) +
                    (surrogate_2nd - 0xdc00);
		unicode_codes.push(unicode_code);
	    }
	    surrogate_1st = 0;
	}
	else if (utf16_code >= 0xd800 && utf16_code <= 0xdbff) {
	    surrogate_1st = utf16_code;
	}
	else {
	    unicode_codes.push(utf16_code);
	}
    }

    var utf8_bytes = "";
    var i, unicode_code;
    for(i=0; i<unicode_codes.length; ++i) {
	unicode_code = unicode_codes[i];
	if (unicode_code < 0x80) {  // 1-byte
	    utf8_bytes += String.fromCharCode(unicode_code);
	}
	else if (unicode_code < (1 << 11)) {  // 2-byte
	    utf8_bytes += String.fromCharCode((unicode_code >>> 6) | 0xC0);
	    utf8_bytes += String.fromCharCode((unicode_code & 0x3F) | 0x80);
	}
	else if (unicode_code < (1 << 16)) {  // 3-byte
	    utf8_bytes += String.fromCharCode((unicode_code >>> 12) | 0xE0);
	    utf8_bytes += String.fromCharCode(((unicode_code >> 6) & 0x3f) | 0x80);
	    utf8_bytes += String.fromCharCode((unicode_code & 0x3F) | 0x80);
	}
	else if (unicode_code < (1 << 21)) {  // 4-byte
	    utf8_bytes += String.fromCharCode((unicode_code >>> 18) | 0xF0);
	    utf8_bytes += String.fromCharCode(((unicode_code >> 12) & 0x3F) | 0x80);
	    utf8_bytes += String.fromCharCode(((unicode_code >> 6) & 0x3F) | 0x80);
	    utf8_bytes += String.fromCharCode((unicode_code & 0x3F) | 0x80);
	}
    }
    return utf8_bytes;
}
1241045
public static function listsToHtml($str)
    {

        $str = preg_replace_callback('/(?:(?:^|\n)[#\*].*)+\n?/m', function($match){

            $type = substr(trim($match[0]), 0, 1) == '#' ? 'ol' : 'ul';

            $star_cnt = 1;
            $lis = preg_replace_callback("~^([\*\#]+)(.*)$~m", function($innermatch){

                return '<li>'.$innermatch[2].'</li>';
            }, trim($match[0]));

            $lis = str_replace(Array("\n</li>", "</li>\n"), "</li>", $lis);
            return '<'.$type.' class="tb">'.$lis.'</'.$type.'>';

        }, $str);

        $str = preg_replace_callback("~\[list\](.*?)\[/list\]\n~s", function($match){

            $lis = preg_replace_callback("~^\w|\s.*$~m", function($inner_match){
                $li = trim($inner_match[0]);
                if(!empty($li)){
                    return '<li class="tb">'.trim($li).'</li>';
                }
                return '';
            }, $match[1]);
            return '<ul class="tb">'.$lis.'</ul>';

        }, $str);

        $str = preg_replace_callback("~\[numlist\](.*?)\[/numlist\]\n?~s", function($match){

            $lis = preg_replace_callback("~^\w|\s.*$~m", function($inner_match){
                $li = trim($inner_match[0]);
                if(!empty($li)){
                    return '<li class="tb">'.trim($li).'</li>';
                }
                return '';
            }, $match[1]);
            return '<ol class="tb">'.$lis.'</ol>';

        }, $str);

        $str = preg_replace_callback('~\n{0,}\t{0,}\[(/?(ol|ul|li))\]\n*~', function($match) {
            if (!strstr($match[1], "/")) {
                return '<' . $match[1] . ' class="tb">';
            } else {
                return '<' . $match[1] . '>';
            }
        }, $str);

        return $str;
    }
767556
function(html) {
       return html.toString()
         .replace(/&/gmi, '&amp;')
         .replace(/"/gmi, '&quot;')
         .replace(/>/gmi, '&gt;')
         .replace(/</gmi, '&lt;')
     }
1233174
public static String reqExpEscape(String str) {
	char[] arr = str.toCharArray();
	StringBuilder sb = new StringBuilder(str.length() * 2);

	for (int i = 0; i < arr.length; i++) {
	    sb.append('\\');
	    sb.append(arr[i]);
	}

	return sb.toString();
    }
334733
function (str) {
      var index = this.html.indexOf(str, this.currentChar) + str.length;
      if (index === -1)
        this.currentChar = this.html.length;
      this.currentChar = index;
    }
556240
function(str) {
      var type, res;

      str = str || '';
      res = str.match(/(uncaught)?\s?(\w+)\:/i);
      type = (res) ? res[2] : null;

      return type;
    }
387420
function process(str) {
        var div = document.createElement('div');
        div.innerHTML = str.trim();
        return format(div, 0).innerHTML;
    }