Nodejs HTML Encode htmlEncode()

Here you can find the source of htmlEncode()

Method Source Code

function getBgColorHex(elem){ 
   var color = elem.css('background-color');
   var hex; /*w ww . j a  va  2s  . c  o  m*/
   if(color.indexOf('#')>-1)
   {
      hex = color; 
   }
   else
   { 
      var rgb = color.match(/\d+/g);
      hex = '#'+ ('0' + parseInt(rgb[0], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[1], 10).toString(16)).slice(-2) + ('0' + parseInt(rgb[2], 10).toString(16)).slice(-2); 
   } 
   return hex; 
}



String.prototype.htmlEncode = function () {
   return String(this)
      .replace(/&/g, '&')
      .replace(/"/g, '"')
      .replace(/'/g, ''')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;');

}

Related

  1. encodeHTML()
    String.prototype.encodeHTML = function () {
        return this.replace(/&/g, '&amp;')
               .replace(/</g, '&lt;')
               .replace(/>/g, '&gt;')
               .replace(/"/g, '&quot;');
    };
    
  2. encodeHTML()
    define(function() {
    String.prototype.encodeHTML=function () {
          var encodeHTMLRules, matchHTML;
          encodeHTMLRules = {
            "&": "&#38;",
            "<": "&#60;",
            ">": "&#62;",
            '"': '&#34;',
            "'": '&#39;',
    ...
    
  3. encodeHTML()
    String.prototype.encodeHTML = function() {
      var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
          matchHTML = /&(?!#?\w+;)|<|>|"|'|\
      return function() {
        return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : this;
      };
    }();
    
  4. encodeHTML();
    function encodeHTMLSource() {
        var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;'},
            matchHTML = /&(?!#?\w+;)|<|>|"|'|\
        return function() {
            return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m; }) : this;
        };
    String.prototype.encodeHTML = encodeHTMLSource();
    
  5. encodeHtml()
    String.prototype.encodeHtml = function() {
     var encodedHtml = escape(this);
     encodedHtml = encodedHtml.replace(/\
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     return encodedHtml;
    
  6. htmlEncode()
    String.prototype.htmlEncode = function() {
      return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g,
          "&gt;").replace(/\"/g, "&#34;").replace(/\'/g, "&#39;");
    };
    
  7. htmlEncode()
    String.prototype.htmlEncode = function () {
        var div = document.createElement('div');
        div.appendChild(document.createTextNode(this));
        var html = div.innerHTML;
        html = html.replace(/\r\n/g, '<br/>');
        html = html.replace(/\n/g, '<br/>');
        return html;
    };