·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3日期格式化以及htmlText的安全转义类

时间:2012-06-26 09:489ria.com
[AS3]as3日期格式化以及htmlText的安全转义类

 

  1. package org.juke{  
  2.         public class Tools {  
  3.                   
  4.                 //时间格式化  
  5.                 public static function fitInTime (second:Number, isShort:Boolean = falsemillisecond:Boolean = false):String   
  6.                 {  
  7.                         return TimeTool.timeFormat (second, isShort, millisecond);  
  8.                 }  
  9.                 //特殊字符转义 HTML 格式  
  10.                 public static function htmlspecialchars (text:String):String  
  11.                 {  
  12.                         return StringTool.htmlspecialchars (text);  
  13.                 }  
  14.         }  
  15. }  
  16.  
  17. /////////////////////////////////////////////////////  
  18. //特殊字符转义 HTML 格式  
  19. /////////////////////////////////////////////////////  
  20. class StringTool  
  21. {  
  22.         static function htmlspecialchars (string = nullquote_style = null)   
  23.         {  
  24.     // Convert special characters to HTML entities    
  25.     //   
  26.     // version: 812.3017  
  27.     // discuss at: http://phpjs.org/functions/htmlspecialchars  
  28.  
  29.     // +   original by: Mirek Slugen  
  30.     // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
  31.     // +   bugfixed by: Nathan  
  32.     // +   bugfixed by: Arno  
  33.     // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
  34.     // -    depends on: get_html_translation_table  
  35.     // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');  
  36.     // *     returns 1: '&lt;a href='test'&gt;Test&lt;/a&gt;'  
  37.           
  38.     var histogram = {}, symbol = ''tmp_str = ''entity = '';  
  39.     tmp_str = string.toString();  
  40.       
  41.     if (false === (histogram = get_html_translation_table('HTML_SPECIALCHARS', quote_style))) {  
  42.         return false;  
  43.     }  
  44.       
  45.     for (symbol in histogram) {  
  46.         entity = histogram[symbol];  
  47.         tmp_strtmp_str = tmp_str.split(symbol).join(entity);  
  48.     }  
  49.       
  50.     return tmp_str;  
  51.         }  
  52.  
  53.         static function get_html_translation_table (table = nullquote_style = null)  
  54.         {  
  55.                 // Returns the internal translation table used by htmlspecialchars and htmlentities    
  56.                 //   
  57.                 // version: 901.714  
  58.                 // discuss at: http://phpjs.org/functions/get_html_translation_table  
  59.  
  60.                 // +   original by: Philip Peterson  
  61.                 // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)  
  62.                 // +   bugfixed by: noname  
  63.                 // %          note: It has been decided that we're not going to add global  
  64.                 // %          note: dependencies to php.js. Meaning the constants are not  
  65.                 // %          note: real constants, but strings instead. integers are also supported if someone  
  66.                 // %          note: chooses to create the constants themselves.  
  67.                 // %          note: Table from http://www.the-art-of-web.com/html/character-codes/  
  68.                 // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');  
  69.                 // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}  
  70.  
  71.                 var entities = {},histogram = {},decimal = 0,symbol = '';  
  72.                 var constMappingTable = {},constMappingQuoteStyle = {};  
  73.                 var useTable = {},useQuoteStyle = {};  
  74.  
  75.                 useTable      = (table ? table.toUpperCase() : 'HTML_SPECIALCHARS');  
  76.                 useQuoteStyle = (quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT');  
  77.  
  78.                 // Translate arguments  
  79.                 constMappingTable[0] = 'HTML_SPECIALCHARS';  
  80.                 constMappingTable[1] = 'HTML_ENTITIES';  
  81.                 constMappingQuoteStyle[0] = 'ENT_NOQUOTES';  
  82.                 constMappingQuoteStyle[2] = 'ENT_COMPAT';  
  83.                 constMappingQuoteStyle[3] = 'ENT_QUOTES';  
  84.  
  85.                 // Map numbers to strings for compatibilty with PHP constants  
  86.                 if (! isNaN(useTable))  
  87.                 {  
  88.                         useTable = constMappingTable[useTable];  
  89.                 }  
  90.                 if (! isNaN(useQuoteStyle))  
  91.                 {  
  92.                         useQuoteStyle = constMappingQuoteStyle[useQuoteStyle];  
  93.                 }  
  94.  
  95.                 if (useQuoteStyle != 'ENT_NOQUOTES')  
  96.                 {  
  97.                         entities['34'] = '&quot;';  
  98.                 }  
  99.  
  100.                 if (useQuoteStyle == 'ENT_QUOTES')  
  101.                 {  
  102.                         entities['39'] = ''';  
  103.                 }  
  104.  
  105.                 if (useTable == 'HTML_SPECIALCHARS')  
  106.                 {  
  107.                         // ascii decimals for better compatibility  
  108.                         entities['38'] = '&amp;';  
  109.                         entities['60'] = '&lt;';  
  110.                         entities['62'] = '&gt;';  
  111.                 }  
  112.                 else if (useTable == 'HTML_ENTITIES')  
  113.                 {  
  114.                         // ascii decimals for better compatibility  
  115.                         entities['38'] = '&amp;';  
  116.                         entities['60'] = '&lt;';  
  117.                         entities['62'] = '&gt;';  
  118.                         entities['160'] = '&nbsp;';  
  119.                         entities['161'] = '&iexcl;';  
  120.                         entities['162'] = '&cent;';  
  121.                         entities['163'] = '&pound;';  
  122.                         entities['164'] = '&curren;';  
  123.                         entities['165'] = '&yen;';  
  124.                         entities['166'] = '&brvbar;';  
  125.                         entities['167'] = '&sect;';  
  126.                         entities['168'] = '&uml;';  
  127.                         entities['169'] = '&copy;';  
  128.                         entities['170'] = '&ordf;';  
  129.                         entities['171'] = '&laquo;';  
  130.                         entities['172'] = '&not;';  
  131.                         entities['173'] = '&shy;';  
  132.                         entities['174'] = '&reg;';  
  133.                         entities['175'] = '&macr;';  
  134.                         entities['176'] = '&deg;';  
  135.                         entities['177'] = '&plusmn;';  
  136.                         entities['178'] = '&sup2;';  
  137.                         entities['179'] = '&sup3;';  
  138.                         entities['180'] = '&acute;';  
  139.                         entities['181'] = '&micro;';  
  140.                         entities['182'] = '&para;';  
  141.                         entities['183'] = '&middot;';  
  142.                         entities['184'] = '&cedil;';  
  143.                         entities['185'] = '&sup1;';  
  144.                         entities['186'] = '&ordm;';  
  145.                         entities['187'] = '&raquo;';  
  146.                         entities['188'] = '&frac14;';  
  147.                         entities['189'] = '&frac12;';  
  148.                         entities['190'] = '&frac34;';  
  149.                         entities['191'] = '&iquest;';  
  150.                         entities['192'] = '&Agrave;';  
  151.                         entities['193'] = '&Aacute;';  
  152.                         entities['194'] = '&Acirc;';  
  153.                         entities['195'] = '&Atilde;';  
  154.                         entities['196'] = '&Auml;';  
  155.                         entities['197'] = '&Aring;';  
  156.                         entities['198'] = '&AElig;';  
  157.                         entities['199'] = '&Ccedil;';  
  158.                         entities['200'] = '&Egrave;';  
  159.                         entities['201'] = '&Eacute;';  
  160.                         entities['202'] = '&Ecirc;';  
  161.                         entities['203'] = '&Euml;';  
  162.                         entities['204'] = '&Igrave;';  
  163.                         entities['205'] = '&Iacute;';  
  164.                         entities['206'] = '&Icirc;';  
  165.                         entities['207'] = '&Iuml;';  
  166.                         entities['208'] = '&ETH;';  
  167.                         entities['209'] = '&Ntilde;';  
  168.                         entities['210'] = '&Ograve;';  
  169.                         entities['211'] = '&Oacute;';  
  170.                         entities['212'] = '&Ocirc;';  
  171.                         entities['213'] = '&Otilde;';  
  172.                         entities['214'] = '&Ouml;';  
  173.                         entities['215'] = '&times;';  
  174.                         entities['216'] = '&Oslash;';  
  175.                         entities['217'] = '&Ugrave;';  
  176.                         entities['218'] = '&Uacute;';  
  177.                         entities['219'] = '&Ucirc;';  
  178.                         entities['220'] = '&Uuml;';  
  179.                         entities['221'] = '&Yacute;';  
  180.                         entities['222'] = '&THORN;';  
  181.                         entities['223'] = '&szlig;';  
  182.                         entities['224'] = '&agrave;';  
  183.                         entities['225'] = '&aacute;';  
  184.                         entities['226'] = '&acirc;';  
  185.                         entities['227'] = '&atilde;';  
  186.                         entities['228'] = '&auml;';  
  187.                         entities['229'] = '&aring;';  
  188.                         entities['230'] = '&aelig;';  
  189.                         entities['231'] = '&ccedil;';  
  190.                         entities['232'] = '&egrave;';  
  191.                         entities['233'] = '&eacute;';  
  192.                         entities['234'] = '&ecirc;';  
  193.                         entities['235'] = '&euml;';  
  194.                         entities['236'] = '&igrave;';  
  195.                         entities['237'] = '&iacute;';  
  196.                         entities['238'] = '&icirc;';  
  197.                         entities['239'] = '&iuml;';  
  198.                         entities['240'] = '&eth;';  
  199.                         entities['241'] = '&ntilde;';  
  200.                         entities['242'] = '&ograve;';  
  201.                         entities['243'] = '&oacute;';  
  202.                         entities['244'] = '&ocirc;';  
  203.                         entities['245'] = '&otilde;';  
  204.                         entities['246'] = '&ouml;';  
  205.                         entities['247'] = '&divide;';  
  206.                         entities['248'] = '&oslash;';  
  207.                         entities['249'] = '&ugrave;';  
  208.                         entities['250'] = '&uacute;';  
  209.                         entities['251'] = '&ucirc;';  
  210.                         entities['252'] = '&uuml;';  
  211.                         entities['253'] = '&yacute;';  
  212.                         entities['254'] = '&thorn;';  
  213.                         entities['255'] = '&yuml;';  
  214.                 }  
  215.                 else  
  216.                 {  
  217.                         throw Error("Table: " + useTable + ' not supported');  
  218.                         return false;  
  219.                 }  
  220.  
  221.                 // ascii decimals to real symbols  
  222.                 for (decimal in entities)  
  223.                 {  
  224.                         symbol = String.fromCharCode(decimal);  
  225.                         histogram[symbol] = entities[decimal];  
  226.                 }  
  227.                 return histogram;  
  228.         }  
  229. }  
  230.  
  231. /////////////////////////////////////////////////////  
  232. //时间格式化代码  
  233. /////////////////////////////////////////////////////  
  234.  
  235. class TimeTool {  
  236.         static const JK_MINUTE:int = 60;  
  237.         static const JK_HOUR:int = 3600;  
  238.         static const JK_DAY:int = 86400;  
  239.         static var s_second:String;  
  240.         static var s_minute:String;  
  241.         static var s_hour:String;  
  242.         static var s_day:String;  
  243.         static function timeFormat (second:Number, isShort:Boolean, millisecond:Boolean):String   
  244.         {  
  245.                 //based on PHP by Kele@Juke;  
  246.                 //convert to AS3 by DDK2@Juke;  
  247.                   
  248.                 if (isShort)   
  249.                 {  
  250.                         s_second = " s ";  
  251.                         s_minute = " m ";  
  252.                         s_hour = " h ";  
  253.                         s_day = " d ";  
  254.                 }  
  255.                 else  
  256.                 {  
  257.                         s_second = " seconds ";  
  258.                         s_minute = " minutes ";  
  259.                         s_hour = " hours ";  
  260.                         s_day = " days ";  
  261.                 }  
  262.                           
  263.                 if (millisecond) {  
  264.                         second /= 1000;  
  265.                         secondsecond = second >> 0;  
  266.                 }  
  267.                           
  268.                 if(second < JK_MINUTE){  
  269.                         return String(second) + s_second;  
  270.                 }  
  271.                           
  272.                 if (second < JK_HOUR) {  
  273.                         return String ((second / JK_MINUTE) >> 0) + s_minute;// + String (second % JK_MINUTE) + s_second;  
  274.                 }  
  275.                   
  276.                 if (second < JK_DAY) {  
  277.                         //var over:int = second % JK_HOUR;  
  278.                         return String ((second / JK_HOUR) >> 0) + s_hour;// + String((over/JK_MINUTE) >> 0) + s_minute +String (over % JK_MINUTE) + s_second;  
  279.                 }  
  280.                           
  281.                 //var overDay:int = second % JK_DAY;  
  282.                 //var overHour:int = overDay % JK_HOUR;  
  283.                 return String ((second / JK_DAY) >> 0) + s_day;// + String((overDay / JK_HOUR) >> 0) + s_hour + String ((overHour / JK_MINUTE) >> 0) + s_minute + String (overHour % JK_MINUTE) + s_second;  
  284.         }  

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器