Skip to content

Instantly share code, notes, and snippets.

@serebrov
Created March 31, 2013 16:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save serebrov/5281148 to your computer and use it in GitHub Desktop.
<?php
/*
* AppClientScript class file.
* @package app.components
* @author seb
*/
/**
* AppClientScript allows to register single package and reset other registered script files.
*
* @package app.components
* @author seb
*/
class AppClientScript extends CClientScript {
/**
* @var boolen whether to combine/minify js
*/
public $minifyJs = false;
/**
* @var boolen whether to combine/minify css
*/
public $minifyCss = false;
/**
* @var boolean whether to load jquery and juery-ui from google servers
*/
public $useGoogleScripts = false;
/**
* Resets registered script files
*/
public function resetFiles() {
$this->cssFiles=array();
$this->scriptFiles=array();
$this->recordCachingAction('clientScript','resetFiles',array());
}
/**
* Prints js code to include jquery and jquery ui usin Google Libraries API
* @return string js code
*/
public function echoGoogleScripts() {
if (!$this->useGoogleScripts) return;
$cs = Yii::app()->clientScript;
$cs->scriptMap = array(
'jquery.js'=>false,
'jquery.min.js'=>false,
'jquery-ui.js'=>false,
'jquery-ui.min.js'=>false,
);
echo CGoogleApi::init();
echo CHtml::script(
//CGoogleApi::load('jquery') . "\n" .
CGoogleApi::load('jquery','1.7.1') . "\n" .
//CGoogleApi::load('jqueryui') . "\n"
CGoogleApi::load('jqueryui', '1.8.17') . "\n"
);
}
/**
* Registers a package, resets already registered script files before registration.
* Uses {@link ExtMinScript} to combine and minify all css / js from the package.
* See http://www.yiiframework.com/extension/minscript/.
* @param string $name package name
*/
public function registerPackageExt($name) {
$this->resetFiles();
$this->registerPackage($name);
$groupMap = array();
if ($this->minifyCss) {
$scripts = $this->collectScripts('css');
if (!empty($scripts)) {
$groupMap[$name.'.css'] = $scripts;
}
}
if ($this->minifyJs) {
$scripts = $this->collectScripts('js');
if (!empty($scripts)) {
$groupMap[$name.'.js'] = $scripts;
}
}
if ($groupMap !== array()) {
$minScript = Yii::app()->minScript;
$groupMap = CMap::mergeArray($minScript->getGroupMap(), $groupMap);
$minScript->setGroupMap($groupMap);
if (isset($groupMap[$name.'.css'])) {
$minScript->generateScriptMap($name.'.css');
}
if (isset($groupMap[$name.'.js'])) {
$minScript->generateScriptMap($name.'.js');
}
}
}
/**
* Collects paths to scripts (css or js depending on 'key' parameter) from the
* currently registered packages.
* @param string $key 'css' or 'js'
* @return array array or script paths
*/
protected function collectScripts($key) {
$scripts = array();
foreach ($this->coreScripts as $name=>$package) {
if (isset($package[$key])) {
foreach ($package[$key] as $i=>$path) {
if(isset($package['baseUrl'])) {
$p = Yii::getPathOfAlias('webroot');
if (!empty($package['baseUrl'])) {
$p .= DIRECTORY_SEPARATOR . $package['baseUrl'];
}
$path = $p . DIRECTORY_SEPARATOR . $path;
} else if(isset($package['basePath'])) {
$baseUrl = Yii::app()->assetManager->publish(
Yii::getPathOfAlias($package['basePath']));
$path = Yii::getPathOfAlias('webroot') .
$baseUrl . DIRECTORY_SEPARATOR . $path;
//$path=Yii::getPathOfAlias($package['basePath']) .
// DIRECTORY_SEPARATOR . $path;
} else {
$path = YII_PATH.'/web/js/source/' . $path;
}
if (!isset($this->scriptMap[basename($path)])) {
$scripts[$path] = $path;
}
}
}
}
return $scripts;
}
protected $activeScriptId;
protected $activeScriptPosition;
public function beginScript($id, $pos = parent::POS_READY) {
$this->activeScriptId = $id;
$this->activeScriptPosition = $pos;
ob_start();
ob_implicit_flush(false);
}
public function endScript() {
$script = ob_get_clean();
$script = strip_tags($script, '<p><a><br><span><div><b><i><strong>');
parent::registerScript($this->activeScriptId, $script, $this->activeScriptPosition);
}
}
?>
@MGHollander
Copy link

Great piece of code. I suggest to remove the script tags using a regular expression, because it may occur that other HTML tags are used within a JavaScript string. You should also add a parameter for htmlOptions since this is available in the registerScript function.

My tested code to remove <script>, <script type="text/javascript"> (or other attributes) and </script> tags.

parent::registerScript($this->id, preg_replace('/\s*<\/?script(.*)>\s*/i', '', ob_get_clean()), $this->position, $this->htmlOptions);

Full code: https://gist.github.com/MGHollander/c3f677260f742e6cacce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment