From 76d7735f8de76dfc4fa0ff671e5f18ae025bbcf3 Mon Sep 17 00:00:00 2001 From: "ctrlaltca@gmail.com" <> Date: Sat, 24 Mar 2012 22:52:29 +0000 Subject: Committer 2nd part of patch for #391 --- framework/TComponent.php | 2 ++ framework/Web/Javascripts/TJavaScript.php | 42 ++++++++++++---------- framework/Web/UI/ActiveControls/TAutoComplete.php | 2 +- framework/Web/UI/ActiveControls/TDropContainer.php | 2 +- framework/Web/UI/TClientScriptManager.php | 4 +-- framework/Web/UI/TTemplateManager.php | 17 ++++++--- framework/Web/UI/WebControls/TAccordion.php | 4 +-- framework/Web/UI/WebControls/TDatePicker.php | 6 ++-- framework/Web/UI/WebControls/TSlider.php | 8 ++--- framework/Web/UI/WebControls/TTabPanel.php | 8 ++--- 10 files changed, 55 insertions(+), 40 deletions(-) diff --git a/framework/TComponent.php b/framework/TComponent.php index dbcf4a60..dcc1064c 100644 --- a/framework/TComponent.php +++ b/framework/TComponent.php @@ -590,6 +590,8 @@ class TPropertyValue */ public static function ensureString($value) { + if (TJavaScript::isJsLiteral($value)) + return $value; if (is_bool($value)) return $value?'true':'false'; else diff --git a/framework/Web/Javascripts/TJavaScript.php b/framework/Web/Javascripts/TJavaScript.php index ad120771..9a152a32 100644 --- a/framework/Web/Javascripts/TJavaScript.php +++ b/framework/Web/Javascripts/TJavaScript.php @@ -85,21 +85,37 @@ class TJavaScript * @return Marks a string as a javascript function. Once marke, the string is considered as a * raw javascript function that is not supposed to be encoded by {@link encode} */ - public static function quoteFunction($js) + public static function quoteJsLiteral($js) { - if($js instanceof TJavaScriptFunction) + if($js instanceof TJavaScriptLiteral) return $js; else - return new TJavaScriptFunction($js); + return new TJavaScriptLiteral($js); + } + + /** + * Deprecated, use {@link quoteJsLiteral} instead + */ + public static function quoteFunction($js) + { + return self::quoteJsLiteral($js); } /** * @return boolean true if the parameter is marked as a javascript function, i.e. if it's considered as a * raw javascript function that is not supposed to be encoded by {@link encode} */ + public static function isJsLiteral($js) + { + return ($js instanceof TJavaScriptLiteral); + } + + /** + * Deprecated, use {@link isJsLiteral} instead + */ public static function isFunction($js) { - return ($js instanceof TJavaScriptFunction); + return self::isJsLiteral($js); } /** @@ -117,9 +133,6 @@ class TJavaScript * 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. * @since 3.1.5 @@ -129,16 +142,7 @@ class TJavaScript public static function encode($value,$toMap=true,$encodeEmptyStrings=false) { if(is_string($value)) - { - if(($n=strlen($value))>2) - { - $first=$value[0]; - $last=$value[$n-1]; - if(($first==='[' && $last===']') || ($first==='{' && $last==='}')) - return $value; - } return self::quoteString($value); - } else if(is_bool($value)) return $value?'true':'false'; else if(is_array($value)) @@ -193,7 +197,7 @@ class TJavaScript } } else if(is_object($value)) - if ($value instanceof TJavaScriptFunction) + if ($value instanceof TJavaScriptLiteral) return preg_replace('/^\s*javascript:/', '', $value); else return self::encode(get_object_vars($value),$toMap); @@ -276,11 +280,11 @@ class TJavaScript } /** - * TJavaScriptFunction class that encloses string literals that are not + * TJavaScriptLiteral class that encloses string literals that are not * supposed to be escaped by TJavaScript::encode() * */ -class TJavaScriptFunction +class TJavaScriptLiteral { private $_s; diff --git a/framework/Web/UI/ActiveControls/TAutoComplete.php b/framework/Web/UI/ActiveControls/TAutoComplete.php index cc63deae..2c160578 100644 --- a/framework/Web/UI/ActiveControls/TAutoComplete.php +++ b/framework/Web/UI/ActiveControls/TAutoComplete.php @@ -328,7 +328,7 @@ class TAutoComplete extends TActiveTextBox implements INamingContainer { $string = strtr($string,array('\t'=>"\t",'\n'=>"\n",'\r'=>"\r")); $token = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); - $options['tokens'] = TJavaScript::encode($token,false); + $options['tokens'] = $token; } if($this->getAutoPostBack()) { diff --git a/framework/Web/UI/ActiveControls/TDropContainer.php b/framework/Web/UI/ActiveControls/TDropContainer.php index e6933147..daa8469b 100755 --- a/framework/Web/UI/ActiveControls/TDropContainer.php +++ b/framework/Web/UI/ActiveControls/TDropContainer.php @@ -157,7 +157,7 @@ class TDropContainer extends TPanel implements IActiveControl, ICallbackEventHan $options['ID'] = $this->getClientID(); $options['EventTarget'] = $this->getUniqueID(); - $options['accept'] = TJavascript::encode($this->getAcceptCssClass()); + $options['accept'] = $this->getAcceptCssClass(); $options['hoverclass'] = $this->getHoverCssClass(); return $options; } diff --git a/framework/Web/UI/TClientScriptManager.php b/framework/Web/UI/TClientScriptManager.php index 1804c9d1..3f1664ac 100644 --- a/framework/Web/UI/TClientScriptManager.php +++ b/framework/Web/UI/TClientScriptManager.php @@ -800,8 +800,8 @@ abstract class TClientSideOptions extends TComponent */ protected function setFunction($name, $code) { - if(!TJavaScript::isFunction($code)) - $code = TJavaScript::quoteFunction($this->ensureFunction($code)); + if(!TJavaScript::isJsLiteral($code)) + $code = TJavaScript::quoteJsLiteral($this->ensureFunction($code)); $this->setOption($name, $code); } diff --git a/framework/Web/UI/TTemplateManager.php b/framework/Web/UI/TTemplateManager.php index a5e635da..566f6876 100644 --- a/framework/Web/UI/TTemplateManager.php +++ b/framework/Web/UI/TTemplateManager.php @@ -423,10 +423,17 @@ class TTemplate extends TApplicationComponent implements ITemplate { if(strncasecmp($name,'on',2)===0) // is an event $this->configureEvent($control,$name,$value,$control); - else if(($pos=strrpos($name,'.'))===false) // is a simple property or custom attribute - $this->configureProperty($control,$name,$value); - else // is a subproperty - $this->configureSubProperty($control,$name,$value); + else { + if(strncasecmp($name,'js',2)===0) + { + $name=substr($name,2); + $value=TJavaScript::quoteJsLiteral($value); + } + if(($pos=strrpos($name,'.'))===false) // is a simple property or custom attribute + $this->configureProperty($control,$name,$value); + else // is a subproperty + $this->configureSubProperty($control,$name,$value); + } } /** @@ -937,6 +944,8 @@ class TTemplate extends TApplicationComponent implements ITemplate } else { + if(strncasecmp($name,'js',2)===0) + $name=substr($name, 2); // a simple property if(!$class->hasMethod('set'.$name)) { diff --git a/framework/Web/UI/WebControls/TAccordion.php b/framework/Web/UI/WebControls/TAccordion.php index 60494617..4e1cd325 100644 --- a/framework/Web/UI/WebControls/TAccordion.php +++ b/framework/Web/UI/WebControls/TAccordion.php @@ -489,10 +489,10 @@ class TAccordion extends TWebControl implements IPostBackDataHandler { if($views!='') $views.=', '; - $views.= '"'.$view->getClientID().'":'.($view->getVisible() ? '1': '0' ); + $views.= TJavaScript::encode($view->getClientID()).':'.($view->getVisible() ? '1': '0' ); } - $options['Views']='{'.$views.='}'; + $options['Views']=TJavaScript::quoteJsLiteral('{'.$views.='}'); $viewIDs=array(); return $options; diff --git a/framework/Web/UI/WebControls/TDatePicker.php b/framework/Web/UI/WebControls/TDatePicker.php index 6d2f1427..a0dee3d4 100644 --- a/framework/Web/UI/WebControls/TDatePicker.php +++ b/framework/Web/UI/WebControls/TDatePicker.php @@ -563,9 +563,9 @@ class TDatePicker extends TTextBox return array(); $date = $this->getLocalizedCalendarInfo(); - $options['MonthNames'] = TJavaScript::encode($date->getMonthNames(),false); - $options['AbbreviatedMonthNames'] = TJavaScript::encode($date->getAbbreviatedMonthNames(),false); - $options['ShortWeekDayNames'] = TJavaScript::encode($date->getAbbreviatedDayNames(),false); + $options['MonthNames'] = $date->getMonthNames(); + $options['AbbreviatedMonthNames'] = $date->getAbbreviatedMonthNames(); + $options['ShortWeekDayNames'] = $date->getAbbreviatedDayNames(); return $options; } diff --git a/framework/Web/UI/WebControls/TSlider.php b/framework/Web/UI/WebControls/TSlider.php index f453e3ac..5dc65d47 100644 --- a/framework/Web/UI/WebControls/TSlider.php +++ b/framework/Web/UI/WebControls/TSlider.php @@ -457,7 +457,7 @@ class TSlider extends TWebControl implements IPostBackDataHandler, IDataRenderer $options['axis'] = strtolower($this->getDirection()); $options['maximum'] = $maxValue; $options['minimum'] = $minValue; - $options['range'] = TJavascript::quoteFunction('$R('.$minValue.",".$maxValue.")"); + $options['range'] = TJavascript::quoteJsLiteral('$R('.$minValue.",".$maxValue.")"); $options['sliderValue'] = $this->getValue(); $options['disabled'] = !$this->getEnabled(); $values=$this->getValues(); @@ -488,7 +488,7 @@ class TSlider extends TWebControl implements IPostBackDataHandler, IDataRenderer // Add max if it's not in the array because of step if (!in_array($maxValue, $values)) $values[]=$maxValue; } - $options['values'] = TJavaScript::Encode($values,false); + $options['values'] = $values; if($this->_clientScript!==null) $options = array_merge($options,$this->_clientScript->getOptions()->toArray()); return $options; @@ -520,7 +520,7 @@ class TSliderClientScript extends TClientSideOptions */ public function setOnChange($javascript) { - $code=TJavascript::quoteFunction("function (value) { {$javascript} }"); + $code=TJavascript::quoteJsLiteral("function (value) { {$javascript} }"); $this->setFunction('onChange', $code); } @@ -537,7 +537,7 @@ class TSliderClientScript extends TClientSideOptions */ public function setOnSlide($javascript) { - $code=TJavascript::quoteFunction("function (value) { {$javascript} }"); + $code=TJavascript::quoteJsLiteral("function (value) { {$javascript} }"); $this->setFunction('onSlide', $code); } diff --git a/framework/Web/UI/WebControls/TTabPanel.php b/framework/Web/UI/WebControls/TTabPanel.php index a1ddca39..558ead4e 100644 --- a/framework/Web/UI/WebControls/TTabPanel.php +++ b/framework/Web/UI/WebControls/TTabPanel.php @@ -443,11 +443,11 @@ class TTabPanel extends TWebControl implements IPostBackDataHandler $viewVis=array(); foreach($this->getViews() as $view) { - $viewIDs[]=$view->getClientID(); - $viewVis[]=$view->getVisible(); + $viewIDs[]=TJavaScript::encode($view->getClientID()); + $viewVis[]=TJavaScript::encode($view->getVisible()); } - $options['Views']='[\''.implode('\',\'',$viewIDs).'\']'; - $options['ViewsVis']='[\''.implode('\',\'',$viewVis).'\']'; + $options['Views']=TJavaScript::quoteJsLiteral('['.implode(',',$viewIDs).']'); + $options['ViewsVis']=TJavaScript::quoteJsLiteral('['.implode(',',$viewVis).']'); return $options; } -- cgit v1.2.3