Commit 18d2f452 authored by Taddeus Kroes's avatar Taddeus Kroes

Added newest version of CSS Parser utility.

parent 7ddcaf4c
...@@ -104,7 +104,7 @@ class CssNode { ...@@ -104,7 +104,7 @@ class CssNode {
if( self::$config['compress_colors'] && strpos($part, 'color') !== false ) if( self::$config['compress_colors'] && strpos($part, 'color') !== false )
$value = self::compress_color($value); $value = self::compress_color($value);
$replacement .= (empty($m[1]) ? ' ' : $m[1]).$value; $replacement .= (!strlen($m[1]) ? ' ' : $m[1]).$value;
} }
} elseif( isset($values[$part]) ) { } elseif( isset($values[$part]) ) {
$value = $values[$part]; $value = $values[$part];
...@@ -147,13 +147,18 @@ class CssNode { ...@@ -147,13 +147,18 @@ class CssNode {
} }
function compress($value, $rule) { function compress($value, $rule) {
// Compress colors
if( self::$config['compress_colors'] && preg_match('/color$/', $rule) ) if( self::$config['compress_colors'] && preg_match('/color$/', $rule) )
$value = self::compress_color($value); $value = self::compress_color($value);
// Replace any redundant margins and paddings // Compress measurements
if( self::$config['compress_measurements'] if( self::$config['compress_measurements']
&& ($rule == 'margin' || $rule == 'padding') && ($rule == 'margin' || $rule == 'padding') ) {
&& preg_match('/^(\w+) (\w+) (\w+)(?: (\w+))?$/', $value, $m) ) { if( preg_match('/^0\w+$/', $value, $m) ) {
// Replace zero with unit by just zero
$value = 0;
} elseif( preg_match('/^(\w+) (\w+) (\w+)(?: (\w+))?$/', $value, $m) ) {
// Replace redundant margins and paddings
$value = $m[1].' '.$m[2]; $value = $m[1].' '.$m[2];
$left_needed = isset($m[4]) && $m[4] != $m[2]; $left_needed = isset($m[4]) && $m[4] != $m[2];
$bottom_needed = $left_needed || $m[3] != $m[1]; $bottom_needed = $left_needed || $m[3] != $m[1];
...@@ -163,6 +168,7 @@ class CssNode { ...@@ -163,6 +168,7 @@ class CssNode {
$left_needed && $value .= ' '.$m[4]; $left_needed && $value .= ' '.$m[4];
} }
} }
}
return $rule.(self::$config['minify'] ? ':' : ': ').trim($value); return $rule.(self::$config['minify'] ? ':' : ': ').trim($value);
} }
...@@ -171,7 +177,7 @@ class CssNode { ...@@ -171,7 +177,7 @@ class CssNode {
foreach( preg_split('/\s*;\s*/', trim($rules)) as $rule ) { foreach( preg_split('/\s*;\s*/', trim($rules)) as $rule ) {
$split = preg_split('/\s*:\s*/', $rule, 2); $split = preg_split('/\s*:\s*/', $rule, 2);
if( count($split) == 2 && !empty($split[0]) && !empty($split[1]) ) if( count($split) == 2 && strlen($split[0]) && strlen($split[1]) )
$this->rules[$split[0]] = $split[1]; $this->rules[$split[0]] = $split[1];
} }
} }
...@@ -204,12 +210,12 @@ class CssNode { ...@@ -204,12 +210,12 @@ class CssNode {
$line = $m[2]; $line = $m[2];
} }
if( empty($line) ) { if( strlen($line) ) {
// End tag
$current_node = $current_node->parent_node;
} else {
// Normal rule // Normal rule
$current_node->parse_rules($line); $current_node->parse_rules($line);
} else {
// End tag
$current_node = $current_node->parent_node;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment