summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/dom/String.js
blob: a5979c026b3d9962a0bbae9c2e292c40447e374b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2007 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Fabian Jakobs (fjakobs)

************************************************************************ */

/* ************************************************************************

#require(qx.lang.Object)

************************************************************************ */

/**
 * Generic escaping and unescaping of DOM strings.
 *
 * {@link qx.html.String} for (un)escaping of HTML strings.
 *
 * {@link qx.xml.String} for (un)escaping of XML strings.
 */
qx.OO.defineClass("qx.dom.String");


/**
 * generic escaping method
 *
 * @param str {String} string to escape
 * @param charcodeToEntities {Map} entity to charcode map
 */
qx.Class.escapeEntities = function(str, charcodeToEntities) {
  var result = [];
  for (var i=0; i<str.length; i++) {
    var chr = str.charAt(i);
    var code = chr.charCodeAt(0)
    if (charcodeToEntities[code]) {
        var entity = "&" + charcodeToEntities[code] + ";";
    } else {
      if (code > 0x7F) {
        entity = "&#" + code + ";";
      } else {
        entity = chr;
      }
    }
    result.push(entity);
  }
  return result.join("");
};


/**
 * generic unescaping method
 *
 * @param str {String} string to unescape
 * @param entitiesToCharCode {Map} charcode to entity map
 */
qx.Class.unescapeEntities = function(str, entitiesToCharCode) {
  return str.replace(/&[#\w]+;/gi, function(entity) {
    var chr = entity;
    var entity = entity.substring(1, entity.length-1);
    var code = entitiesToCharCode[entity];
    if (code) {
      chr = String.fromCharCode(code);
    } else {
      if (entity.charAt(0) == '#') {
        if (entity.charAt(1).toUpperCase() == 'X') {
          var code = entity.substring(2);
          // match hex number
          if (code.match(/^[0-9A-Fa-f]+$/gi)) {
            chr = String.fromCharCode(parseInt("0x" + code));
          }
        } else {
        var code = entity.substring(1);
          // match integer
          if (code.match(/^\d+$/gi)) {
            chr = String.fromCharCode(parseInt(code));
          }
        }
      }
    }
    return chr;
  });
};


/**
 * Remove HTML/XML tags from a string
 * Example:
 * <pre>qx.dom.String.stripTags("&lt;h1>Hello&lt;/h1>") == "Hello"</pre>
 *
 * @param str {String} string containing tags
 * @return {String} the string with stripped tags
 */
qx.Class.stripTags = function(str) {
  return str.replace(/<\/?[^>]+>/gi, "");
};