github-num vs my-code-num

Edit on GitHub

Setup

str:123

Tests

  • github-num

    var htmlentities = (function(document){
    
    function encode(str){
    	var div = document.createElement('div');
    	div.appendChild(document.createTextNode(str));
    	str = div.innerHTML;
    	div = null;
    	return str;
    }
    
    encode.decode = function(str){
    	var div = document.createElement('div');
    	div.innerHTML = str;
    	str = div.innerText || div.textContent;
    	div = null;
    	return str;
    };
    return (encode.encode = encode);
    
    }(document));
  • my-code-num

    function encodeHTML (str){
    	if(str === null || str === undefined){
    	return "";
    	}
    	if(typeof str !== "string"){
    	str = str + "";
    	}
    	var escapeChar = function (chr) {
    	return ZComponents.escapeChars[chr] || "&";
    	};
    	return str.replace(this.badChars, escapeChar);
    }