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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* ************************************************************************
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)
************************************************************************ */
/**
* A Collection of utility functions to escape and unescape strings.
*/
qx.OO.defineClass("qx.html.String");
/**
* Escapes the characters in a <code>String</code> using HTML entities.
*
* For example: <tt>"bread" & "butter"</tt> => <tt>&quot;bread&quot; &amp; &quot;butter&quot;</tt>.
* Supports all known HTML 4.0 entities, including funky accents.
*
* * <a href="http://www.w3.org/TR/REC-html32#latin1">HTML 3.2 Character Entities for ISO Latin-1</a>
* * <a href="http://www.w3.org/TR/REC-html40/sgml/entities.html">HTML 4.0 Character entity references</a>
* * <a href="http://www.w3.org/TR/html401/charset.html#h-5.3">HTML 4.01 Character References</a>
* * <a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
*
* @see #unescape
*
* @param str {String} the String to escape
* @return {String} a new escaped String
*/
qx.Class.escape = function(str) {
return qx.dom.String.escapeEntities(
str,
qx.html.Entity.FROM_CHARCODE
);
};
/**
* Unescapes a string containing entity escapes to a string
* containing the actual Unicode characters corresponding to the
* escapes. Supports HTML 4.0 entities.
*
* For example, the string "&lt;Fran&ccedil;ais&gt;"
* will become "<Français>"
*
* If an entity is unrecognized, it is left alone, and inserted
* verbatim into the result string. e.g. "&gt;&zzzz;x" will
* become ">&zzzz;x".
*
* @see #escape
*
* @param str {String} the String to unescape, may be null
* @return a new unescaped String
*/
qx.Class.unescape = function(str) {
return qx.dom.String.unescapeEntities(
str,
qx.html.Entity.TO_CHARCODE
);
};
/**
* Converts a plain text string into HTML.
* This is similar to {@link #escape} but converts new lines to
* <tt><:br>:</tt> and preserves whitespaces.
*
* @see #escape
*
* @param str {String} the String to convert
* @return {String} a new converted String
*/
qx.Class.fromText = function(str) {
return qx.html.String.escape(str).replace(/( |\n)/g, function(chr) {
var map = {
" ": " ",
"\n": "<br>"
}
return map[chr] || chr;
});
}
/**
* Converts HTML to plain text.
*
* * Strips all HTML tags
* * converts <tt><:br>:</tt> to new line
* * unescapes HTML entities
*
* @param str {String} HTML string to converts
* @return {String} plain text representaion of the HTML string
*/
qx.Class.toText = function(str) {
return qx.html.String.unescape(str.replace(/\s+|<([^>])+>/gi, function(chr) {
if (/\s+/.test(chr)) {
return " ";
}
else if (/^<BR|^<br/gi.test(chr)) {
return "\n";
} else {
return "";
}
}));
};
|