diff options
Diffstat (limited to 'webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/html/String.js')
-rw-r--r-- | webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/html/String.js | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/html/String.js b/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/html/String.js new file mode 100644 index 0000000000..b2664ecb6e --- /dev/null +++ b/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/html/String.js @@ -0,0 +1,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 ""; + } + })); +}; |