From 1f63d5c05ba117e0158c02d5bc79fa1f38f8ce85 Mon Sep 17 00:00:00 2001 From: xue <> Date: Thu, 27 Apr 2006 13:13:17 +0000 Subject: merge from 3.0 branch till 978. --- framework/Web/Javascripts/TJavaScript.php | 42 ++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'framework/Web/Javascripts/TJavaScript.php') diff --git a/framework/Web/Javascripts/TJavaScript.php b/framework/Web/Javascripts/TJavaScript.php index a3848201..75fc2438 100644 --- a/framework/Web/Javascripts/TJavaScript.php +++ b/framework/Web/Javascripts/TJavaScript.php @@ -84,6 +84,26 @@ class TJavaScript else return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\')); } + + /** + * @return string considers the string as raw javascript function code + */ + public static function quoteFunction($js) + { + if(self::isFunction($js)) + return $js; + else + return 'javascript:'.$js; + } + + /** + * @return boolean true if string is raw javascript function code, i.e., if + * the string begins with javascript: + */ + public static function isFunction($js) + { + return preg_match('/^\s*javascript:/', $js); + } /** * Encodes a PHP variable into javascript representation. @@ -96,16 +116,12 @@ class TJavaScript * //expects the following javascript code * // {'onLoading':'doit','onComplete':'more'} * - * - * To pass raw javascript statements start strings with - * javascript:. E.g. - * - * $options['onLoading'] = "javascript:function(){ alert('hello'); }"; - * //outputs {'onLoading':function(){ alert('hello'); }} - * * - * For higher complexity data structures use {@link jsonEncode} and {@link - * jsonDecode} to serialize and unserialize. + * For higher complexity data structures use {@link jsonEncode} and {@link jsonDecode} + * to serialize and unserialize. + * + * Note: strings begining with javascript: will be considered as + * raw javascript code and no encoding of that string will be enforced. * * @param mixed PHP variable to be encoded * @param boolean whether the output is a map or a list. @@ -122,9 +138,11 @@ class TJavaScript if(($first==='[' && $last===']') || ($first==='{' && $last==='}')) return $value; } - else if(strpos($value, 'javascript:')===0) - return substr($value,11); - return "'".self::quoteString($value)."'"; + // if string begins with javascript: return the raw string minus the prefix + if(self::isFunction($value)) + return preg_replace('/^\s*javascript:/', '', $value); + else + return "'".self::quoteString($value)."'"; } else if(is_bool($value)) return $value?'true':'false'; -- cgit v1.2.3