Changeset 552 for trunk/js/Utilities.js


Ignore:
Timestamp:
Nov 25, 2015 7:29:35 PM (8 years ago)
Author:
anonymous
Message:

Updated js.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/js/Utilities.js

    r527 r552  
    7070
    7171/*
    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.
    7374Then we pull the encoded contents back out. The div never exists on the page.
    7475---------------------------------------------------------------------
    75 $('select').append($('<option>', {
    76     value: value,
    77     text: Strangecode.htmlEncode(text)
    78 }));
     76$('input').val(Strangecode.htmlEncode(string));
    7977---------------------------------------------------------------------
    80 *
    81 * @access   public
    82 * @version  1.1
    83 * @since    30 Jun 2013
     78
     79@access   public
     80@version  2.0
     81@since    30 Jun 2013
    8482*/
    85 Strangecode.htmlEncode = function(text) {
    86     return $('<div/>').text(text).html();
     83Strangecode.htmlEncode = function (str) {
     84    var entityMap = {
     85        '&': '&amp;',
     86        '<': '&lt;',
     87        '>': '&gt;',
     88        '"': '&quot;',
     89        "'": '&#39;',
     90        '/': '&#x2F;',
     91        '`': '&DiacriticalGrave;'
     92    };
     93    return String(str).replace(/[&<>"'\/`]/g, function (s) {
     94        return entityMap[s];
     95    });
    8796};
    88 Strangecode.htmlDecode = function(text) {
    89     return $('<div/>').html(value).text();
     97Strangecode.htmlDecode = function (str) {
     98    return $('<div>').html(str).text();
    9099};
    91100
    92101
    93102/*
    94 * Returns a string with URL-unsafe characters removed.
     103Returns a string with URL-unsafe characters removed.
    95104---------------------------------------------------------------------
    96105var urlslug = $('.url').val().slug();
     
    109118    return str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-');
    110119};
     120
     121
     122
     123/*
     124Remove rounding errors caused by representation of finite binary floating point numbers.
     125---------------------------------------------------------------------
     126> (.1*.2)
     1270.020000000000000004
     128> (.1*.2).trim()
     1290.02
     130---------------------------------------------------------------------
     131* @access   public
     132* @version  1.0
     133* @since    24 Nov 2015
     134*/
     135if (!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/*
     143Uppercase the first letter of string.
     144---------------------------------------------------------------------
     145> 'hello world'.trim()
     146Hello world
     147---------------------------------------------------------------------
     148* @access   public
     149* @version  1.0
     150* @since    24 Nov 2015
     151*/
     152if (!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.