Changeset 552 for trunk/js/Utilities.js
- Timestamp:
- Nov 25, 2015 7:29:35 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/js/Utilities.js
r527 r552 70 70 71 71 /* 72 * Encode/decode HTML by proxying content via an in-memory div, setting its inner text which jQuery automatically encodes. 72 * Encode html entities by specific mapping table. 73 * Decode HTML by proxying content via an in-memory div, setting its inner text which jQuery automatically encodes. 73 74 Then we pull the encoded contents back out. The div never exists on the page. 74 75 --------------------------------------------------------------------- 75 $('select').append($('<option>', { 76 value: value, 77 text: Strangecode.htmlEncode(text) 78 })); 76 $('input').val(Strangecode.htmlEncode(string)); 79 77 --------------------------------------------------------------------- 80 * 81 *@access public82 * @version 1.1 83 *@since 30 Jun 201378 79 @access public 80 @version 2.0 81 @since 30 Jun 2013 84 82 */ 85 Strangecode.htmlEncode = function(text) { 86 return $('<div/>').text(text).html(); 83 Strangecode.htmlEncode = function (str) { 84 var entityMap = { 85 '&': '&', 86 '<': '<', 87 '>': '>', 88 '"': '"', 89 "'": ''', 90 '/': '/', 91 '`': '`' 92 }; 93 return String(str).replace(/[&<>"'\/`]/g, function (s) { 94 return entityMap[s]; 95 }); 87 96 }; 88 Strangecode.htmlDecode = function (text) {89 return $('<div />').html(value).text();97 Strangecode.htmlDecode = function (str) { 98 return $('<div>').html(str).text(); 90 99 }; 91 100 92 101 93 102 /* 94 *Returns a string with URL-unsafe characters removed.103 Returns a string with URL-unsafe characters removed. 95 104 --------------------------------------------------------------------- 96 105 var urlslug = $('.url').val().slug(); … … 109 118 return str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); 110 119 }; 120 121 122 123 /* 124 Remove rounding errors caused by representation of finite binary floating point numbers. 125 --------------------------------------------------------------------- 126 > (.1*.2) 127 0.020000000000000004 128 > (.1*.2).trim() 129 0.02 130 --------------------------------------------------------------------- 131 * @access public 132 * @version 1.0 133 * @since 24 Nov 2015 134 */ 135 if (!Number.prototype.trim) { 136 Number.prototype.trim = function (precision) { 137 var precision = precision || 11; 138 return Math.round(this * Math.pow(10, precision)) / Math.pow(10, precision); 139 }; 140 } 141 142 /* 143 Uppercase the first letter of string. 144 --------------------------------------------------------------------- 145 > 'hello world'.trim() 146 Hello world 147 --------------------------------------------------------------------- 148 * @access public 149 * @version 1.0 150 * @since 24 Nov 2015 151 */ 152 if (!String.prototype.ucfirst) { 153 String.prototype.ucfirst = function() { 154 return this.charAt(0).toUpperCase() + this.slice(1); 155 }; 156 } 157
Note: See TracChangeset
for help on using the changeset viewer.