Skip to content
This repository
Browse code

Merge pull request #2172 from klimov-paul/1724-Allow-script-html-options

Issue #1724: allow script html options.
  • Loading branch information...
commit 0fde5cf9fcd120e9f6ffe5ce28d989c98c8b508d 2 parents 2e4ddc1 + 6737184
Alexander Makarov samdark authored
1  CHANGELOG
@@ -7,6 +7,7 @@ Version 1.1.14 work in progress
7 7 - Bug #150: Fixed CWidget was not switching between view paths when using themes (antoncpu)
8 8 - Bug #1464: Fixed transparent background for ImageMagick in CCaptchaAction (manuel-84, cebe)
9 9 - Bug #1693: Fixed log file will rotate twice when high performance (monque)
  10 +- Bug #1724: Allow CClientScript registering scripts and script files with the HTML options (klimov-paul)
10 11 - Bug #1763: CSqlDataProvider was appending another ORDER BY string to an existing ORDER BY statement when using fieldname with dot (szako)
11 12 - Bug #1915: CDataProviderIterator: fixed init in case of disabled pagination (antoncpu)
12 13 - Bug #1941: yiiactiveform.js form reset now uses CHtml::errorCss instead of a hardcoded value (mdomba)
127 framework/web/CClientScript.php
@@ -66,14 +66,14 @@ class CClientScript extends CApplicationComponent
66 66 * <pre>
67 67 * array(
68 68 * 'package-name'=>array(
69   - * 'basePath'=>'alias of the directory containing the script files',
70   - * 'baseUrl'=>'base URL for the script files',
71   - * 'js'=>array(list of js files relative to basePath/baseUrl),
72   - * 'css'=>array(list of css files relative to basePath/baseUrl),
73   - * 'depends'=>array(list of dependent packages),
74   - * ),
75   - * ......
76   - * )
  69 + * 'basePath'=>'alias of the directory containing the script files',
  70 + * 'baseUrl'=>'base URL for the script files',
  71 + * 'js'=>array(list of js files relative to basePath/baseUrl),
  72 + * 'css'=>array(list of css files relative to basePath/baseUrl),
  73 + * 'depends'=>array(list of dependent packages),
  74 + * ),
  75 + * ......
  76 + * )
77 77 * </pre>
78 78 *
79 79 * The JS and CSS files listed are relative to 'basePath'.
@@ -234,10 +234,10 @@ protected function unifyScripts()
234 234
235 235 if(isset($this->scriptFiles[self::POS_BEGIN]))
236 236 {
237   - foreach($this->scriptFiles[self::POS_BEGIN] as $key=>$scriptFile)
  237 + foreach($this->scriptFiles[self::POS_BEGIN] as $scriptFile=>$scriptFileValue)
238 238 {
239 239 if(isset($map[$scriptFile]))
240   - unset($this->scriptFiles[self::POS_BEGIN][$key]);
  240 + unset($this->scriptFiles[self::POS_BEGIN][$scriptFile]);
241 241 else
242 242 $map[$scriptFile]=true;
243 243 }
@@ -247,7 +247,7 @@ protected function unifyScripts()
247 247 {
248 248 foreach($this->scriptFiles[self::POS_END] as $key=>$scriptFile)
249 249 {
250   - if(isset($map[$scriptFile]))
  250 + if(isset($map[$key]))
251 251 unset($this->scriptFiles[self::POS_END][$key]);
252 252 }
253 253 }
@@ -278,12 +278,12 @@ protected function remapScripts()
278 278 $this->cssFiles=$cssFiles;
279 279
280 280 $jsFiles=array();
281   - foreach($this->scriptFiles as $position=>$scripts)
  281 + foreach($this->scriptFiles as $position=>$scriptFiles)
282 282 {
283 283 $jsFiles[$position]=array();
284   - foreach($scripts as $key=>$script)
  284 + foreach($scriptFiles as $scriptFile=>$scriptFileValue)
285 285 {
286   - $name=basename($script);
  286 + $name=basename($scriptFileValue);
287 287 if(isset($this->scriptMap[$name]))
288 288 {
289 289 if($this->scriptMap[$name]!==false)
@@ -295,13 +295,46 @@ protected function remapScripts()
295 295 $jsFiles[$position][$this->scriptMap['*.js']]=$this->scriptMap['*.js'];
296 296 }
297 297 else
298   - $jsFiles[$position][$key]=$script;
  298 + $jsFiles[$position][$scriptFile]=$scriptFileValue;
299 299 }
300 300 }
301 301 $this->scriptFiles=$jsFiles;
302 302 }
303 303
304 304 /**
  305 + * Composes script HTML block from the given script values,
  306 + * attempting to group scripts at single 'script' tag if possible.
  307 + * @param array $scripts script values to process.
  308 + * @return string HTML output
  309 + */
  310 + protected function renderScriptBatch(array $scripts)
  311 + {
  312 + $html = '';
  313 + $scriptBatches = array();
  314 + foreach($scripts as $scriptValue)
  315 + {
  316 + if(is_array($scriptValue))
  317 + {
  318 + $scriptContent = $scriptValue['content'];
  319 + unset($scriptValue['content']);
  320 + $scriptHtmlOptions = $scriptValue;
  321 + }
  322 + else
  323 + {
  324 + $scriptContent = $scriptValue;
  325 + $scriptHtmlOptions = array();
  326 + }
  327 + $key=serialize(ksort($scriptHtmlOptions));
  328 + $scriptBatches[$key]['htmlOptions']=$scriptHtmlOptions;
  329 + $scriptBatches[$key]['scripts'][]=$scriptContent;
  330 + }
  331 + foreach($scriptBatches as $scriptBatch)
  332 + if(!empty($scriptBatch['scripts']))
  333 + $html.=CHtml::script(implode("\n",$scriptBatch['scripts']),$scriptBatch['htmlOptions'])."\n";
  334 + return $html;
  335 + }
  336 +
  337 + /**
305 338 * Renders the specified core javascript library.
306 339 */
307 340 public function renderCoreScripts()
@@ -335,8 +368,8 @@ public function renderCoreScripts()
335 368 {
336 369 if(isset($this->scriptFiles[$this->coreScriptPosition]))
337 370 {
338   - foreach($this->scriptFiles[$this->coreScriptPosition] as $url)
339   - $jsFiles[$url]=$url;
  371 + foreach($this->scriptFiles[$this->coreScriptPosition] as $url => $value)
  372 + $jsFiles[$url]=$value;
340 373 }
341 374 $this->scriptFiles[$this->coreScriptPosition]=$jsFiles;
342 375 }
@@ -361,12 +394,17 @@ public function renderHead(&$output)
361 394 {
362 395 if(isset($this->scriptFiles[self::POS_HEAD]))
363 396 {
364   - foreach($this->scriptFiles[self::POS_HEAD] as $scriptFile)
365   - $html.=CHtml::scriptFile($scriptFile)."\n";
  397 + foreach($this->scriptFiles[self::POS_HEAD] as $scriptFileValueUrl=>$scriptFileValue)
  398 + {
  399 + if(is_array($scriptFileValue))
  400 + $html.=CHtml::scriptFile($scriptFileValueUrl,$scriptFileValue)."\n";
  401 + else
  402 + $html.=CHtml::scriptFile($scriptFileValueUrl)."\n";
  403 + }
366 404 }
367 405
368 406 if(isset($this->scripts[self::POS_HEAD]))
369   - $html.=CHtml::script(implode("\n",$this->scripts[self::POS_HEAD]))."\n";
  407 + $html.=$this->renderScriptBatch($this->scripts[self::POS_HEAD]);
370 408 }
371 409
372 410 if($html!=='')
@@ -389,11 +427,16 @@ public function renderBodyBegin(&$output)
389 427 $html='';
390 428 if(isset($this->scriptFiles[self::POS_BEGIN]))
391 429 {
392   - foreach($this->scriptFiles[self::POS_BEGIN] as $scriptFile)
393   - $html.=CHtml::scriptFile($scriptFile)."\n";
  430 + foreach($this->scriptFiles[self::POS_BEGIN] as $scriptFileUrl=>$scriptFileValue)
  431 + {
  432 + if(is_array($scriptFileValue))
  433 + $html.=CHtml::scriptFile($scriptFileUrl,$scriptFileValue)."\n";
  434 + else
  435 + $html.=CHtml::scriptFile($scriptFileUrl)."\n";
  436 + }
394 437 }
395 438 if(isset($this->scripts[self::POS_BEGIN]))
396   - $html.=CHtml::script(implode("\n",$this->scripts[self::POS_BEGIN]))."\n";
  439 + $html.=$this->renderScriptBatch($this->scripts[self::POS_BEGIN]);
397 440
398 441 if($html!=='')
399 442 {
@@ -421,8 +464,13 @@ public function renderBodyEnd(&$output)
421 464 $html='';
422 465 if(isset($this->scriptFiles[self::POS_END]))
423 466 {
424   - foreach($this->scriptFiles[self::POS_END] as $scriptFile)
425   - $html.=CHtml::scriptFile($scriptFile)."\n";
  467 + foreach($this->scriptFiles[self::POS_END] as $scriptFileUrl=>$scriptFileValue)
  468 + {
  469 + if(is_array($scriptFileValue))
  470 + $html.=CHtml::scriptFile($scriptFileUrl,$scriptFileValue)."\n";
  471 + else
  472 + $html.=CHtml::scriptFile($scriptFileUrl)."\n";
  473 + }
426 474 }
427 475 $scripts=isset($this->scripts[self::POS_END]) ? $this->scripts[self::POS_END] : array();
428 476 if(isset($this->scripts[self::POS_READY]))
@@ -440,7 +488,7 @@ public function renderBodyEnd(&$output)
440 488 $scripts[]=implode("\n",$this->scripts[self::POS_LOAD]);
441 489 }
442 490 if(!empty($scripts))
443   - $html.=CHtml::script(implode("\n",$scripts))."\n";
  491 + $html.=$this->renderScriptBatch($scripts);
444 492
445 493 if($fullPage)
446 494 $output=str_replace('<###end###>',$html,$output);
@@ -588,14 +636,22 @@ public function registerCss($id,$css,$media='')
588 636 * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
589 637 * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
590 638 * </ul>
  639 + * @param array $htmlOptions additional HTML attributes
591 640 * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
592 641 */
593   - public function registerScriptFile($url,$position=null)
  642 + public function registerScriptFile($url,$position=null,array $htmlOptions=array())
594 643 {
595 644 if($position===null)
596 645 $position=$this->defaultScriptFilePosition;
597 646 $this->hasScripts=true;
598   - $this->scriptFiles[$position][$url]=$url;
  647 + if(empty($htmlOptions))
  648 + $value=$url;
  649 + else
  650 + {
  651 + $value=$htmlOptions;
  652 + $value['src']=$url;
  653 + }
  654 + $this->scriptFiles[$position][$url]=$value;
599 655 $params=func_get_args();
600 656 $this->recordCachingAction('clientScript','registerScriptFile',$params);
601 657 return $this;
@@ -613,14 +669,25 @@ public function registerScriptFile($url,$position=null)
613 669 * <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
614 670 * <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
615 671 * </ul>
  672 + * @param array $htmlOptions additional HTML attributes
  673 + * Note: HTML attributes are not allowed for script positions "CClientScript::POS_LOAD" and "CClientScript::POS_READY".
616 674 * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
617 675 */
618   - public function registerScript($id,$script,$position=null)
  676 + public function registerScript($id,$script,$position=null,array $htmlOptions=array())
619 677 {
620 678 if($position===null)
621 679 $position=$this->defaultScriptPosition;
622 680 $this->hasScripts=true;
623   - $this->scripts[$position][$id]=$script;
  681 + if(empty($htmlOptions))
  682 + $scriptValue=$script;
  683 + else
  684 + {
  685 + if($position==self::POS_LOAD || $position==self::POS_READY)
  686 + throw new CException(Yii::t('yii','Script HTML options are not allowed for "CClientScript::POS_LOAD" and "CClientScript::POS_READY".'));
  687 + $scriptValue=$htmlOptions;
  688 + $scriptValue['content']=$script;
  689 + }
  690 + $this->scripts[$position][$id]=$scriptValue;
624 691 if($position===self::POS_READY || $position===self::POS_LOAD)
625 692 $this->registerCoreScript('jquery');
626 693 $params=func_get_args();
19 framework/web/helpers/CHtml.php
@@ -276,21 +276,32 @@ public static function cssFile($url,$media='')
276 276 /**
277 277 * Encloses the given JavaScript within a script tag.
278 278 * @param string $text the JavaScript to be enclosed
  279 + * @param array $htmlOptions additional HTML attributes (see {@link tag})
279 280 * @return string the enclosed JavaScript
280 281 */
281   - public static function script($text)
  282 + public static function script($text,array $htmlOptions=array())
282 283 {
283   - return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n</script>";
  284 + $defaultHtmlOptions=array(
  285 + 'type'=>'text/javascript',
  286 + );
  287 + $htmlOptions=array_merge($defaultHtmlOptions,$htmlOptions);
  288 + return self::tag('script',$htmlOptions,"\n/*<![CDATA[*/\n{$text}\n/*]]>*/\n");
284 289 }
285 290
286 291 /**
287 292 * Includes a JavaScript file.
288 293 * @param string $url URL for the JavaScript file
  294 + * @param array $htmlOptions additional HTML attributes (see {@link tag})
289 295 * @return string the JavaScript file tag
290 296 */
291   - public static function scriptFile($url)
  297 + public static function scriptFile($url,array $htmlOptions=array())
292 298 {
293   - return '<script type="text/javascript" src="'.self::encode($url).'"></script>';
  299 + $defaultHtmlOptions=array(
  300 + 'type'=>'text/javascript',
  301 + 'src'=>$url
  302 + );
  303 + $htmlOptions=array_merge($defaultHtmlOptions,$htmlOptions);
  304 + return self::tag('script',$htmlOptions,'');
294 305 }
295 306
296 307 /**
246 tests/framework/web/CClientScriptTest.php
@@ -81,6 +81,110 @@ public function testRegisterPackage($name, $assertion)
81 81 $returnedClientScript = $this->_clientScript->registerPackage($name);
82 82 $this->assertEquals($assertion, $returnedClientScript->corePackages[$name]);
83 83 }
  84 +
  85 + public function providerScriptFiles()
  86 + {
  87 + return array(
  88 + array('/some/script.js', CClientScript::POS_HEAD, '/some/script.js'),
  89 + array('http://some/script.js', CClientScript::POS_BEGIN, 'http://some/script.js'),
  90 + array('/some/script.js', CClientScript::POS_END, '/some/script.js'),
  91 + );
  92 + }
  93 +
  94 + /**
  95 + * @dataProvider providerScriptFiles
  96 + *
  97 + * @param string $url
  98 + * @param integer $position
  99 + * @param string $assertion
  100 + */
  101 + public function testRegisterScriptFile($url, $position, $assertion)
  102 + {
  103 + $returnedClientScript = $this->_clientScript->registerScriptFile($url, $position);
  104 + $scriptFiles = $this->readAttribute($returnedClientScript, 'scriptFiles');
  105 + $this->assertEquals($assertion, $scriptFiles[$position][$url]);
  106 + }
  107 +
  108 + public function providerScriptFilesWithHtmlOptions()
  109 + {
  110 + return array(
  111 + array(
  112 + '/some/script.js',
  113 + CClientScript::POS_HEAD,
  114 + array('defer'=>true),
  115 + array(
  116 + 'src'=>'/some/script.js',
  117 + 'defer'=>true
  118 + )
  119 + ),
  120 + );
  121 + }
  122 +
  123 + /**
  124 + * @dataProvider providerScriptFilesWithHtmlOptions
  125 + *
  126 + * @param string $url
  127 + * @param integer $position
  128 + * @param array $htmlOptions
  129 + * @param string $assertion
  130 + */
  131 + public function testRegisterScriptFileWithHtmlOptions($url, $position, $htmlOptions, $assertion)
  132 + {
  133 + $returnedClientScript = $this->_clientScript->registerScriptFile($url, $position, $htmlOptions);
  134 + $scriptFiles = $this->readAttribute($returnedClientScript, 'scriptFiles');
  135 + $this->assertEquals($assertion, $scriptFiles[$position][$url]);
  136 + }
  137 +
  138 + public function providerScripts()
  139 + {
  140 + return array(
  141 + array('jsId', "function() {alert('alert')}", CClientScript::POS_HEAD, "function() {alert('alert')}"),
  142 + array('jsId', "function() {alert('alert')}", CClientScript::POS_BEGIN, "function() {alert('alert')}"),
  143 + );
  144 + }
  145 +
  146 + /**
  147 + * @dataProvider providerScripts
  148 + *
  149 + * @param string $id
  150 + * @param string $script
  151 + * @param integer $position
  152 + * @param string $assertion
  153 + */
  154 + public function testRegisterScript($id, $script, $position, $assertion) {
  155 + $returnedClientScript = $this->_clientScript->registerScript($id, $script, $position);
  156 + $this->assertEquals($assertion, $returnedClientScript->scripts[$position][$id]);
  157 + }
  158 +
  159 + public function providerScriptsWithHtmlOptions()
  160 + {
  161 + return array(
  162 + array(
  163 + 'jsId',
  164 + "function() {alert('alert')}",
  165 + CClientScript::POS_HEAD,
  166 + array('defer'=>true),
  167 + array(
  168 + 'content'=>"function() {alert('alert')}",
  169 + 'defer'=>true,
  170 + )
  171 + ),
  172 + );
  173 + }
  174 +
  175 + /**
  176 + * @dataProvider providerScriptsWithHtmlOptions
  177 + *
  178 + * @param string $id
  179 + * @param string $script
  180 + * @param integer $position
  181 + * @param array $htmlOptions
  182 + * @param string $assertion
  183 + */
  184 + public function testRegisterScriptWithHtmlOptions($id, $script, $position, $htmlOptions, $assertion) {
  185 + $returnedClientScript = $this->_clientScript->registerScript($id, $script, $position, $htmlOptions);
  186 + $this->assertEquals($assertion, $returnedClientScript->scripts[$position][$id]);
  187 + }
84 188
85 189 public function providerRegisterCss()
86 190 {
@@ -170,4 +274,146 @@ public function testRegisterDuplicatingMetaTag() {
170 274
171 275 /* Test Script Renderers */
172 276
  277 + public function providerRenderScriptFiles()
  278 + {
  279 + return array(
  280 + array(
  281 + '/some/script.js',
  282 + CClientScript::POS_HEAD,
  283 + array(),
  284 + '<script type="text/javascript" src="/some/script.js"></script>'
  285 + ),
  286 + array(
  287 + '/some/script.js',
  288 + CClientScript::POS_BEGIN,
  289 + array(),
  290 + '<script type="text/javascript" src="/some/script.js"></script>'
  291 + ),
  292 + array(
  293 + '/some/script.js',
  294 + CClientScript::POS_END,
  295 + array(),
  296 + '<script type="text/javascript" src="/some/script.js"></script>'
  297 + ),
  298 + array(
  299 + '/options/script.js',
  300 + CClientScript::POS_HEAD,
  301 + array('defer'=>true),
  302 + '<script type="text/javascript" src="/options/script.js" defer="defer"></script>'
  303 + ),
  304 + array(
  305 + '/options/script.js',
  306 + CClientScript::POS_BEGIN,
  307 + array('defer'=>true),
  308 + '<script type="text/javascript" src="/options/script.js" defer="defer"></script>'
  309 + ),
  310 + array(
  311 + '/options/script.js',
  312 + CClientScript::POS_END,
  313 + array('defer'=>true),
  314 + '<script type="text/javascript" src="/options/script.js" defer="defer"></script>'
  315 + ),
  316 + );
  317 + }
  318 +
  319 + /**
  320 + * @depends testRegisterScriptFile
  321 + * @depends testRegisterScriptFileWithHtmlOptions
  322 + *
  323 + * @dataProvider providerRenderScriptFiles
  324 + *
  325 + * @param string $url
  326 + * @param integer $position
  327 + * @param array $htmlOptions
  328 + * @param string $assertion
  329 + */
  330 + public function testRenderScriptFiles($url, $position, $htmlOptions, $assertion)
  331 + {
  332 + $returnedClientScript = $this->_clientScript->registerScriptFile($url, $position, $htmlOptions);
  333 + $output = '<head></head>';
  334 + $returnedClientScript->render($output);
  335 + $this->assertContains($assertion, $output);
  336 + }
  337 +
  338 + public function providerRenderScripts()
  339 + {
  340 + return array(
  341 + array(
  342 + 'some_js_id',
  343 + "function() {alert('script')}",
  344 + CClientScript::POS_HEAD,
  345 + array(),
  346 + CHtml::script("function() {alert('script')}")
  347 + ),
  348 + array(
  349 + 'some_js_id',
  350 + "function() {alert('script')}",
  351 + CClientScript::POS_BEGIN,
  352 + array(),
  353 + CHtml::script("function() {alert('script')}")
  354 + ),
  355 + array(
  356 + 'some_js_id',
  357 + "function() {alert('script')}",
  358 + CClientScript::POS_END,
  359 + array(),
  360 + CHtml::script("function() {alert('script')}")
  361 + ),
  362 + array(
  363 + 'some_js_id',
  364 + "function() {alert('script')}",
  365 + CClientScript::POS_LOAD,
  366 + array(),
  367 + CHtml::script("function() {alert('script')}")
  368 + ),
  369 + array(
  370 + 'some_js_id',
  371 + "function() {alert('script')}",
  372 + CClientScript::POS_READY,
  373 + array(),
  374 + CHtml::script("function() {alert('script')}")
  375 + ),
  376 + // With HTML options
  377 + array(
  378 + 'option_js_id',
  379 + "function() {alert('script')}",
  380 + CClientScript::POS_HEAD,
  381 + array('defer'=>true),
  382 + CHtml::script("function() {alert('script')}",array('defer'=>true))
  383 + ),
  384 + array(
  385 + 'option_js_id',
  386 + "function() {alert('script')}",
  387 + CClientScript::POS_BEGIN,
  388 + array('defer'=>true),
  389 + CHtml::script("function() {alert('script')}",array('defer'=>true))
  390 + ),
  391 + array(
  392 + 'option_js_id',
  393 + "function() {alert('script')}",
  394 + CClientScript::POS_END,
  395 + array('defer'=>true),
  396 + CHtml::script("function() {alert('script')}",array('defer'=>true))
  397 + ),
  398 + );
  399 + }
  400 +
  401 + /**
  402 + * @depends testRegisterScript
  403 + *
  404 + * @dataProvider providerRenderScripts
  405 + *
  406 + * @param string $id
  407 + * @param string $script
  408 + * @param integer $position
  409 + * @param array $htmlOptions
  410 + * @param string $assertion
  411 + */
  412 + public function testRenderScripts($id, $script, $position, $htmlOptions, $assertion)
  413 + {
  414 + $returnedClientScript = $this->_clientScript->registerScript($id, $script, $position, $htmlOptions);
  415 + $output = '<head></head>';
  416 + $returnedClientScript->render($output);
  417 + $this->assertContains($assertion, $output);
  418 + }
173 419 }
343 tests/framework/web/helpers/CHtmlTest.php
@@ -20,129 +20,129 @@ public function tearDown()
20 20 Yii::app()->clientScript->reset();
21 21 }
22 22
23   - /* HTML characters encode/decode tests */
  23 + /* HTML characters encode/decode tests */
24 24
25   - public static function providerEncodeArray()
26   - {
27   - return array(
28   - array( array('lessThanExpression'=>'4 < 9'), array('lessThanExpression'=>'4 &lt; 9') ),
29   - array( array(array('lessThanExpression'=>'4 < 9')), array(array('lessThanExpression'=>'4 &lt; 9')) ),
30   - array( array(array('lessThanExpression'=>'4 < 9'), 'greaterThanExpression'=>'4 > 9'), array(array('lessThanExpression'=>'4 &lt; 9'), 'greaterThanExpression'=>'4 &gt; 9') )
31   - );
32   - }
  25 + public static function providerEncodeArray()
  26 + {
  27 + return array(
  28 + array( array('lessThanExpression'=>'4 < 9'), array('lessThanExpression'=>'4 &lt; 9') ),
  29 + array( array(array('lessThanExpression'=>'4 < 9')), array(array('lessThanExpression'=>'4 &lt; 9')) ),
  30 + array( array(array('lessThanExpression'=>'4 < 9'), 'greaterThanExpression'=>'4 > 9'), array(array('lessThanExpression'=>'4 &lt; 9'), 'greaterThanExpression'=>'4 &gt; 9') )
  31 + );
  32 + }
33 33
34   - /**
35   - * @dataProvider providerEncodeArray
36   - *
37   - * @param type $data
38   - * @param type $assertion
39   - */
40   - public function testEncodeArray($data, $assertion)
41   - {
42   - $this->assertEquals($assertion, CHtml::encodeArray($data));
43   - }
44   -
45   - /* Javascript generator tests */
46   -
47   - public static function providerAjax()
48   - {
49   - return array(
50   - array(array("url" => "index"), "jQuery.ajax({'url':'index','cache':false});"),
51   - array(array("url" => "index", "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'url':'index','success':function() { this.alert(\"HI\"); },'cache':false});"),
52   - array(array("async" => true, "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'async':true,'success':function() { this.alert(\"HI\"); },'url':location.href,'cache':false});"),
53   - array(array("update" =>"#my-div", "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'success':function() { this.alert(\"HI\"); },'url':location.href,'cache':false});"),
54   - array(array("update" =>"#my-div"), "jQuery.ajax({'url':location.href,'cache':false,'success':function(html){jQuery(\"#my-div\").html(html)}});"),
55   - array(array("replace" =>"#my-div", "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'success':function() { this.alert(\"HI\"); },'url':location.href,'cache':false});"),
56   - array(array("replace" =>"#my-div"), "jQuery.ajax({'url':location.href,'cache':false,'success':function(html){jQuery(\"#my-div\").replaceWith(html)}});")
57   - );
58   - }
  34 + /**
  35 + * @dataProvider providerEncodeArray
  36 + *
  37 + * @param type $data
  38 + * @param type $assertion
  39 + */
  40 + public function testEncodeArray($data, $assertion)
  41 + {
  42 + $this->assertEquals($assertion, CHtml::encodeArray($data));
  43 + }
59 44
60   - /**
61   - * @dataProvider providerAjax
62   - *
63   - * @param type $options
64   - * @param type $assertion
65   - */
66   - public function testAjax($options, $assertion)
67   - {
68   - $this->assertEquals($assertion, CHtml::ajax($options));
69   - }
70   -
71   - /* DOM element generated from model attribute tests */
72   -
73   - public static function providerActiveDOMElements()
74   - {
75   - return array(
76   - array(new CHtmlTestModel(array('attr1'=>true)), 'attr1', array(), '<input id="ytCHtmlTestModel_attr1" type="hidden" value="0" name="CHtmlTestModel[attr1]" /><input name="CHtmlTestModel[attr1]" id="CHtmlTestModel_attr1" value="1" type="checkbox" />'),
77   - array(new CHtmlTestModel(array('attr1'=>false)), 'attr1', array(), '<input id="ytCHtmlTestModel_attr1" type="hidden" value="0" name="CHtmlTestModel[attr1]" /><input name="CHtmlTestModel[attr1]" id="CHtmlTestModel_attr1" value="1" type="checkbox" />')
78   - );
79   - }
  45 + /* Javascript generator tests */
80 46
81   - /**
82   - * @dataProvider providerActiveDOMElements
83   - *
84   - * @param string $action
85   - * @param string $method
86   - * @param array $htmlOptions
87   - * @param string $assertion
88   - */
89   - public function testActiveCheckbox($model,$attribute,$htmlOptions, $assertion)
90   - {
91   - $this->assertEquals($assertion, CHtml::activeCheckBox($model,$attribute,$htmlOptions));
92   - }
93   -
94   - /* Static DOM element generator tests */
95   -
96   - public static function providerBeginForm()
97   - {
98   - return array(
99   - array("index", "get", array(), '<form action="index" method="get">'),
100   - array("index", "post", array(), '<form action="index" method="post">'),
101   - array("index?myFirstParam=3&mySecondParam=true", "get", array(),
  47 + public static function providerAjax()
  48 + {
  49 + return array(
  50 + array(array("url" => "index"), "jQuery.ajax({'url':'index','cache':false});"),
  51 + array(array("url" => "index", "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'url':'index','success':function() { this.alert(\"HI\"); },'cache':false});"),
  52 + array(array("async" => true, "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'async':true,'success':function() { this.alert(\"HI\"); },'url':location.href,'cache':false});"),
  53 + array(array("update" =>"#my-div", "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'success':function() { this.alert(\"HI\"); },'url':location.href,'cache':false});"),
  54 + array(array("update" =>"#my-div"), "jQuery.ajax({'url':location.href,'cache':false,'success':function(html){jQuery(\"#my-div\").html(html)}});"),
  55 + array(array("replace" =>"#my-div", "success" => "function() { this.alert(\"HI\"); }"), "jQuery.ajax({'success':function() { this.alert(\"HI\"); },'url':location.href,'cache':false});"),
  56 + array(array("replace" =>"#my-div"), "jQuery.ajax({'url':location.href,'cache':false,'success':function(html){jQuery(\"#my-div\").replaceWith(html)}});")
  57 + );
  58 + }
  59 +
  60 + /**
  61 + * @dataProvider providerAjax
  62 + *
  63 + * @param type $options
  64 + * @param type $assertion
  65 + */
  66 + public function testAjax($options, $assertion)
  67 + {
  68 + $this->assertEquals($assertion, CHtml::ajax($options));
  69 + }
  70 +
  71 + /* DOM element generated from model attribute tests */
  72 +
  73 + public static function providerActiveDOMElements()
  74 + {
  75 + return array(
  76 + array(new CHtmlTestModel(array('attr1'=>true)), 'attr1', array(), '<input id="ytCHtmlTestModel_attr1" type="hidden" value="0" name="CHtmlTestModel[attr1]" /><input name="CHtmlTestModel[attr1]" id="CHtmlTestModel_attr1" value="1" type="checkbox" />'),
  77 + array(new CHtmlTestModel(array('attr1'=>false)), 'attr1', array(), '<input id="ytCHtmlTestModel_attr1" type="hidden" value="0" name="CHtmlTestModel[attr1]" /><input name="CHtmlTestModel[attr1]" id="CHtmlTestModel_attr1" value="1" type="checkbox" />')
  78 + );
  79 + }
  80 +
  81 + /**
  82 + * @dataProvider providerActiveDOMElements
  83 + *
  84 + * @param string $action
  85 + * @param string $method
  86 + * @param array $htmlOptions
  87 + * @param string $assertion
  88 + */
  89 + public function testActiveCheckbox($model,$attribute,$htmlOptions, $assertion)
  90 + {
  91 + $this->assertEquals($assertion, CHtml::activeCheckBox($model,$attribute,$htmlOptions));
  92 + }
  93 +
  94 + /* Static DOM element generator tests */
  95 +
  96 + public static function providerBeginForm()
  97 + {
  98 + return array(
  99 + array("index", "get", array(), '<form action="index" method="get">'),
  100 + array("index", "post", array(), '<form action="index" method="post">'),
  101 + array("index?myFirstParam=3&mySecondParam=true", "get", array(),
102 102 "<form action=\"index?myFirstParam=3&amp;mySecondParam=true\" method=\"get\">\n".
103 103 "<div style=\"display:none\"><input type=\"hidden\" value=\"3\" name=\"myFirstParam\" />\n".
104 104 "<input type=\"hidden\" value=\"true\" name=\"mySecondParam\" /></div>"),
105 105
106   - );
107   - }
  106 + );
  107 + }
108 108
109   - /**
110   - * @dataProvider providerBeginForm
111   - *
112   - * @param string $action
113   - * @param string $method
114   - * @param array $htmlOptions
115   - * @param string $assertion
116   - */
117   - public function testBeginForm($action, $method, $htmlOptions, $assertion)
118   - {
119   - /* TODO - Steven Wexler - 3/5/11 - Mock out static methods in this function when CHtml leverages late static method binding
120   - * because PHPUnit. This is only possible Yii supports only >= PHP 5.3 - */
121   - $this->assertEquals($assertion, CHtml::beginForm($action, $method, $htmlOptions));
  109 + /**
  110 + * @dataProvider providerBeginForm
  111 + *
  112 + * @param string $action
  113 + * @param string $method
  114 + * @param array $htmlOptions
  115 + * @param string $assertion
  116 + */
  117 + public function testBeginForm($action, $method, $htmlOptions, $assertion)
  118 + {
  119 + /* TODO - Steven Wexler - 3/5/11 - Mock out static methods in this function when CHtml leverages late static method binding
  120 + * because PHPUnit. This is only possible Yii supports only >= PHP 5.3 - */
  121 + $this->assertEquals($assertion, CHtml::beginForm($action, $method, $htmlOptions));
122 122 $this->assertEquals($assertion, CHtml::form($action, $method, $htmlOptions));
123   - }
  123 + }
124 124
125   - public static function providerTextArea()
126   - {
127   - return array(
128   - array("textareaone", '', array(), "<textarea name=\"textareaone\" id=\"textareaone\"></textarea>"),
129   - array("textareaone", '', array("id"=>"MyAwesomeTextArea", "dog"=>"Lassie", "class"=>"colorful bright"), "<textarea id=\"MyAwesomeTextArea\" dog=\"Lassie\" class=\"colorful bright\" name=\"textareaone\"></textarea>"),
130   - array("textareaone", '', array("id"=>false), "<textarea name=\"textareaone\"></textarea>"),
131   - );
132   - }
  125 + public static function providerTextArea()
  126 + {
  127 + return array(
  128 + array("textareaone", '', array(), "<textarea name=\"textareaone\" id=\"textareaone\"></textarea>"),
  129 + array("textareaone", '', array("id"=>"MyAwesomeTextArea", "dog"=>"Lassie", "class"=>"colorful bright"), "<textarea id=\"MyAwesomeTextArea\" dog=\"Lassie\" class=\"colorful bright\" name=\"textareaone\"></textarea>"),
  130 + array("textareaone", '', array("id"=>false), "<textarea name=\"textareaone\"></textarea>"),
  131 + );
  132 + }
133 133
134   - /**
135   - * @dataProvider providerTextArea
136   - *
137   - * @param string $name
138   - * @param string $value
139   - * @param array $htmlOptions
140   - * @param string $assertion
141   - */
142   - public function testTextArea($name, $value, $htmlOptions, $assertion)
143   - {
144   - $this->assertEquals($assertion, CHtml::textArea($name, $value, $htmlOptions));
145   - }
  134 + /**
  135 + * @dataProvider providerTextArea
  136 + *
  137 + * @param string $name
  138 + * @param string $value
  139 + * @param array $htmlOptions
  140 + * @param string $assertion
  141 + */
  142 + public function testTextArea($name, $value, $htmlOptions, $assertion)
  143 + {
  144 + $this->assertEquals($assertion, CHtml::textArea($name, $value, $htmlOptions));
  145 + }
146 146
147 147 public function providerOpenTag()
148 148 {
@@ -329,6 +329,35 @@ public function testScript($text, $assertion)
329 329 $this->assertEquals($assertion, CHtml::script($text));
330 330 }
331 331
  332 + public static function providerScriptWithHtmlOptions()
  333 + {
  334 + return array(
  335 + array(
  336 + 'var a = 10;',
  337 + array('defer'=>true),
  338 + "<script type=\"text/javascript\" defer=\"defer\">\n/*<![CDATA[*/\nvar a = 10;\n/*]]>*/\n</script>"
  339 + ),
  340 + array(
  341 + 'var a = 10;',
  342 + array('async'=>true),
  343 + "<script type=\"text/javascript\" async=\"async\">\n/*<![CDATA[*/\nvar a = 10;\n/*]]>*/\n</script>"
  344 + ),
  345 + );
  346 + }
  347 +
  348 + /**
  349 + * @depends testScript
  350 + * @dataProvider providerScriptWithHtmlOptions
  351 + *
  352 + * @param string $text
  353 + * @param array $htmlOptions
  354 + * @param string $assertion
  355 + */
  356 + public function testScriptWithHtmlOptions($text, $htmlOptions, $assertion)
  357 + {
  358 + $this->assertEquals($assertion, CHtml::script($text,$htmlOptions));
  359 + }
  360 +
332 361 public static function providerScriptFile()
333 362 {
334 363 return array(
@@ -349,6 +378,40 @@ public function testScriptFile($text, $assertion)
349 378 $this->assertEquals($assertion, CHtml::scriptFile($text));
350 379 }
351 380
  381 + public static function providerScriptFileWithHtmlOptions()
  382 + {
  383 + return array(
  384 + array(
  385 + '/js/main.js?a=2&b=4',
  386 + array('defer'=>true),
  387 + '<script type="text/javascript" src="/js/main.js?a=2&amp;b=4" defer="defer"></script>'
  388 + ),
  389 + array(
  390 + '/js/main.js?a=2&b=4',
  391 + array('async'=>true),
  392 + '<script type="text/javascript" src="/js/main.js?a=2&amp;b=4" async="async"></script>'
  393 + ),
  394 + array(
  395 + '/js/main.js?a=2&b=4',
  396 + array('onload'=>"some_js_function();"),
  397 + '<script type="text/javascript" src="/js/main.js?a=2&amp;b=4" onload="some_js_function();"></script>'
  398 + ),
  399 + );
  400 + }
  401 +
  402 + /**
  403 + * @depends testScriptFile
  404 + * @dataProvider providerScriptFileWithHtmlOptions
  405 + *
  406 + * @param string $text
  407 + * @param array $htmlOptions
  408 + * @param string $assertion
  409 + */
  410 + public function testScriptFileWithHtmlOptions($text, $htmlOptions, $assertion)
  411 + {
  412 + $this->assertEquals($assertion, CHtml::scriptFile($text, $htmlOptions));
  413 + }
  414 +
352 415 public function testEndForm()
353 416 {
354 417 $this->assertEquals('</form>', CHtml::endForm());
@@ -998,39 +1061,39 @@ public function testAjaxCallbacks()
998 1061
999 1062 class CHtmlTestModel extends CModel
1000 1063 {
1001   - private static $_names=array();
  1064 + private static $_names=array();
1002 1065
1003   - /**
1004   - * @property mixed $attr1
1005   - */
1006   - public $attr1;
  1066 + /**
  1067 + * @property mixed $attr1
  1068 + */
  1069 + public $attr1;
1007 1070
1008   - /**
1009   - * @property mixed $attr2
1010   - */
1011   - public $attr2;
  1071 + /**
  1072 + * @property mixed $attr2
  1073 + */
  1074 + public $attr2;
1012 1075
1013   - /**
1014   - * @property mixed $attr3
1015   - */
1016   - public $attr3;
  1076 + /**
  1077 + * @property mixed $attr3
  1078 + */
  1079 + public $attr3;
1017 1080
1018   - /**
1019   - * @property mixed $attr4
1020   - */
1021   - public $attr4;
1022   -
1023   - public function __constructor(array $properties)
1024   - {
1025   - foreach($properties as $property=>$value)
1026   - {
1027   - if(!property_exists($this, $property))
1028   - {
1029   - throw new Exception("$property is not a property of this class, and I'm not allowing you to add it!");
1030   - }
1031   - $this->{$property} = $value;
1032   - }
1033   - }
  1081 + /**
  1082 + * @property mixed $attr4
  1083 + */
  1084 + public $attr4;
  1085 +
  1086 + public function __constructor(array $properties)
  1087 + {
  1088 + foreach($properties as $property=>$value)
  1089 + {
  1090 + if(!property_exists($this, $property))
  1091 + {
  1092 + throw new Exception("$property is not a property of this class, and I'm not allowing you to add it!");
  1093 + }
  1094 + $this->{$property} = $value;
  1095 + }
  1096 + }
1034 1097
1035 1098 /**
1036 1099 * Returns the list of attribute names.

0 comments on commit 0fde5cf

Please sign in to comment.
Something went wrong with that request. Please try again.