本文整理汇总了PHP中csstidy类的典型用法代码示例。如果您正苦于以下问题:PHP csstidy类的具体用法?PHP csstidy怎么用?PHP csstidy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了csstidy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parseCSS
public function parseCSS($text)
{
$css = new \csstidy();
$css->parse($text);
$rules = array();
$position = 0;
foreach ($css->css as $declarations) {
foreach ($declarations as $selectors => $properties) {
foreach (explode(",", $selectors) as $selector) {
$rules[] = array('position' => $position, 'specificity' => self::calculateCSSSpecifity($selector), 'selector' => $selector, 'properties' => $properties);
}
$position += 1;
}
}
usort($rules, function ($a, $b) {
if ($a['specificity'] > $b['specificity']) {
return 1;
} else {
if ($a['specificity'] < $b['specificity']) {
return -1;
} else {
if ($a['position'] > $b['position']) {
return 1;
} else {
return -1;
}
}
}
});
return $rules;
}
示例2: minify_css
/**
* 压缩css文件
*
* @param mixed $sourceFile
* @param mixed $targetFile
* @return void
*/
function minify_css($sourceFile, $targetFile)
{
$css = new csstidy();
$css->load_template('highest_compression');
/* $css->set_cfg('remove_bslash',false);
$css->set_cfg('compress_colors',false);
$css->set_cfg('compress_font-weight',false);
$css->set_cfg('lowercase_s',true);
$css->set_cfg('optimise_shorthands',$_REQUEST['optimise_shorthands']);
$css->set_cfg('remove_last_;',true);
$css->set_cfg('case_properties',$_REQUEST['case_properties']);
$css->set_cfg('sort_properties',true);
$css->set_cfg('sort_selectors',true);
$css->set_cfg('merge_selectors', $_REQUEST['merge_selectors']);
$css->set_cfg('discard_invalid_properties',true);
$css->set_cfg('preserve_css',true);
$css->set_cfg('timestamp',true);
*/
$strSourceCSS = file_get_contents($sourceFile);
$strResult = $css->parse($strSourceCSS);
$handle = fopen($targetFile, 'w');
if ($handle) {
if (fwrite($handle, $css->print->plain())) {
$file_ok = true;
}
}
fclose($handle);
}
示例3: minify
public static function minify($css, $options = array())
{
$options = array_merge(array('remove_bslash' => true, 'compress_colors' => true, 'compress_font-weight' => true, 'lowercase_s' => false, 'optimise_shorthands' => 1, 'remove_last_;' => false, 'case_properties' => 1, 'sort_properties' => false, 'sort_selectors' => false, 'merge_selectors' => 2, 'discard_invalid_properties' => false, 'css_level' => 'CSS2.1', 'preserve_css' => false, 'timestamp' => false, 'template' => 'default'), $options);
set_include_path(get_include_path() . PATH_SEPARATOR . W3TC_LIB_CSSTIDY_DIR);
require_once 'class.csstidy.php';
$csstidy = new csstidy();
foreach ($options as $option => $value) {
$csstidy->set_cfg($option, $value);
}
$csstidy->load_template($options['template']);
$csstidy->parse($css);
$css = $csstidy->print->plain();
if (isset($options['currentDir']) || isset($options['prependRelativePath'])) {
require_once W3TC_LIB_MINIFY_DIR . '/Minify/CSS/UriRewriter.php';
$browsercache_id = isset($options['browserCacheId']) ? $options['browserCacheId'] : 0;
$browsercache_extensions = isset($options['browserCacheExtensions']) ? $options['browserCacheExtensions'] : array();
if (isset($options['currentDir'])) {
$document_root = isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'];
$symlinks = isset($options['symlinks']) ? $options['symlinks'] : array();
return Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], $document_root, $symlinks, $browsercache_id, $browsercache_extensions);
} else {
return Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath'], $browsercache_id, $browsercache_extensions);
}
}
return $css;
}
示例4: compress
function compress($Media)
{
$Tidy = new csstidy();
$Tidy->load_template($this->_template);
$Tidy->parse($Media->contents['raw']);
if ($compressed = $Tidy->print->plain()) {
$Media->content['raw'] = $compressed;
return true;
}
return false;
}
示例5: minifyCss
public static function minifyCss($path)
{
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$css = new \csstidy();
$css->set_cfg('allow_html_in_templates', false);
$css->set_cfg('compress_colors', true);
$css->set_cfg('compress_font-weight', true);
$css->set_cfg('remove_last_', true);
$css->set_cfg('remove_bslash', true);
$css->set_cfg('template', 'highest');
$css->set_cfg('preserve_css', true);
$css->set_cfg('silent', true);
$css->parse(file_get_contents($path));
return $css->print->plain();
}
示例6: minify
public static function minify($css, $options = array())
{
$options = array_merge(array('remove_bslash' => true, 'compress_colors' => true, 'compress_font-weight' => true, 'lowercase_s' => false, 'optimise_shorthands' => 1, 'remove_last_;' => false, 'case_properties' => 1, 'sort_properties' => false, 'sort_selectors' => false, 'merge_selectors' => 2, 'discard_invalid_properties' => false, 'css_level' => 'CSS2.1', 'preserve_css' => false, 'timestamp' => false, 'template' => 'default'), $options);
set_include_path(get_include_path() . PATH_SEPARATOR . W3TC_LIB_DIR . '/CSSTidy');
require_once 'class.csstidy.php';
$csstidy = new csstidy();
foreach ($options as $option => $value) {
$csstidy->set_cfg($option, $value);
}
$csstidy->load_template($options['template']);
$csstidy->parse($css);
$css = $csstidy->print->plain();
$css = Minify_CSS_UriRewriter::rewrite($css, $options);
return $css;
}
示例7: test
/**
* Implements SimpleExpectation::test().
* @param $filename Filename of test file to test.
*/
function test($filename = false)
{
if ($filename) {
$this->load($filename);
}
$css = new csstidy();
$css->set_cfg($this->settings);
$css->parse($this->css);
if ($this->fullexpect) {
$this->actual = var_export($css->css, true);
} elseif (isset($css->css[41])) {
$this->actual = var_export($css->css[41], true);
} else {
$this->actual = 'Key 41 does not exist';
}
return $this->expect === $this->actual;
}
示例8: sanitize_css
public static function sanitize_css($css)
{
if (!class_exists('csstidy')) {
require_once 'class.csstidy.php';
}
$csstidy = new csstidy();
$csstidy->set_cfg('remove_bslash', FALSE);
$csstidy->set_cfg('compress_colors', FALSE);
$csstidy->set_cfg('compress_font-weight', FALSE);
$csstidy->set_cfg('discard_invalid_properties', TRUE);
$csstidy->set_cfg('merge_selectors', FALSE);
$csstidy->set_cfg('remove_last_;', FALSE);
$csstidy->set_cfg('css_level', 'CSS3.0');
$csstovalidateindiv = preg_replace('/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css);
$csstovalidateindiv = wp_kses_split($csstovalidateindiv, array(), array());
$csstidy->parse($csstovalidateindiv);
$cssvalidated = $csstidy->print->plain();
return $cssvalidated;
}
示例9: pixopoint_validate_css
function pixopoint_validate_css($css)
{
// SafeCSS / CSSTidy stuff
require_once 'csstidy.php';
// CSS sanitising gizmo
$csstidy = new csstidy();
$csstidy->optimise = new safecss($csstidy);
$csstidy->set_cfg('remove_bslash', false);
$csstidy->set_cfg('compress_colors', false);
$csstidy->set_cfg('compress_font-weight', false);
$csstidy->set_cfg('discard_invalid_properties', true);
$csstidy->set_cfg('merge_selectors', false);
$csstidy->set_cfg('preserve_css', true);
// Outputs code comments
// $csstidy->set_cfg( 'lowercase_s', false );
// $csstidy->set_cfg( 'optimise_shorthands', 1 );
// $csstidy->set_cfg( 'remove_last_;', false );
// $csstidy->set_cfg( 'case_properties', 1 );
// $csstidy->set_cfg( 'sort_properties', false );
// $csstidy->set_cfg( 'sort_selectors', false );
// Santisation stuff copied from SafeCSS by Automattic
$css = stripslashes($css);
$css = preg_replace('/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $prev = $css);
$css = str_replace('<=', '<=', $css);
// Some people put weird stuff in their CSS, KSES tends to be greedy
$css = wp_kses_split($prev = $css, array(), array());
// Why KSES instead of strip_tags? Who knows?
$css = str_replace('>', '>', $css);
// kses replaces lone '>' with >
$css = strip_tags($css);
// Why both KSES and strip_tags? Because we just added some '>'.
// Parse with CSS tidy
$csstidy->parse($css);
// Parse with CSS Tidy
$css = $csstidy->print->plain();
// Grab CSS output
// Make CSS look pretty
$css = pixopoint_pretty_css($css);
return $css;
}
示例10: gvw_important
/**
* Returns a value without !important
* @param string $value
* @return string
* @access public
* @version 1.0
*/
static function gvw_important($value)
{
if (csstidy::is_important($value)) {
$value = trim($value);
$value = substr($value, 0, -9);
$value = trim($value);
$value = substr($value, 0, -1);
$value = trim($value);
return $value;
}
return $value;
}
示例11: escaped
/**
* Checks if a character is escaped (and returns true if it is)
* @param string $string
* @param integer $pos
* @access public
* @return bool
* @version 1.02
*/
static function escaped(&$string, $pos)
{
return !(@($string[$pos - 1] !== '\\') || csstidy::escaped($string, $pos - 1));
}
示例12: sanitize_css
/**
* sanitize user entered css
* as seen here: http://wordpress.stackexchange.com/questions/53970/sanitize-user-entered-css
*
* @param type $css
*/
function sanitize_css($css)
{
if (!class_exists('csstidy')) {
include_once 'csstidy/class.csstidy.php';
}
$csstidy = new csstidy();
$csstidy->set_cfg('remove_bslash', false);
$csstidy->set_cfg('compress_colors', false);
$csstidy->set_cfg('compress_font-weight', false);
$csstidy->set_cfg('discard_invalid_properties', true);
$csstidy->set_cfg('merge_selectors', false);
$csstidy->set_cfg('remove_last_;', false);
$csstidy->set_cfg('css_level', 'CSS3.0');
$css = preg_replace('/\\\\([0-9a-fA-F]{4})/', '\\\\\\\\$1', $css);
$css = wp_kses_split($css, array(), array());
$csstidy->parse($css);
return $csstidy->print->plain();
}
示例13: file_get_contents
//die();
// Get scripts from servlet
$json = file_get_contents($getScripts);
$scripts = json_decode($json, true);
//print_r($scripts);
//$css_core = array();
$core_files = array();
$errors = array();
$warnings = array();
$completed = array();
$script_hash = array();
$script_size = array();
$full_script_size = array();
// Load, concat and minify css
foreach ($scripts['css'] as $core_name => $core_group) {
$cssTidy = new csstidy();
$cssTidy->load_template('highest_compression');
$cssTidy->settings['merge_selectors'] = 2;
$core_files[$core_name] = "";
$script_hash[$core_name] = getFileHash($pathToCore . "/{$core_name}");
$script_size[$core_name] = getFileSize($pathToCore . "/{$core_name}");
$full_script_size[$core_name] = 0;
foreach ($core_group as $script) {
$filePath = $pathToRoot . $script;
if (file_exists($filePath)) {
/*
$core_files[$core_name] .= Minify_CSS::minify(file_get_contents($filePath), array(
'currentDir' => dirname($filePath)
));
*/
//echo "Proccessing: $filePath<br />\n";
示例14: __process
function __process($type, $assets)
{
$path = $this->__getPath($type);
$folder = new Folder($this->paths['wwwRoot'] . $this->cachePaths[$type], true);
//check if the cached file exists
$scripts = Set::extract('/script', $assets);
$fileName = $folder->find($this->__generateFileName($scripts) . '_([0-9]{10}).' . $type);
if ($fileName) {
//take the first file...really should only be one.
$fileName = $fileName[0];
}
//make sure all the pieces that went into the packed script
//are OLDER then the packed version
if ($this->checkTs && $fileName) {
$packed_ts = filemtime($this->paths['wwwRoot'] . $this->cachePaths[$type] . DS . $fileName);
$latest_ts = 0;
foreach ($assets as $asset) {
$assetFile = $this->__findFile($asset, $type);
if (!$assetFile) {
continue;
}
$latest_ts = max($latest_ts, filemtime($assetFile));
}
//an original file is newer. need to rebuild
if ($latest_ts > $packed_ts) {
unlink($this->paths['wwwRoot'] . $this->cachePaths[$type] . DS . $fileName);
$fileName = null;
}
}
//file doesn't exist. create it.
if (!$fileName) {
$ts = time();
switch ($type) {
case 'js':
if (PHP5) {
App::import('Vendor', 'jsmin/jsmin');
}
break;
case 'css':
App::import('Vendor', 'csstidy', array('file' => 'class.csstidy.php'));
$tidy = new csstidy();
$tidy->load_template($this->cssCompression);
break;
}
//merge the script
$scriptBuffer = '';
foreach ($assets as $asset) {
$buffer = $this->__getFileContents($asset, $type);
$origSize = strlen($buffer);
switch ($type) {
case 'js':
//jsmin only works with PHP5
if (PHP5) {
$buffer = trim(JSMin::minify($buffer));
}
break;
case 'css':
$tidy->parse($buffer);
$buffer = $tidy->print->plain();
break;
}
$delta = 0;
if ($origSize > 0) {
$delta = strlen($buffer) / $origSize * 100;
}
$scriptBuffer .= sprintf("/* %s.%s (%d%%) */\n", $asset['script'], $type, $delta);
$scriptBuffer .= $buffer . "\n\n";
}
//write the file
$fileName = $this->__generateFileName($scripts) . '_' . $ts . '.' . $type;
$file = new File($this->paths['wwwRoot'] . $this->cachePaths[$type] . DS . $fileName);
$file->write(trim($scriptBuffer));
}
if ($type == 'css') {
//$html->css doesn't check if the file already has
//the .css extension and adds it automatically, so we need to remove it.
$fileName = str_replace('.css', '', $fileName);
}
return $fileName;
}
示例15: gvw_important
/**
* Returns a value without !important
* @param string $value
* @return string
* @access public
* @version 1.0
*/
function gvw_important($value)
{
// Apenas para evitar o erro por passar a referencia do objeto diretamente a função
// Maikon.Will - 21/06/2012
$value0 = $value;
if (csstidy::is_important($value0)) {
$value = trim($value);
$value = substr($value, 0, -9);
$value = trim($value);
$value = substr($value, 0, -1);
$value = trim($value);
return $value;
}
return $value;
}