Permalink
Browse files

* All tests are now working

* Rewrote areas which were not working
* Major rewrite to remove_properties_with_value, now uses a single preg_replace, which will be a lot more effecient
* Expanded some tests to be easier to read
* Expanded regexs to be easier to read, and added regex comments
* There was a typo in the CSSTest where there was an extra semi-colon preventing a test from working
* Added ignore all to cache directories
  • Loading branch information...
1 parent 2f5a776 commit 8fdc284ccc43162f8c5cc20d568c906a9b4b7029 @balupton balupton committed with Anthony Short Jun 26, 2010
View
@@ -0,0 +1 @@
+*
@@ -216,26 +216,53 @@ public static function valid_selector($string)
* @param $css
* @return array
*/
- public static function find_properties_with_value($property,$value,$css)
+ public static function find_properties_with_value($property,$value,$css,$escape_value=true)
{
$return = array();
$escaped_property = self::escape_regex($property);
- $value = self::escape_regex($value);
-
- $regex = "/([^{}]*)\s*\{\s*(($escaped_property|[^}]*[\s\;]$escaped_property)(\s*:\s*$value\s*\;*))[^}]*}/sx";
+ $value = $escape_value ? self::escape_regex($value) : $value;
- if(preg_match_all($regex,$css,$match))
+ $regex = "/
+ \s*
+
+ ([^{}]*) (?# selector)
+
+ \s*
+
+ \{
+
+ (?: (?# property is complicated, as we may be first or intermediate - dbackground instance)
+ [^}]*[\s\;]
+ |
+ \s*
+ )
+ ($escaped_property)
+ \s*
+
+ : (?# property value seperator)
+
+ \s*
+ ($value)
+ ;?
+
+ [^}]* (?# everything after our property of interest)
+
+ \} (?# end content)
+
+ /sx";
+
+ if(preg_match_all($regex,$css,$matches,PREG_SET_ORDER))
{
- foreach($match[0] as $key => $value)
+ foreach ( $matches as $key => $match )
{
$return[$key] = array(
- 'string' => $value,
- 'selector' => $match[1][$key],
- 'property' => $property . $match[4][$key]
+ 'string' => trim($match[0]),
+ 'selector' => $match[1],
+ 'property' => $match[2].':'.$match[3]
);
}
}
-
+
return $return;
}
@@ -246,16 +273,38 @@ public static function find_properties_with_value($property,$value,$css)
* @param $value string
* @param $css string
*/
- public static function remove_properties_with_value($property,$value,$string)
+ public static function remove_properties_with_value($property,$value,$string,$escape_value=true)
{
- if($props = self::find_properties_with_value($property,$value,$string))
- {
- foreach($props as $prop)
- {
- $new = str_replace($prop['property'],'',$prop['string']);
- $string = str_replace($prop['string'],$new,$string);
- }
- }
+ # Prepare
+ $escaped_property = self::escape_regex($property);
+ $value = $escape_value ? self::escape_regex($value) : $value;
+
+ # Generate regex
+ $regex = "/
+ (
+ \{
+ (?: (?# property is complicated, as we may be first or intermediate - dbackground instance)
+ \s*
+ |
+ [^}]*[\s\;]
+ )
+ )
+
+ ($escaped_property)
+ \s*
+
+ : (?# property value seperator)
+
+ \s*
+ ($value)
+ ;?
+
+ /sx";
+
+ # Remove property
+ $string = preg_replace($regex, '$1', $string);
+
+ # Return string
return $string;
}
@@ -267,7 +316,7 @@ public static function remove_properties_with_value($property,$value,$string)
*/
public static function find_properties($property,$string)
{
- return self::find_properties_with_value($property,'[^;}]*',$string);
+ return self::find_properties_with_value($property,'[^;}]*',$string,false);
}
/**
@@ -278,7 +327,7 @@ public static function find_properties($property,$string)
*/
public static function remove_properties($property,$string)
{
- return self::remove_properties_with_value($property,'[^;}]*',$string);
+ return self::remove_properties_with_value($property,'[^;}]*',$string,false);
}
// ============================
@@ -297,15 +346,19 @@ public static function find_atrule($name,$string)
{
$name = self::escape_regex($name);
$regex = "/
+ @{$name} (?# the name to find)
+
+ ([^{]*?) (?# atrules params)
- @{$name}([^{]*?)
- \{
- ((".self::$_identifier."*?)\s*\{((?:[^{}]+|(?2))*)\})
- \}
+ \{ (?# start atrule content)
+ (.*?)
+ \} (?# end atrule content)
/xs";
- return preg_match_all($regex, $string, $matches) ? $matches : array();
+ $result = preg_match_all($regex, $string, $matches, PREG_SET_ORDER) ? $matches : array();
+
+ return $result;
}
/**
@@ -71,9 +71,9 @@ public function testFind_atrule()
$expected = array
(
0 => array(
- 'string' => '@constants{property:value;property:value;}',
- 'params' => '',
- 'content' => 'property:value;property:value;'
+ 0 => '@constants{property:value;property:value;}', // string
+ 1 => '', // params
+ 2 => 'property:value;property:value;' // content
)
);
@@ -197,7 +197,7 @@ public function Find_all_properties_with_a_value()
0 => array(
'string' => '#id5{color:red;background:blue}',
'selector' => '#id5',
- 'property' => 'color:red;'
+ 'property' => 'color:red'
)
);
$output = Scaffold_Helper_CSS::find_properties_with_value('color','red',$string);
@@ -390,9 +390,14 @@ public function Find_selectors()
public function testFind_property()
{
- $string = 'id{background:blue}#id2{color:blue}#id3{background-color:red}#id4{dbackground:blue}#id5{color:red;background:blue}';
- $expected = array
- (
+ $string = '
+ id{background:blue}
+ #id2{color:blue}
+ #id3{background-color:red}
+ #id4{dbackground:blue}
+ #id5{color:red;background:blue}
+ ';
+ $expected = array(
0 => array(
'string' => 'id{background:blue}',
'selector' => 'id',
@@ -405,6 +410,7 @@ public function testFind_property()
),
);
$output = Scaffold_Helper_CSS::find_properties('background',$string);
+
$this->assertEquals($expected,$output);
}
@@ -0,0 +1 @@
+*
View
No changes.
@@ -66,8 +66,7 @@ <h2 id="Scaffold_Helper_CSSTest">
</li>
<li>Find css style functions
</li>
- <li>
- <strike>testFind atrule</strike>
+ <li>testFind atrule
</li>
<li>
<strike>testRemove atrule</strike>
@@ -84,15 +83,13 @@ <h2 id="Scaffold_Helper_CSSTest">
</li>
<li>Find selectors
</li>
- <li>
- <strike>testFind property</strike>
+ <li>testFind property
</li>
<li>testSelector exists
</li>
<li>testRemove properties with value
</li>
- <li>
- <strike>testRemove properties</strike>
+ <li>testRemove properties
</li>
</ul>
<h2 id="Scaffold_Response_CacheTest">

0 comments on commit 8fdc284

Please sign in to comment.