From 257432be153ad442dad6b3588cdec04c27190a86 Mon Sep 17 00:00:00 2001 From: Raymond Geerts Date: Tue, 5 Oct 2010 13:48:21 +0200 Subject: [PATCH 1/3] Added new math plugin complete rebuild --- lib/cssp.php | 4 +- plugins/math.php | 141 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 plugins/math.php diff --git a/lib/cssp.php b/lib/cssp.php index 4c18599..0be0e69 100644 --- a/lib/cssp.php +++ b/lib/cssp.php @@ -384,7 +384,7 @@ private function apply_copying_values($block, $selector, $property, $values, $fo preg_match_all($copying_pattern, $values[$i], $matches); // Exact selector matches if(isset($this->parsed[$block][$matches[1][0]][$matches[2][0]])){ - $dest[$i] = $this->get_final_value($this->parsed[$block][$matches[1][0]][$matches[2][0]], $property); + str_replace($matches[0][0], $this->get_final_value($this->parsed[$block][$matches[1][0]][$matches[2][0]], $property), $dest[$i]); $found = true; } // Search for partial selector matches, ie. "#foo" in "#bar, #foo, #blah" @@ -393,7 +393,7 @@ private function apply_copying_values($block, $selector, $property, $values, $fo $tokenized_selectors = $this->tokenize($full_selectors, ','); if(in_array($matches[1][0], $tokenized_selectors)){ if(isset($this->parsed[$block][$full_selectors][$matches[2][0]])){ - $dest[$i] = $this->get_final_value($this->parsed[$block][$full_selectors][$matches[2][0]], $property); + $dest[$i] = str_replace($matches[0][0], $this->get_final_value($this->parsed[$block][$full_selectors][$matches[2][0]], $property), $dest[$i]); $found = true; } } diff --git a/plugins/math.php b/plugins/math.php new file mode 100644 index 0000000..67aeac4 --- /dev/null +++ b/plugins/math.php @@ -0,0 +1,141 @@ + xpos ypos */ + 'background', 'background-position', + /* Border -> length */ + 'border', 'border-width', 'border-bottom', 'border-bottom-width', 'border-left', 'border-left-width', 'border-right', 'border-right-width', 'border-top', 'border-top-width', 'border-spacing', + /* Dimension -> length */ + 'height', 'line-height', 'max-height', 'min-height', 'width', 'max-width', 'min-width', + /* Font -> length */ + 'font', 'font-size', + /* List -> length */ + 'marker-offset', + /* Margin -> length */ + 'margin', 'margin-bottom', 'margin-left', 'margin-right', 'margin-top', + /* Outline -> length */ + 'outline', 'outline-width', + /* Padding -> length */ + 'padding', 'padding-bottom', 'padding-left', 'padding-right', 'padding-top', + /* Positioning -> length */ + 'bottom', 'left', 'right', 'top', + /* Text -> length */ + 'letter-spacing', 'text-indent', 'word-spacing' + ); + // For every possible property... + foreach($properties as $search){ + // ... loop through the css... + foreach($parsed as $block => $css){ + foreach($parsed[$block] as $selector => $styles){ + if($selector != '@turbine' && isset($parsed[$block][$selector][$search])){ + $num_values = count($parsed[$block][$selector][$search]); + // ... loop through the values + for($i = 0; $i < $num_values; $i++){ + // generate empty array + $occurences = array(); + if(preg_match('/[\s]*[*\/+-][\s]*/',$parsed[$block][$selector][$search][$i])){ + // remove spaces inbetween values to calculate + $parsed[$block][$selector][$search][$i] = preg_replace('/[\s]*([*\/+-])[\s]*/','$1',$parsed[$block][$selector][$search][$i]); + // put each part with its own key in an array + $occurences = preg_split('/[\s]+/',$parsed[$block][$selector][$search][$i]); + // generate empty array's + $unit = array(); + $unitcheck = array(); + foreach($occurences AS $key=>$occurence){ + $count = 0; + if (preg_match_all($unitspattern, $occurence, $matches, PREG_PATTERN_ORDER)){ + $count = count(array_unique($matches[0])); + $unitcheck[$key] = $count; + $unit[$key] = ($count<2)?$matches[0][0]:null; + } + else + { + $unitcheck[$key] = '0'; + $unit[$key] = null; + } + } + $comment = null; + $output = array(); + foreach($occurences AS $key=>$occurence){ + if ($unitcheck[$key]==0) { + $output[$key] = $occurence; + } + else if ($unitcheck[$key]==1) { + $occurence = preg_replace($unitspattern,'',$occurence); + $output[$key] = @eval("return (" . $occurence . ");").$unit[$key]; + } + else { + $output[$key] = $occurence; + $unique = array(); + foreach($unit AS $u) { + if($u) $unique[] = $u; + } + $comment = 'math plugin: incompatible units '.implode(', ', array_unique($unique)); + } + } + + $cssp->parsed[$block][$selector][$search][0] = implode(' ', $output); + + if(!$comment){ + CSSP::comment( + $cssp->parsed[$block][$selector], + $search, + 'math plugin: calcutated value' + ); + } + else { + CSSP::comment( + $cssp->parsed[$block][$selector], + $search, + $comment + ); + } + } + } + } + } + } + } +} + + +/** + * Register the plugin + */ +$cssp->register_plugin('before_glue', 0, 'math'); + + +?> \ No newline at end of file From 0a9ac0e4021571fb236867124654f65134b9df4c Mon Sep 17 00:00:00 2001 From: Raymond Geerts Date: Tue, 5 Oct 2010 13:52:06 +0200 Subject: [PATCH 2/3] Fix for cssp.php --- lib/cssp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cssp.php b/lib/cssp.php index 0be0e69..094a669 100644 --- a/lib/cssp.php +++ b/lib/cssp.php @@ -384,7 +384,7 @@ private function apply_copying_values($block, $selector, $property, $values, $fo preg_match_all($copying_pattern, $values[$i], $matches); // Exact selector matches if(isset($this->parsed[$block][$matches[1][0]][$matches[2][0]])){ - str_replace($matches[0][0], $this->get_final_value($this->parsed[$block][$matches[1][0]][$matches[2][0]], $property), $dest[$i]); + $dest[$i] = str_replace($matches[0][0], $this->get_final_value($this->parsed[$block][$matches[1][0]][$matches[2][0]], $property), $dest[$i]); $found = true; } // Search for partial selector matches, ie. "#foo" in "#bar, #foo, #blah" From de00db1b176690868b03a88fa85c62ac1743e943 Mon Sep 17 00:00:00 2001 From: Raymond Geerts Date: Fri, 12 Nov 2010 12:08:29 +0100 Subject: [PATCH 3/3] Fixed problem in math.php regarding eval() code --- plugins/math.php | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/math.php b/plugins/math.php index 67aeac4..7fa1eac 100644 --- a/plugins/math.php +++ b/plugins/math.php @@ -95,6 +95,7 @@ function math(&$parsed){ } else if ($unitcheck[$key]==1) { $occurence = preg_replace($unitspattern,'',$occurence); + $occurence = preg_replace('/[$a-zA-Z_]/','',$occurence); $output[$key] = @eval("return (" . $occurence . ");").$unit[$key]; } else {