/* The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is ASGard Framework. The Initial Developer of the Original Code is ALCARAZ Marc (aka eKameleon) . Portions created by the Initial Developer are Copyright (C) 2004-2009 the Initial Developer. All Rights Reserved. Contributor(s) : */ package asgard.net { /** * This static tool class encode or decode the HTML entities in a string. */ public class HTMLEntities { /** * Determinates all entities. */ public static var entities:Array = [ "€" , """ , "'" , "&" , "<" , ">" , "¡" , "¢" , "£" , "¤" , "¥" , "¦" , "§" , "¨" , "©" , "ª" , "¬" , "­" , "®" , "¯" , "°" , "±" , "²" , "³" , "´" , "µ" , "¶" , "·" , "¸" , "¹" , "º" , "»" , "¼" , "½" , "¾" , "¿" , "À" , "Á" , "Ã" , "Ä" , "Å" , "Æ" , "Ç" , "È" , "É" , "Ê" , "Ì" , "Í" , "Î" , "Ï" , "Ð" , "Ñ" , "Ò" , "Ó" , "Ô" , "Õ" , "Ö" , "×" , "Ø" , "Ù" , "Ú" , "Û" , "Ü" , "Ý" , "Þ" , "ß" , "à" , "á" , "â" , "ã" , "ä" , "å" , "æ" , "ç" , "è" , "é" , "ê" , "ë" , "ì" , "í" , "î" , "ï" , "ð" , "ñ" , "ò" , "ó" , "ô" , "õ" , "ö" , "÷" , "ø" , "ù" , "ú" , "û" , "ü" , "ý" , "þ" , " " ] ; /** * Determinates all special chars. */ public static var specialchars:Array = [ "€" , "\"", "'" , "&" , "<" , ">" , "¡" , "¢" , "£" , "¤" , "¥" , "¦" , "§" , "¨" , "©" , "ª" , "¬" , "­" , "®" , "¯" , "°" , "±" , "²" , "³" , "´" , "µ" , "¶" , "·" , "¸" , "¹" , "º" , "»" , "¼" , "½" , "¾" , "¿" , "À" , "Á" , "Ã" , "Ä" , "Å" , "Æ" , "Ç" , "È" , "É" , "Ê" , "Ì" , "Í" , "Î" , "Ï" , "Ð" , "Ñ" , "Ò" , "Ó" , "Ô" , "Õ" , "Ö" , "×" , "Ø" , "Ù" , "Ú" , "Û" , "Ü" , "Ý" , "Þ" , "ß" , "à" , "á" , "â" , "ã" , "ä" , "å" , "æ" , "ç" , "è" , "é" , "ê" , "ë" , "ì" , "í" , "î" , "ï" , "ð" , "ñ" , "ò" , "ó" , "ô" , "õ" , "ö" , "÷" , "ø" , "ù" , "ú" , "û" , "ü" , "ý" , "þ" , "\u00A0" ] ; /** * Decodes the specified string. *

Example :

*
         * import asgard.net.HTMLEntities  ;
         * trace( HTMLEntities.decode("<b>hello world</b>" ) ) ; // hello world
         * 
* @return the decode string. */ public static function decode( text:String, removeCRLF:Boolean=false ):String { var ch:String ; var entity:String ; var i:int ; var len:int = entities.length ; for( i=0 ; i -1 ) { text = text.replace( new RegExp( entity , "g" ) , ch ); } } if( removeCRLF ) { text = (text.split("\r\n")).join("") ; } return text; } /** * Encodes the specified text passed in argument. *

Example :

*
         * import asgard.net.HTMLEntities  ;
         * trace( HTMLEntities.encode("hello world" ) ) ; // <b>hello world</b>
         * 
* @return a string encode text. */ public static function encode( text:String ):String { var ch:String ; var entity:String ; var i:int ; var len:int = entities.length ; for( i=0; i < len; i++ ) { ch = specialchars[i]; entity = entities[i]; if( text.indexOf( ch ) > -1 ) { text = text.replace( new RegExp( ch , "g" ) , entity ); } } return text.toString() ; } /** * Returns the entity name of the specified character in argument. * Returns an empty string value if the char passed-in argument isn't a special char to transform in entity string representation. *

Example :

*
         * import asgard.net.HTMLEntities  ;
         * trace( HTMLEntities.getCharToEntity("<") ) ; // <
         * 
* @return the entity name of the specified character in argument. */ public static function getCharToEntity( char:String ):String { var index:int = specialchars.indexOf( char.charAt(0) ) ; return index > -1 ? entities[index] : "" ; } /** * Returns the entity number string representation of the specified character in argument. *

Example :

*
         * import asgard.net.HTMLEntities  ;
         * trace( HTMLEntities.getCharToEntityNumber("<") ) ; // <
         * 
* @return the entity number string representation of the specified character in argument. */ public static function getCharToEntityNumber( char:String ):String { return "&#" + char.charCodeAt(0) + ";" ; } /** * Returns the char representation of the specified entity number string value in argument or an empty string value. *

Example :

*
         * import asgard.net.HTMLEntities  ;
         * trace( HTMLEntities.getEntityNumberToChar("<")) ; // <
         * 
* @return the char representation of the specified entity number string value in argument or an empty string value. */ public static function getEntityNumberToChar( entityNumber:String ):String { if ( entityNumber.charAt(0) == "&" && entityNumber.charAt(1) == "#" && entityNumber.charAt(entityNumber.length - 1) == ";" ) { return String.fromCharCode( parseInt( ( entityNumber.split("&#") )[1]) ) ; } else { return "" ; } } } }